Class: ActiveRecord::Bixformer::Model::Csv::Base
- Inherits:
-
Base
- Object
- Base
- ActiveRecord::Bixformer::Model::Csv::Base
show all
- Defined in:
- lib/activerecord-bixformer/model/csv/base.rb
Instance Attribute Summary
Attributes inherited from Base
#associations, #attributes, #name, #options, #parent, #preferred_skip_attributes, #translator
Instance Method Summary
collapse
Methods inherited from Base
#activerecord_constant, #add_association, #find_record_by!, #initialize, #parent_foreign_key, #parents, #plan, #set_parent, #setup, #should_be_included
#presence_value?
Instance Method Details
#csv_title(attribute_name) ⇒ Object
Also known as:
translate
61
62
63
|
# File 'lib/activerecord-bixformer/model/csv/base.rb', line 61
def csv_title(attribute_name)
@translator.translate_attribute(attribute_name)
end
|
#csv_titles ⇒ Object
54
55
56
57
58
59
|
# File 'lib/activerecord-bixformer/model/csv/base.rb', line 54
def csv_titles
[
*@attributes.map { |attr| csv_title(attr.name) },
*@associations.flat_map(&:csv_titles)
]
end
|
#export(record_or_relation) ⇒ Object
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/activerecord-bixformer/model/csv/base.rb', line 6
def export(record_or_relation)
run_bixformer_callback :export do
values = run_bixformer_callback :export, type: :attribute do
@attributes.map do |attr|
attribute_value = if record_or_relation
run_bixformer_callback :export, on: attr.name do
attr.export(record_or_relation)
end
end
[csv_title(attr.name), attribute_value]
end.to_h.with_indifferent_access
end
run_bixformer_callback :export, type: :association do
@associations.inject(values) do |each_values, association|
association_value = if record_or_relation
run_bixformer_callback :export, on: association.name do
record_or_relation.__send__(association.name)
end
end
each_values.merge(association.export(association_value))
end
end
end
end
|
#import(csv_body_row, parent_record_id = nil) ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/activerecord-bixformer/model/csv/base.rb', line 35
def import(csv_body_row, parent_record_id = nil)
run_bixformer_callback :import do
values = make_each_attribute_import_value(parent_record_id) do |attr|
csv_value = csv_body_row[csv_title(attr.name)]
attr.import(csv_value)
end
make_each_association_import_value(values) do |association, self_record_id|
association.import(csv_body_row, self_record_id)
end
end
end
|
#verify_csv_titles(csv_title_row) ⇒ Object
49
50
51
52
|
# File 'lib/activerecord-bixformer/model/csv/base.rb', line 49
def verify_csv_titles(csv_title_row)
@attributes.map { |attr| csv_title(attr.name) }.all? { |title| csv_title_row.include?(title) } &&
@associations.all? { |ass| ass.verify_csv_titles(csv_title_row) }
end
|