wolf_core 1.0.67 → 1.0.68

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: 1fd91295408ff2f54a66a3c894a46c34cbf8ba9c79d0c5d2604cfcef554e2ab0
4
+ data.tar.gz: 28a7c16e6a124fa97cc80b1a9b8decc9d4446c72d92eb4905e712f0921fec22c
5
5
  SHA512:
6
- metadata.gz: 3489825ef30779df10ce818c9f44cb044b236362025b10fe5088a3e7fd9a0e496cae3deae9052d1f07323c609a801c287c0b311cf27a4dfadd7024df45ccd153
7
- data.tar.gz: f2cef0c7a7d517777f469c344f535b256cfe40283c1685936fb1503b870e1319f250ae1cce52b82e0e4fcdcfa8e1a737b81f5e665e764fb956ce02bfb717f2b5
6
+ metadata.gz: 44b79a76c0b17651d393a7bd523acf3adf14dc93211d7d7a9eb32a6a38759f5041dfa0ea1620bf8e8b95e6392c97eefefdca1b3d2cdbdf2e54c99853f3c6272b
7
+ data.tar.gz: f6b3a6515df0d2868071cc483b47fa166459b2aeb31cfef5c9b50c0509c6fb9cbd41ae9f642c95bea550943bba9177ea055ca402db5c132cc7ad2a0c07ef090d
@@ -1,3 +1,18 @@
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
+
1
16
  module WolfCore
2
17
  module FriendlyModelId
3
18
  extend ActiveSupport::Concern
@@ -17,12 +32,7 @@ module WolfCore
17
32
  return if self.friendly_id.present?
18
33
 
19
34
  friendly_id_id_candidates.each do |candidate|
20
- candidate = Array(candidate)
21
-
22
- friendly_id = candidate.map do |field|
23
- field_value = send(field)
24
- parse_field_value(field_value)
25
- end.join('-')
35
+ friendly_id = convert_candidate_to_friendly_id(candidate)
26
36
  next if friendly_id.blank?
27
37
 
28
38
  friendly_id_used = self.class.where.not(id: self.id).find_by(friendly_id: friendly_id).present?
@@ -30,6 +40,20 @@ module WolfCore
30
40
 
31
41
  self.friendly_id = friendly_id
32
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('-')
33
57
  end
34
58
 
35
59
  def parse_field_value(field_value)
@@ -38,7 +62,7 @@ module WolfCore
38
62
  end
39
63
 
40
64
  def friendly_id_candidates
41
- raise NotImplementedError
65
+ [:id]
42
66
  end
43
67
  end
44
68
  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.68"
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.68
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo