Sha256: 1d06ced248fe2141ef516877a3c54e9be48d1b9f02204cefe96fbbe80568af60

Contents?: true

Size: 957 Bytes

Versions: 4

Compression:

Stored size: 957 Bytes

Contents

module Mobility
  module Backend
    class ActiveRecord::Serialized::QueryMethods < ActiveRecord::QueryMethods
      def initialize(attributes, **options)
        super
        attributes_extractor = @attributes_extractor
        opts_checker = @opts_checker = lambda do |opts|
          if keys = attributes_extractor.call(opts)
            raise ArgumentError,
              "You cannot query on mobility attributes translated with the Serialized backend (#{keys.join(", ")})."
          end
        end

        define_method :where! do |opts, *rest|
          opts_checker.call(opts) || super(opts, *rest)
        end
      end

      def extended(relation)
        super
        opts_checker = @opts_checker

        mod = Module.new do
          define_method :not do |opts, *rest|
            opts_checker.call(opts) || super(opts, *rest)
          end
        end
        relation.model.mobility_where_chain.prepend(mod)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mobility-0.1.3 lib/mobility/backend/active_record/serialized/query_methods.rb
mobility-0.1.2 lib/mobility/backend/active_record/serialized/query_methods.rb
mobility-0.1.1 lib/mobility/backend/active_record/serialized/query_methods.rb
mobility-0.1.0 lib/mobility/backend/active_record/serialized/query_methods.rb