Class: Basepack::Forms::Export

Inherits:
Base
  • Object
show all
Defined in:
lib/basepack/forms/export.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#association_chain, #factory, #groups, #nested_in, #partial, #resource, #resource_class, #view

Instance Method Summary collapse

Methods inherited from Base

#build_from_factory, #chain, #chain_with_class, #configure, #content_for_field, #default_group, #field, #field_names, #fields, #fields_hash, #group, #has_field?, #hide_field, #hide_fields, #inspect, #inverse_of_nested_in?, #label, #label_plural, #new_form, #new_record?, #permit_params, #render_field, #render_field!, #sanitize_params, #show_fields, #show_only_fields, #translate, #visible_field, #visible_fields, #visible_groups, #with_resource

Constructor Details

#initialize(factory, chain, options = {}) ⇒ Export

Returns a new instance of Export.



6
7
8
9
# File 'lib/basepack/forms/export.rb', line 6

def initialize(factory, chain, options = {})
  super(factory, chain, options)
  @query_form = options[:query_form]
end

Instance Attribute Details

#query_formObject (readonly)

Returns the value of attribute query_form.



4
5
6
# File 'lib/basepack/forms/export.rb', line 4

def query_form
  @query_form
end

Instance Method Details

#collectionObject



24
25
26
# File 'lib/basepack/forms/export.rb', line 24

def collection
  @query_form ? @query_form.collection : []
end

#csv_header(fields) ⇒ Object



69
70
71
# File 'lib/basepack/forms/export.rb', line 69

def csv_header(fields)
  fields.flatten.map {|field| field.nested_label}
end

#csv_row_for_resource(resource, fields) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/basepack/forms/export.rb', line 73

def csv_row_for_resource(resource, fields)
  with_resource(resource) do
    row = []
    fields.each do |nfields|
      if nfields.is_a? Array
        # nested form
        nform = nfields.first.form
        f = nform.nested_in
        values = f.multiple? ? f.value : [f.value]
        values = values.map {|v| nform.csv_row_for_resource(v, nfields)}
        nfields.zip(*values) {|nf, *v| row << v.join(',') }
      else
        f = nfields
        if f.polymorphic?
          row << resource.try(f.method_name) << resource.try(f.association[:foreign_type])
        else
          row << f.export_value
        end
      end
    end
    row
  end
end

#default_partialObject



20
21
22
# File 'lib/basepack/forms/export.rb', line 20

def default_partial
  'forms/export'
end

#fields_from_params(params = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/basepack/forms/export.rb', line 53

def fields_from_params(params = nil)
  if params.present?
    fields = []
    Array.wrap(params).each do |name|
      if name.is_a? Hash
        name.each {|n, v| fields << fields_for_field(visible_field(n.to_sym), v) }
      else
        fields << fields_for_field(visible_field(name.to_sym))
      end
    end
    fields
  else
    visible_fields.map {|f| fields_for_field(f)}
  end
end

#pathObject



16
17
18
# File 'lib/basepack/forms/export.rb', line 16

def path
  view.polymorphic_path([:export, association_chain, resource_class].flatten)
end

#schema_from_fields(fields) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/basepack/forms/export.rb', line 32

def schema_from_fields(fields)
  schema = { only: [], methods: [], include: {} }

  fields.each do |nfields|
    if nfields.is_a? Array
      # nested form
      f = nfields.first.form.nested_in
      schema[:include][f.name] = schema_from_fields(nfields)
    else
      f = nfields
      if f.polymorphic?
        schema[:methods] << f.method_name << f.association.foreign_type
      else
        schema[:only] << f.name
      end
    end
  end

  schema
end

#schema_from_params(params = nil) ⇒ Object



28
29
30
# File 'lib/basepack/forms/export.rb', line 28

def schema_from_params(params = nil)
  schema_from_fields(fields_from_params(params))
end

#view=(view) ⇒ Object



11
12
13
14
# File 'lib/basepack/forms/export.rb', line 11

def view=(view)
  super
  @query_form.view = view if @query_form
end