Skip to content
Browse files

Scopes simplified by using class methods

  • Loading branch information...
1 parent 41f13f9 commit ea8729a5d569fc87945025c4db27f95982b6b249 @ledermann committed Mar 8, 2013
Showing with 22 additions and 26 deletions.
  1. +2 −2 lib/rails-settings.rb
  2. +20 −24 lib/rails-settings/scopes.rb
View
4 lib/rails-settings.rb
@@ -7,8 +7,8 @@
def self.has_settings(*args, &block)
RailsSettings::Configuration.new(*args.unshift(self), &block)
- include RailsSettings::Base unless self.include?(RailsSettings::Base)
- include RailsSettings::Scopes unless self.include?(RailsSettings::Scopes)
+ include RailsSettings::Base
+ extend RailsSettings::Scopes
end
end
View
44 lib/rails-settings/scopes.rb
@@ -1,33 +1,29 @@
module RailsSettings
module Scopes
- def self.included(base)
- base.class_eval do
- scope :with_settings, lambda {
- joins("INNER JOIN settings ON #{settings_join_condition}").
- uniq
- }
+ def with_settings
+ joins("INNER JOIN settings ON #{settings_join_condition}").
+ uniq
+ end
- scope :with_settings_for, lambda { |var|
- raise ArgumentError unless var.is_a?(Symbol)
- joins("INNER JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'")
- }
+ def with_settings_for(var)
+ raise ArgumentError.new('Symbol expected!') unless var.is_a?(Symbol)
+ joins("INNER JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'")
+ end
- scope :without_settings, lambda {
- joins("LEFT JOIN settings ON #{settings_join_condition}").
- where('settings.id IS NULL')
- }
+ def without_settings
+ joins("LEFT JOIN settings ON #{settings_join_condition}").
+ where('settings.id IS NULL')
+ end
- scope :without_settings_for, lambda { |var|
- raise ArgumentError unless var.is_a?(Symbol)
- joins("LEFT JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'").
- where('settings.id IS NULL')
- }
+ def without_settings_for(var)
+ raise ArgumentError.new('Symbol expected!') unless var.is_a?(Symbol)
+ joins("LEFT JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'").
+ where('settings.id IS NULL')
+ end
- def self.settings_join_condition
- "settings.target_id = #{table_name}.#{primary_key} AND
- settings.target_type = '#{base_class.name}'"
- end
- end
+ def settings_join_condition
+ "settings.target_id = #{table_name}.#{primary_key} AND
+ settings.target_type = '#{base_class.name}'"
end
end
end

0 comments on commit ea8729a

Please sign in to comment.
Something went wrong with that request. Please try again.