wolf_core 1.0.68 → 1.0.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/wolf_core/utils/friendly_model_id.rb +32 -24
- data/lib/wolf_core/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4d5fa238459aefb14f513bd81c063aff7227a31f27ff2b3d56fbc98b4cfcd38b
|
4
|
+
data.tar.gz: ec08c21d261cde65e3107cde28b3d297d797ee27724df5996b5aea23e43b4755
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a5f7d94c752f04cef2bd762cfef9ba6ae77ff3c57e5d0913fbce580fa8f88a18e29c2c40cb6c287540cfa193fa95b4ef42335824050390fc52f9c1585ed9d64
|
7
|
+
data.tar.gz: cf5ad9f109e43bbb6beac1f24eaba663515a83f0c955a949c29469067c433c6637c870dc35c7a1c9e89d42bd8bbddae61ad90099503e11e403939b45b0791ce3
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# In order to use this module on a model you only need to include it and implement the friendly_id_candidates method.
|
2
4
|
#
|
3
5
|
# Example:
|
@@ -9,7 +11,8 @@
|
|
9
11
|
# [[:name], [:name, :id]]
|
10
12
|
# end
|
11
13
|
# end
|
12
|
-
# This will generate a friendly_id based on the name field
|
14
|
+
# This will generate a friendly_id based on the name field.
|
15
|
+
# If it is not unique it will append the id to the friendly_id.
|
13
16
|
#
|
14
17
|
# You can also override the set_friendly_id method to customize the friendly_id generation
|
15
18
|
|
@@ -19,50 +22,55 @@ module WolfCore
|
|
19
22
|
include WolfCore::StringUtils
|
20
23
|
|
21
24
|
included do
|
22
|
-
unless column_names.include?(
|
23
|
-
raise "Model #{self.name} does not have a friendly_id column"
|
24
|
-
end
|
25
|
+
raise "Model #{name} does not have a friendly_id column" unless column_names.include?("friendly_id")
|
25
26
|
|
26
27
|
scope :by_friendly_id, ->(friendly_id) { where(friendly_id: friendly_id) }
|
27
|
-
|
28
|
+
|
28
29
|
before_save :set_friendly_id
|
29
30
|
end
|
30
31
|
|
31
32
|
def set_friendly_id
|
32
|
-
return if
|
33
|
+
return if friendly_id.present?
|
33
34
|
|
34
|
-
|
35
|
-
|
36
|
-
next if friendly_id.blank?
|
35
|
+
self.friendly_id = generate_friendly_id
|
36
|
+
end
|
37
37
|
|
38
|
-
|
39
|
-
|
38
|
+
def generate_friendly_id
|
39
|
+
friendly_id = find_unique_friendly_id
|
40
|
+
return friendly_id if friendly_id.present?
|
40
41
|
|
41
|
-
|
42
|
-
|
42
|
+
default_candidate = Array(friendly_id_candidates.first) + [:id]
|
43
|
+
convert_candidate_to_friendly_id(default_candidate)
|
44
|
+
end
|
43
45
|
|
44
|
-
|
46
|
+
def find_unique_friendly_id
|
47
|
+
friendly_id_candidates.each do |candidate|
|
48
|
+
friendly_id = convert_candidate_to_friendly_id(candidate)
|
49
|
+
return friendly_id if friendly_id.present? && !friendly_id_used?(friendly_id)
|
50
|
+
end
|
51
|
+
nil
|
52
|
+
end
|
45
53
|
|
46
|
-
|
47
|
-
|
54
|
+
def friendly_id_candidates
|
55
|
+
[:id]
|
48
56
|
end
|
49
57
|
|
50
58
|
def convert_candidate_to_friendly_id(candidate)
|
51
59
|
candidate = Array(candidate)
|
52
|
-
candidate.map!
|
60
|
+
candidate.map!(&:to_sym).uniq!
|
53
61
|
candidate.map do |field|
|
54
62
|
field_value = send(field)
|
55
63
|
parse_field_value(field_value)
|
56
|
-
end.join(
|
64
|
+
end.join("-")
|
57
65
|
end
|
58
66
|
|
59
|
-
def
|
60
|
-
|
61
|
-
remove_non_alphanumeric_chars(field_value, exceptions: '-')
|
67
|
+
def friendly_id_used?(friendly_id)
|
68
|
+
self.class.where.not(id: id).find_by(friendly_id: friendly_id).present?
|
62
69
|
end
|
63
70
|
|
64
|
-
def
|
65
|
-
|
71
|
+
def parse_field_value(field_value)
|
72
|
+
field_value = to_kebab_case(field_value)
|
73
|
+
remove_non_alphanumeric_chars(field_value, exceptions: "-")
|
66
74
|
end
|
67
75
|
end
|
68
|
-
end
|
76
|
+
end
|
data/lib/wolf_core/version.rb
CHANGED