active_permalink 0.4.0 → 0.5.3

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: ffd6fe6ef2f0721cae7068fd6e5134917f6c7490f1b203e516eff02f467f3f5e
4
- data.tar.gz: 7880019fab93553b42cab37f01f0ed60d5a665082c6e511bf368e92d8a933183
3
+ metadata.gz: df15e663dd9dd5bb8b4b873ccdd92a6d0b116547d143336ed9520bd4ac25ea81
4
+ data.tar.gz: 3bd4f5b7cdaef63d23ffd393647a4207d5d57c2ded5c98838e92ad91c1b62dcd
5
5
  SHA512:
6
- metadata.gz: 5186051a831200b0651a816e089ac42737241eaeb981ebb6bb39d99822562bc36dbead824e172a9604cf1040eab0e4aec8368f637dbf670246dfbfa63b595ae1
7
- data.tar.gz: 2c6e6ea08a68a7a1adc934e0731ad241d5912fc24533e58bd11c75662215730374b308fabc90a6752c852a8fa5c23440442b6968ca981091165bdd7f040264dd
6
+ metadata.gz: d14ea28b8b4d5ef42ad0272f3a265c51dba1aa07f42349dcea0ed7c8c35e36c8b416f069e81573ce7e284509d31c7c8f6053d9bdcd925b1475208129f968a0d2
7
+ data.tar.gz: 1086cd95f88ba1a9d516026fa5376f549209a06b93334efe4a68ae7fc95dd35b53c19e1da2ddaa2f30bda46787c5f92e2633405eb44ae8391d7e6303f1df97c2
@@ -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
@@ -1,10 +1,12 @@
1
+ require 'any_ascii'
2
+
1
3
  module ActivePermalink
2
4
  class Generator
3
5
  class << self
4
- def generate(record, value, locale = nil)
6
+ def generate(record, value, locale = nil, raw: false)
5
7
  return if value.nil? && !record.slug_should_generate?
6
8
 
7
- options = record.permalink_options.merge(locale: locale)
9
+ options = record.permalink_options.merge(locale: locale, raw: raw)
8
10
  generator = Generator.new(record, options)
9
11
  generator.generate(value)
10
12
 
@@ -89,7 +91,12 @@ module ActivePermalink
89
91
  def slug_from_column
90
92
  @slug_from_column ||= begin
91
93
  value = @new_value.presence || @record.send(@field)
92
- value.to_s.to_url
94
+ return if value.blank?
95
+
96
+ return value if @options[:raw] == true
97
+
98
+ value = AnyAscii.transliterate(value)
99
+ value.parameterize
93
100
  end
94
101
  end
95
102
 
@@ -9,25 +9,34 @@ 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
25
31
 
26
- def _generate_permalink_slug(value)
27
- slug_backend.write(value, I18n.locale)
32
+ def _generate_permalink_slug(value, **options)
33
+ slug_backend.write(value, I18n.locale, **options)
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
- def write(value, locale)
53
- update_slug(value, locale)
63
+ def write(value, locale, **options)
64
+ enforce_available_locales!(locale)
65
+ update_slug(value, locale, **options)
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
@@ -78,8 +97,8 @@ module ActivePermalink
78
97
  permalink.try(:slug)
79
98
  end
80
99
 
81
- def update_slug(value, locale)
82
- Generator.generate(record, value, locale)
100
+ def update_slug(value, locale, **options)
101
+ Generator.generate(record, value, locale, **options)
83
102
  @permalinks = record.permalinks.to_a
84
103
  end
85
104
  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,11 @@ 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
+
15
+ def raw_slug=(value)
16
+ _generate_permalink_slug(value, raw: true)
17
+ end
14
18
 
15
19
  def slug=(value)
16
20
  _generate_permalink_slug(value)
@@ -26,6 +30,10 @@ module ActivePermalink
26
30
 
27
31
  private
28
32
 
33
+ def slug_needs_generate?
34
+ !slug? && send(:"#{permalink_options[:field]}_changed?")
35
+ end
36
+
29
37
  def slug_should_generate!
30
38
  @slug_should_generate = true
31
39
  _generate_permalink_slug(self[:slug])
@@ -33,8 +41,8 @@ module ActivePermalink
33
41
  @slug_should_generate = false
34
42
  end
35
43
 
36
- def _generate_permalink_slug(value)
37
- Generator.generate(self, value)
44
+ def _generate_permalink_slug(value, **options)
45
+ Generator.generate(self, value, **options)
38
46
  end
39
47
  end
40
48
  end
@@ -1,3 +1,3 @@
1
1
  module ActivePermalink
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.3'
3
3
  end
@@ -1,4 +1,3 @@
1
- require 'stringex'
2
1
  require 'active_record'
3
2
  require 'active_delegate'
4
3
  require 'active_permalink/version'
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.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonian Guveli
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-02 00:00:00.000000000 Z
11
+ date: 2022-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -45,19 +45,19 @@ dependencies:
45
45
  - !ruby/object:Gem::Version
46
46
  version: '1.0'
47
47
  - !ruby/object:Gem::Dependency
48
- name: stringex
48
+ name: any_ascii
49
49
  requirement: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - "~>"
52
52
  - !ruby/object:Gem::Version
53
- version: '2.0'
53
+ version: '0.1'
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - "~>"
59
59
  - !ruby/object:Gem::Version
60
- version: '2.0'
60
+ version: '0.1'
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: bundler
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -127,7 +127,7 @@ homepage: https://github.com/hardpixel/active-permalink
127
127
  licenses:
128
128
  - MIT
129
129
  metadata: {}
130
- post_install_message:
130
+ post_install_message:
131
131
  rdoc_options: []
132
132
  require_paths:
133
133
  - lib
@@ -142,8 +142,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
142
  - !ruby/object:Gem::Version
143
143
  version: '0'
144
144
  requirements: []
145
- rubygems_version: 3.1.2
146
- signing_key:
145
+ rubygems_version: 3.1.6
146
+ signing_key:
147
147
  specification_version: 4
148
148
  summary: Add permalinks to ActiveRecord models
149
149
  test_files: []