Sha256: 14a44518531e4da5c4c6e1844a518144527087dff1b0f3049e7739150d82bc3c

Contents?: true

Size: 1.84 KB

Versions: 10

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module PaperTrail
  module Queries
    module Versions
      # For public API documentation, see `where_object` in
      # `paper_trail/version_concern.rb`.
      # @api private
      class WhereObject
        # - version_model_class - The class that VersionConcern was mixed into.
        # - attributes - A `Hash` of attributes and values. See the public API
        #   documentation for details.
        # @api private
        def initialize(version_model_class, attributes)
          @version_model_class = version_model_class
          @attributes = attributes
        end

        # @api private
        def execute
          column = @version_model_class.columns_hash["object"]
          raise "where_object can't be called without an object column" unless column

          case column.type
          when :jsonb
            jsonb
          when :json
            json
          else
            text
          end
        end

        private

        # @api private
        def json
          predicates = []
          values = []
          @attributes.each do |field, value|
            predicates.push "object->>? = ?"
            values.concat([field, value.to_s])
          end
          sql = predicates.join(" and ")
          @version_model_class.where(sql, *values)
        end

        # @api private
        def jsonb
          @version_model_class.where("object @> ?", @attributes.to_json)
        end

        # @api private
        def text
          arel_field = @version_model_class.arel_table[:object]
          where_conditions = @attributes.map { |field, value|
            ::PaperTrail.serializer.where_object_condition(arel_field, field, value)
          }
          where_conditions = where_conditions.reduce { |a, e| a.and(e) }
          @version_model_class.where(where_conditions)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
paper_trail-12.0.0 lib/paper_trail/queries/versions/where_object.rb
paper_trail-11.1.0 lib/paper_trail/queries/versions/where_object.rb
paper_trail-11.0.0 lib/paper_trail/queries/versions/where_object.rb
paper_trail-10.3.1 lib/paper_trail/queries/versions/where_object.rb
paper_trail-10.3.0 lib/paper_trail/queries/versions/where_object.rb
paper_trail-10.2.1 lib/paper_trail/queries/versions/where_object.rb
paper_trail-10.2.0 lib/paper_trail/queries/versions/where_object.rb
paper_trail-10.1.0 lib/paper_trail/queries/versions/where_object.rb
paper_trail-10.0.1 lib/paper_trail/queries/versions/where_object.rb
paper_trail-10.0.0 lib/paper_trail/queries/versions/where_object.rb