Sha256: 2303ccf28f21e4f10063718c8778da451510b7557e72d9f491984e9461e29845

Contents?: true

Size: 1.24 KB

Versions: 5

Compression:

Stored size: 1.24 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
        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
        # @note Only want to define this once, even if multiple QueryMethods
        #   modules are included, so define it here in extended method
        def extended(relation)
          unless relation.methods(false).include?(:mobility_where_chain)
            relation.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

5 entries across 5 versions & 1 rubygems

Version Path
mobility-0.1.20 lib/mobility/backend/active_record/query_methods.rb
mobility-0.1.19 lib/mobility/backend/active_record/query_methods.rb
mobility-0.1.18 lib/mobility/backend/active_record/query_methods.rb
mobility-0.1.17 lib/mobility/backend/active_record/query_methods.rb
mobility-0.1.16 lib/mobility/backend/active_record/query_methods.rb