Class: Siphon::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/siphon/base.rb

Overview

Base

use : Handles the ActiveRelation on which

scopes from a **FormObject** will be applied

e.g : Siphon::Base.new(Book.published).scope(BookForm.new(params))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ Base

Siphon.new(Book.scoped)



15
16
17
# File 'lib/siphon/base.rb', line 15

def initialize(relation)
  @relation = relation
end

Instance Attribute Details

#relationObject

Returns the value of attribute relation.



12
13
14
# File 'lib/siphon/base.rb', line 12

def relation
  @relation
end

Instance Method Details

#scope(formobject) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/siphon/base.rb', line 19

def scope(formobject)
  scopes_hash = Siphon::Adapter.new(formobject).call

  scopes_hash.each do |meth, arg|
    self.relation = if arg.is_a?(Array)
      arg.any? ? relation.send(meth, *arg) : relation
    elsif arg.nil?
      relation.send(meth)
    else
      relation.send(meth, arg)
    end
  end

  relation
end