class Golden::QueryContext

Attributes

page[RW]
per[RW]
pluck_fields[R]
sort_direction[RW]
sort_field[RW]

Public Class Methods

attributes() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 6
def attributes
  merge_attributes!(:sort)
  accessor_attributes
end
new(accessors = {}) click to toggle source
# File lib/golden/objects/query/query_context.rb, line 20
def initialize(accessors = {})
  assign_attributes(accessors || {})
  @page ||= 1
  @per ||= 100
end

Public Instance Methods

perform(mode = :paginated, pluck: nil) click to toggle source
# File lib/golden/objects/query/query_context.rb, line 26
def perform(mode = :paginated, pluck: nil)
  @plucking = pluck.present?
  @pluck_fields = pluck
  send("find_#{mode}")
end
sort=(values) click to toggle source
# File lib/golden/objects/query/query_context.rb, line 32
def sort=(values)
  return if values.blank?

  field, direction = values.to_s.split(',')
  @sort_field = field.presence&.to_sym
  @sort_direction = direction.presence&.to_sym
end

Private Instance Methods

find_all() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 54
def find_all
  return [] if invalid?

  finder = relations.where(query_scopes).order(sort).all
  find_or_pluck(finder)
end
find_count() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 61
def find_count
  return nil if invalid?

  relations.where(query_scopes).order(sort).count
end
find_first() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 42
def find_first
  return nil if invalid?

  relations.where(query_scopes).order(sort).first
end
find_last() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 48
def find_last
  return nil if invalid?

  relations.where(query_scopes).order(sort).last
end
find_or_pluck(finder) click to toggle source
# File lib/golden/objects/query/query_context.rb, line 80
def find_or_pluck(finder)
  return finder unless plucking?

  finder.pluck(safe_table_fields(pluck_fields) || Arel.star)
end
find_paginated() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 67
def find_paginated
  return paginated_blank_result if invalid?

  finder = relations.where(query_scopes).order(sort).page(page).per(per)
  find_or_pluck(finder)
end
paginated_blank_result() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 74
def paginated_blank_result
  return Kaminari.paginate_array([], total_count: 0).page(1) if ::Object.const_defined? 'Kaminari'

  raise NotImplementedError
end
query_scopes() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 130
    def query_scopes
      raise NotImplementedError, <<~ERROR
        Please define #{__method__} like
        ```
          def #{__method__}
            @scopes = nil
            concat_xxx_scope
            @scopes
          end
        ```
        And define concat_xxx_scope like
        ```
          def concat_xxx_scope
            scope = Record.arel_table[:xxx_number].eq(@xxx_number)
            scopes_and(scope)
          end
        ```
      ERROR
    end
relations() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 96
    def relations
      raise NotImplementedError, <<~ERROR
        Please define #{__method__} like
        ```
          def #{__method__}
            @#{__method__} ||= Record.joins().includes().eager_load()
          end
        ```
      ERROR
    end
safe_table_fields(table_and_fields) click to toggle source
# File lib/golden/objects/query/query_context.rb, line 86
def safe_table_fields(table_and_fields)
  table_fields = table_and_fields.flat_map do |table, fields|
    table_name = send(table)&.name
    next if table_name.blank?

    fields.map { |field| [table_name, field.to_s].join('.') }
  end.compact
  Arel.sql(table_fields.join(', '))
end
scopes_and(scope) click to toggle source
# File lib/golden/objects/query/query_context.rb, line 150
def scopes_and(scope)
  @scopes = @scopes ? @scopes.and(scope) : scope
end
sort() click to toggle source
# File lib/golden/objects/query/query_context.rb, line 107
    def sort
      raise NotImplementedError, <<~ERROR
        Please define #{__method__} like
        ```
          def #{__method__}
            @sort_field ||= :id
            @sort_direction ||= :desc
            @#{__method__} = send("sort_by_\#{@sort_field}")
          rescue NoMethodError
            @#{__method__} = sort_by_id
          end
        ```
        And define sort_by_xxx like
        ```
          def sort_by_id
            [
              Record.arel_table[sort_field].send(sort_direction)
            ]
          end
        ```
      ERROR
    end