active_permalink 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_permalink.rb +11 -0
- data/lib/active_permalink/config.rb +25 -0
- data/lib/active_permalink/generator.rb +15 -1
- data/lib/active_permalink/loader.rb +9 -16
- data/lib/active_permalink/localizer.rb +57 -36
- data/lib/active_permalink/permalink.rb +0 -2
- data/lib/active_permalink/persistence.rb +26 -9
- data/lib/active_permalink/querying.rb +0 -4
- data/lib/active_permalink/version.rb +1 -1
- data/lib/generators/active_permalink/install_generator.rb +4 -0
- data/lib/generators/active_permalink/templates/initializer.rb +8 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ffd6fe6ef2f0721cae7068fd6e5134917f6c7490f1b203e516eff02f467f3f5e
|
4
|
+
data.tar.gz: 7880019fab93553b42cab37f01f0ed60d5a665082c6e511bf368e92d8a933183
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5186051a831200b0651a816e089ac42737241eaeb981ebb6bb39d99822562bc36dbead824e172a9604cf1040eab0e4aec8368f637dbf670246dfbfa63b595ae1
|
7
|
+
data.tar.gz: 2c6e6ea08a68a7a1adc934e0731ad241d5912fc24533e58bd11c75662215730374b308fabc90a6752c852a8fa5c23440442b6968ca981091165bdd7f040264dd
|
data/lib/active_permalink.rb
CHANGED
@@ -12,6 +12,17 @@ module ActivePermalink
|
|
12
12
|
autoload :Localizer
|
13
13
|
autoload :Querying
|
14
14
|
autoload :Loader
|
15
|
+
autoload :Config
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def config
|
19
|
+
@config ||= Config.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def setup
|
23
|
+
yield config
|
24
|
+
end
|
25
|
+
end
|
15
26
|
end
|
16
27
|
|
17
28
|
ActiveSupport.on_load(:active_record) do
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
3
|
+
module ActivePermalink
|
4
|
+
class Config
|
5
|
+
include ActiveSupport::Configurable
|
6
|
+
|
7
|
+
config_accessor(:class_name) { 'ActivePermalink::Permalink' }
|
8
|
+
config_accessor(:querying) { true }
|
9
|
+
config_accessor(:localized) { false }
|
10
|
+
config_accessor(:locale_column) { :locale }
|
11
|
+
config_accessor(:locale_accessors) { true }
|
12
|
+
config_accessor(:fallbacks) { false }
|
13
|
+
|
14
|
+
def options_for(**options)
|
15
|
+
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
|
22
|
+
)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -1,6 +1,20 @@
|
|
1
1
|
module ActivePermalink
|
2
2
|
class Generator
|
3
|
+
class << self
|
4
|
+
def generate(record, value, locale = nil)
|
5
|
+
return if value.nil? && !record.slug_should_generate?
|
6
|
+
|
7
|
+
options = record.permalink_options.merge(locale: locale)
|
8
|
+
generator = Generator.new(record, options)
|
9
|
+
generator.generate(value)
|
10
|
+
|
11
|
+
record.permalinks = generator.permalinks
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
3
15
|
def initialize(record, options = {})
|
16
|
+
options[:locale] = options.fetch(:locale, I18n.locale).to_s
|
17
|
+
|
4
18
|
@record = record
|
5
19
|
@options = options
|
6
20
|
@field = options[:field]
|
@@ -22,7 +36,7 @@ module ActivePermalink
|
|
22
36
|
private
|
23
37
|
|
24
38
|
def locale
|
25
|
-
|
39
|
+
@options[:locale]
|
26
40
|
end
|
27
41
|
|
28
42
|
def changed?
|
@@ -3,18 +3,13 @@ module ActivePermalink
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
class_methods do
|
6
|
-
def has_permalink(field,
|
7
|
-
|
6
|
+
def has_permalink(field, **options)
|
7
|
+
options = ActivePermalink.config.options_for(field: field, **options)
|
8
8
|
|
9
9
|
class_attribute :permalink_options
|
10
|
+
self.permalink_options = options
|
10
11
|
|
11
|
-
|
12
|
-
field: field,
|
13
|
-
localized: localized,
|
14
|
-
locale_column: locale_column
|
15
|
-
)
|
16
|
-
|
17
|
-
with_options(as: :sluggable, class_name: 'ActivePermalink::Permalink') do
|
12
|
+
with_options(as: :sluggable, class_name: options[:class_name]) do
|
18
13
|
has_many :permalinks,
|
19
14
|
dependent: :destroy,
|
20
15
|
autosave: true
|
@@ -22,24 +17,22 @@ module ActivePermalink
|
|
22
17
|
has_many :old_permalinks,
|
23
18
|
-> { inactive }
|
24
19
|
|
25
|
-
if localized
|
20
|
+
if options[:localized]
|
26
21
|
has_many :active_permalinks,
|
27
22
|
-> { active }
|
28
23
|
|
29
24
|
has_one :active_permalink,
|
30
|
-
-> { active.where(locale_column => I18n.locale) }
|
25
|
+
-> { active.where(options[:locale_column] => I18n.locale) }
|
31
26
|
else
|
32
27
|
has_one :active_permalink,
|
33
28
|
-> { active }
|
34
29
|
end
|
35
30
|
end
|
36
31
|
|
37
|
-
delegate_attribute :slug, :string,
|
38
|
-
to: :active_permalink
|
39
|
-
|
40
32
|
include Persistence
|
41
|
-
|
42
|
-
include
|
33
|
+
|
34
|
+
include Querying if options[:querying]
|
35
|
+
include Localizer if options[:localized]
|
43
36
|
end
|
44
37
|
end
|
45
38
|
end
|
@@ -3,51 +3,54 @@ module ActivePermalink
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
def
|
7
|
-
|
6
|
+
def slug_backend
|
7
|
+
@slug_backend ||= PermalinkBackend.new(self)
|
8
8
|
end
|
9
9
|
|
10
|
+
alias permalink_reader slug_backend
|
11
|
+
|
10
12
|
def slug?
|
11
|
-
|
13
|
+
slug_backend.exists?(I18n.locale)
|
12
14
|
end
|
13
15
|
|
14
16
|
def slug
|
15
|
-
|
17
|
+
slug_backend.read(I18n.locale)
|
16
18
|
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
20
|
+
if permalink_options[:locale_accessors]
|
21
|
+
include SlugLocaleAccessors
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
I18n.with_locale(locale) { slug }
|
25
|
-
end
|
24
|
+
private
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
end
|
26
|
+
def _generate_permalink_slug(value)
|
27
|
+
slug_backend.write(value, I18n.locale)
|
30
28
|
end
|
31
29
|
end
|
32
30
|
|
33
|
-
class
|
34
|
-
attr_reader :permalinks, :options
|
31
|
+
class PermalinkBackend
|
32
|
+
attr_reader :record, :permalinks, :options
|
35
33
|
|
36
|
-
def initialize(
|
37
|
-
@
|
38
|
-
@options =
|
34
|
+
def initialize(record)
|
35
|
+
@record = record
|
36
|
+
@options = record.permalink_options
|
37
|
+
@permalinks = record.permalinks.to_a
|
39
38
|
end
|
40
39
|
|
41
40
|
def fallbacks?
|
42
41
|
options[:fallbacks].present?
|
43
42
|
end
|
44
43
|
|
45
|
-
def exists?
|
46
|
-
find_permalink(
|
44
|
+
def exists?(locale)
|
45
|
+
find_permalink(locale).present?
|
46
|
+
end
|
47
|
+
|
48
|
+
def read(locale)
|
49
|
+
find_slug(locale)
|
47
50
|
end
|
48
51
|
|
49
|
-
def value
|
50
|
-
|
52
|
+
def write(value, locale)
|
53
|
+
update_slug(value, locale)
|
51
54
|
end
|
52
55
|
|
53
56
|
private
|
@@ -56,23 +59,16 @@ module ActivePermalink
|
|
56
59
|
@options[:locale_column]
|
57
60
|
end
|
58
61
|
|
59
|
-
def find_permalink(
|
60
|
-
permalinks.find do |permalink|
|
61
|
-
permalink.send(locale_column)
|
62
|
+
def find_permalink(*locales)
|
63
|
+
permalinks.select(&:active?).find do |permalink|
|
64
|
+
locale = permalink.send(locale_column)
|
65
|
+
locales.include?(locale.to_sym)
|
62
66
|
end
|
63
67
|
end
|
64
68
|
|
65
69
|
def find_fallback(locale)
|
66
|
-
|
67
|
-
|
68
|
-
permalink = nil
|
69
|
-
|
70
|
-
fallbacks.find do |fallback|
|
71
|
-
permalink = find_permalink(fallback)
|
72
|
-
permalink.present?
|
73
|
-
end
|
74
|
-
|
75
|
-
permalink
|
70
|
+
locales = I18n.fallbacks[locale]
|
71
|
+
find_permalink(*locales) if locales.present?
|
76
72
|
end
|
77
73
|
|
78
74
|
def find_slug(locale)
|
@@ -81,6 +77,31 @@ module ActivePermalink
|
|
81
77
|
|
82
78
|
permalink.try(:slug)
|
83
79
|
end
|
80
|
+
|
81
|
+
def update_slug(value, locale)
|
82
|
+
Generator.generate(record, value, locale)
|
83
|
+
@permalinks = record.permalinks.to_a
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
module SlugLocaleAccessors
|
88
|
+
extend ActiveSupport::Concern
|
89
|
+
|
90
|
+
included do
|
91
|
+
I18n.available_locales.each do |locale|
|
92
|
+
define_method(:"slug_#{locale}?") do
|
93
|
+
slug_backend.exists?(locale)
|
94
|
+
end
|
95
|
+
|
96
|
+
define_method(:"slug_#{locale}") do
|
97
|
+
slug_backend.read(locale)
|
98
|
+
end
|
99
|
+
|
100
|
+
define_method(:"slug_#{locale}=") do |value|
|
101
|
+
slug_backend.write(value, locale)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
84
105
|
end
|
85
106
|
end
|
86
107
|
end
|
@@ -2,8 +2,6 @@ module ActivePermalink
|
|
2
2
|
class Permalink < ActiveRecord::Base
|
3
3
|
self.table_name = 'permalinks'
|
4
4
|
|
5
|
-
default_scope { order created_at: :desc }
|
6
|
-
|
7
5
|
scope :global, -> { where scope: :global }
|
8
6
|
scope :active, -> { where active: true }
|
9
7
|
scope :inactive, -> { where active: false }
|
@@ -3,21 +3,38 @@ module ActivePermalink
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
|
7
|
-
|
6
|
+
include ActiveDelegate
|
7
|
+
|
8
|
+
delegate_attribute :slug, :string,
|
9
|
+
to: :active_permalink
|
10
|
+
|
11
|
+
before_validation :slug_should_generate!,
|
12
|
+
on: [:create, :update],
|
13
|
+
unless: :slug?
|
14
|
+
|
15
|
+
def slug=(value)
|
16
|
+
_generate_permalink_slug(value)
|
8
17
|
end
|
9
18
|
|
10
|
-
|
11
|
-
|
19
|
+
def old_slugs
|
20
|
+
@old_slugs ||= old_permalinks.pluck(:slug)
|
12
21
|
end
|
13
22
|
|
14
|
-
def
|
15
|
-
|
23
|
+
def slug_should_generate?
|
24
|
+
@slug_should_generate == true
|
25
|
+
end
|
16
26
|
|
17
|
-
|
18
|
-
|
27
|
+
private
|
28
|
+
|
29
|
+
def slug_should_generate!
|
30
|
+
@slug_should_generate = true
|
31
|
+
_generate_permalink_slug(self[:slug])
|
32
|
+
ensure
|
33
|
+
@slug_should_generate = false
|
34
|
+
end
|
19
35
|
|
20
|
-
|
36
|
+
def _generate_permalink_slug(value)
|
37
|
+
Generator.generate(self, value)
|
21
38
|
end
|
22
39
|
end
|
23
40
|
end
|
@@ -13,6 +13,10 @@ module ActivePermalink
|
|
13
13
|
migration_template 'migration.rb', 'db/migrate/create_permalinks.rb'
|
14
14
|
end
|
15
15
|
|
16
|
+
def create_config_file
|
17
|
+
template 'initializer.rb', 'config/initializers/active_permalink.rb'
|
18
|
+
end
|
19
|
+
|
16
20
|
def self.next_migration_number(dirname)
|
17
21
|
::ActiveRecord::Generators::Base.next_migration_number(dirname)
|
18
22
|
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
|
+
version: 0.4.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-
|
11
|
+
date: 2020-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -112,6 +112,7 @@ files:
|
|
112
112
|
- README.md
|
113
113
|
- Rakefile
|
114
114
|
- lib/active_permalink.rb
|
115
|
+
- lib/active_permalink/config.rb
|
115
116
|
- lib/active_permalink/generator.rb
|
116
117
|
- lib/active_permalink/loader.rb
|
117
118
|
- lib/active_permalink/localizer.rb
|
@@ -120,6 +121,7 @@ files:
|
|
120
121
|
- lib/active_permalink/querying.rb
|
121
122
|
- lib/active_permalink/version.rb
|
122
123
|
- lib/generators/active_permalink/install_generator.rb
|
124
|
+
- lib/generators/active_permalink/templates/initializer.rb
|
123
125
|
- lib/generators/active_permalink/templates/migration.rb
|
124
126
|
homepage: https://github.com/hardpixel/active-permalink
|
125
127
|
licenses:
|
@@ -140,7 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
140
142
|
- !ruby/object:Gem::Version
|
141
143
|
version: '0'
|
142
144
|
requirements: []
|
143
|
-
rubygems_version: 3.
|
145
|
+
rubygems_version: 3.1.2
|
144
146
|
signing_key:
|
145
147
|
specification_version: 4
|
146
148
|
summary: Add permalinks to ActiveRecord models
|