Class: Spyro::ActionViewExtension::CollectionForHelper::Parser::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/spyro/collections/parsers/base.rb

Direct Known Subclasses

ActiveRecordRelation, Model

Constant Summary collapse

@@types =
{}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, helper, options) ⇒ Base

Returns a new instance of Base.



16
17
18
19
20
21
22
23
24
25
# File 'lib/spyro/collections/parsers/base.rb', line 16

def initialize collection, helper, options
  @h = helper
  @collection = collection
  @options = options

  @unicollection = UniData::Collection.new
  options.each do |key, value|
    @unicollection.add_meta key, value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, args) ⇒ Object



48
49
50
# File 'lib/spyro/collections/parsers/base.rb', line 48

def method_missing method, args
  @unicollection.add_meta method, args
end

Instance Attribute Details

#unicollectionObject

Returns the value of attribute unicollection.



10
11
12
# File 'lib/spyro/collections/parsers/base.rb', line 10

def unicollection
  @unicollection
end

Instance Method Details

#hash_for_name(model, name) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/spyro/collections/parsers/base.rb', line 33

def hash_for_name model, name
  #value = model[name] # en vrai
  value = model.send(name) # intra

  if name =~ /(.+)_id$/
    model_belong = model.send($1) rescue nil
    if model_belong
      link = link(model_belong) rescue nil
      return {value: model_belong.try(:to_desc) || model_belong.try(:name) || model_belong.id,
              type: String, name: name, db_type: String, link: link}
    end
  end
  {value: value, type: value.class, name: name, db_type: (model.has_attribute?(name) ? model.type_for_attribute(name) : nil)}
end


27
28
29
30
31
# File 'lib/spyro/collections/parsers/base.rb', line 27

def link model_belong
  final_link = polymorphic_path([@options[:parents].first, model_belong]) rescue nil
  final_link = polymorphic_path([*@options[:parents], model_belong]) rescue nil if final_link.nil?
  final_link
end

#parse_defaultObject



52
53
# File 'lib/spyro/collections/parsers/base.rb', line 52

def parse_default
end