Sha256: 67a1a9fe5f94218a004ee7fabb6bb74019b07926255c2fe3a70f6cf4aa69135b

Contents?: true

Size: 1.17 KB

Versions: 4

Compression:

Stored size: 1.17 KB

Contents

# frozen_string_literal: true

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

4 entries across 4 versions & 1 rubygems

Version Path
attr_json-2.5.0 lib/attr_json/record/query_scopes.rb
attr_json-2.4.0 lib/attr_json/record/query_scopes.rb
attr_json-2.3.1 lib/attr_json/record/query_scopes.rb
attr_json-2.3.0 lib/attr_json/record/query_scopes.rb