Class: ActiveRecord::RelationDrop
- Inherits:
-
Liquid::Drop
- Object
- Liquid::Drop
- ActiveRecord::RelationDrop
- Defined in:
- lib/liquid-rails/drops/collection_drop.rb
Class Attribute Summary collapse
-
._scopes ⇒ Object
Returns the value of attribute _scopes.
Class Method Summary collapse
Instance Method Summary collapse
-
#[](method) ⇒ Object
:[] is invoked by parser before the actual.
- #dropped_collection ⇒ Object
-
#initialize(objects, options = {}) ⇒ RelationDrop
constructor
A new instance of RelationDrop.
- #inspect ⇒ Object
- #kind_of?(klass) ⇒ Boolean (also: #is_a?)
- #page(number) ⇒ Object
- #per(number) ⇒ Object
-
#to_liquid ⇒ Object
Need to override this.
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, ={}) .assert_valid_keys(:with, :scope) @objects = [:scope].nil? ? objects : objects.send([:scope]) @drop_class_name = [:with] end |
Class Attribute Details
._scopes ⇒ Object
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_collection ⇒ Object
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 |
#inspect ⇒ Object
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?
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_liquid ⇒ Object
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 |