Class: Golden::QueryContext

Inherits:
ApplicationContext show all
Includes:
ActiveRecord::Sanitization::ClassMethods
Defined in:
lib/golden/objects/query/query_context.rb

Constant Summary

Constants included from BooleanAccessor

BooleanAccessor::TRUE_VALUES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationContext

permits

Methods included from DateTimeAccessor

#parse_time

Methods included from ExtendedAttrAccessor

#attributes, #strip_attributes, #strip_attributes!

Constructor Details

#initialize(accessors = {}) ⇒ QueryContext

Returns a new instance of QueryContext.



20
21
22
23
24
# File 'lib/golden/objects/query/query_context.rb', line 20

def initialize(accessors = {})
  assign_attributes(accessors || {})
  @page ||= 1
  @per ||= 100
end

Instance Attribute Details

#pageObject

Returns the value of attribute page.



14
15
16
# File 'lib/golden/objects/query/query_context.rb', line 14

def page
  @page
end

#perObject

Returns the value of attribute per.



14
15
16
# File 'lib/golden/objects/query/query_context.rb', line 14

def per
  @per
end

#pluck_fieldsObject (readonly)

Returns the value of attribute pluck_fields.



18
19
20
# File 'lib/golden/objects/query/query_context.rb', line 18

def pluck_fields
  @pluck_fields
end

#sort_directionObject

Returns the value of attribute sort_direction.



15
16
17
# File 'lib/golden/objects/query/query_context.rb', line 15

def sort_direction
  @sort_direction
end

#sort_fieldObject

Returns the value of attribute sort_field.



15
16
17
# File 'lib/golden/objects/query/query_context.rb', line 15

def sort_field
  @sort_field
end

Class Method Details

.attributesObject



6
7
8
9
# File 'lib/golden/objects/query/query_context.rb', line 6

def attributes
  merge_attributes!(:sort)
  accessor_attributes
end

Instance Method Details

#perform(mode = :paginated, pluck: nil) ⇒ Object



26
27
28
29
30
# 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) ⇒ Object



32
33
34
35
36
37
38
# 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