Sha256: e707241b2b3903a56d1ff1bf218ab240ad93cf0615717cb6d0e87ac777d4347f

Contents?: true

Size: 1.18 KB

Versions: 2

Compression:

Stored size: 1.18 KB

Contents

module Mobility
  module Backend
    module ActiveRecord
=begin

Defines query method overrides to handle translated attributes for ActiveRecord
models. For details see backend-specific subclasses.

=end
      class QueryMethods < Module
        # @param [Array<String>] attributes Translated attributes
        # @param [Hash] options Backend options
        def initialize(attributes, **)
          @attributes = attributes
          @attributes_extractor = lambda do |opts|
            opts.is_a?(Hash) && (opts.keys.map(&:to_s) & attributes).presence
          end
        end

        # @param [ActiveRecord::Relation] relation Relation being extended
        def extended(relation)
          model_class = relation.model
          unless model_class.respond_to?(:mobility_where_chain)
            model_class.define_singleton_method(:mobility_where_chain) do
              @mobility_where_chain ||= Class.new(::ActiveRecord::QueryMethods::WhereChain)
            end

            relation.define_singleton_method :where do |opts = :chain, *rest|
              opts == :chain ? mobility_where_chain.new(spawn) : super(opts, *rest)
            end
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mobility-0.1.5 lib/mobility/backend/active_record/query_methods.rb
mobility-0.1.4 lib/mobility/backend/active_record/query_methods.rb