noodall-core 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.7
1
+ 0.4.8
data/lib/noodall/node.rb CHANGED
@@ -31,6 +31,7 @@ module Noodall
31
31
  alias_method :keywords=, :tag_list=
32
32
 
33
33
  attr_accessor :publish, :hide #for publishing
34
+ attr_accessor :previous_parent_id, :moved #for redordering
34
35
 
35
36
  acts_as_tree :order => "position", :search_class => Noodall::Node
36
37
 
@@ -76,31 +77,17 @@ module Noodall
76
77
  end
77
78
 
78
79
  def last?
79
- position == search_class.count(:_id => {"$ne" => self._id}, parent_id_field => self[parent_id_field])
80
+ position == siblings.count
80
81
  end
82
+
81
83
  def move_lower
82
84
  sibling = search_class.first(:position => {"$gt" => self.position}, parent_id_field => self[parent_id_field], :order => 'position ASC')
83
-
84
- tmp = sibling.position
85
- sibling.position = self.position
86
- self.position = tmp
87
-
88
- search_class.collection.update({:_id => self._id}, self.to_mongo)
89
- search_class.collection.update({:_id => sibling._id}, sibling.to_mongo)
90
-
91
- global_updated!
85
+ switch_position(sibling)
92
86
  end
87
+
93
88
  def move_higher
94
89
  sibling = search_class.first(:position => {"$lt" => self.position}, parent_id_field => self[parent_id_field], :order => 'position DESC')
95
-
96
- tmp = sibling.position
97
- sibling.position = self.position
98
- self.position = tmp
99
-
100
- search_class.collection.update({:_id => self._id}, self.to_mongo)
101
- search_class.collection.update({:_id => sibling._id}, sibling.to_mongo)
102
-
103
- global_updated!
90
+ switch_position(sibling)
104
91
  end
105
92
 
106
93
  def run_callbacks(kind, options = {}, &block)
@@ -154,7 +141,6 @@ module Noodall
154
141
 
155
142
  def self_and_siblings
156
143
  search_class.where(parent_id_field => self[parent_id_field]).order(tree_order)
157
-
158
144
  end
159
145
 
160
146
  def children
@@ -167,6 +153,17 @@ module Noodall
167
153
 
168
154
  private
169
155
 
156
+ def switch_position(sibling)
157
+ tmp = sibling.position
158
+ sibling.position = self.position
159
+ self.position = tmp
160
+
161
+ search_class.collection.update({:_id => self._id}, self.to_mongo)
162
+ search_class.collection.update({:_id => sibling._id}, sibling.to_mongo)
163
+
164
+ global_updated!
165
+ end
166
+
170
167
  def current_time
171
168
  self.class.current_time
172
169
  end
@@ -235,11 +232,38 @@ module Noodall
235
232
  end
236
233
  end
237
234
 
235
+ before_update :move_check
236
+ def move_check
237
+ set_previous_parent if self.parent_id_changed?
238
+ self.moved = true if self.position_changed? or self.parent_id_changed?
239
+ end
240
+
241
+ before_destroy :set_previous_parent #so the child list it was removed from normalises order
242
+ def set_previous_parent
243
+ self.previous_parent_id = self.parent_id_was
244
+ end
245
+
246
+ before_create :set_moved #so if it is placed at the top of list it normalises order
247
+ def set_moved
248
+ self.moved = true
249
+ end
250
+
238
251
  after_save :order_siblings
239
252
  def order_siblings
240
- if position_changed?
241
- search_class.collection.update({:_id => {"$ne" => self._id}, :position => {"$gte" => self.position}, parent_id_field => self[parent_id_field]}, { "$inc" => { :position => 1 }}, { :multi => true })
242
- self_and_siblings.each_with_index do |sibling, index|
253
+ search_class.collection.update({:_id => {"$ne" => self._id}, :position => {"$gte" => self.position}, parent_id_field => self[parent_id_field]}, { "$inc" => { :position => 1 }}, { :multi => true }) if moved
254
+ self_and_siblings.each_with_index do |sibling, index|
255
+ unless sibling.position == index
256
+ sibling.position = index
257
+ search_class.collection.save(sibling.to_mongo, :safe => true)
258
+ end
259
+ end
260
+ order_previous_siblings
261
+ end
262
+
263
+ after_destroy :order_previous_siblings
264
+ def order_previous_siblings
265
+ unless previous_parent_id.nil?
266
+ search_class.where(parent_id_field => previous_parent_id).each_with_index do |sibling, index|
243
267
  unless sibling.position == index
244
268
  sibling.position = index
245
269
  search_class.collection.save(sibling.to_mongo, :safe => true)
data/noodall-core.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{noodall-core}
8
- s.version = "0.4.7"
8
+ s.version = "0.4.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Steve England"]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noodall-core
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- - 7
10
- version: 0.4.7
9
+ - 8
10
+ version: 0.4.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Steve England