activerecord-bixformer 0.3.13 → 0.3.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 564f32bc689495aaa583d76e3b886d169836fbc6
4
- data.tar.gz: 788af0b283a54778d15f9b08b0bf998144262740
3
+ metadata.gz: a7898f4b6bcb4f7f67dee3086bc210a9b5d9d0ae
4
+ data.tar.gz: 6b1bb29aa4259ceadc55a3f28ec5b1516cfbab01
5
5
  SHA512:
6
- metadata.gz: 61dea4b45228be03b65657407baa3a5e8850c9d33138dd055983a059ffdf80f5b565e6edae0c2dff3bd72c33b1838a144e8527302b9139ab4889fb6b823ae075
7
- data.tar.gz: 6283c88b7103966dc1142dc69e915ca81a3e6089a6d2edea672f8e19aef3d754bed8283a8e883637bd864f2f7402bfd4e604a88bc24d4ca98d6cbe2035db6172
6
+ metadata.gz: edd9cb8ef73e6376605114a14d8af10b09dd1360f7cfe10341ae7e892e88a95d62a222b1f794952973dfa71fd45ef283d77a1a42f8bdd33f7e89a34d9f2c7d32
7
+ data.tar.gz: d370ebca7f66c3d58781e5d5a735822b4f0a311bbe6897e7ae558e444bd9bf386422bbf7984f0c596ae09731b25fc2810533bdfcf033ccbc9ce15b2c39478617
@@ -6,7 +6,9 @@ module ActiveRecord
6
6
  autoload :Plan, 'activerecord-bixformer/plan'
7
7
  autoload :PlanAccessor, 'activerecord-bixformer/plan_accessor'
8
8
  autoload :ModelCallback, 'activerecord-bixformer/model_callback'
9
+ autoload :Errors, 'activerecord-bixformer/error'
9
10
  autoload :ImportError, 'activerecord-bixformer/error'
11
+ autoload :AttributeError, 'activerecord-bixformer/error'
10
12
  autoload :DataInvalid, 'activerecord-bixformer/error'
11
13
 
12
14
  module Attribute
@@ -1,14 +1,40 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
+ class Errors < Array
4
+ def messages
5
+ map(&:message)
6
+ end
7
+
8
+ def full_messages
9
+ map(&:full_message)
10
+ end
11
+ end
12
+
3
13
  class ImportError < ::StandardError
14
+ attr_reader :model
15
+
16
+ def initialize(model)
17
+ @model = model
18
+
19
+ super("failed to import #{model.name}")
20
+ end
21
+ end
22
+
23
+ class AttributeError < ::StandardError
4
24
  def initialize(attribute, value, type)
25
+ @attribute = attribute
26
+
27
+ super(generate_message(attribute, type, value))
28
+ end
29
+
30
+ def full_message
5
31
  options = {
6
32
  default: "%{attribute} %{message}",
7
- attribute: attribute.model.translate(attribute.name),
8
- message: generate_message(attribute, type, value)
33
+ attribute: @attribute.model.translate(@attribute.name),
34
+ message: message
9
35
  }
10
36
 
11
- super(I18n.t(:"errors.format", options))
37
+ I18n.t(:"errors.format", options)
12
38
  end
13
39
 
14
40
  private
@@ -49,7 +75,7 @@ module ActiveRecord
49
75
  end
50
76
  end
51
77
 
52
- class DataInvalid < ImportError
78
+ class DataInvalid < AttributeError
53
79
  def initialize(attribute, value)
54
80
  super(attribute, value, :invalid)
55
81
  end
@@ -11,7 +11,13 @@ module ActiveRecord
11
11
  end
12
12
 
13
13
  def assignable_attributes(csv_body_row)
14
- compile.import(csv_body_row)
14
+ model = compile
15
+
16
+ result = model.import(csv_body_row)
17
+
18
+ raise ::ActiveRecord::Bixformer::ImportError.new(model) if model.errors.present?
19
+
20
+ result
15
21
  end
16
22
  end
17
23
  end
@@ -17,12 +17,13 @@ module ActiveRecord
17
17
  include ::ActiveRecord::Bixformer::ModelCallback
18
18
 
19
19
  attr_reader :name, :options, :parent, :attributes, :associations,
20
- :preferred_skip_attributes, :translator
20
+ :preferred_skip_attributes, :translator, :errors
21
21
 
22
22
  def initialize(model_or_association_name, options)
23
23
  @name = model_or_association_name.to_s
24
24
  @options = (options.is_a?(::Hash) ? options : {}).with_indifferent_access
25
25
  @associations = []
26
+ @errors = ::ActiveRecord::Bixformer::Errors.new
26
27
  end
27
28
 
28
29
  def setup(plan)
@@ -122,9 +123,15 @@ module ActiveRecord
122
123
 
123
124
  run_bixformer_callback :import, type: :attribute do
124
125
  @attributes.each do |attr|
125
- attribute_value = run_bixformer_callback :import, on: attr.name do
126
- block.call(attr)
127
- end
126
+ attribute_value = begin
127
+ run_bixformer_callback :import, on: attr.name do
128
+ block.call(attr)
129
+ end
130
+ rescue ::ActiveRecord::Bixformer::AttributeError => e
131
+ @errors << e
132
+
133
+ next
134
+ end
128
135
 
129
136
  # 取り込み時は、 preferred_skip な属性では、有効でない値は取り込まない
130
137
  next if ! presence_value?(attribute_value) &&
@@ -165,6 +172,12 @@ module ActiveRecord
165
172
  block.call(association, self_record_id)
166
173
  end
167
174
 
175
+ if association.errors.present?
176
+ @errors += association.errors
177
+
178
+ next
179
+ end
180
+
168
181
  if association_value.is_a?(::Array)
169
182
  # has_many な場合は、配列が返ってくるが、空と思われる要素は結果に含めない
170
183
  association_value = association_value.reject { |v| ! presence_value?(v) }
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
- VERSION = "0.3.13"
3
+ VERSION = "0.3.14"
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.13
4
+ version: 0.3.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroaki Otsu