Class: DisplayFor::Builder::Csv

Inherits:
CollectionBase show all
Defined in:
lib/display_for/builder/csv.rb

Instance Attribute Summary

Attributes inherited from CollectionBase

#collection

Attributes inherited from Base

#html_options, #namespace, #resource_class, #template

Instance Method Summary collapse

Methods inherited from CollectionBase

#default_actions, #initialize

Methods inherited from Base

#action, #attribute, #html, #initialize

Constructor Details

This class inherits a constructor from DisplayFor::Builder::CollectionBase

Instance Method Details

#build_actions(resource) ⇒ Object



13
14
15
# File 'lib/display_for/builder/csv.rb', line 13

def build_actions(resource)
  nil
end

#build_headerObject



5
6
7
# File 'lib/display_for/builder/csv.rb', line 5

def build_header
  @attributes.map { |attribute| quote(attribute.label(@resource_class)) }.join(',').html_safe
end

#build_row(resource) ⇒ Object



9
10
11
# File 'lib/display_for/builder/csv.rb', line 9

def build_row(resource)
  @attributes.map { |attribute| quote(attribute.content(resource)) }.join(',').html_safe
end

#quote(string) ⇒ Object



17
18
19
# File 'lib/display_for/builder/csv.rb', line 17

def quote(string)
  "\"#{string.gsub('"', '""')}\"".html_safe
end

#to_sObject



21
22
23
24
25
26
27
28
29
# File 'lib/display_for/builder/csv.rb', line 21

def to_s
  result = [build_header]
  
  @collection.each do |resource|
    result << build_row(resource)
  end

  result.join("\n").html_safe
end