Class: ActiveRecord::Bixformer::Model::Csv::Indexed

Inherits:
Base
  • Object
show all
Defined in:
lib/activerecord-bixformer/model/csv/indexed.rb

Instance Attribute Summary

Attributes inherited from Base

#associations, #attributes, #errors, #name, #options, #parent, #preferred_skip_attributes, #translator

Instance Method Summary collapse

Methods inherited from Base

#csv_titles

Methods inherited from Base

#activerecord_constant, #add_association, #find_record_by!, #parent_foreign_key, #parents, #plan, #set_parent, #setup, #should_be_included, #translate

Methods included from ImportValueValidatable

#presence_value?

Constructor Details

#initialize(model_or_association_name, options) ⇒ Indexed

Returns a new instance of Indexed.



6
7
8
9
10
# File 'lib/activerecord-bixformer/model/csv/indexed.rb', line 6

def initialize(model_or_association_name, options)
  super

  @options[:size] ||= 1
end

Instance Method Details

#csv_title(attribute_name) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/activerecord-bixformer/model/csv/indexed.rb', line 47

def csv_title(attribute_name)
  # TODO: indexed 以外の複数を扱うクラスがあった時の対処が必要
  if parents.find { |parent| parent.is_a?(::ActiveRecord::Bixformer::Model::Csv::Indexed) }
    parents.map { |parent| parent.translator.translate_model }.join + super
  else
    super
  end
end

#export(record_or_relation) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/activerecord-bixformer/model/csv/indexed.rb', line 12

def export(record_or_relation)
  record_or_relation ||= []

  errors.clear

  run_bixformer_callback :export do
    # has_many でしか使わない想定なので record_or_relation は ActiveRecord::Relation のはず
    (1..options[:size]).inject({}) do |values, index|
      update_translator(index)

      values.merge(do_export(record_or_relation[index-1]))
    end
  end
end

#import(csv_body_row, parent_record_id = nil) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/activerecord-bixformer/model/csv/indexed.rb', line 27

def import(csv_body_row, parent_record_id = nil)
  errors.clear

  run_bixformer_callback :import do
    # has_many でしか使わない想定なので ActiveRecord::Relation を返却
    (1..options[:size]).map do |index|
      update_translator(index)

      do_import(csv_body_row, parent_record_id)
    end
  end
end

#verify_csv_titles(csv_title_row) ⇒ Object



40
41
42
43
44
45
# File 'lib/activerecord-bixformer/model/csv/indexed.rb', line 40

def verify_csv_titles(csv_title_row)
  # size は可変長なので、'1'だけ検証する
  update_translator(1)

  super
end