vidibus-permalink 0.1.9 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a0f6739c06733a192c1cc9d64e185be44323af2a
4
+ data.tar.gz: 87b4010b49dab36fbe7e7bd23a0566a046721fd2
5
+ SHA512:
6
+ metadata.gz: a32a63c0e96135aa6915add4a471666008bdfac1fe19053094b53a874268dc49925e60d61df67efa40e377797fdabe5c8944444fa21f23677308a8ea08f13a8e
7
+ data.tar.gz: 55eac63dd2eba1965e68624cdb516bb99d6028c6e17c079a1a0c0fd6d7c13a559a0592dab10f3656da438de8bef6701b74ee486bee45a7bed3ab67727a1144cf
data/README.md CHANGED
@@ -28,4 +28,4 @@ Then call bundle install on your console.
28
28
 
29
29
  ## Copyright
30
30
 
31
- Copyright (c) 2010-2013 Andre Pankratz. See LICENSE for details.
31
+ Copyright (c) 2010-2019 Andre Pankratz. See LICENSE for details.
@@ -2,41 +2,19 @@ class Permalink
2
2
  include Mongoid::Document
3
3
  include Mongoid::Timestamps
4
4
 
5
- class UuidRequiredError < StandardError; end
5
+ belongs_to :linkable, polymorphic: true
6
6
 
7
7
  field :value
8
- field :linkable_class
9
- field :linkable_uuid
10
- field :scope, :type => Array
11
- field :_current, :type => Boolean, :default => true
8
+ field :scope, type: Array
9
+ field :_current, type: Boolean, default: true
12
10
 
13
11
  before_validation :sanitize_value!
14
- after_save :unset_other_current, :if => :current?
15
- after_destroy :set_last_current, :if => :current?
16
-
17
- validates :linkable_uuid, :uuid => true
18
- validates :value, :linkable_class, :presence => true
19
-
20
- index value: 1
21
-
22
- # Sets object as linkable.
23
- def linkable=(obj)
24
- @linkable = nil
25
- self.linkable_class = obj.class.to_s
26
- if uuid = obj.try!(:uuid)
27
- self.linkable_uuid = uuid
28
- else
29
- raise UuidRequiredError.new("The linkable object must respond to #uuid. The gem vidibus-uuid will help you.")
30
- end
31
- end
12
+ after_save :unset_other_current, if: :current?
13
+ after_destroy :set_last_current, if: :current?
32
14
 
33
- # Returns the linkable object.
34
- def linkable
35
- @linkable ||= begin
36
- return unless linkable_class and linkable_uuid
37
- linkable_class.constantize.where(:uuid => linkable_uuid).first
38
- end
39
- end
15
+ validates :value, :linkable, presence: true
16
+
17
+ index({value: 1}, {sparse: true})
40
18
 
41
19
  # Sanitizes and increments string, if necessary.
42
20
  def sanitize_value!
@@ -69,7 +47,7 @@ class Permalink
69
47
  if current?
70
48
  self
71
49
  else
72
- Permalink.where(:linkable_uuid => linkable_uuid, :_current => true).first
50
+ Permalink.where(linkable_id: linkable.id, _current: true).first
73
51
  end
74
52
  end
75
53
  end
@@ -83,18 +61,18 @@ class Permalink
83
61
  class << self
84
62
  # Scope method for finding Permalinks for given object.
85
63
  def for_linkable(object)
86
- where(:linkable_uuid => object.uuid)
64
+ where(linkable_id: object.id)
87
65
  end
88
66
 
89
67
  # Scope method for finding Permalinks for given value.
90
68
  # The value will be sanitized.
91
69
  def for_value(value)
92
- where(:value => sanitize(value))
70
+ where(value: sanitize(value))
93
71
  end
94
72
 
95
73
  def for_scope(scope)
96
74
  return all unless scope
97
- all_in(:scope => scope_list(scope))
75
+ all_in(scope: scope_list(scope))
98
76
  end
99
77
 
100
78
  # Returns a dispatcher object for given path.
@@ -112,7 +90,7 @@ class Permalink
112
90
  def scope_list(scope)
113
91
  return [] unless scope
114
92
  return scope if scope.kind_of?(Array)
115
- scope.map {|key, value| "#{key}:#{value}"}
93
+ scope.map { |key, value| "#{key}:#{value}" }
116
94
  end
117
95
  end
118
96
 
@@ -158,24 +136,32 @@ class Permalink
158
136
  # Finds existing permalinks with current value.
159
137
  def existing(string)
160
138
  @existing ||= {}
161
- @existing[string] ||=
162
- Permalink.for_scope(scope).where(:value => /^#{string}(-\d+)?$/).excludes(:_id => id).to_a
139
+ @existing[string] ||= Permalink
140
+ .for_scope(scope)
141
+ .where(value: /^#{string}(-\d+)?$/)
142
+ .excludes(:_id => id)
143
+ .to_a
163
144
  end
164
145
 
165
146
  # Sets _current to false on all permalinks of the assigned linkable.
166
147
  def unset_other_current
167
148
  return unless linkable
168
- conditions = {:linkable_uuid => linkable_uuid, :_id => {"$ne" => _id}}
149
+ conditions = {linkable_id: linkable.id, _id: {'$ne' => id}}
169
150
  conditions[:scope] = Permalink.scope_list(scope) if scope.present?
170
- collection.
171
- find(conditions).
172
- update({'$set' => {_current: false}}, {multi: true})
151
+ Permalink.where(conditions).each do |obj|
152
+ obj.set(_current: false)
153
+ end
173
154
  end
174
155
 
175
156
  # Sets the lastly updated permalink of the assigned linkable as current one.
176
157
  def set_last_current
177
- if last = Permalink.where(:linkable_uuid => linkable_uuid).order_by(:updated_at.desc).limit(1).first
178
- last.update_attributes!(:_current => true)
158
+ last = Permalink
159
+ .where(linkable_id: linkable.id)
160
+ .order_by(:updated_at.desc)
161
+ .limit(1)
162
+ .first
163
+ if last
164
+ last.update_attributes!(_current: true)
179
165
  end
180
166
  end
181
167
  end
@@ -60,12 +60,12 @@ module Vidibus
60
60
  private
61
61
 
62
62
  def resolve_path
63
- results = ::Permalink.for_scope(@scope).any_in(:value => parts)
63
+ results = ::Permalink.for_scope(@scope).any_in(value: parts)
64
64
  links = Array.new(parts.length)
65
65
  done = {}
66
66
  for result in results
67
67
  if i = parts.index(result.value)
68
- key = "#{result.linkable_class}##{result.linkable_uuid}"
68
+ key = "#{result.linkable_type}##{result.linkable_id}"
69
69
  next if done[key]
70
70
  links[i] = result
71
71
  done[key] = true
@@ -76,11 +76,11 @@ module Vidibus
76
76
 
77
77
  # Returns an array containing the current permalinks of all objects.
78
78
  def current_parts
79
- objects.map {|o| o.current.value}
79
+ objects.map { |o| o.current.value }
80
80
  end
81
81
 
82
82
  def redirectables
83
- objects.select {|o| !o.current?}
83
+ objects.select { |o| !o.current? }
84
84
  end
85
85
  end
86
86
  end
@@ -6,18 +6,18 @@ module Vidibus
6
6
  class PermalinkConfigurationError < StandardError; end
7
7
 
8
8
  included do
9
- field :permalink, :type => String
10
- field :static_permalink, :type => String
9
+ field :permalink, type: String
10
+ field :static_permalink, type: String
11
11
 
12
- index permalink: 1
13
- index static_permalink: 1
12
+ index({permalink: 1}, {sparse: true})
13
+ index({static_permalink: 1}, {sparse: true})
14
14
 
15
15
  attr_accessor :skip_permalink
16
16
 
17
- before_validation :set_permalink, :unless => :skip_permalink
18
- validates :permalink, :presence => true, :unless => :skip_permalink
17
+ before_validation :set_permalink, unless: :skip_permalink
18
+ validates :permalink, presence: true, unless: :skip_permalink
19
19
 
20
- after_save :store_permalink_object, :unless => :skip_permalink
20
+ after_save :store_permalink_object, unless: :skip_permalink
21
21
  after_destroy :destroy_permalink_objects
22
22
  end
23
23
 
@@ -42,18 +42,23 @@ module Vidibus
42
42
 
43
43
  # Returns the defined permalink repository object.
44
44
  def permalink_repository
45
- @permalink_repository ||= (self.class.permalink_options[:repository] == false) ? nil : ::Permalink
45
+ @permalink_repository ||= begin
46
+ self.class.permalink_options[:repository] == false ? nil : ::Permalink
47
+ end
46
48
  end
47
49
 
48
50
  # Returns the current permalink object.
49
51
  def permalink_object
50
- @permalink_object ||
51
- permalink_repository.for_linkable(self).where(:_current => true).first if permalink_repository
52
+ @permalink_object || if permalink_repository
53
+ permalink_repository.for_linkable(self).where(_current: true).first
54
+ end
52
55
  end
53
56
 
54
57
  # Returns all permalink objects ordered by time of update.
55
58
  def permalink_objects
56
- permalink_repository.for_linkable(self).asc(:updated_at) if permalink_repository
59
+ if permalink_repository
60
+ permalink_repository.for_linkable(self).asc(:updated_at)
61
+ end
57
62
  end
58
63
 
59
64
  # Returns permalink scope.
@@ -66,14 +71,21 @@ module Vidibus
66
71
  # Returns a existing or new permalink object with wanted value.
67
72
  # The permalink scope is also applied
68
73
  def permalink_object_by_value(value)
69
- permalink_repository.for_linkable(self).for_value(value).for_scope(permalink_scope).first ||
70
- permalink_repository.new(:value => value, :scope => permalink_scope, :linkable => self)
74
+ item = permalink_repository
75
+ .for_linkable(self)
76
+ .for_value(value)
77
+ .for_scope(permalink_scope)
78
+ .first
79
+ item ||= permalink_repository.new({
80
+ value: value,
81
+ scope: permalink_scope,
82
+ linkable: self
83
+ })
71
84
  end
72
85
 
73
86
  def get_scope
74
87
  scope = self.class.permalink_options[:scope]
75
88
  return unless scope
76
-
77
89
  {}.tap do |hash|
78
90
  scope.each do |key, value|
79
91
  if value.kind_of?(String)
@@ -99,12 +111,12 @@ module Vidibus
99
111
 
100
112
  changed = false
101
113
  values = []
102
- for a in attribute_names
103
- changed = send("#{a}_changed?") unless changed == true
104
- values << send(a)
114
+ attribute_names.each do |name|
115
+ changed ||= changes[name].present?
116
+ values << attributes[name.to_s]
105
117
  end
106
- return unless permalink.blank? or changed
107
- value = values.join(" ")
118
+ return unless permalink.blank? || changed
119
+ value = values.join(' ')
108
120
  if permalink_repository
109
121
  @permalink_object = permalink_object_by_value(value)
110
122
  @permalink_object.sanitize_value!
@@ -123,13 +135,16 @@ module Vidibus
123
135
 
124
136
  # Stores current new permalink object or updates an existing one that matches.
125
137
  def store_permalink_object
126
- return unless @permalink_object
127
- @permalink_object.updated_at = Time.now
128
- @permalink_object.save!
138
+ return unless obj = @permalink_object
139
+ @permalink_object = nil # reset to avoid storing it again
140
+ obj.updated_at = Time.now
141
+ obj.save!
129
142
  end
130
143
 
131
144
  def destroy_permalink_objects
132
- permalink_repository.delete_all(:conditions => {:linkable_uuid => uuid}) if permalink_repository
145
+ if permalink_repository
146
+ permalink_repository.where(linkable_id: id).delete
147
+ end
133
148
  end
134
149
  end
135
150
  end
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Permalink
3
- VERSION = '0.1.9'
3
+ VERSION = '1.1.1'
4
4
  end
5
5
  end
@@ -1,7 +1,6 @@
1
1
  require "active_support"
2
2
  require "mongoid"
3
3
  require "vidibus-core_extensions"
4
- require "vidibus-uuid"
5
4
  require "vidibus-words"
6
5
 
7
6
  require "vidibus/permalink"
metadata CHANGED
@@ -1,190 +1,153 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-permalink
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.9
5
- prerelease:
4
+ version: 1.1.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Andre Pankratz
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2016-01-02 00:00:00.000000000 Z
11
+ date: 2019-03-28 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '3'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '3'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: mongoid
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
- version: '3'
33
+ version: '6'
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
- version: '3'
40
+ version: '6'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: vidibus-core_extensions
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ! '>='
52
- - !ruby/object:Gem::Version
53
- version: '0'
54
- type: :runtime
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: vidibus-uuid
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
44
  requirements:
67
- - - ! '>='
45
+ - - ">="
68
46
  - !ruby/object:Gem::Version
69
47
  version: '0'
70
48
  type: :runtime
71
49
  prerelease: false
72
50
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
51
  requirements:
75
- - - ! '>='
52
+ - - ">="
76
53
  - !ruby/object:Gem::Version
77
54
  version: '0'
78
55
  - !ruby/object:Gem::Dependency
79
56
  name: vidibus-words
80
57
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
58
  requirements:
83
- - - ! '>='
59
+ - - ">="
84
60
  - !ruby/object:Gem::Version
85
61
  version: '0'
86
62
  type: :runtime
87
63
  prerelease: false
88
64
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
65
  requirements:
91
- - - ! '>='
66
+ - - ">="
92
67
  - !ruby/object:Gem::Version
93
68
  version: '0'
94
69
  - !ruby/object:Gem::Dependency
95
70
  name: bundler
96
71
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
72
  requirements:
99
- - - ! '>='
73
+ - - ">="
100
74
  - !ruby/object:Gem::Version
101
75
  version: 1.0.0
102
76
  type: :development
103
77
  prerelease: false
104
78
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
79
  requirements:
107
- - - ! '>='
80
+ - - ">="
108
81
  - !ruby/object:Gem::Version
109
82
  version: 1.0.0
110
83
  - !ruby/object:Gem::Dependency
111
84
  name: rspec
112
85
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
86
  requirements:
115
- - - ~>
87
+ - - "~>"
116
88
  - !ruby/object:Gem::Version
117
89
  version: '2'
118
90
  type: :development
119
91
  prerelease: false
120
92
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
93
  requirements:
123
- - - ~>
94
+ - - "~>"
124
95
  - !ruby/object:Gem::Version
125
96
  version: '2'
126
97
  - !ruby/object:Gem::Dependency
127
98
  name: rr
128
99
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
100
  requirements:
131
- - - ! '>='
101
+ - - ">="
132
102
  - !ruby/object:Gem::Version
133
103
  version: '0'
134
104
  type: :development
135
105
  prerelease: false
136
106
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
107
  requirements:
139
- - - ! '>='
108
+ - - ">="
140
109
  - !ruby/object:Gem::Version
141
110
  version: '0'
142
111
  - !ruby/object:Gem::Dependency
143
112
  name: rake
144
113
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
114
  requirements:
147
- - - ! '>='
115
+ - - ">="
148
116
  - !ruby/object:Gem::Version
149
117
  version: '0'
150
118
  type: :development
151
119
  prerelease: false
152
120
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
121
  requirements:
155
- - - ! '>='
122
+ - - ">="
156
123
  - !ruby/object:Gem::Version
157
124
  version: '0'
158
125
  - !ruby/object:Gem::Dependency
159
126
  name: rdoc
160
127
  requirement: !ruby/object:Gem::Requirement
161
- none: false
162
128
  requirements:
163
- - - ~>
129
+ - - "~>"
164
130
  - !ruby/object:Gem::Version
165
131
  version: '3.4'
166
132
  type: :development
167
133
  prerelease: false
168
134
  version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
135
  requirements:
171
- - - ~>
136
+ - - "~>"
172
137
  - !ruby/object:Gem::Version
173
138
  version: '3.4'
174
139
  - !ruby/object:Gem::Dependency
175
140
  name: simplecov
176
141
  requirement: !ruby/object:Gem::Requirement
177
- none: false
178
142
  requirements:
179
- - - ! '>='
143
+ - - ">="
180
144
  - !ruby/object:Gem::Version
181
145
  version: '0'
182
146
  type: :development
183
147
  prerelease: false
184
148
  version_requirements: !ruby/object:Gem::Requirement
185
- none: false
186
149
  requirements:
187
- - - ! '>='
150
+ - - ">="
188
151
  - !ruby/object:Gem::Version
189
152
  version: '0'
190
153
  description: Allows changeable permalinks (good for SEO).
@@ -193,41 +156,37 @@ executables: []
193
156
  extensions: []
194
157
  extra_rdoc_files: []
195
158
  files:
196
- - lib/vidibus/permalink/dispatcher.rb
197
- - lib/vidibus/permalink/mongoid.rb
198
- - lib/vidibus/permalink/version.rb
199
- - lib/vidibus/permalink.rb
200
- - lib/vidibus-permalink.rb
201
- - app/models/permalink.rb
202
159
  - LICENSE
203
160
  - README.md
204
161
  - Rakefile
162
+ - app/models/permalink.rb
163
+ - lib/vidibus-permalink.rb
164
+ - lib/vidibus/permalink.rb
165
+ - lib/vidibus/permalink/dispatcher.rb
166
+ - lib/vidibus/permalink/mongoid.rb
167
+ - lib/vidibus/permalink/version.rb
205
168
  homepage: https://github.com/vidibus/vidibus-permalink
206
169
  licenses:
207
170
  - MIT
171
+ metadata: {}
208
172
  post_install_message:
209
173
  rdoc_options: []
210
174
  require_paths:
211
175
  - lib
212
176
  required_ruby_version: !ruby/object:Gem::Requirement
213
- none: false
214
177
  requirements:
215
- - - ! '>='
178
+ - - ">="
216
179
  - !ruby/object:Gem::Version
217
180
  version: '0'
218
- segments:
219
- - 0
220
- hash: -3383487206857057643
221
181
  required_rubygems_version: !ruby/object:Gem::Requirement
222
- none: false
223
182
  requirements:
224
- - - ! '>='
183
+ - - ">="
225
184
  - !ruby/object:Gem::Version
226
185
  version: 1.3.6
227
186
  requirements: []
228
187
  rubyforge_project:
229
- rubygems_version: 1.8.24
188
+ rubygems_version: 2.6.11
230
189
  signing_key:
231
- specification_version: 3
190
+ specification_version: 4
232
191
  summary: Permalink handling
233
192
  test_files: []