Class: ActiveList::Exporters::AbstractExporter
- Inherits:
-
Object
- Object
- ActiveList::Exporters::AbstractExporter
show all
- Defined in:
- lib/active_list/exporters/abstract_exporter.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of AbstractExporter.
6
7
8
9
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 6
def initialize(generator)
@generator = generator
@table = generator.table
end
|
Instance Attribute Details
#generator ⇒ Object
Returns the value of attribute generator.
4
5
6
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 4
def generator
@generator
end
|
#table ⇒ Object
Returns the value of attribute table.
4
5
6
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 4
def table
@table
end
|
Instance Method Details
#columns_headers(options = {}) ⇒ Object
50
51
52
53
54
55
56
57
58
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 50
def columns_headers(options = {})
= []
columns = table.exportable_columns
for column in columns
datum = column.
<< (options[:encoding] ? datum + ".to_s.encode('#{options[:encoding]}', invalid: :replace, undef: :replace)" : datum)
end
end
|
#columns_to_array(nature, options = {}) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 60
def columns_to_array(nature, options = {})
columns = table.exportable_columns
array = []
record = options[:record] || 'record_of_the_death'
for column in columns
next unless column.is_a?(ActiveList::Definition::AbstractColumn)
datum = if nature == :header
column.
else
column.exporting_datum_code(record)
end
array << (options[:encoding] ? datum + ".to_s.encode('#{options[:encoding]}', invalid: :replace, undef: :replace)" : datum)
end
array
end
|
#columns_to_hash ⇒ Object
77
78
79
80
81
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 77
def columns_to_hash
table.exportable_columns.map do |column|
[column., column.exporting_datum_code('record').to_s]
end.to_h
end
|
#file_extension ⇒ Object
11
12
13
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 11
def file_extension
'txt'
end
|
#file_name_code ⇒ Object
15
16
17
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 15
def file_name_code
"file_name = #{table.model.name}.model_name.human\n".c
end
|
#generate_data_code ⇒ Object
46
47
48
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 46
def generate_data_code
raise NotImplementedError.new("#{self.class.name}#generate_data_code must be implemented in sub-classes.")
end
|
#generate_file_code(format) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 23
def generate_file_code(format)
code = file_name_code
if generator.export_class
code << generator.exportable_query_code
code << "#{generator.export_class}.perform_later(user: current_user,\n"
code << " query: query,\n"
code << " content: #{columns_to_hash},\n"
code << " file_name: file_name,\n"
code << " format: '#{format}',\n"
code << " file_extension: '#{file_extension}')\n"
code << "notify_success(:document_in_preparation)\n"
code << "redirect_to(:back)\n"
else
code << generate_data_code
code << send_data_code
end
code.c
end
|
#mime_type ⇒ Object
19
20
21
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 19
def mime_type
Mime::TEXT
end
|
#send_data_code ⇒ Object
42
43
44
|
# File 'lib/active_list/exporters/abstract_exporter.rb', line 42
def send_data_code
raise NotImplementedError.new("#{self.class.name}#send_data_code must be implemented in sub-classes.")
end
|