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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1fd91295408ff2f54a66a3c894a46c34cbf8ba9c79d0c5d2604cfcef554e2ab0
4
- data.tar.gz: 28a7c16e6a124fa97cc80b1a9b8decc9d4446c72d92eb4905e712f0921fec22c
3
+ metadata.gz: 4d5fa238459aefb14f513bd81c063aff7227a31f27ff2b3d56fbc98b4cfcd38b
4
+ data.tar.gz: ec08c21d261cde65e3107cde28b3d297d797ee27724df5996b5aea23e43b4755
5
5
  SHA512:
6
- metadata.gz: 44b79a76c0b17651d393a7bd523acf3adf14dc93211d7d7a9eb32a6a38759f5041dfa0ea1620bf8e8b95e6392c97eefefdca1b3d2cdbdf2e54c99853f3c6272b
7
- data.tar.gz: f6b3a6515df0d2868071cc483b47fa166459b2aeb31cfef5c9b50c0509c6fb9cbd41ae9f642c95bea550943bba9177ea055ca402db5c132cc7ad2a0c07ef090d
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, if it is not unique it will append the id to the friendly_id.
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?('friendly_id')
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 self.friendly_id.present?
33
+ return if friendly_id.present?
33
34
 
34
- friendly_id_id_candidates.each do |candidate|
35
- friendly_id = convert_candidate_to_friendly_id(candidate)
36
- next if friendly_id.blank?
35
+ self.friendly_id = generate_friendly_id
36
+ end
37
37
 
38
- friendly_id_used = self.class.where.not(id: self.id).find_by(friendly_id: friendly_id).present?
39
- next if field_value_used
38
+ def generate_friendly_id
39
+ friendly_id = find_unique_friendly_id
40
+ return friendly_id if friendly_id.present?
40
41
 
41
- self.friendly_id = friendly_id
42
- end
42
+ default_candidate = Array(friendly_id_candidates.first) + [:id]
43
+ convert_candidate_to_friendly_id(default_candidate)
44
+ end
43
45
 
44
- return if self.friendly_id.present?
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
- default_candidate = Array(friendly_id_id_candidates.first) + [:id]
47
- self.friendly_id = convert_candidate_to_friendly_id(default_candidate)
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! { |field| field.to_sym }.uniq!
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 parse_field_value(field_value)
60
- field_value = to_kebab_case(field_value)
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 friendly_id_candidates
65
- [:id]
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WolfCore
4
- VERSION = "1.0.68"
4
+ VERSION = "1.0.69"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wolf_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.68
4
+ version: 1.0.69
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo