Sha256: d4f840003dc0fdc7738a4e2d3cc9ee51852e8b60e4d992be90f052ba494fd872

Contents?: true

Size: 1.14 KB

Versions: 11

Compression:

Stored size: 1.14 KB

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
    #
    #      some_model.not_jsonb_contains(a_string: "bar").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)

        scope(:not_jsonb_contains, lambda do |attributes|
          QueryBuilder.new(self, attributes).contains_not_relation
        end)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
attr_json-2.2.0 lib/attr_json/record/query_scopes.rb
attr_json-2.1.0 lib/attr_json/record/query_scopes.rb
attr_json-2.0.1 lib/attr_json/record/query_scopes.rb
attr_json-2.0.0 lib/attr_json/record/query_scopes.rb
attr_json-2.0.0.rc1 lib/attr_json/record/query_scopes.rb
attr_json-1.5.0 lib/attr_json/record/query_scopes.rb
attr_json-1.4.1 lib/attr_json/record/query_scopes.rb
attr_json-1.4.0 lib/attr_json/record/query_scopes.rb
attr_json-1.3.0 lib/attr_json/record/query_scopes.rb
attr_json-1.2.0 lib/attr_json/record/query_scopes.rb
attr_json-1.1.0 lib/attr_json/record/query_scopes.rb