Permalink
Browse files

Scopes: Refactoring to remove code duplication

  • Loading branch information...
1 parent 2bd272b commit a58905515b11eb4083f5f7af1a5b568325c662bf @ledermann committed Mar 7, 2013
Showing with 9 additions and 12 deletions.
  1. +9 −12 lib/rails-settings/scopes.rb
@@ -3,33 +3,30 @@ module Scopes
def self.included(base)
base.class_eval do
scope :with_settings, lambda {
- joins(:setting_objects).
+ joins("INNER JOIN settings ON #{settings_join_condition}").
uniq
}
scope :with_settings_for, lambda { |var|
raise ArgumentError unless var.is_a?(Symbol)
-
- joins("JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
- settings.target_type = '#{self.base_class.name}') AND
- settings.var = '#{var}'")
+ joins("INNER JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'")
}
scope :without_settings, lambda {
- joins("LEFT JOIN settings ON
- (settings.target_id = #{self.table_name}.#{self.primary_key} AND
- settings.target_type = '#{self.base_class.name}')").
+ joins("LEFT JOIN settings ON #{settings_join_condition}").
where('settings.id IS NULL')
}
scope :without_settings_for, lambda { |var|
raise ArgumentError unless var.is_a?(Symbol)
-
- joins("LEFT JOIN settings ON (settings.target_id = #{self.table_name}.#{self.primary_key} AND
- settings.target_type = '#{self.base_class.name}') AND
- settings.var = '#{var}'").
+ joins("LEFT JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'").
where('settings.id IS NULL')
}
+
+ def self.settings_join_condition
+ "settings.target_id = #{table_name}.#{primary_key} AND
+ settings.target_type = '#{base_class.name}'"
+ end
end
end
end

0 comments on commit a589055

Please sign in to comment.