Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes rails 4 deprecations, rspec changes. #9

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ tmtags
coverage
rdoc
pkg
Gemfile.lock

## PROJECT::SPECIFIC
1 change: 1 addition & 0 deletions .ruby-gemset
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
acts_as_restful_list
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.1.2
1 change: 0 additions & 1 deletion .rvmrc

This file was deleted.

43 changes: 0 additions & 43 deletions Gemfile.lock

This file was deleted.

2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ task :default do
exec "bundle exec rspec #{specs_unit}"
end

require 'rake/rdoctask'
require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

Expand Down
62 changes: 34 additions & 28 deletions lib/acts_as_restful_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ module ClassMethods
# * +scope+: The column to scope the list to. It takes a symbol with our without the _id.
def acts_as_restful_list(options = {})
include InstanceMethods

configuration = {:column => :position}.merge(options)

before_create :set_position
after_update :reset_order_after_update
after_destroy :reset_order_after_destroy

define_method 'position_column' do
configuration[:column].to_s
end

define_method 'scope_condition' do
if configuration[:scope].nil?
nil
Expand All @@ -35,7 +35,7 @@ def acts_as_restful_list(options = {})
scopes.join(' AND ')
end
end

define_method 'scope_condition_was' do
if configuration[:scope].nil?
nil
Expand All @@ -48,73 +48,79 @@ def acts_as_restful_list(options = {})
scopes.join(' AND ')
end
end

define_method 'optimistic_locking_update' do
self.class.column_names.include?("lock_version") ? ", lock_version = (lock_version + 1)" : ""
end
end
end

module InstanceMethods
def set_position
initialize_order if !last_record.nil? and last_record_position.nil?

self.send( "#{position_column}=", last_record.nil? ? 1 : last_record_position + 1)
end

def reset_order_after_update
if previous_position.nil?
initialize_order
else
else
if scope_condition != scope_condition_was
self.class.update_all( decrement_position_sql, [scope_condition_was, "#{position_column} > #{previous_position}", "id != #{id}"].compact.join(' AND '))
self.class.update_all( increment_position_sql, [scope_condition, "#{position_column} >= #{current_position}", "id != #{id}"].compact.join(' AND '))
conditions = [scope_condition_was, "#{position_column} > #{previous_position}", "id != #{id}"].compact.join(' AND ')
self.class.where(conditions).update_all(decrement_position_sql)

conditions = [scope_condition, "#{position_column} >= #{current_position}", "id != #{id}"].compact.join(' AND ')
self.class.where(conditions).update_all(increment_position_sql)
elsif self.send( "#{position_column}_changed?" )
if previous_position > current_position
self.class.update_all( increment_position_sql, [scope_condition, "#{position_column} >= #{current_position}", "id != #{id}", "#{position_column} < #{previous_position}"].compact.join(' AND '))
conditions = [scope_condition, "#{position_column} >= #{current_position}", "id != #{id}", "#{position_column} < #{previous_position}"].compact.join(' AND ')
self.class.where(conditions).update_all(increment_position_sql)
else
self.class.update_all( decrement_position_sql, [scope_condition, "#{position_column} <= #{current_position}", "#{position_column} >= #{previous_position}", "id != #{id}"].compact.join(' AND '))
conditions = [scope_condition, "#{position_column} <= #{current_position}", "#{position_column} >= #{previous_position}", "id != #{id}"].compact.join(' AND ')
self.class.where(conditions).update_all(decrement_position_sql)
end
end
end
end

def reset_order_after_destroy
self.class.update_all("#{position_column} = (#{position_column} - 1) #{optimistic_locking_update}", [scope_condition, "#{position_column} > #{self.send( position_column )}"].compact.join(' AND '))
conditions = [scope_condition, "#{position_column} > #{self.send(position_column)}"].compact.join(' AND ')
self.class.where(conditions).update_all("#{position_column} = (#{position_column} - 1) #{optimistic_locking_update}")
end

def initialize_order
initial_set = self.class.find(:all,:conditions=>scope_condition,:select=>"id",:order=>"created_at ASC")
initial_set = self.class.where(scope_condition).select("id").order("created_at ASC")

initial_set.each_with_index do |item,idx|
ActiveRecord::Base.connection.execute("update #{self.class.table_name} set position = #{idx + 1} where id = #{item.id};")
end
end

def last_record
self.class.last( :conditions => scope_condition, :order => "#{position_column} ASC" )
self.class.order("#{position_column} ASC").where(scope_condition).last
end

def last_record_position
last_record.send(position_column)
end

def current_position
self.send( position_column )
end

def current_position=(value)
self.send( "#{position_column}=", value )
end

def previous_position
self.send( "#{position_column}_was" )
end

def increment_position_sql
"#{position_column} = (#{position_column} + 1) #{optimistic_locking_update}"
end

def decrement_position_sql
"#{position_column} = (#{position_column} - 1) #{optimistic_locking_update}"
end
Expand All @@ -123,4 +129,4 @@ def decrement_position_sql

if Object.const_defined?("ActiveRecord")
ActiveRecord::Base.send(:include, ActsAsRestfulList)
end
end
Loading