Sha256: 0d721aa1d9243478da9016bce3b466fa81f19d78081ded9632e2a553b36fad5a

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

module RailsSettings
  module Extend
    extend ActiveSupport::Concern
    
    included do 
      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, ->(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}')")
        .where("settings.id IS NULL")    
      } 
                                     
      scope :without_settings_for, ->(var) {
        where('settings.id IS NULL')
        .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}'")
      }
                                                                                    
    end
    
    def settings
      ScopedSettings.for_thing(self)
    end
  end
end


Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails-settings-cached-0.4.1 lib/rails-settings/extend.rb
rails-settings-cached-0.4.0 lib/rails-settings/extend.rb
rails-settings-cached-0.3.2 lib/rails-settings/extend.rb
rails-settings-cached-0.3.1 lib/rails-settings/extend.rb