Class: Spyro::ActionViewExtension::CollectionForHelper::Parser::ActiveRecordRelation

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

Direct Known Subclasses

Array

Instance Attribute Summary

Attributes inherited from Base

#unicollection

Instance Method Summary collapse

Methods inherited from Base

#hash_for_name, #link, #method_missing, #parse_default

Constructor Details

#initialize(collection, helper, options) ⇒ ActiveRecordRelation

Returns a new instance of ActiveRecordRelation.



11
12
13
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 11

def initialize collection, helper, options
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Spyro::ActionViewExtension::CollectionForHelper::Parser::Base

Instance Method Details

#actions(val = true) ⇒ Object



78
79
80
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 78

def actions val = true
  @unicollection.add_meta :actions, val
end

#body(opts = {}, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 82

def body opts = {}, &block
  def row opts
    @r.add_elem_meta :opts, opts
  end

  @collection.each do |elem|
    @unicollection.meta[:model_class] ||= elem.class.to_s
    @unicollection.row do |r|
      @elem = elem
      @r = r
      r.add_elem_meta :id, elem.id if elem.respond_to?(:id)
      block.call elem
    end
  end

  @unicollection.add_meta :html_body, opts[:html]
end

#buttons(buttons = nil, opts = {}, &block) ⇒ Object Also known as: button



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 120

def buttons buttons = nil, opts = {}, &block
  if block_given?
    @r.add_elem_meta :actions, @h.capture(&block)
  else
    buttons = :all if buttons.nil?
    buttons = [*buttons]
    buttons.map! {|b| b == :all ? [:show, :edit, :destroy] : b}
    buttons.flatten!
    @r.add_elem_meta :actions, populate_actions(@elem, @r, @options, [*buttons])
  end
end

#cell(value = nil, options = {}, &block) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 100

def cell value = nil, options = {}, &block
  if value.is_a? Hash
    options = value
    value = nil
  end

  value = @h.capture(&block) || "" if block_given?

  options[:link] = true if @unicollection.meta[:data] and @unicollection.meta[:data][@unicollection.rows.last[:data].count] == :name
  options[:link] = link @elem if options[:link] == true

  hash = if value
           {value: value, type: String, name: "custom", db_type: String}
         else
           hash_for_name @elem, @unicollection.meta[:data][@unicollection.rows.last[:data].count]
         end

  @r.add_elem UniData::Element.new(hash.merge(options))
end

#data(args, opts = {}, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 19

def data args, opts = {}, &block
  if args.is_a? ::Array
    @unicollection.add_meta :data, args
  else
    @unicollection.add_meta :data, args.values.reduce(:+).uniq
    args.each do |key, value|
      @unicollection.add_meta "data-#{key}".to_sym, value
    end
  end

  self.actions if opts[:actions]
  @unicollection.add_meta :html_header, opts[:html]
  @unicollection.add_meta :block_header, block if block_given?
end


60
61
62
63
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 60

def footer content, opts = {}
  @unicollection.add_meta :footer, [*content]
  @unicollection.add_meta :html_footer, opts[:html]
end

#header(args, opts = {}, &block) ⇒ Object



15
16
17
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 15

def header args, opts = {}, &block
  self.data args, opts, &block
end

#parseObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 34

def parse
  unless @unicollection.has_meta? :data
    @unicollection.add_meta :data, @collection.first.try(:attribute_names).try(:compact) || []
    @unicollection.add_meta :actions, true
  end

  if @unicollection.rows.empty?
    @collection.each do |model|
      @unicollection.meta[:model_class] ||= model.class.to_s

      @unicollection.row do |r|
        @unicollection.meta[:data].each do |name|
          hash = hash_for_name model, name
          hash[:link] = link model if name == :name
          r.add_elem UniData::Element.new(hash)
        end
        r.add_elem_meta :actions, populate_actions(model, r, @options, [:show, :edit, :destroy])
        r.add_elem_meta :id, model.id
      end
    end
  end

  @unicollection.add_meta :header, @unicollection.meta[:data]
  @unicollection.try_add_meta :footer, ["Total: #{@unicollection.rows.count}"]
end

#populate_actions(model, row, options, buttons) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 65

def populate_actions model, row, options, buttons
  actions = {}

  buttons.each do |button|
    actions[:show] = polymorphic_path([*options[:parents], model]) rescue nil if button == :show
    actions[:edit] = (actions[:show] ? "#{actions[:show]}/edit" : edit_polymorphic_path([*options[:parents], model])) rescue nil if button == :edit
    actions[:destroy] = actions[:show] || polymorphic_path([*options[:parents], model]) rescue nil if button == :destroy
    actions[button.keys.first] = button.values.first if button.is_a? Hash
  end

  actions.delete_if { |_, v| v.nil? }
end

#row(opts) ⇒ Object



83
84
85
# File 'lib/spyro/collections/parsers/active_record_relation.rb', line 83

def row opts
  @r.add_elem_meta :opts, opts
end