activerecord-bixformer 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4b1922e288ee7d68cfe00b2bff27d6d4d348eb9e
4
- data.tar.gz: 6e1b63257628fdd39a0f723584f3ed6df7b52ce9
3
+ metadata.gz: e07760f320a0cfdfc606124616f62f5085cd9a66
4
+ data.tar.gz: 976bbd889da827467283740dead4eeb6bed7b9fb
5
5
  SHA512:
6
- metadata.gz: f8173268968241277e1926bb7b6922d2e52c4ea1778e3041b5f6ebb62f2f9ae0dd4384f18042a325b403c1e8cbb6beebba04549266adfcece4864a32f407e85b
7
- data.tar.gz: be05e229c2c15c1e85ac6aab75cdbee77b033cf3027edab750848a6156c919ecb0597a9a81b7b6c6eb155c2dbe37c0d64573e121d2852b84d44590caeb79bbba
6
+ metadata.gz: 7935499df28d4d7beb4d1d8a0b4bc97ef8b6f91244f9f8ebda7862c9fedfe83213e35d850c92d9ead5d5e574e00c97774a5acd9beef9a6c2032d0e459607a51a
7
+ data.tar.gz: 43ab80022d46e4e9f85a8bb5076daf935901435893b1d66af27380456061e187af2218f720510e86d77fe93fc0ee3901e10e915f39a83d7f2b70a854f849fdd1
@@ -3,14 +3,14 @@ module ActiveRecord
3
3
  module Model
4
4
  module Csv
5
5
  class Base < ::ActiveRecord::Bixformer::Model::Base
6
- def export(record_or_records)
6
+ def export(record_or_relation)
7
7
  run_bixformer_callback :export do
8
8
  values = run_bixformer_callback :export, type: :attribute do
9
- # has_one でしか使わない想定なので record_or_records は ActiveRecord::Base のはず
9
+ # has_one でしか使わない想定なので record_or_relation は ActiveRecord::Base のはず
10
10
  @attributes.map do |attr|
11
- attribute_value = if record_or_records
11
+ attribute_value = if record_or_relation
12
12
  run_bixformer_callback :export, on: attr.name do
13
- attr.export(record_or_records)
13
+ attr.export(record_or_relation)
14
14
  end
15
15
  end
16
16
 
@@ -20,14 +20,12 @@ module ActiveRecord
20
20
 
21
21
  run_bixformer_callback :export, type: :association do
22
22
  @associations.inject(values) do |each_values, association|
23
- association_value = if record_or_records
23
+ association_value = if record_or_relation
24
24
  run_bixformer_callback :export, on: association.name do
25
- record_or_records.__send__(association.name)
25
+ record_or_relation.__send__(association.name)
26
26
  end
27
27
  end
28
28
 
29
- association_value = association_value.to_a if association_value.is_a?(::ActiveRecord::Relation)
30
-
31
29
  each_values.merge(association.export(association_value))
32
30
  end
33
31
  end
@@ -9,19 +9,19 @@ module ActiveRecord
9
9
  @options[:size] ||= 1
10
10
  end
11
11
 
12
- def export(record_or_records)
13
- record_or_records ||= []
12
+ def export(record_or_relation)
13
+ record_or_relation ||= []
14
14
 
15
- # has_many でしか使わない想定なので record_or_recordsArray のはず
15
+ # has_many でしか使わない想定なので record_or_relationActiveRecord::Relation のはず
16
16
  (1..options[:size]).inject({}) do |values, index|
17
17
  update_translator(index)
18
18
 
19
- values.merge(super(record_or_records[index-1]))
19
+ values.merge(super(record_or_relation[index-1]))
20
20
  end
21
21
  end
22
22
 
23
23
  def import(csv_body_row, parent_record_id = nil)
24
- # has_many でしか使わない想定なので Array を返却
24
+ # has_many でしか使わない想定なので ActiveRecord::Relation を返却
25
25
  (1..options[:size]).map do |index|
26
26
  update_translator(index)
27
27
 
@@ -0,0 +1,72 @@
1
+ module ActiveRecord
2
+ module Bixformer
3
+ module Model
4
+ module Csv
5
+ class Mapped < ::ActiveRecord::Bixformer::Model::Csv::Base
6
+ def initialize(model_or_association_name, options)
7
+ super
8
+
9
+ unless options[:key] || options[:in]
10
+ raise ArgumentError.new 'Not configure required options : key, in'
11
+ end
12
+ end
13
+
14
+ def export(record_or_relation)
15
+ # has_many でしか使わない想定なので record_or_relation は ActiveRecord::Relation のはず
16
+ record_of = record_or_relation&.where(@options[:key] => @options[:in])&.index_by(@options[:key]) || {}
17
+
18
+ @options[:in].inject({}) do |values, key|
19
+ update_translator(key)
20
+
21
+ values.merge(super(record_of[key]))
22
+ end
23
+ end
24
+
25
+ def import(csv_body_row, parent_record_id = nil)
26
+ @options[:in].map do |key|
27
+ update_translator(key)
28
+
29
+ super
30
+ end
31
+ end
32
+
33
+ def verify_csv_titles(csv_title_row)
34
+ @options[:in].map do |key|
35
+ update_translator(key)
36
+
37
+ return false unless super
38
+ end
39
+ end
40
+
41
+ def csv_titles
42
+ @options[:in].flat_map do |key|
43
+ update_translator(key)
44
+
45
+ super
46
+ end
47
+ end
48
+
49
+ def csv_title(attribute_name)
50
+ if parents.find { |parent| parent.is_a?(::ActiveRecord::Bixformer::Model::Csv::Mapped) }
51
+ parents.map { |parent| parent.translator.translate_model }.join + super
52
+ else
53
+ super
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def update_translator(key)
60
+ key = @options[:translate] ? @options[:translate].call(key) : key
61
+
62
+ @translator.model_arguments = { key: key }
63
+
64
+ @translator.attribute_arguments_map = @attributes.map do |attr|
65
+ [attr.name, { key: key }]
66
+ end.to_h
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
- VERSION = "0.3.2"
3
+ VERSION = "0.3.3"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-bixformer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroaki Otsu
@@ -239,6 +239,7 @@ files:
239
239
  - lib/activerecord-bixformer/model/base.rb
240
240
  - lib/activerecord-bixformer/model/csv/base.rb
241
241
  - lib/activerecord-bixformer/model/csv/indexed.rb
242
+ - lib/activerecord-bixformer/model/csv/mapped.rb
242
243
  - lib/activerecord-bixformer/model_callback.rb
243
244
  - lib/activerecord-bixformer/plan.rb
244
245
  - lib/activerecord-bixformer/plan_accessor.rb