wolf_core 1.0.66 → 1.0.68

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: ebc787c1df3765c741ea7eadaae3f24acaae3318a982266c1d421c8147ff6e92
4
- data.tar.gz: 1b521b606a8ef09504d4444c6b3c6ae65f9504177a55c99a2b9945f3965838f5
3
+ metadata.gz: 1fd91295408ff2f54a66a3c894a46c34cbf8ba9c79d0c5d2604cfcef554e2ab0
4
+ data.tar.gz: 28a7c16e6a124fa97cc80b1a9b8decc9d4446c72d92eb4905e712f0921fec22c
5
5
  SHA512:
6
- metadata.gz: 5ad0b112786b8f29b5b2eb3798f9128a573594f684393b915804767be41dd75b008f57cc688f3a254f129e29864dc2bde41b823c9c02f86f80b2d0f6df47c19c
7
- data.tar.gz: 8506ca870e4fee626223ad32222c3fa4874496f7291ed4d2675b654d51d50f427db371593d7fe616e31b849a7bf3394b3791c288c7cc6ebc9b773803e3cb56ad
6
+ metadata.gz: 44b79a76c0b17651d393a7bd523acf3adf14dc93211d7d7a9eb32a6a38759f5041dfa0ea1620bf8e8b95e6392c97eefefdca1b3d2cdbdf2e54c99853f3c6272b
7
+ data.tar.gz: f6b3a6515df0d2868071cc483b47fa166459b2aeb31cfef5c9b50c0509c6fb9cbd41ae9f642c95bea550943bba9177ea055ca402db5c132cc7ad2a0c07ef090d
@@ -0,0 +1,68 @@
1
+ # In order to use this module on a model you only need to include it and implement the friendly_id_candidates method.
2
+ #
3
+ # Example:
4
+ #
5
+ # class MyModel < ApplicationRecord
6
+ # include WolfCore::FriendlyModelId
7
+ #
8
+ # def friendly_id_candidates
9
+ # [[:name], [:name, :id]]
10
+ # end
11
+ # 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
+ #
14
+ # You can also override the set_friendly_id method to customize the friendly_id generation
15
+
16
+ module WolfCore
17
+ module FriendlyModelId
18
+ extend ActiveSupport::Concern
19
+ include WolfCore::StringUtils
20
+
21
+ included do
22
+ unless column_names.include?('friendly_id')
23
+ raise "Model #{self.name} does not have a friendly_id column"
24
+ end
25
+
26
+ scope :by_friendly_id, ->(friendly_id) { where(friendly_id: friendly_id) }
27
+
28
+ before_save :set_friendly_id
29
+ end
30
+
31
+ def set_friendly_id
32
+ return if self.friendly_id.present?
33
+
34
+ friendly_id_id_candidates.each do |candidate|
35
+ friendly_id = convert_candidate_to_friendly_id(candidate)
36
+ next if friendly_id.blank?
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
40
+
41
+ self.friendly_id = friendly_id
42
+ end
43
+
44
+ return if self.friendly_id.present?
45
+
46
+ default_candidate = Array(friendly_id_id_candidates.first) + [:id]
47
+ self.friendly_id = convert_candidate_to_friendly_id(default_candidate)
48
+ end
49
+
50
+ def convert_candidate_to_friendly_id(candidate)
51
+ candidate = Array(candidate)
52
+ candidate.map! { |field| field.to_sym }.uniq!
53
+ candidate.map do |field|
54
+ field_value = send(field)
55
+ parse_field_value(field_value)
56
+ end.join('-')
57
+ end
58
+
59
+ def parse_field_value(field_value)
60
+ field_value = to_kebab_case(field_value)
61
+ remove_non_alphanumeric_chars(field_value, exceptions: '-')
62
+ end
63
+
64
+ def friendly_id_candidates
65
+ [:id]
66
+ end
67
+ end
68
+ end
@@ -5,6 +5,22 @@ module WolfCore
5
5
  str.gsub(/([A-Z])/, ' \1').strip.downcase.capitalize
6
6
  end
7
7
 
8
+ def to_snake_case(str)
9
+ str.gsub(/\s+/, '_').downcase
10
+ end
11
+
12
+ def to_kebab_case(str)
13
+ str.gsub(/\s+/, '-').downcase
14
+ end
15
+
16
+ def remove_non_alphanumeric_chars(str, exceptions: nil)
17
+ exceptions ||= []
18
+ exceptions = Array(exceptions)
19
+ escaped_exceptions = exceptions.map { |char| Regexp.escape(char) }.join
20
+ regex = /[^a-zA-Z0-9#{escaped_exceptions}]/
21
+ str.gsub(regex, '')
22
+ end
23
+
8
24
  def clean_phone_number(phone)
9
25
  return if phone.nil?
10
26
  cleaned_phone = phone.to_s.gsub(/\D/, '')
@@ -46,11 +62,5 @@ module WolfCore
46
62
  base64_regex = /^[A-Za-z0-9+\/]+={0,2}$/
47
63
  str.match?(base64_regex)
48
64
  end
49
-
50
- def to_snake_case(str)
51
- str.gsub(/[^a-zA-Z0-9\s_]/, '')
52
- .gsub(/\s+/, '_')
53
- .downcase
54
- end
55
65
  end
56
66
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WolfCore
4
- VERSION = "1.0.66"
4
+ VERSION = "1.0.68"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wolf_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.66
4
+ version: 1.0.68
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-01-24 00:00:00.000000000 Z
11
+ date: 2025-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -130,6 +130,7 @@ files:
130
130
  - lib/wolf_core/utils/array_utils.rb
131
131
  - lib/wolf_core/utils/async_utils.rb
132
132
  - lib/wolf_core/utils/file_utils.rb
133
+ - lib/wolf_core/utils/friendly_model_id.rb
133
134
  - lib/wolf_core/utils/logging_utils.rb
134
135
  - lib/wolf_core/utils/result.rb
135
136
  - lib/wolf_core/utils/string_utils.rb