activerecord-bixformer 0.3.12 → 0.3.13
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 +4 -4
- data/lib/activerecord-bixformer/attribute/base.rb +2 -0
- data/lib/activerecord-bixformer/attribute/boolean.rb +4 -0
- data/lib/activerecord-bixformer/attribute/booletania.rb +7 -2
- data/lib/activerecord-bixformer/attribute/date.rb +11 -11
- data/lib/activerecord-bixformer/attribute/enumerize.rb +1 -1
- data/lib/activerecord-bixformer/attribute/time.rb +11 -11
- data/lib/activerecord-bixformer/autoload.rb +2 -0
- data/lib/activerecord-bixformer/error.rb +58 -0
- data/lib/activerecord-bixformer/model/base.rb +4 -0
- data/lib/activerecord-bixformer/model/csv/base.rb +2 -0
- data/lib/activerecord-bixformer/model/csv/indexed.rb +1 -0
- data/lib/activerecord-bixformer/model/csv/mapped.rb +2 -1
- data/lib/activerecord-bixformer/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 564f32bc689495aaa583d76e3b886d169836fbc6
|
4
|
+
data.tar.gz: 788af0b283a54778d15f9b08b0bf998144262740
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61dea4b45228be03b65657407baa3a5e8850c9d33138dd055983a059ffdf80f5b565e6edae0c2dff3bd72c33b1838a144e8527302b9139ab4889fb6b823ae075
|
7
|
+
data.tar.gz: 6283c88b7103966dc1142dc69e915ca81a3e6089a6d2edea672f8e19aef3d754bed8283a8e883637bd864f2f7402bfd4e604a88bc24d4ca98d6cbe2035db6172
|
@@ -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
|
14
|
+
return nil if value.blank?
|
15
15
|
|
16
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
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)
|
@@ -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
|
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
|
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.
|
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
|
+
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
|