active_permalink 0.4.0 → 0.5.0

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: ffd6fe6ef2f0721cae7068fd6e5134917f6c7490f1b203e516eff02f467f3f5e
4
- data.tar.gz: 7880019fab93553b42cab37f01f0ed60d5a665082c6e511bf368e92d8a933183
3
+ metadata.gz: 5b035a60dc56c8501fd344655442f7b6e09c8c03657e99058c6690e2849a4b97
4
+ data.tar.gz: 25d2e648c1f8147d7b05cd20ebabc4567c9dd1a303977dbeae51efa584377055
5
5
  SHA512:
6
- metadata.gz: 5186051a831200b0651a816e089ac42737241eaeb981ebb6bb39d99822562bc36dbead824e172a9604cf1040eab0e4aec8368f637dbf670246dfbfa63b595ae1
7
- data.tar.gz: 2c6e6ea08a68a7a1adc934e0731ad241d5912fc24533e58bd11c75662215730374b308fabc90a6752c852a8fa5c23440442b6968ca981091165bdd7f040264dd
6
+ metadata.gz: 697f482ff529dfc72707ac3701d1cd2a33ec516477c7454e6b1599bd629fa81a6a9de37cbbb9ba706b49cb158fe2bdd1c31cfaa4882aa94e012e3bcbef1c05d5
7
+ data.tar.gz: 509f6ea30cb4cdda4fbd7f565024f17c79099b50ebad9ef49b4a2dabd07408c5bffaf40ef0bf39fe15ea0d81a758763907c3bfa4c143aacba8801bbac2af55b1
@@ -9,16 +9,18 @@ module ActivePermalink
9
9
  config_accessor(:localized) { false }
10
10
  config_accessor(:locale_column) { :locale }
11
11
  config_accessor(:locale_accessors) { true }
12
+ config_accessor(:fallthrough_accessors) { false }
12
13
  config_accessor(:fallbacks) { false }
13
14
 
14
15
  def options_for(**options)
15
16
  options.reverse_merge(
16
- class_name: class_name,
17
- querying: querying,
18
- localized: localized,
19
- locale_column: locale_column,
20
- locale_accessors: locale_accessors,
21
- fallbacks: fallbacks
17
+ class_name: class_name,
18
+ querying: querying,
19
+ localized: localized,
20
+ locale_column: locale_column,
21
+ locale_accessors: locale_accessors,
22
+ fallthrough_accessors: fallthrough_accessors,
23
+ fallbacks: fallbacks
22
24
  )
23
25
  end
24
26
  end
@@ -9,16 +9,22 @@ module ActivePermalink
9
9
 
10
10
  alias permalink_reader slug_backend
11
11
 
12
- def slug?
13
- slug_backend.exists?(I18n.locale)
12
+ def slug?(locale: nil)
13
+ slug_backend.exists?(locale || I18n.locale)
14
14
  end
15
15
 
16
- def slug
17
- slug_backend.read(I18n.locale)
16
+ def slug(locale: nil)
17
+ slug_backend.read(locale || I18n.locale)
18
+ end
19
+
20
+ def slug=(value, locale: nil)
21
+ slug_backend.write(value, locale || I18n.locale)
18
22
  end
19
23
 
20
24
  if permalink_options[:locale_accessors]
21
25
  include SlugLocaleAccessors
26
+ elsif permalink_options[:fallthrough_accessors]
27
+ include SlugFallthroughAccessors
22
28
  end
23
29
 
24
30
  private
@@ -28,6 +34,9 @@ module ActivePermalink
28
34
  end
29
35
  end
30
36
 
37
+ class InvalidLocale < I18n::InvalidLocale
38
+ end
39
+
31
40
  class PermalinkBackend
32
41
  attr_reader :record, :permalinks, :options
33
42
 
@@ -42,19 +51,29 @@ module ActivePermalink
42
51
  end
43
52
 
44
53
  def exists?(locale)
54
+ enforce_available_locales!(locale)
45
55
  find_permalink(locale).present?
46
56
  end
47
57
 
48
58
  def read(locale)
59
+ enforce_available_locales!(locale)
49
60
  find_slug(locale)
50
61
  end
51
62
 
52
63
  def write(value, locale)
64
+ enforce_available_locales!(locale)
53
65
  update_slug(value, locale)
54
66
  end
55
67
 
56
68
  private
57
69
 
70
+ def enforce_available_locales!(locale)
71
+ return unless I18n.enforce_available_locales
72
+ return if I18n.available_locales.include?(locale.to_sym)
73
+
74
+ raise InvalidLocale.new(locale)
75
+ end
76
+
58
77
  def locale_column
59
78
  @options[:locale_column]
60
79
  end
@@ -103,5 +122,27 @@ module ActivePermalink
103
122
  end
104
123
  end
105
124
  end
125
+
126
+ module SlugFallthroughAccessors
127
+ extend ActiveSupport::Concern
128
+
129
+ included do
130
+ method_name_regex = /\Aslug_([a-z]{2}(_[a-z]{2})?)(=?|\??)\z/.freeze
131
+
132
+ define_method :method_missing do |method_name, *arguments, **options, &block|
133
+ if method_name =~ method_name_regex
134
+ locale, suffix = $1.split('_')
135
+ locale = "#{locale}-#{suffix.upcase}" if suffix
136
+ public_send("slug#{$3}", *arguments, **options, locale: locale.to_sym)
137
+ else
138
+ super(method_name, *arguments, &block)
139
+ end
140
+ end
141
+
142
+ define_method :respond_to_missing? do |method_name, include_private = false|
143
+ (method_name =~ method_name_regex) || super(method_name, include_private)
144
+ end
145
+ end
146
+ end
106
147
  end
107
148
  end
@@ -10,7 +10,7 @@ module ActivePermalink
10
10
 
11
11
  before_validation :slug_should_generate!,
12
12
  on: [:create, :update],
13
- unless: :slug?
13
+ if: :slug_needs_generate?
14
14
 
15
15
  def slug=(value)
16
16
  _generate_permalink_slug(value)
@@ -26,6 +26,10 @@ module ActivePermalink
26
26
 
27
27
  private
28
28
 
29
+ def slug_needs_generate?
30
+ !slug? && send(:"#{permalink_options[:field]}_changed?")
31
+ end
32
+
29
33
  def slug_should_generate!
30
34
  @slug_should_generate = true
31
35
  _generate_permalink_slug(self[:slug])
@@ -1,3 +1,3 @@
1
1
  module ActivePermalink
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_permalink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonian Guveli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-02 00:00:00.000000000 Z
11
+ date: 2020-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord