Sha256: 3c4d42f3f68b66afa2fd59a090ba4d88f1b6a9b692cce6ba3c489f0c30e82670

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

module Mobility
  module Backend
    class ActiveRecord::Column::QueryMethods < Backend::ActiveRecord::QueryMethods
      def initialize(attributes, **options)
        super
        attributes_extractor = @attributes_extractor
        @opts_converter = opts_converter = lambda do |opts|
          if i18n_keys = attributes_extractor.call(opts)
            opts = opts.with_indifferent_access
            i18n_keys.each { |attr| opts[Column.column_name_for(attr)] = opts.delete(attr) }
          end
          return opts
        end

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

        attributes.each do |attribute|
          define_method :"find_by_#{attribute}" do |value|
            find_by(Column.column_name_for(attribute) => value)
          end
        end
      end

      def extended(relation)
        super
        opts_converter = @opts_converter

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

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mobility-0.1.12 lib/mobility/backend/active_record/column/query_methods.rb
mobility-0.1.11 lib/mobility/backend/active_record/column/query_methods.rb
mobility-0.1.10 lib/mobility/backend/active_record/column/query_methods.rb
mobility-0.1.7 lib/mobility/backend/active_record/column/query_methods.rb
mobility-0.1.6 lib/mobility/backend/active_record/column/query_methods.rb
mobility-0.1.5 lib/mobility/backend/active_record/column/query_methods.rb
mobility-0.1.4 lib/mobility/backend/active_record/column/query_methods.rb