wolf_core 1.0.68 → 1.0.70

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: bc7e1aed79786c312930031227ec5cbd339d3415a22178ec0539f606f43abb89
4
+ data.tar.gz: 4e3245f77f19ad9d00b1eb11258e4c0828694938a06bf647f45988488b7f6158
5
5
  SHA512:
6
- metadata.gz: 44b79a76c0b17651d393a7bd523acf3adf14dc93211d7d7a9eb32a6a38759f5041dfa0ea1620bf8e8b95e6392c97eefefdca1b3d2cdbdf2e54c99853f3c6272b
7
- data.tar.gz: f6b3a6515df0d2868071cc483b47fa166459b2aeb31cfef5c9b50c0509c6fb9cbd41ae9f642c95bea550943bba9177ea055ca402db5c132cc7ad2a0c07ef090d
6
+ metadata.gz: 7e1a7e8f0020bc1e1a4b1ed6edb861680cb543167d431b76b47153bd8ee9e536fe4e5662a26728d8205a4f282d9c32b382c1f3704c3f8d267b9c7a286a5741f2
7
+ data.tar.gz: dc29ae8cf0eb87f0c3bede9f273bbfb924d5fbc66df9d935b28863d92f8638553541df92e1671602e86501c594af157cc698abe94006618b95972262653fb6d2
@@ -1,4 +1,9 @@
1
- # In order to use this module on a model you only need to include it and implement the friendly_id_candidates method.
1
+ # frozen_string_literal: true
2
+
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.
2
7
  #
3
8
  # Example:
4
9
  #
@@ -6,12 +11,15 @@
6
11
  # include WolfCore::FriendlyModelId
7
12
  #
8
13
  # def friendly_id_candidates
9
- # [[:name], [:name, :id]]
14
+ # [:fullname, :address]
10
15
  # end
11
16
  # 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
17
  #
14
- # You can also override the set_friendly_id method to customize the friendly_id generation
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.
15
23
 
16
24
  module WolfCore
17
25
  module FriendlyModelId
@@ -19,50 +27,59 @@ module WolfCore
19
27
  include WolfCore::StringUtils
20
28
 
21
29
  included do
22
- unless column_names.include?('friendly_id')
23
- raise "Model #{self.name} does not have a friendly_id column"
24
- end
30
+ raise "Model #{name} does not have a friendly_id column" unless column_names.include?("friendly_id")
25
31
 
26
32
  scope :by_friendly_id, ->(friendly_id) { where(friendly_id: friendly_id) }
27
-
33
+
28
34
  before_save :set_friendly_id
29
35
  end
30
36
 
31
37
  def set_friendly_id
32
- return if self.friendly_id.present?
38
+ return if friendly_id.present?
33
39
 
34
- friendly_id_id_candidates.each do |candidate|
35
- friendly_id = convert_candidate_to_friendly_id(candidate)
36
- next if friendly_id.blank?
40
+ self.friendly_id = generate_friendly_id
41
+ end
37
42
 
38
- friendly_id_used = self.class.where.not(id: self.id).find_by(friendly_id: friendly_id).present?
39
- next if field_value_used
43
+ def generate_friendly_id
44
+ friendly_id = find_unique_friendly_id
45
+ return friendly_id if friendly_id.present?
40
46
 
41
- self.friendly_id = friendly_id
47
+ default_candidate = friendly_id_candidates_array.first + [:id]
48
+ convert_candidate_to_friendly_id(default_candidate)
49
+ end
50
+
51
+ def find_unique_friendly_id
52
+ friendly_id_candidates_array.each do |candidate|
53
+ friendly_id = convert_candidate_to_friendly_id(candidate)
54
+ return friendly_id if friendly_id.present? && !friendly_id_used?(friendly_id)
42
55
  end
56
+ nil
57
+ end
43
58
 
44
- return if self.friendly_id.present?
59
+ def friendly_id_candidates
60
+ [:id]
61
+ end
45
62
 
46
- default_candidate = Array(friendly_id_id_candidates.first) + [:id]
47
- self.friendly_id = convert_candidate_to_friendly_id(default_candidate)
63
+ def friendly_id_candidates_array
64
+ Array(friendly_id_candidates)
48
65
  end
49
66
 
50
67
  def convert_candidate_to_friendly_id(candidate)
51
68
  candidate = Array(candidate)
52
- candidate.map! { |field| field.to_sym }.uniq!
69
+ candidate.map!(&:to_sym).uniq!
53
70
  candidate.map do |field|
54
71
  field_value = send(field)
55
72
  parse_field_value(field_value)
56
- end.join('-')
73
+ end.join("-")
57
74
  end
58
75
 
59
- def parse_field_value(field_value)
60
- field_value = to_kebab_case(field_value)
61
- remove_non_alphanumeric_chars(field_value, exceptions: '-')
76
+ def friendly_id_used?(friendly_id)
77
+ self.class.where.not(id: id).find_by(friendly_id: friendly_id).present?
62
78
  end
63
79
 
64
- def friendly_id_candidates
65
- [:id]
80
+ def parse_field_value(field_value)
81
+ field_value = to_kebab_case(field_value)
82
+ remove_non_alphanumeric_chars(field_value, exceptions: "-")
66
83
  end
67
84
  end
68
- end
85
+ 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.70"
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.70
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo