Sha256: 2532524aaf08f89ca3aa4eb15f399bbefe6a0563def7c92ee920a4df51074fa3

Contents?: true

Size: 961 Bytes

Versions: 8

Compression:

Stored size: 961 Bytes

Contents

require 'attr_json/record/query_builder'

module AttrJson
  module Record
    # Adds query-ing scopes into a AttrJson::Record, based
    # on postgres jsonb.
    #
    # Has to be mixed into something that also is a AttrJson::Record please!
    #
    # @example
    #      class MyRecord < ActiveRecord::Base
    #        include AttrJson::Record
    #        include AttrJson::Record::QueryScopes
    #
    #        attr_json :a_string, :string
    #      end
    #
    #      some_model.jsonb_contains(a_string: "foo").first
    #
    # See more in {file:README} docs.
    module QueryScopes
      extend ActiveSupport::Concern

      included do
        unless self < AttrJson::Record
          raise TypeError, "AttrJson::Record::QueryScopes can only be included in a AttrJson::Record"
        end

        scope(:jsonb_contains, lambda do |attributes|
          QueryBuilder.new(self, attributes).contains_relation
        end)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
attr_json-1.0.0 lib/attr_json/record/query_scopes.rb
attr_json-0.7.0 lib/attr_json/record/query_scopes.rb
attr_json-0.6.0 lib/attr_json/record/query_scopes.rb
attr_json-0.5.0 lib/attr_json/record/query_scopes.rb
attr_json-0.4.0 lib/attr_json/record/query_scopes.rb
attr_json-0.3.0 lib/attr_json/record/query_scopes.rb
attr_json-0.2.0 lib/attr_json/record/query_scopes.rb
attr_json-0.1.0 lib/attr_json/record/query_scopes.rb