Sha256: 231cd77a4acfc151f9ae25bc196cd6622e405a69fc379cf0cf22121d82bf501a

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module RailsSettings
  module Extend
    extend ActiveSupport::Concern

    included do
      scope :with_settings, lambda {
        joins("JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
                                 settings.thing_type = '#{base_class.name}')")
          .select("DISTINCT #{table_name}.*")
      }

      scope :with_settings_for, lambda { |var|
        joins("JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
                                 settings.thing_type = '#{base_class.name}') AND settings.var = '#{var}'")
      }

      scope :without_settings, lambda {
        joins("LEFT JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
                                      settings.thing_type = '#{base_class.name}')")
          .where('settings.id IS NULL')
      }

      scope :without_settings_for, lambda { |var|
        where('settings.id IS NULL')
          .joins("LEFT JOIN settings ON (settings.thing_id = #{table_name}.#{primary_key} AND
                                       settings.thing_type = '#{base_class.name}') AND settings.var = '#{var}'")
      }
    end

    def settings
      ScopedSettings.for_thing(self)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails-settings-cached-0.7.0 lib/rails-settings/extend.rb
rails-settings-cached-0.6.6 lib/rails-settings/extend.rb
rails-settings-cached-0.6.5 lib/rails-settings/extend.rb