Sha256: 62e62388ab33ebbd3ceabdac9313a1110f7f34f9404d1a44265b49d90cb4071c

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

module ActiveRecord
  module ActsAs
    module QueryMethods
      def where(opts = :chain, *rest)
        if acting_as? && opts.is_a?(Hash)
          if table_name_opts = opts.delete(klass.table_name)
            opts = opts.merge(table_name_opts)
          end

          # Filter out the conditions that should be
          # applied to the `acting_as_model`. Ignore
          # conditions that contain a dot or are attributes
          # of the submodel.
          opts, acts_as_opts = opts.stringify_keys.partition { |k, _| k =~ /\./ || attribute_method?(k) }.map(&:to_h)

          if acts_as_opts.any?
            opts[acting_as_model.table_name] = acts_as_opts
          end
        end

        super
      end
    end

    module ScopeForCreate
      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

  Relation.send(:prepend, ActsAs::QueryMethods)
  Relation.send(:prepend, ActsAs::ScopeForCreate)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_record-acts_as-2.1.0 lib/active_record/acts_as/querying.rb
active_record-acts_as-2.0.9 lib/active_record/acts_as/querying.rb