Sha256: 0d2ab9a0739b137fd04bc4848af893ea0b97d85c7a8c61ab5ad9a36aa089e249

Contents?: true

Size: 1.2 KB

Versions: 3

Compression:

Stored size: 1.2 KB

Contents

module ActiveRecord::Acts::List::ScopeMethodDefiner #:nodoc:
  extend ActiveSupport::Inflector

  def self.call(caller_class, scope)
    scope = idify(scope) if scope.is_a?(Symbol)

    caller_class.class_eval do
      define_method :scope_name do
        scope
      end

      if scope.is_a?(Symbol)
        define_method :scope_condition do
          { scope => send(:"#{scope}") }
        end

        define_method :scope_changed? do
          changed.include?(scope_name.to_s)
        end
      elsif scope.is_a?(Array)
        define_method :scope_condition do
          scope.inject({}) do |hash, column|
            hash.merge!({ column.to_sym => read_attribute(column.to_sym) })
          end
        end

        define_method :scope_changed? do
          (scope_condition.keys & changed.map(&:to_sym)).any?
        end
      else
        define_method :scope_condition do
          eval "%{#{scope}}"
        end

        define_method :scope_changed? do
          false
        end
      end

      self.scope :in_list, lambda { where("#{quoted_position_column_with_table_name} IS NOT NULL") }
    end
  end

  def self.idify(name)
    return name if name.to_s =~ /_id$/

    foreign_key(name).to_sym
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
acts_as_list-0.9.2 lib/acts_as_list/active_record/acts/scope_method_definer.rb
acts_as_list-0.9.1 lib/acts_as_list/active_record/acts/scope_method_definer.rb
acts_as_list-0.9.0 lib/acts_as_list/active_record/acts/scope_method_definer.rb