wolf_core 1.0.67 → 1.0.69
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 +53 -21
- 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,44 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# In order to use this module on a model you only need to include it and implement the friendly_id_candidates method.
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
#
|
7
|
+
# class MyModel < ApplicationRecord
|
8
|
+
# include WolfCore::FriendlyModelId
|
9
|
+
#
|
10
|
+
# def friendly_id_candidates
|
11
|
+
# [[:name], [:name, :id]]
|
12
|
+
# end
|
13
|
+
# 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
|
+
# You can also override the set_friendly_id method to customize the friendly_id generation
|
18
|
+
|
1
19
|
module WolfCore
|
2
20
|
module FriendlyModelId
|
3
21
|
extend ActiveSupport::Concern
|
4
22
|
include WolfCore::StringUtils
|
5
23
|
|
6
24
|
included do
|
7
|
-
unless column_names.include?(
|
8
|
-
raise "Model #{self.name} does not have a friendly_id column"
|
9
|
-
end
|
25
|
+
raise "Model #{name} does not have a friendly_id column" unless column_names.include?("friendly_id")
|
10
26
|
|
11
27
|
scope :by_friendly_id, ->(friendly_id) { where(friendly_id: friendly_id) }
|
12
|
-
|
28
|
+
|
13
29
|
before_save :set_friendly_id
|
14
30
|
end
|
15
31
|
|
16
32
|
def set_friendly_id
|
17
|
-
return if
|
33
|
+
return if friendly_id.present?
|
18
34
|
|
19
|
-
|
20
|
-
|
35
|
+
self.friendly_id = generate_friendly_id
|
36
|
+
end
|
21
37
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
end.join('-')
|
26
|
-
next if friendly_id.blank?
|
38
|
+
def generate_friendly_id
|
39
|
+
friendly_id = find_unique_friendly_id
|
40
|
+
return friendly_id if friendly_id.present?
|
27
41
|
|
28
|
-
|
29
|
-
|
42
|
+
default_candidate = Array(friendly_id_candidates.first) + [:id]
|
43
|
+
convert_candidate_to_friendly_id(default_candidate)
|
44
|
+
end
|
30
45
|
|
31
|
-
|
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)
|
32
50
|
end
|
51
|
+
nil
|
33
52
|
end
|
34
53
|
|
35
|
-
def
|
36
|
-
|
37
|
-
remove_non_alphanumeric_chars(field_value, exceptions: '-')
|
54
|
+
def friendly_id_candidates
|
55
|
+
[:id]
|
38
56
|
end
|
39
57
|
|
40
|
-
def
|
41
|
-
|
58
|
+
def convert_candidate_to_friendly_id(candidate)
|
59
|
+
candidate = Array(candidate)
|
60
|
+
candidate.map!(&:to_sym).uniq!
|
61
|
+
candidate.map do |field|
|
62
|
+
field_value = send(field)
|
63
|
+
parse_field_value(field_value)
|
64
|
+
end.join("-")
|
65
|
+
end
|
66
|
+
|
67
|
+
def friendly_id_used?(friendly_id)
|
68
|
+
self.class.where.not(id: id).find_by(friendly_id: friendly_id).present?
|
69
|
+
end
|
70
|
+
|
71
|
+
def parse_field_value(field_value)
|
72
|
+
field_value = to_kebab_case(field_value)
|
73
|
+
remove_non_alphanumeric_chars(field_value, exceptions: "-")
|
42
74
|
end
|
43
75
|
end
|
44
|
-
end
|
76
|
+
end
|
data/lib/wolf_core/version.rb
CHANGED