Class: ActiveRecord::RelationDrop

Inherits:
Liquid::Drop
  • Object
show all
Defined in:
lib/liquid-rails/drops/collection_drop.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(objects, options = {}) ⇒ RelationDrop

Returns a new instance of RelationDrop.



30
31
32
33
34
35
# File 'lib/liquid-rails/drops/collection_drop.rb', line 30

def initialize(objects, options={})
  options.assert_valid_keys(:with, :scope)

  @objects         = options[:scope].nil? ? objects : objects.send(options[:scope])
  @drop_class_name = options[:with]
end

Class Attribute Details

._scopesObject

Returns the value of attribute _scopes.



5
6
7
# File 'lib/liquid-rails/drops/collection_drop.rb', line 5

def _scopes
  @_scopes
end

Class Method Details

.inherited(base) ⇒ Object



8
9
10
# File 'lib/liquid-rails/drops/collection_drop.rb', line 8

def self.inherited(base)
  base._scopes = []
end

.scope(*scope_names) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/liquid-rails/drops/collection_drop.rb', line 12

def self.scope(*scope_names)
  @_scopes.concat scope_names

  scope_names.each do |scope_name|
    define_method(scope_name) do
      value = instance_variable_get("@_#{scope_name}")
      return value if value

      raise ::Liquid::ArgumentError, "#{objects.class.name} doesn't define scope: #{scope_name}" unless objects.respond_to?(scope_name)
      instance_variable_set("@_#{scope_name}", self.class.new(objects.send(scope_name)))
    end
  end
end

Instance Method Details

#[](method) ⇒ Object

:[] is invoked by parser before the actual. However, this method has the same name as array method. Override this, so it will work for both cases. post_drop.comments } post_drop.<other_methods> }



58
59
60
61
62
63
64
# File 'lib/liquid-rails/drops/collection_drop.rb', line 58

def [](method)
  if method.is_a?(Integer)
    dropped_collection.at(method)
  else
    public_send(method)
  end
end

#dropped_collectionObject



45
46
47
# File 'lib/liquid-rails/drops/collection_drop.rb', line 45

def dropped_collection
  @dropped_collection ||= @objects.map { |item| drop_item(item) }
end

#inspectObject



72
73
74
# File 'lib/liquid-rails/drops/collection_drop.rb', line 72

def inspect
  "#<#{self.class.name} of #{drop_class} for #{objects.inspect}>"
end

#kind_of?(klass) ⇒ Boolean Also known as: is_a?

Returns:

  • (Boolean)


49
50
51
# File 'lib/liquid-rails/drops/collection_drop.rb', line 49

def kind_of?(klass)
  dropped_collection.kind_of?(klass) || super
end

#page(number) ⇒ Object



37
38
39
# File 'lib/liquid-rails/drops/collection_drop.rb', line 37

def page(number)
  self.class.new(objects.page(number))
end

#per(number) ⇒ Object



41
42
43
# File 'lib/liquid-rails/drops/collection_drop.rb', line 41

def per(number)
  self.class.new(objects.per(number))
end

#to_liquidObject

Need to override this. I don’t understand too, otherwise it will return an array of drop objects. Need to return self so that we can do chaining.



68
69
70
# File 'lib/liquid-rails/drops/collection_drop.rb', line 68

def to_liquid
  self
end