Sha256: 9af448b142ae1f969ab6b47bbed95cf29e8aa5a456261789e50212c8a3a0e2d1

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 & 2 rubygems

Version Path
jdx-rails-settings-0.7.4 lib/rails-settings/extend.rb
rails-settings-cached-0.7.2 lib/rails-settings/extend.rb
rails-settings-cached-0.7.1 lib/rails-settings/extend.rb