activerecord-bixformer 0.3.12 → 0.3.13

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: b3667d87da3e07b91f9b2503f6a50a68f2df1ce8
4
- data.tar.gz: 2843f952ab85cc1c703453abd410474cb5a1cbbe
3
+ metadata.gz: 564f32bc689495aaa583d76e3b886d169836fbc6
4
+ data.tar.gz: 788af0b283a54778d15f9b08b0bf998144262740
5
5
  SHA512:
6
- metadata.gz: dbd484c941c19e05e1468b523d8dc1fb840140bb2bbc4a1e024a911c558de59c496a889f97054be043f6381da00c0407d8696fb0f282984bc5dd7084c6620f85
7
- data.tar.gz: 336495e16118c229f7188e8b92c8117a3e4b1d09d448e925c2efa84be468289c175f307917589a4cf2a412811f64480d7677bf446dff2950238d9ca794de3152
6
+ metadata.gz: 61dea4b45228be03b65657407baa3a5e8850c9d33138dd055983a059ffdf80f5b565e6edae0c2dff3bd72c33b1838a144e8527302b9139ab4889fb6b823ae075
7
+ data.tar.gz: 6283c88b7103966dc1142dc69e915ca81a3e6089a6d2edea672f8e19aef3d754bed8283a8e883637bd864f2f7402bfd4e604a88bc24d4ca98d6cbe2035db6172
@@ -8,6 +8,8 @@ module ActiveRecord
8
8
  @model = model
9
9
  @name = attribute_name.to_s
10
10
  @options = (options.is_a?(::Hash) ? options : {}).with_indifferent_access
11
+
12
+ @options[:raise] = true unless @options.key?(:raise)
11
13
  end
12
14
 
13
15
  def export(record)
@@ -10,6 +10,8 @@ module ActiveRecord
10
10
  end
11
11
 
12
12
  def import(value)
13
+ return nil if value.blank?
14
+
13
15
  true_value = (@options.is_a?(::Hash) && @options[:true]) || 'true'
14
16
  false_value = (@options.is_a?(::Hash) && @options[:false]) || 'false'
15
17
 
@@ -18,6 +20,8 @@ module ActiveRecord
18
20
  true
19
21
  when false_value
20
22
  false
23
+ else
24
+ raise ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
21
25
  end
22
26
  end
23
27
  end
@@ -11,9 +11,14 @@ module ActiveRecord
11
11
  end
12
12
 
13
13
  def import(value)
14
- return nil unless value
14
+ return nil if value.blank?
15
15
 
16
- @model.activerecord_constant.__send__("#{@name}_options").to_h[value.strip]
16
+ value = value.strip
17
+ boolean_of = @model.activerecord_constant.__send__("#{@name}_options").to_h
18
+
19
+ return boolean_of[value] if boolean_of.key?(value)
20
+
21
+ raise ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
17
22
  end
18
23
  end
19
24
  end
@@ -13,17 +13,17 @@ module ActiveRecord
13
13
  def import(value)
14
14
  return nil if value.blank?
15
15
 
16
- begin
17
- ::Date.parse(value)
18
- rescue => e
19
- format_string = ::Date::DATE_FORMATS[option_format]
20
-
21
- if format_string
22
- ::Date.strptime(value, format_string)
23
- else
24
- raise e
25
- end
26
- end
16
+ result = begin
17
+ ::Date.parse(value)
18
+ rescue
19
+ format_string = ::Date::DATE_FORMATS[option_format]
20
+
21
+ ::Date.strptime(value, format_string) rescue nil if format_string
22
+ end
23
+
24
+ return result if result
25
+
26
+ raise ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
27
27
  end
28
28
 
29
29
  private
@@ -14,7 +14,7 @@ module ActiveRecord
14
14
  return nil if value.blank?
15
15
 
16
16
  @model.activerecord_constant.__send__(@name).options.to_h[value.strip] or
17
- raise ArgumentError.new "Not acceptable enumerize value : #{value}"
17
+ raise ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
18
18
  end
19
19
  end
20
20
  end
@@ -13,17 +13,17 @@ module ActiveRecord
13
13
  def import(value)
14
14
  return nil if value.blank?
15
15
 
16
- begin
17
- ::Time.parse(value)
18
- rescue => e
19
- format_string = ::Time::DATE_FORMATS[option_format]
20
-
21
- if format_string
22
- ::Time.strptime(value, format_string)
23
- else
24
- raise e
25
- end
26
- end
16
+ result = begin
17
+ ::Time.parse(value)
18
+ rescue
19
+ format_string = ::Time::DATE_FORMATS[option_format]
20
+
21
+ ::Time.strptime(value, format_string) rescue nil if format_string
22
+ end
23
+
24
+ return result if result
25
+
26
+ raise ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
27
27
  end
28
28
 
29
29
  private
@@ -6,6 +6,8 @@ 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 :ImportError, 'activerecord-bixformer/error'
10
+ autoload :DataInvalid, 'activerecord-bixformer/error'
9
11
 
10
12
  module Attribute
11
13
  autoload :Base, 'activerecord-bixformer/attribute/base'
@@ -0,0 +1,58 @@
1
+ module ActiveRecord
2
+ module Bixformer
3
+ class ImportError < ::StandardError
4
+ def initialize(attribute, value, type)
5
+ options = {
6
+ default: "%{attribute} %{message}",
7
+ attribute: attribute.model.translate(attribute.name),
8
+ message: generate_message(attribute, type, value)
9
+ }
10
+
11
+ super(I18n.t(:"errors.format", options))
12
+ end
13
+
14
+ private
15
+
16
+ # implemented with referencing to https://github.com/rails/rails/blob/517cf249c369d4bca40b1f590ca641d8b717985e/activemodel/lib/active_model/errors.rb#L462
17
+ def generate_message(attribute, type, value)
18
+ model_klass = attribute.model.activerecord_constant
19
+ model_scope = model_klass.i18n_scope
20
+
21
+ defaults = if model_scope
22
+ model_klass.lookup_ancestors.map do |klass|
23
+ [
24
+ :"#{model_scope}.errors.models.#{klass.model_name.i18n_key}.attributes.#{attribute.name}.#{type}",
25
+ :"#{model_scope}.errors.models.#{klass.model_name.i18n_key}.#{type}"
26
+ ]
27
+ end
28
+ else
29
+ []
30
+ end
31
+
32
+ defaults << :"#{model_scope}.errors.messages.#{type}" if model_scope
33
+ defaults << :"errors.attributes.#{attribute.name}.#{type}"
34
+ defaults << :"errors.messages.#{type}"
35
+
36
+ defaults.compact!
37
+ defaults.flatten!
38
+
39
+ key = defaults.shift
40
+
41
+ options = {
42
+ default: defaults,
43
+ model: model_klass.model_name.human,
44
+ attribute: model_klass.human_attribute_name(attribute.name),
45
+ value: value
46
+ }
47
+
48
+ I18n.translate(key, options)
49
+ end
50
+ end
51
+
52
+ class DataInvalid < ImportError
53
+ def initialize(attribute, value)
54
+ super(attribute, value, :invalid)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -110,6 +110,10 @@ module ActiveRecord
110
110
  activerecord_constant.find_by!(condition)
111
111
  end
112
112
 
113
+ def translate(attribute_name)
114
+ @translator.translate_attribute(attribute_name)
115
+ end
116
+
113
117
  private
114
118
 
115
119
  def make_each_attribute_import_value(parent_record_id = nil, &block)
@@ -61,6 +61,8 @@ module ActiveRecord
61
61
  def csv_title(attribute_name)
62
62
  @translator.translate_attribute(attribute_name)
63
63
  end
64
+
65
+ alias_method :translate, :csv_title
64
66
  end
65
67
  end
66
68
  end
@@ -45,6 +45,7 @@ module ActiveRecord
45
45
  end
46
46
 
47
47
  def csv_title(attribute_name)
48
+ # TODO: indexed 以外の複数を扱うクラスがあった時の対処が必要
48
49
  if parents.find { |parent| parent.is_a?(::ActiveRecord::Bixformer::Model::Csv::Indexed) }
49
50
  parents.map { |parent| parent.translator.translate_model }.join + super
50
51
  else
@@ -46,7 +46,8 @@ module ActiveRecord
46
46
  end
47
47
  end
48
48
 
49
- def csv_title(attribute_name)
49
+ def translate(attribute_name)
50
+ # TODO: mapped 以外の複数を扱うクラスがあった時の対処が必要
50
51
  if parents.find { |parent| parent.is_a?(::ActiveRecord::Bixformer::Model::Csv::Mapped) }
51
52
  parents.map { |parent| parent.translator.translate_model }.join + super
52
53
  else
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
- VERSION = "0.3.12"
3
+ VERSION = "0.3.13"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-bixformer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.12
4
+ version: 0.3.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroaki Otsu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-11 00:00:00.000000000 Z
11
+ date: 2016-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -234,6 +234,7 @@ files:
234
234
  - lib/activerecord-bixformer/attribute/time.rb
235
235
  - lib/activerecord-bixformer/autoload.rb
236
236
  - lib/activerecord-bixformer/compiler.rb
237
+ - lib/activerecord-bixformer/error.rb
237
238
  - lib/activerecord-bixformer/format/csv.rb
238
239
  - lib/activerecord-bixformer/from/csv.rb
239
240
  - lib/activerecord-bixformer/import_value_validatable.rb