Module: Faceted::Presenter::ClassMethods

Defined in:
lib/faceted/presenter.rb

Instance Method Summary collapse

Instance Method Details

#allObject



35
36
37
# File 'lib/faceted/presenter.rb', line 35

def all
  materialize(klass.all)
end

#find(id) ⇒ Object



39
40
41
# File 'lib/faceted/presenter.rb', line 39

def find(id)
  materialize(klass.where(id: id)).first
end

#klassObject



20
21
22
# File 'lib/faceted/presenter.rb', line 20

def klass
  @presents
end

#presents(name, args = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/faceted/presenter.rb', line 24

def presents(name, args={})
  class_name = args[:class_name] || name.to_s.classify # LineItem
  @presents = eval(class_name)
  define_method :find_by do
    args[:find_by] || :id
  end
  define_method :"#{name}" do
    object
  end
end

#where(args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/faceted/presenter.rb', line 43

def where(args)
  if klass.respond_to? :fields
    if klass.fields.respond_to?(:keys)
      # Mongoid
      attrs = args.select{|k,v| klass.fields.keys.include? k.to_s}
    else
      attrs = args.select{|k,v| klass.fields.include? k.to_s}
    end
  else
    # ActiveRecord et al
    attrs = args.select{|k,v| klass.column_names.include? k.to_s}
  end
  materialize(klass.where(attrs))
end