Created
April 8, 2010 18:48
-
-
Save gvarela/360389 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module IsContentChunk | |
def self.included(base) | |
base.class_eval do | |
named_scope :by_association, lambda{ |key| {:conditions => ['association = ?', key] } } | |
named_scope :regional_and_or_default, lambda{ |key| { | |
:include => [:region], | |
:conditions => ['association = ? AND ( region_id = ? OR region_id = ? )', key, self.current_region, Region.default], | |
:order => "FIELD(region_id, #{id_or_null( self.current_region )}, #{id_or_null( Region.default )})" | |
} } | |
def self.regional_or_default(key) | |
self.current_region ? self.regional_and_or_default(key).first : self.in_default_region.by_association(key).first | |
end | |
def self.get(*args) | |
args.inject([]) do |results, key| | |
results << self.regional_or_default(key) | |
end | |
end | |
def self.cache_key(key,region = self.current_region) | |
"#{self.name.underscore.pluralize}/#{key}-#{region.to_param}" | |
end | |
# lame but order doesn't cleanup nicely like conditions. | |
def self.id_or_null(region) | |
region && region.is_a?(Region) ? region.id : 'NULL' | |
end | |
end | |
end | |
end |
This file contains hidden or 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
module IsRegional | |
def self.included(base) | |
base.class_eval do | |
named_scope :regional, lambda{ |*args| | |
region = args && args[0].is_a?(Region) ? args[0] : self.current_region | |
{ | |
:conditions => ['regions.id = ?', region] | |
}.merge(self.is_regional_includes) | |
} | |
named_scope :in_default_region, lambda{ { | |
:include => :region, | |
:conditions => ['regions.id = ?', Region.default] | |
}} | |
named_scope :no_regions, lambda { self.is_regional_includes } | |
def self.is_regional_includes | |
{:include => :region} | |
end | |
def self.regional_or_default | |
self.regional(self.current_region || Region.default) | |
end | |
def self.regional_or_global(region=nil) | |
region.nil? ? self.no_regions : self.regional(region) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment