vidibus-permalink 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,7 +10,7 @@ class Permalink
10
10
  field :scope, :type => Array
11
11
  field :_current, :type => Boolean, :default => true
12
12
 
13
- before_save :set_current
13
+ after_save :unset_other_current, :if => :current?
14
14
  after_destroy :set_last_current, :if => :current?
15
15
 
16
16
  validates :linkable_uuid, :uuid => true
@@ -61,7 +61,7 @@ class Permalink
61
61
  # Returns true if this permalink is the current one
62
62
  # of the assigned linkable.
63
63
  def current?
64
- @is_current ||= !!_current
64
+ !!_current
65
65
  end
66
66
 
67
67
  # Returns the current permalink of the assigned linkable.
@@ -75,6 +75,12 @@ class Permalink
75
75
  end
76
76
  end
77
77
 
78
+ # Sets this permalink as the current one.
79
+ # All other permalinks of this linkable will be updated after saving.
80
+ def current!
81
+ self._current = true
82
+ end
83
+
78
84
  class << self
79
85
  # Scope method for finding Permalinks for given object.
80
86
  def for_linkable(object)
@@ -106,7 +112,7 @@ class Permalink
106
112
 
107
113
  def scope_list(scope)
108
114
  return [] unless scope
109
- scope.inject([]) { |array, (key, value)| array << "#{key}:#{value}"; array}
115
+ scope.map {|key, value| "#{key}:#{value}"}
110
116
  end
111
117
  end
112
118
 
@@ -154,17 +160,13 @@ class Permalink
154
160
  @existing[string] ||= Permalink.where(:value => /^#{string}(-\d+)?$/).excludes(:_id => id).to_a
155
161
  end
156
162
 
157
- # Sets this permalink as the current one and unsets all others.
158
- def set_current
159
- unset_other_current
160
- self._current = true
161
- end
162
-
163
163
  # Sets _current to false on all permalinks of the assigned linkable.
164
164
  def unset_other_current
165
165
  return unless linkable
166
+ conditions = {:linkable_uuid => linkable_uuid, :_id => {"$ne" => _id}}
167
+ conditions[:scope] = Permalink.scope_list(scope) if scope.present?
166
168
  collection.update(
167
- {:linkable_uuid => linkable_uuid, :_id => {"$ne" => _id}},
169
+ conditions,
168
170
  {"$set" => {:_current => false}},
169
171
  {:multi => true}
170
172
  )
@@ -55,13 +55,15 @@ module Vidibus
55
55
  @permalink_scope ||= get_scope
56
56
  end
57
57
 
58
- def find_or_initialize(value, scope)
59
- permalink_repository.for_linkable(self).for_value(value).for_scope(scope).first ||
60
- permalink_repository.new(:value => value, :scope => scope, :linkable => self)
61
- end
62
-
63
58
  private
64
59
 
60
+ # Returns a existing or new permalink object with wanted value.
61
+ # The permalink scope is also applied
62
+ def permalink_object_by_value(value)
63
+ permalink_repository.for_linkable(self).for_value(value).for_scope(permalink_scope).first ||
64
+ permalink_repository.new(:value => value, :scope => permalink_scope, :linkable => self)
65
+ end
66
+
65
67
  def get_scope
66
68
  scope = self.class.permalink_options[:scope]
67
69
  return unless scope
@@ -98,7 +100,8 @@ module Vidibus
98
100
  return unless permalink.blank? or changed
99
101
  value = values.join(" ")
100
102
  if permalink_repository
101
- @permalink_object = find_or_initialize(value, permalink_scope)
103
+ @permalink_object = permalink_object_by_value(value)
104
+ @permalink_object.current!
102
105
  self.permalink = @permalink_object.value
103
106
  else
104
107
  self.permalink = ::Permalink.sanitize(value)
@@ -1,5 +1,5 @@
1
1
  module Vidibus
2
2
  module Permalink
3
- VERSION = "0.1.2"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vidibus-permalink
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andre Pankratz
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-05 00:00:00 +02:00
18
+ date: 2011-09-06 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency