wolf_core 1.0.69 → 1.0.71
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 +16 -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: c2e6081627f490bed11dd13370491f42735a1de3b7dad63bc35789cefa2d5dbb
|
4
|
+
data.tar.gz: 9860dea6b35f8398fa5b5c75bb0a8da22202da1444228301bc4a2b42ba8adb2c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce8ca164abbc65923a382e938fac533dec11c93fea768bf20648112edcda2d29bfd9df8a7087d3e68c8b387ee5c46cfd38b021e7775652f2df62e623f587f658
|
7
|
+
data.tar.gz: 1e53e8c06581844f81dc9300be83dbf4ceab46e12fd297fc192d4f41b61c9429b726266120f35c750b2ff707ebfa727ff67a59fbf1dc4107e07ffca49e91c49b
|
@@ -1,6 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
#
|
3
|
+
# Steps to use this module:
|
4
|
+
# 1. Create a migration to add the friendly_id column to your model.
|
5
|
+
# 2. Include the WolfCore::FriendlyModelId in your model.
|
6
|
+
# 3. Implement the friendly_id_candidates method in your model.
|
4
7
|
#
|
5
8
|
# Example:
|
6
9
|
#
|
@@ -8,13 +11,15 @@
|
|
8
11
|
# include WolfCore::FriendlyModelId
|
9
12
|
#
|
10
13
|
# def friendly_id_candidates
|
11
|
-
# [
|
14
|
+
# [:fullname, :address]
|
12
15
|
# end
|
13
16
|
# end
|
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.
|
16
17
|
#
|
17
|
-
#
|
18
|
+
# This will generate a friendly_id based on the fullname field.
|
19
|
+
# If the fullname is not unique, it will attempt to use the address field instead.
|
20
|
+
# If neither can be used to generate a unique friendly_id, it will append the id to
|
21
|
+
# the value of the first field name in the array returned by the friendly_id_candidates
|
22
|
+
# method, which is fullname in this case.
|
18
23
|
|
19
24
|
module WolfCore
|
20
25
|
module FriendlyModelId
|
@@ -39,12 +44,12 @@ module WolfCore
|
|
39
44
|
friendly_id = find_unique_friendly_id
|
40
45
|
return friendly_id if friendly_id.present?
|
41
46
|
|
42
|
-
default_candidate = Array(
|
47
|
+
default_candidate = Array(friendly_id_candidates_array.first) + [:id]
|
43
48
|
convert_candidate_to_friendly_id(default_candidate)
|
44
49
|
end
|
45
50
|
|
46
51
|
def find_unique_friendly_id
|
47
|
-
|
52
|
+
friendly_id_candidates_array.each do |candidate|
|
48
53
|
friendly_id = convert_candidate_to_friendly_id(candidate)
|
49
54
|
return friendly_id if friendly_id.present? && !friendly_id_used?(friendly_id)
|
50
55
|
end
|
@@ -55,6 +60,10 @@ module WolfCore
|
|
55
60
|
[:id]
|
56
61
|
end
|
57
62
|
|
63
|
+
def friendly_id_candidates_array
|
64
|
+
Array(friendly_id_candidates)
|
65
|
+
end
|
66
|
+
|
58
67
|
def convert_candidate_to_friendly_id(candidate)
|
59
68
|
candidate = Array(candidate)
|
60
69
|
candidate.map!(&:to_sym).uniq!
|
data/lib/wolf_core/version.rb
CHANGED