Module: Siphon::Scope::InstanceMethods

Defined in:
lib/siphon/scope.rb

Instance Method Summary collapse

Instance Method Details

#convert_to_hash(params) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/siphon/scope.rb', line 33

def convert_to_hash(params)
  if params.nil?
    {}
  elsif params.respond_to?(:permit!)
    params.permit!
  elsif params.respond_to?(:with_indifferent_access)
    params.with_indifferent_access
  else
    params
  end
end

#initialize(params = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/siphon/scope.rb', line 23

def initialize( params = {})
  @params = convert_to_hash(params)
  super @params

  @ransack_attributes = attributes.slice(*self.class.ransack_set)
  @siphon_attributes  = attributes.slice(*self.class.siphon_set)

  self.order_by   = @params["order_by"]
end

#merge(relation) ⇒ Object



53
54
55
56
# File 'lib/siphon/scope.rb', line 53

def merge(relation)
  @relation = @relation.merge(relation)
  self
end

#preformat_date(value = nil) ⇒ Object



67
68
69
# File 'lib/siphon/scope.rb', line 67

def preformat_date(value = nil)
  value.is_a?(String) && value.present? ? Date.strptime(value, I18n.t('date.formats.long')) : value
end

#ransackObject



49
50
51
# File 'lib/siphon/scope.rb', line 49

def ransack
  @relation.search(ransack_attributes)
end

#resultObject

memoized or it’ll break after attributes reconciled



63
64
65
# File 'lib/siphon/scope.rb', line 63

def result
  @result ||= siphoned.merge(ransack.result)
end

#siphonedObject



58
59
60
# File 'lib/siphon/scope.rb', line 58

def siphoned
  Siphon::Base.new(@relation).scope( self )
end

#table_nameObject



45
46
47
# File 'lib/siphon/scope.rb', line 45

def table_name
  self.class.model.table_name
end