wolf_core 1.0.67 → 1.0.68
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 +31 -7
- 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: 1fd91295408ff2f54a66a3c894a46c34cbf8ba9c79d0c5d2604cfcef554e2ab0
|
4
|
+
data.tar.gz: 28a7c16e6a124fa97cc80b1a9b8decc9d4446c72d92eb4905e712f0921fec22c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44b79a76c0b17651d393a7bd523acf3adf14dc93211d7d7a9eb32a6a38759f5041dfa0ea1620bf8e8b95e6392c97eefefdca1b3d2cdbdf2e54c99853f3c6272b
|
7
|
+
data.tar.gz: f6b3a6515df0d2868071cc483b47fa166459b2aeb31cfef5c9b50c0509c6fb9cbd41ae9f642c95bea550943bba9177ea055ca402db5c132cc7ad2a0c07ef090d
|
@@ -1,3 +1,18 @@
|
|
1
|
+
# In order to use this module on a model you only need to include it and implement the friendly_id_candidates method.
|
2
|
+
#
|
3
|
+
# Example:
|
4
|
+
#
|
5
|
+
# class MyModel < ApplicationRecord
|
6
|
+
# include WolfCore::FriendlyModelId
|
7
|
+
#
|
8
|
+
# def friendly_id_candidates
|
9
|
+
# [[:name], [:name, :id]]
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
# This will generate a friendly_id based on the name field, if it is not unique it will append the id to the friendly_id.
|
13
|
+
#
|
14
|
+
# You can also override the set_friendly_id method to customize the friendly_id generation
|
15
|
+
|
1
16
|
module WolfCore
|
2
17
|
module FriendlyModelId
|
3
18
|
extend ActiveSupport::Concern
|
@@ -17,12 +32,7 @@ module WolfCore
|
|
17
32
|
return if self.friendly_id.present?
|
18
33
|
|
19
34
|
friendly_id_id_candidates.each do |candidate|
|
20
|
-
|
21
|
-
|
22
|
-
friendly_id = candidate.map do |field|
|
23
|
-
field_value = send(field)
|
24
|
-
parse_field_value(field_value)
|
25
|
-
end.join('-')
|
35
|
+
friendly_id = convert_candidate_to_friendly_id(candidate)
|
26
36
|
next if friendly_id.blank?
|
27
37
|
|
28
38
|
friendly_id_used = self.class.where.not(id: self.id).find_by(friendly_id: friendly_id).present?
|
@@ -30,6 +40,20 @@ module WolfCore
|
|
30
40
|
|
31
41
|
self.friendly_id = friendly_id
|
32
42
|
end
|
43
|
+
|
44
|
+
return if self.friendly_id.present?
|
45
|
+
|
46
|
+
default_candidate = Array(friendly_id_id_candidates.first) + [:id]
|
47
|
+
self.friendly_id = convert_candidate_to_friendly_id(default_candidate)
|
48
|
+
end
|
49
|
+
|
50
|
+
def convert_candidate_to_friendly_id(candidate)
|
51
|
+
candidate = Array(candidate)
|
52
|
+
candidate.map! { |field| field.to_sym }.uniq!
|
53
|
+
candidate.map do |field|
|
54
|
+
field_value = send(field)
|
55
|
+
parse_field_value(field_value)
|
56
|
+
end.join('-')
|
33
57
|
end
|
34
58
|
|
35
59
|
def parse_field_value(field_value)
|
@@ -38,7 +62,7 @@ module WolfCore
|
|
38
62
|
end
|
39
63
|
|
40
64
|
def friendly_id_candidates
|
41
|
-
|
65
|
+
[:id]
|
42
66
|
end
|
43
67
|
end
|
44
68
|
end
|
data/lib/wolf_core/version.rb
CHANGED