Class: RailsSettings::Railtie

Inherits:
Rails::Railtie
  • Object
show all
Defined in:
lib/rails-settings/railtie.rb,
lib/rails-settings/railtie.rb

Class Method Summary collapse

Class Method Details

.extend_active_recordObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rails-settings/railtie.rb', line 11

def self.extend_active_record
  ActiveRecord::Base.class_eval do
    def self.has_settings
      class_eval do
        def settings
          RailsSettings::ScopedSettings.for_thing(self)
        end
        
        scope :with_settings, :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
                                                                 settings.thing_type = '#{self.base_class.name}')",
                                    :select => "DISTINCT #{self.table_name}.*" 
  
        scope :with_settings_for, lambda { |var| { :joins => "JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
                                                                                      settings.thing_type = '#{self.base_class.name}') AND
                                                                                      settings.var = '#{var}'" } }
                                                                 
        scope :without_settings, :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
                                                                         settings.thing_type = '#{self.base_class.name}')",
                                       :conditions => 'settings.id IS NULL'
                                       
        scope :without_settings_for, lambda { |var| { :joins => "LEFT JOIN settings ON (settings.thing_id = #{self.table_name}.#{self.primary_key} AND
                                                                                              settings.thing_type = '#{self.base_class.name}') AND
                                                                                              settings.var = '#{var}'",
                                                            :conditions => 'settings.id IS NULL' } }
      end
    end
  end
end