Sha256: cb163bf05b6148020fae69f243d9a70020c93016a60a7348d0f687aba9ff9af2

Contents?: true

Size: 984 Bytes

Versions: 3

Compression:

Stored size: 984 Bytes

Contents

require 'active_record/relation'
require 'pgrel/active_record/store_chain'
require 'pgrel/active_record/store_chain/array_chain'
require 'pgrel/active_record/store_chain/hstore_chain'
require 'pgrel/active_record/store_chain/jsonb_chain'

module ActiveRecord
  module QueryMethods
    # Extend WhereChain with 'store' method.
    class WhereChain
      def store(store_name, *opts)
        store_name = store_name.to_s
        column = @scope.klass.columns_hash[store_name]

        # Rails 4 column has method 'array'
        # but Rails 5 has 'array?'.
        #
        # So, check both(
        if (arr = column.try(:array)) || (arr.nil? && column.array?)
          klass = ArrayChain
        else
          column_klass = column.type.capitalize
          klass = "ActiveRecord::QueryMethods::#{column_klass}Chain".constantize
        end
        chain = klass.new(@scope, store_name)
        return chain.where(*opts) unless opts.empty?
        chain
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pgrel-0.1.2 lib/pgrel/active_record/query_methods.rb
pgrel-0.1.1 lib/pgrel/active_record/query_methods.rb
pgrel-0.1.0 lib/pgrel/active_record/query_methods.rb