Sha256: 7c11ebd9dec1e4a7003f30962edb88b51e8c3af9c4d28dd87ce4042155729a17

Contents?: true

Size: 1.23 KB

Versions: 4

Compression:

Stored size: 1.23 KB

Contents

module Mobility
  module Backend
    class ActiveRecord::Column::QueryMethods < Backend::ActiveRecord::QueryMethods
      def initialize(attributes, **options)
        super
        attributes_extractor = @attributes_extractor

        define_method :where! do |opts, *rest|
          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
          super(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
        attributes_extractor = @attributes_extractor

        mod = Module.new do
          define_method :not do |opts, *rest|
            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
            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/column/query_methods.rb
mobility-0.1.2 lib/mobility/backend/active_record/column/query_methods.rb
mobility-0.1.1 lib/mobility/backend/active_record/column/query_methods.rb
mobility-0.1.0 lib/mobility/backend/active_record/column/query_methods.rb