Sha256: 41d3a4151d3200362516a30798399e29d41bd8375af88cd42883b2939f58e9c2

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

Contents

class ActiveRecord::Base
	def self.has_movable(association_id, options = {}, &extension)
		define_method('last_order_index') do
			max = -6500
			self.send(association_id.to_s).each { |p| max = p.order_index.to_i if p.order_index.to_i > max.to_i }
			max < -1 ? -1 : max
		end

		define_method('update_order_indexes') do |moved_page_id, above_page_id|
			moved_page = self.send(association_id.to_s).find(moved_page_id)
			above_page = self.send(association_id.to_s).find(above_page_id)
			next_next_order_index = above_page.order_index.to_i + 2
			self.send(association_id.to_s).find(:all, :conditions => ['order_index > ?', above_page.order_index]).each { |p|
				p.order_index = next_next_order_index
				p.save
				next_next_order_index +=1
			}
			moved_page.order_index = above_page.order_index.to_i + 1
			moved_page.save
		end

	end

	def self.is_movable_within_a(association_id, options = {}, &extension)
		before_create { |model| model.order_index = model.send(association_id.to_s).last_order_index + 1 }
	end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
smklib-1.2.0 lib/smklib/movable_children.rb
smklib-1.1.2 lib/smklib/movable_children.rb
smklib-1.1.1 lib/smklib/movable_children.rb
smklib-1.1.0 lib/smklib/movable_children.rb
smklib-1.0.6 lib/smklib/movable_children.rb