Sha256: af9e98a4ddd0d8ca976de29c7b74df74fed0079689b3bb70bc40ccfa6eea2e88

Contents?: true

Size: 1.44 KB

Versions: 9

Compression:

Stored size: 1.44 KB

Contents

# encoding: utf-8

module Uuids

  # @api hide
  # Contains complex queries to be used in model scopes.
  module Queries

    # @api hide
    # The query selects records by uuids.
    #
    # @example Selects records by an array of uuids
    #   ByUuid.select(
    #     MyModel,
    #     [
    #       "20547204-3875-0234-7d52-437523057235",
    #       "04702362-355e-9237-5e32-da2057054720"
    #     ]
    #   )
    #
    # @example Selects records by a list of uuids
    #   ByUuid.select(
    #     MyModel,
    #     "20547204-3875-0234-7d52-437523057235",
    #     "04702362-355e-9237-5e32-da2057054720"
    #   )
    #
    # @example Selects all records when no values given
    #   result =  ByUuid.select MyModel
    #   result == MyModel.all # => true
    class ByUuid < Hexx::Scope

      # @api hide
      # Initializes the query object.
      # @param [ActiveRecord::Base, #uuids] model The model for selecting
      #   records of.
      # @param [Array<String>, nil] values The values to select records by.
      def initialize(model, *values)
        super(model)
        @values = values.flatten
      end

      # @api hide
      # Runs and returns the query
      # @return [ActiveRecord::Relation] The updated scope.
      def select
        values.any? ? by_uuid : model.all
      end

      private

      attr_reader :values

      def by_uuid
        model.joins(:uuids).where(uuids_uuids: { value: values }).uniq
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
uuids-4.1.8 app/queries/by_uuid.rb
uuids-4.1.7 app/queries/by_uuid.rb
uuids-4.1.6 app/queries/by_uuid.rb
uuids-4.1.5 app/queries/by_uuid.rb
uuids-4.1.4 app/queries/by_uuid.rb
uuids-4.1.3 app/queries/by_uuid.rb
uuids-4.1.2 app/queries/by_uuid.rb
uuids-4.1.1 app/queries/by_uuid.rb
uuids-4.1.0 app/queries/by_uuid.rb