Skip to content

Commit

Permalink
Refactor class_eval string usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdvdijk committed Aug 26, 2016
1 parent 67a9724 commit 3ef9657
Showing 1 changed file with 64 additions and 60 deletions.
124 changes: 64 additions & 60 deletions lib/acts_as_list/active_record/acts/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,102 +42,106 @@ def acts_as_list(options = {})
configuration[:scope] = :"#{configuration[:scope]}_id"
end

if configuration[:scope].is_a?(Symbol)
scope_methods = %(
def scope_condition
{ #{configuration[:scope]}: send(:#{configuration[:scope]}) }
end
def scope_changed?
changed.include?(scope_name.to_s)
end
)
elsif configuration[:scope].is_a?(Array)
scope_methods = %(
def scope_condition
#{configuration[:scope]}.inject({}) do |hash, column|
hash.merge!({ column.to_sym => read_attribute(column.to_sym) })
end
end
class_calling_acts_as_list = self

def scope_changed?
(scope_condition.keys & changed.map(&:to_sym)).any?
end
)
else
scope_methods = %(
def scope_condition
"#{configuration[:scope]}"
end
def scope_changed?() false end
)
end

quoted_position_column = connection.quote_column_name(configuration[:column])
quoted_position_column_with_table_name = "#{quoted_table_name}.#{quoted_position_column}"

class_eval <<-EOV, __FILE__, __LINE__ + 1
def self.acts_as_list_top
#{configuration[:top_of_list]}.to_i
class_eval do
define_singleton_method :acts_as_list_top do
configuration[:top_of_list].to_i
end

def acts_as_list_top
#{configuration[:top_of_list]}.to_i
define_method :acts_as_list_top do
configuration[:top_of_list].to_i
end

def acts_as_list_class
::#{self.name}
define_method :acts_as_list_class do
class_calling_acts_as_list
end

def position_column
'#{configuration[:column]}'
define_method :position_column do
configuration[:column]
end

def scope_name
'#{configuration[:scope]}'
define_method :scope_name do
configuration[:scope]
end

def add_new_at
'#{configuration[:add_new_at]}'
define_method :add_new_at do
configuration[:add_new_at]
end

def #{configuration[:column]}=(position)
write_attribute(:#{configuration[:column]}, position)
define_method :"#{configuration[:column]}=" do |position|
write_attribute(configuration[:column], position)
@position_changed = true
end

#{scope_methods}
if configuration[:scope].is_a?(Symbol)
define_method :scope_condition do
{ configuration[:scope] => send(:"#{configuration[:scope]}") }
end

define_method :scope_changed? do
changed.include?(scope_name.to_s)
end
elsif configuration[:scope].is_a?(Array)
define_method :scope_condition do
configuration[:scope].inject({}) do |hash, column|
hash.merge!({ column.to_sym => read_attribute(column.to_sym) })
end
end

define_method :scope_changed? do
(scope_condition.keys & changed.map(&:to_sym)).any?
end
else
define_method :scope_condition do
eval "%{#{configuration[:scope]}}"
end

define_method :scope_changed? do
false
end
end

# only add to attr_accessible
# if the class has some mass_assignment_protection
if defined?(accessible_attributes) and !accessible_attributes.blank?
attr_accessible :#{configuration[:column]}
attr_accessible :"#{configuration[:column]}"
end

scope :in_list, lambda { where(%q{#{quoted_position_column_with_table_name} IS NOT NULL}) }
define_singleton_method :quoted_table_name do
@_quoted_table_name ||= acts_as_list_class.quoted_table_name
end

def self.decrement_all
update_all_with_touch %q(#{quoted_position_column} = (#{quoted_position_column_with_table_name} - 1))
define_singleton_method :quoted_position_column do
connection.quote_column_name(configuration[:column])
end

def self.increment_all
update_all_with_touch %q(#{quoted_position_column} = (#{quoted_position_column_with_table_name} + 1))
define_singleton_method :quoted_position_column_with_table_name do
@_quoted_position_column_with_table_name ||= "#{quoted_table_name}.#{quoted_position_column}"
end

def self.update_all_with_touch(updates)
scope :in_list, lambda { where("#{quoted_position_column_with_table_name} IS NOT NULL") }

define_singleton_method :decrement_all do
update_all_with_touch "#{quoted_position_column} = (#{quoted_position_column_with_table_name} - 1)"
end

define_singleton_method :increment_all do
update_all_with_touch "#{quoted_position_column} = (#{quoted_position_column_with_table_name} + 1)"
end

define_singleton_method :update_all_with_touch do |updates|
record = new
attrs = record.send(:timestamp_attributes_for_update_in_model)
now = record.send(:current_time_from_proper_timezone)

query = attrs.map { |attr| %(\#{connection.quote_column_name(attr)} = :now) }
query = attrs.map { |attr| "#{connection.quote_column_name(attr)} = :now" }
query.push updates
query = query.join(", ")

update_all([query, now: now])
end
EOV
end

attr_reader :position_changed

Expand Down

0 comments on commit 3ef9657

Please sign in to comment.