vidibus-permalink 1.1.2 → 1.4.0

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
- SHA1:
3
- metadata.gz: f00dec174def77cba9500663b540a6ffc8cf7a0b
4
- data.tar.gz: fd3d2f0a87f99dd7c36ddec51003ee2fa17bf059
2
+ SHA256:
3
+ metadata.gz: bac98f8cc4d3e6a7e575bab1edf72c67eb04eeffde1a0715d7ab8232037db918
4
+ data.tar.gz: 1d4204aea086cb8a69017ae2f0b3985839895c949f6a2dd9a780a461b41ff6bb
5
5
  SHA512:
6
- metadata.gz: '0915a7c00cdf3e4212c24f13fc4bb044222230473f56a44c4efd49d3457d2b5e37be1b95fcf52f2469eefcd2e4a5ebc35b55349de44370a50286f94612ca4104'
7
- data.tar.gz: 1bae7199adbea407806612f98f15e60f26d84065c29de92e23c81138c6b1950998b6eb2f4e4ac41371442c42e29486be949b5a058474151976480a617c060a26
6
+ metadata.gz: ff6e288516dd3863daedf254e561b1f7257d0e9cf49254c07295bf3f334ab962b6a96838076ee4653cfbd7f2e4a8d3ca49bc1f71969dbef7e7132c9666fe6ce0
7
+ data.tar.gz: ca8be7dde7894295b1ed8cdd7356be5ef24875138773df1dccd841cd2f83ee9d69c616ffe5ac5d3bacf0092998dce1dd2cc2ed219bbf08eade1c9c5efab282ad
@@ -2,7 +2,7 @@ class Permalink
2
2
  include Mongoid::Document
3
3
  include Mongoid::Timestamps
4
4
 
5
- belongs_to :linkable, polymorphic: true
5
+ belongs_to :linkable, polymorphic: true, autosave: false
6
6
 
7
7
  field :value
8
8
  field :scope, type: Array
@@ -47,7 +47,11 @@ class Permalink
47
47
  if current?
48
48
  self
49
49
  else
50
- Permalink.where(linkable_id: linkable.id, _current: true).first
50
+ Permalink.where({
51
+ linkable_id: linkable.id,
52
+ linkable_type: linkable.class.to_s,
53
+ _current: true
54
+ }).first
51
55
  end
52
56
  end
53
57
  end
@@ -61,13 +65,20 @@ class Permalink
61
65
  class << self
62
66
  # Scope method for finding Permalinks for given object.
63
67
  def for_linkable(object)
64
- where(linkable_id: object.id)
68
+ where(linkable_id: object.id, linkable_type: object.class.to_s)
65
69
  end
66
70
 
67
71
  # Scope method for finding Permalinks for given value.
68
72
  # The value will be sanitized.
69
- def for_value(value)
70
- where(value: sanitize(value))
73
+ def for_value(value, do_sanitize = true)
74
+ if do_sanitize
75
+ self.or([
76
+ {value: /^#{sanitize(value)}(-\d+)?$/},
77
+ {value: /^#{sanitize(value, true)}(-\d+)?$/}
78
+ ])
79
+ else
80
+ where(value: /^#{value}(-\d+)?$/)
81
+ end
71
82
  end
72
83
 
73
84
  def for_scope(scope)
@@ -82,9 +93,10 @@ class Permalink
82
93
 
83
94
  # Sanitizes string: Remove stopwords and format as permalink.
84
95
  # See Vidibus::CoreExtensions::String for details.
85
- def sanitize(string)
96
+ def sanitize(string, keep_stopwords = false)
86
97
  return if string.blank?
87
- remove_stopwords(string).permalink
98
+ string = remove_stopwords(string) unless keep_stopwords
99
+ string.permalink
88
100
  end
89
101
 
90
102
  def scope_list(scope)
@@ -138,7 +150,7 @@ class Permalink
138
150
  @existing ||= {}
139
151
  @existing[string] ||= Permalink
140
152
  .for_scope(scope)
141
- .where(value: /^#{string}(-\d+)?$/)
153
+ .for_value(string, false)
142
154
  .excludes(:_id => id)
143
155
  .to_a
144
156
  end
@@ -146,7 +158,11 @@ class Permalink
146
158
  # Sets _current to false on all permalinks of the assigned linkable.
147
159
  def unset_other_current
148
160
  return unless linkable
149
- conditions = {linkable_id: linkable.id, _id: {'$ne' => id}}
161
+ conditions = {
162
+ linkable_id: linkable.id,
163
+ linkable_type: linkable.class.to_s,
164
+ _id: {'$ne' => id}
165
+ }
150
166
  conditions[:scope] = Permalink.scope_list(scope) if scope.present?
151
167
  Permalink.where(conditions).each do |obj|
152
168
  obj.set(_current: false)
@@ -156,7 +172,7 @@ class Permalink
156
172
  # Sets the lastly updated permalink of the assigned linkable as current one.
157
173
  def set_last_current
158
174
  last = Permalink
159
- .where(linkable_id: linkable.id)
175
+ .where(linkable_id: linkable.id, linkable_type: linkable.class.to_s)
160
176
  .order_by(:updated_at.desc)
161
177
  .limit(1)
162
178
  .first
@@ -40,6 +40,18 @@ module Vidibus
40
40
  end
41
41
  end
42
42
 
43
+ # Ensure that a current permalink object exists for this record.
44
+ def ensure_permalink_object
45
+ if permalink_repository
46
+ permalink_object || begin
47
+ obj = permalink_object_by_value(permalink)
48
+ obj.sanitize_value!
49
+ obj.current!
50
+ obj.save
51
+ end
52
+ end
53
+ end
54
+
43
55
  # Returns the defined permalink repository object.
44
56
  def permalink_repository
45
57
  @permalink_repository ||= begin
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Permalink
3
- VERSION = '1.1.2'
3
+ VERSION = '1.4.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-permalink
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andre Pankratz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-28 00:00:00.000000000 Z
11
+ date: 2020-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -184,8 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
184
  - !ruby/object:Gem::Version
185
185
  version: 1.3.6
186
186
  requirements: []
187
- rubyforge_project:
188
- rubygems_version: 2.6.11
187
+ rubygems_version: 3.0.6
189
188
  signing_key:
190
189
  specification_version: 4
191
190
  summary: Permalink handling