Module: Models::LiquidMethods::ClassMethods

Defined in:
app/concerns/models/liquid_methods.rb

Instance Method Summary collapse

Instance Method Details

#liquid_methods(*allowed_methods) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/concerns/models/liquid_methods.rb', line 9

def liquid_methods(*allowed_methods)
  drop_class = eval "class #{self}::LiquidDropClass < Liquid::Drop; self; end"

  define_method :to_liquid do
    drop_class.new(self)
  end

  drop_class.class_eval do
    def initialize(object)
      @object = object
    end

    allowed_methods.each do |sym|
      define_method sym do
        r = @object.send sym
        # You can pass an array of objects to Liquid but not another type of collection
        if r.is_a?(ActiveRecord::Relation)
          r.to_a
        else
          r
        end
      end
    end
  end
end