Module: ActsAsRestfulList::InstanceMethods

Defined in:
lib/acts_as_restful_list.rb

Instance Method Summary collapse

Instance Method Details

#current_positionObject



102
103
104
# File 'lib/acts_as_restful_list.rb', line 102

def current_position
  self.send( position_column )
end

#current_position=(value) ⇒ Object



106
107
108
# File 'lib/acts_as_restful_list.rb', line 106

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

#decrement_position_sqlObject



118
119
120
# File 'lib/acts_as_restful_list.rb', line 118

def decrement_position_sql
  "#{position_column} = (#{position_column} - 1) #{optimistic_locking_update}"
end

#increment_position_sqlObject



114
115
116
# File 'lib/acts_as_restful_list.rb', line 114

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

#initialize_orderObject



86
87
88
89
90
91
92
# File 'lib/acts_as_restful_list.rb', line 86

def initialize_order
  initial_set = self.class.find(:all,:conditions=>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

#last_recordObject



94
95
96
# File 'lib/acts_as_restful_list.rb', line 94

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

#last_record_positionObject



98
99
100
# File 'lib/acts_as_restful_list.rb', line 98

def last_record_position
  last_record.send(position_column)
end

#previous_positionObject



110
111
112
# File 'lib/acts_as_restful_list.rb', line 110

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

#reset_order_after_destroyObject



82
83
84
# File 'lib/acts_as_restful_list.rb', line 82

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 '))
end

#reset_order_after_updateObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/acts_as_restful_list.rb', line 65

def reset_order_after_update
  if previous_position.nil?
    initialize_order
  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 '))
    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 '))
      else
        self.class.update_all( decrement_position_sql, [scope_condition, "#{position_column} <= #{current_position}", "#{position_column} >= #{previous_position}", "id != #{id}"].compact.join(' AND '))
      end
    end
  end
end

#set_positionObject



59
60
61
62
63
# File 'lib/acts_as_restful_list.rb', line 59

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