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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99535923be72a8bcbb80245eaa43c9b15633b274b82bf01f5e1363174656f83a
4
- data.tar.gz: 7d3aa39f2ffd464a5aa0dedf9396fff2bf6af1ca72c39efc2dedf5d092e18110
3
+ metadata.gz: 4d5fa238459aefb14f513bd81c063aff7227a31f27ff2b3d56fbc98b4cfcd38b
4
+ data.tar.gz: ec08c21d261cde65e3107cde28b3d297d797ee27724df5996b5aea23e43b4755
5
5
  SHA512:
6
- metadata.gz: 3489825ef30779df10ce818c9f44cb044b236362025b10fe5088a3e7fd9a0e496cae3deae9052d1f07323c609a801c287c0b311cf27a4dfadd7024df45ccd153
7
- data.tar.gz: f2cef0c7a7d517777f469c344f535b256cfe40283c1685936fb1503b870e1319f250ae1cce52b82e0e4fcdcfa8e1a737b81f5e665e764fb956ce02bfb717f2b5
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?('friendly_id')
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 self.friendly_id.present?
33
+ return if friendly_id.present?
18
34
 
19
- friendly_id_id_candidates.each do |candidate|
20
- candidate = Array(candidate)
35
+ self.friendly_id = generate_friendly_id
36
+ end
21
37
 
22
- friendly_id = candidate.map do |field|
23
- field_value = send(field)
24
- parse_field_value(field_value)
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
- friendly_id_used = self.class.where.not(id: self.id).find_by(friendly_id: friendly_id).present?
29
- next if field_value_used
42
+ default_candidate = Array(friendly_id_candidates.first) + [:id]
43
+ convert_candidate_to_friendly_id(default_candidate)
44
+ end
30
45
 
31
- self.friendly_id = friendly_id
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 parse_field_value(field_value)
36
- field_value = to_kebab_case(field_value)
37
- remove_non_alphanumeric_chars(field_value, exceptions: '-')
54
+ def friendly_id_candidates
55
+ [:id]
38
56
  end
39
57
 
40
- def friendly_id_candidates
41
- raise NotImplementedError
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WolfCore
4
- VERSION = "1.0.67"
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.67
4
+ version: 1.0.69
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo