Sha256: 71bc5f21e15ae9b44a55f9e85ab507af71fc9a9a6908cfe49df8e428a68c0ff4

Contents?: true

Size: 872 Bytes

Versions: 2

Compression:

Stored size: 872 Bytes

Contents

module ActiveRecord
  module ActsAs
    module Querying
      def where(opts = :chain, *rest)
        if opts.is_a? Hash
          opts, acts_as_opts = opts.partition { |k,v| attribute_names.include?(k.to_s) }
          opts, acts_as_opts = Hash[opts], Hash[acts_as_opts]
          opts[acting_as_model.table_name] = acts_as_opts
        end
        super(opts, *rest)
      end

      def find_by(*args)
        where(*args).take
      end

      def find_by!(*args)
        where(*args).take!
      end
    end
  end

  class Relation
    alias_method :scope_for_create_without_acting_as, :scope_for_create

    def scope_for_create
      @scope_for_create ||= if acting_as?
        where_values_hash.merge(where_values_hash(acting_as_model.table_name)).merge(create_with_value)
      else
        where_values_hash.merge(create_with_value)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_record-acts_as-1.0.0 lib/active_record/acts_as/querying.rb
active_record-acts_as-1.0.0.rc lib/active_record/acts_as/querying.rb