wolf_core 1.0.66 → 1.0.67
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/wolf_core/utils/friendly_model_id.rb +44 -0
- data/lib/wolf_core/utils/string_utils.rb +16 -6
- data/lib/wolf_core/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99535923be72a8bcbb80245eaa43c9b15633b274b82bf01f5e1363174656f83a
|
4
|
+
data.tar.gz: 7d3aa39f2ffd464a5aa0dedf9396fff2bf6af1ca72c39efc2dedf5d092e18110
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3489825ef30779df10ce818c9f44cb044b236362025b10fe5088a3e7fd9a0e496cae3deae9052d1f07323c609a801c287c0b311cf27a4dfadd7024df45ccd153
|
7
|
+
data.tar.gz: f2cef0c7a7d517777f469c344f535b256cfe40283c1685936fb1503b870e1319f250ae1cce52b82e0e4fcdcfa8e1a737b81f5e665e764fb956ce02bfb717f2b5
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module WolfCore
|
2
|
+
module FriendlyModelId
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
include WolfCore::StringUtils
|
5
|
+
|
6
|
+
included do
|
7
|
+
unless column_names.include?('friendly_id')
|
8
|
+
raise "Model #{self.name} does not have a friendly_id column"
|
9
|
+
end
|
10
|
+
|
11
|
+
scope :by_friendly_id, ->(friendly_id) { where(friendly_id: friendly_id) }
|
12
|
+
|
13
|
+
before_save :set_friendly_id
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_friendly_id
|
17
|
+
return if self.friendly_id.present?
|
18
|
+
|
19
|
+
friendly_id_id_candidates.each do |candidate|
|
20
|
+
candidate = Array(candidate)
|
21
|
+
|
22
|
+
friendly_id = candidate.map do |field|
|
23
|
+
field_value = send(field)
|
24
|
+
parse_field_value(field_value)
|
25
|
+
end.join('-')
|
26
|
+
next if friendly_id.blank?
|
27
|
+
|
28
|
+
friendly_id_used = self.class.where.not(id: self.id).find_by(friendly_id: friendly_id).present?
|
29
|
+
next if field_value_used
|
30
|
+
|
31
|
+
self.friendly_id = friendly_id
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_field_value(field_value)
|
36
|
+
field_value = to_kebab_case(field_value)
|
37
|
+
remove_non_alphanumeric_chars(field_value, exceptions: '-')
|
38
|
+
end
|
39
|
+
|
40
|
+
def friendly_id_candidates
|
41
|
+
raise NotImplementedError
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -5,6 +5,22 @@ module WolfCore
|
|
5
5
|
str.gsub(/([A-Z])/, ' \1').strip.downcase.capitalize
|
6
6
|
end
|
7
7
|
|
8
|
+
def to_snake_case(str)
|
9
|
+
str.gsub(/\s+/, '_').downcase
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_kebab_case(str)
|
13
|
+
str.gsub(/\s+/, '-').downcase
|
14
|
+
end
|
15
|
+
|
16
|
+
def remove_non_alphanumeric_chars(str, exceptions: nil)
|
17
|
+
exceptions ||= []
|
18
|
+
exceptions = Array(exceptions)
|
19
|
+
escaped_exceptions = exceptions.map { |char| Regexp.escape(char) }.join
|
20
|
+
regex = /[^a-zA-Z0-9#{escaped_exceptions}]/
|
21
|
+
str.gsub(regex, '')
|
22
|
+
end
|
23
|
+
|
8
24
|
def clean_phone_number(phone)
|
9
25
|
return if phone.nil?
|
10
26
|
cleaned_phone = phone.to_s.gsub(/\D/, '')
|
@@ -46,11 +62,5 @@ module WolfCore
|
|
46
62
|
base64_regex = /^[A-Za-z0-9+\/]+={0,2}$/
|
47
63
|
str.match?(base64_regex)
|
48
64
|
end
|
49
|
-
|
50
|
-
def to_snake_case(str)
|
51
|
-
str.gsub(/[^a-zA-Z0-9\s_]/, '')
|
52
|
-
.gsub(/\s+/, '_')
|
53
|
-
.downcase
|
54
|
-
end
|
55
65
|
end
|
56
66
|
end
|
data/lib/wolf_core/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wolf_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.67
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Javier Roncallo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-01-
|
11
|
+
date: 2025-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -130,6 +130,7 @@ files:
|
|
130
130
|
- lib/wolf_core/utils/array_utils.rb
|
131
131
|
- lib/wolf_core/utils/async_utils.rb
|
132
132
|
- lib/wolf_core/utils/file_utils.rb
|
133
|
+
- lib/wolf_core/utils/friendly_model_id.rb
|
133
134
|
- lib/wolf_core/utils/logging_utils.rb
|
134
135
|
- lib/wolf_core/utils/result.rb
|
135
136
|
- lib/wolf_core/utils/string_utils.rb
|