Sha256: a3ba1ff83767e10fdf127557228a074a869873ecc20b96aba03f112d5fb12d50

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

Contents

require "mobility/backends/active_record/query_methods"

module Mobility
  module Backends
    class ActiveRecord::Column::QueryMethods < ActiveRecord::QueryMethods
      def initialize(attributes, _)
        super
        q = self

        define_method :where! do |opts, *rest|
          super(q.convert_opts(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
        q = self

        mod = Module.new do
          define_method :not do |opts, *rest|
            super(q.convert_opts(opts), *rest)
          end
        end
        relation.mobility_where_chain.include(mod)
      end

      def convert_opts(opts)
        if i18n_keys = extract_attributes(opts)
          opts = opts.with_indifferent_access
          i18n_keys.each { |attr| opts[Column.column_name_for(attr)] = opts.delete(attr) }
        end
        opts
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mobility-0.4.1 lib/mobility/backends/active_record/column/query_methods.rb
mobility-0.4.0 lib/mobility/backends/active_record/column/query_methods.rb