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

Refactor class_eval with string into class_eval with block #215

Merged
merged 6 commits into from
Aug 28, 2016
Merged
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ gem "appraisal"
gem "github_changelog_generator", "1.9.0"

group :test do
gem "minitest", "~> 5.0"
gem "minitest", "~> 5.0"
gem "test_after_commit", "~> 0.4.2"
end
120 changes: 60 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,102 @@ 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

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}"
caller_class = self

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
caller_class
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

define_singleton_method :quoted_position_column do
@_quoted_position_column ||= connection.quote_column_name(configuration[:column])
end

define_singleton_method :quoted_position_column_with_table_name do
@_quoted_position_column_with_table_name ||= "#{caller_class.quoted_table_name}.#{quoted_position_column}"
end

scope :in_list, lambda { where(%q{#{quoted_position_column_with_table_name} IS NOT NULL}) }
scope :in_list, lambda { where("#{quoted_position_column_with_table_name} IS NOT NULL") }

def self.decrement_all
update_all_with_touch %q(#{quoted_position_column} = (#{quoted_position_column_with_table_name} - 1))
define_singleton_method :decrement_all do
update_all_with_touch "#{quoted_position_column} = (#{quoted_position_column_with_table_name} - 1)"
end

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

def self.update_all_with_touch(updates)
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