Class: Gyroscope::SearchBase

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/gyroscope/search_base.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SearchBase

Returns a new instance of SearchBase.



15
16
17
18
# File 'lib/gyroscope/search_base.rb', line 15

def initialize(*args)
  super(*args)
  normalize_order attributes[:order]
end

Instance Method Details

#normalize_order(ordering) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/gyroscope/search_base.rb', line 34

def normalize_order(ordering)
  return unless attributes[:order]

  unless /\./.match ordering
    self.order = "#{target_model.table_name}.#{ordering}"
  else
    self.order = ordering
  end
end

#searchObject



24
25
26
27
28
29
30
31
32
# File 'lib/gyroscope/search_base.rb', line 24

def search
  # is there a more generic ActiveModel validation error?
  unless valid?
    Rails.logger.debug("the errors: #{errors.inspect}")
  end
  fail ActiveModel::ForbiddenAttributesError, "Check your default values for correctness" unless valid?

  build_search_scope
end

#target_modelObject



20
21
22
# File 'lib/gyroscope/search_base.rb', line 20

def target_model
  "::#{self.class.name.demodulize}".constantize
end