Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Scopes simplified by using class methods
- Loading branch information
Showing
2 changed files
with
22 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |