Sha256: 7546f1c150955cea474989e4e47d8f8b5a59de7a6b1ff916618cf18cba360709

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module RailsSettings
  module Scopes
    def self.included(base)
      base.class_eval do
        scope :with_settings, lambda {
          joins("INNER JOIN settings ON #{settings_join_condition}").
          uniq
        }

        scope :with_settings_for, lambda { |var|
          raise ArgumentError unless var.is_a?(Symbol)
          joins("INNER JOIN settings ON #{settings_join_condition} AND settings.var = '#{var}'")
        }

        scope :without_settings, lambda {
          joins("LEFT JOIN settings ON #{settings_join_condition}").
          where('settings.id IS NULL')
        }

        scope :without_settings_for, lambda { |var|
          raise ArgumentError unless var.is_a?(Symbol)
          joins("LEFT JOIN settings ON  #{settings_join_condition} AND settings.var = '#{var}'").
          where('settings.id IS NULL')
        }

        def self.settings_join_condition
          "settings.target_id   = #{table_name}.#{primary_key} AND
           settings.target_type = '#{base_class.name}'"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ledermann-rails-settings-2.0.0 lib/rails-settings/scopes.rb