activerecord-bixformer 0.3.17 → 0.4.0

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: 7a553dd137e4cb28794ed4ee3dc0d23021c6a6a5
4
- data.tar.gz: 4e95330e0d2e63fd593f0edff3f2308675b5a877
3
+ metadata.gz: b7480345d2a5db1dcd0809914ddaba4bbe4de9cc
4
+ data.tar.gz: eda722ac022f0565ae4b3a84e6ddafc727f049ba
5
5
  SHA512:
6
- metadata.gz: 5267c6cb19fdc3033fa5b1c237cccfa8f54690ae3747cb21552b69b549c1db2b1d4d924f5ae4e1c80ecf7b4aa0363f8fef15a7b6cc682444a796a4a405fa7ba8
7
- data.tar.gz: d03a119e6beac47d8dd0bc54216280bde2820ef8676e2e80edfe040e5bb8b090a5782206e0f880919a695c0549840acd27795388ee9623f864224a3498126815
6
+ metadata.gz: 4c0d417aa86d387084d73e7b0f21a5bb2216419ecccf473624fa1b455495539a37ac3cba99a5c14027eb3640a1f9ed7b552858181f29d80592a61db1932e46f9
7
+ data.tar.gz: dba239a89c70239e002a504fb85042a8c3a92aee9084d57f625f41fa16db2cd9e82fdc3cc71f9d40b341b9b102e822c36e81458fa012a2f34d3ea9398aba186d
@@ -23,7 +23,7 @@ module ActiveRecord
23
23
  private
24
24
 
25
25
  def record_attribute_value(record)
26
- return nil if @name.match(/\A_/)
26
+ return nil if @name.match(/\A_/) && ! record.respond_to?(@name)
27
27
 
28
28
  record.__send__(@name)
29
29
  end
@@ -3,7 +3,7 @@ module ActiveRecord
3
3
  module Attribute
4
4
  class Boolean < ::ActiveRecord::Bixformer::Attribute::Base
5
5
  def export(record)
6
- true_value = (@options.is_a?(::Hash) && @options[:true]) || 'true'
6
+ true_value = (@options.is_a?(::Hash) && @options[:true]) || 'true'
7
7
  false_value = (@options.is_a?(::Hash) && @options[:false]) || 'false'
8
8
 
9
9
  record_attribute_value(record).present? ? true_value : false_value
@@ -12,7 +12,7 @@ module ActiveRecord
12
12
  def import(value)
13
13
  return nil if value.blank?
14
14
 
15
- true_value = (@options.is_a?(::Hash) && @options[:true]) || 'true'
15
+ true_value = (@options.is_a?(::Hash) && @options[:true]) || 'true'
16
16
  false_value = (@options.is_a?(::Hash) && @options[:false]) || 'false'
17
17
 
18
18
  case value
@@ -21,7 +21,7 @@ module ActiveRecord
21
21
  when false_value
22
22
  false
23
23
  else
24
- raise ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
24
+ raise ::ActiveRecord::Bixformer::AttributeError.new(self, value) if @options[:raise]
25
25
  end
26
26
  end
27
27
  end
@@ -18,7 +18,7 @@ module ActiveRecord
18
18
 
19
19
  return boolean_of[value] if boolean_of.key?(value)
20
20
 
21
- raise ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
21
+ raise ::ActiveRecord::Bixformer::AttributeError.new(self, value) if @options[:raise]
22
22
  end
23
23
  end
24
24
  end
@@ -23,7 +23,7 @@ module ActiveRecord
23
23
 
24
24
  return result if result
25
25
 
26
- raise ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
26
+ raise ::ActiveRecord::Bixformer::AttributeError.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 ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
17
+ raise ::ActiveRecord::Bixformer::AttributeError.new(self, value) if @options[:raise]
18
18
  end
19
19
  end
20
20
  end
@@ -0,0 +1,42 @@
1
+ module ActiveRecord
2
+ module Bixformer
3
+ module Attribute
4
+ class Integer < ::ActiveRecord::Bixformer::Attribute::Base
5
+ def import(value)
6
+ return nil if value.blank?
7
+
8
+ unless value.strip.match(/\A[0-9]+\z/)
9
+ if @options[:raise]
10
+ raise ::ActiveRecord::Bixformer::AttributeError.new(self, value, :not_an_integer)
11
+ else
12
+ return nil
13
+ end
14
+ end
15
+
16
+ numeric_value = value.to_i
17
+
18
+ [
19
+ [:greater_than, -> (o, n) { n > o }],
20
+ [:greater_than_or_equal_to, -> (o, n) { n >= o }],
21
+ [:less_than, -> (o, n) { n < o }],
22
+ [:less_than_or_equal_to, -> (o, n) { n <= o }],
23
+ ].each do |restrict_name, validator|
24
+ restrict_value = @options[restrict_name]
25
+
26
+ next unless restrict_value.is_a?(::Integer)
27
+
28
+ next if validator.call(restrict_value, numeric_value)
29
+
30
+ if @options[:raise]
31
+ raise ::ActiveRecord::Bixformer::AttributeError.new(self, value, restrict_name, count: restrict_value)
32
+ else
33
+ return nil
34
+ end
35
+ end
36
+
37
+ numeric_value
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -23,7 +23,7 @@ module ActiveRecord
23
23
 
24
24
  return result if result
25
25
 
26
- raise ::ActiveRecord::Bixformer::DataInvalid.new(self, value) if @options[:raise]
26
+ raise ::ActiveRecord::Bixformer::AttributeError.new(self, value) if @options[:raise]
27
27
  end
28
28
 
29
29
  private
@@ -9,7 +9,6 @@ module ActiveRecord
9
9
  autoload :Errors, 'activerecord-bixformer/error'
10
10
  autoload :ImportError, 'activerecord-bixformer/error'
11
11
  autoload :AttributeError, 'activerecord-bixformer/error'
12
- autoload :DataInvalid, 'activerecord-bixformer/error'
13
12
 
14
13
  module Attribute
15
14
  autoload :Base, 'activerecord-bixformer/attribute/base'
@@ -18,6 +17,7 @@ module ActiveRecord
18
17
  autoload :Date, 'activerecord-bixformer/attribute/date'
19
18
  autoload :Enumerize, 'activerecord-bixformer/attribute/enumerize'
20
19
  autoload :FormattedForeignKey, 'activerecord-bixformer/attribute/formatted_foreign_key'
20
+ autoload :Integer, 'activerecord-bixformer/attribute/integer'
21
21
  autoload :Override, 'activerecord-bixformer/attribute/override'
22
22
  autoload :String, 'activerecord-bixformer/attribute/string'
23
23
  autoload :Time, 'activerecord-bixformer/attribute/time'
@@ -21,10 +21,10 @@ module ActiveRecord
21
21
  end
22
22
 
23
23
  class AttributeError < ::StandardError
24
- def initialize(attribute, value, type)
24
+ def initialize(attribute, value, type = :invalid, options = {})
25
25
  @attribute = attribute.model.translate(attribute.name)
26
26
 
27
- super(generate_message(attribute, type, value))
27
+ super(generate_message(attribute, type, value, options))
28
28
  end
29
29
 
30
30
  def full_message
@@ -34,7 +34,7 @@ module ActiveRecord
34
34
  private
35
35
 
36
36
  # implemented with referencing to https://github.com/rails/rails/blob/517cf249c369d4bca40b1f590ca641d8b717985e/activemodel/lib/active_model/errors.rb#L462
37
- def generate_message(attribute, type, value)
37
+ def generate_message(attribute, type, value, options)
38
38
  model_klass = attribute.model.activerecord_constant
39
39
  model_scope = model_klass.i18n_scope
40
40
 
@@ -58,21 +58,15 @@ module ActiveRecord
58
58
 
59
59
  key = defaults.shift
60
60
 
61
- options = {
61
+ options = options.merge(
62
62
  default: defaults,
63
63
  model: model_klass.model_name.human,
64
64
  attribute: model_klass.human_attribute_name(attribute.name),
65
65
  value: value
66
- }
66
+ )
67
67
 
68
68
  I18n.translate(key, options)
69
69
  end
70
70
  end
71
-
72
- class DataInvalid < AttributeError
73
- def initialize(attribute, value)
74
- super(attribute, value, :invalid)
75
- end
76
- end
77
71
  end
78
72
  end
@@ -39,6 +39,7 @@ module ActiveRecord
39
39
 
40
40
  @preferred_skip_attributes = @plan.pickup_value_for(self, :preferred_skip_attributes, [])
41
41
  @default_values = @plan.pickup_value_for(self, :default_values, {})
42
+ @sort_indexes = @plan.pickup_value_for(self, :sort_indexes, {})
42
43
 
43
44
  # At present, translation function is only i18n
44
45
  @translator = ::ActiveRecord::Bixformer::Translator::I18n.new
@@ -193,6 +194,25 @@ module ActiveRecord
193
194
 
194
195
  values
195
196
  end
197
+
198
+ def sortable_value(attribute, value)
199
+ index = @sort_indexes[attribute.name] || @plan.entry_attribute_size
200
+
201
+ { index: index, value: value }
202
+ end
203
+
204
+ def sort(sortable_values)
205
+ sortable_values_size = sortable_values.size.to_f
206
+
207
+ sortable_values.sort_by.with_index(0) do |v, i|
208
+ # Enumerable#sort は安定ではないので、 sort_by を使って、
209
+ # 明示的にユニークで必ず増加する数値を加えることで、同じ index だった場合に
210
+ # 順序が変わらないことを保証する
211
+ v[:index] + i / sortable_values_size
212
+ end.map do |v|
213
+ v[:value]
214
+ end
215
+ end
196
216
  end
197
217
  end
198
218
  end
@@ -25,10 +25,7 @@ module ActiveRecord
25
25
  end
26
26
 
27
27
  def csv_titles
28
- [
29
- *@attributes.map { |attr| csv_title(attr.name) },
30
- *@associations.flat_map(&:csv_titles)
31
- ]
28
+ sort(sortable_csv_titles)
32
29
  end
33
30
 
34
31
  def csv_title(attribute_name)
@@ -75,6 +72,13 @@ module ActiveRecord
75
72
  association.import(csv_body_row, self_record_id)
76
73
  end
77
74
  end
75
+
76
+ def sortable_csv_titles
77
+ [
78
+ *@attributes.map { |attr| sortable_value(attr, csv_title(attr.name)) },
79
+ *@associations.flat_map { |assoc| assoc.__send__(:sortable_csv_titles) }
80
+ ]
81
+ end
78
82
  end
79
83
  end
80
84
  end
@@ -44,14 +44,6 @@ module ActiveRecord
44
44
  super
45
45
  end
46
46
 
47
- def csv_titles
48
- (1..options[:size]).flat_map do |index|
49
- update_translator(index)
50
-
51
- super
52
- end
53
- end
54
-
55
47
  def csv_title(attribute_name)
56
48
  # TODO: indexed 以外の複数を扱うクラスがあった時の対処が必要
57
49
  if parents.find { |parent| parent.is_a?(::ActiveRecord::Bixformer::Model::Csv::Indexed) }
@@ -63,6 +55,14 @@ module ActiveRecord
63
55
 
64
56
  private
65
57
 
58
+ def sortable_csv_titles
59
+ (1..options[:size]).flat_map do |index|
60
+ update_translator(index)
61
+
62
+ super
63
+ end
64
+ end
65
+
66
66
  def update_translator(index)
67
67
  @translator.model_arguments = { index: index }
68
68
 
@@ -46,14 +46,6 @@ module ActiveRecord
46
46
  end
47
47
  end
48
48
 
49
- def csv_titles
50
- @options[:in].flat_map do |key|
51
- update_translator(key)
52
-
53
- super
54
- end
55
- end
56
-
57
49
  def translate(attribute_name)
58
50
  # TODO: mapped 以外の複数を扱うクラスがあった時の対処が必要
59
51
  if parents.find { |parent| parent.is_a?(::ActiveRecord::Bixformer::Model::Csv::Mapped) }
@@ -65,6 +57,14 @@ module ActiveRecord
65
57
 
66
58
  private
67
59
 
60
+ def sortable_csv_titles
61
+ @options[:in].flat_map do |key|
62
+ update_translator(key)
63
+
64
+ super
65
+ end
66
+ end
67
+
68
68
  def update_translator(key)
69
69
  key = @options[:translate] ? @options[:translate].call(key) : key
70
70
 
@@ -14,6 +14,7 @@ module ActiveRecord
14
14
  class_attribute :__bixformer_unique_attributes
15
15
  class_attribute :__bixformer_required_condition
16
16
  class_attribute :__bixformer_default_values
17
+ class_attribute :__bixformer_sort_indexes
17
18
  class_attribute :__bixformer_translation_config
18
19
 
19
20
  self.__bixformer_entry = {}
@@ -22,6 +23,7 @@ module ActiveRecord
22
23
  self.__bixformer_unique_attributes = []
23
24
  self.__bixformer_required_condition = {}
24
25
  self.__bixformer_default_values = {}
26
+ self.__bixformer_sort_indexes = {}
25
27
  self.__bixformer_translation_config = { scope: :bixformer, extend_scopes: [] }
26
28
  end
27
29
 
@@ -36,7 +38,7 @@ module ActiveRecord
36
38
 
37
39
  [
38
40
  :entry, :preferred_skip_attributes, :required_attributes, :unique_attributes,
39
- :required_condition, :default_values, :translation_config
41
+ :required_condition, :default_values, :sort_indexes, :translation_config
40
42
  ].each do |config_name|
41
43
  define_method "bixformer_#{config_name}" do |v|
42
44
  self.__send__("__bixformer_#{config_name}=", v)
@@ -100,6 +100,16 @@ module ActiveRecord
100
100
  [type, arguments]
101
101
  end
102
102
 
103
+ def entry_attribute_size
104
+ counter = -> (v) do
105
+ (v[:attributes] || {}).keys.size + (v[:associations] || {}).values.inject(0) do |sum, assoc_v|
106
+ sum + counter.call(assoc_v)
107
+ end
108
+ end
109
+
110
+ @entry_attribute_size ||= counter.call(compile_config_value(:entry))
111
+ end
112
+
103
113
  private
104
114
 
105
115
  def compile_config_value(config_name)
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Bixformer
3
- VERSION = "0.3.17"
3
+ VERSION = "0.4.0"
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.17
4
+ version: 0.4.0
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-12 00:00:00.000000000 Z
11
+ date: 2016-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -229,6 +229,7 @@ files:
229
229
  - lib/activerecord-bixformer/attribute/date.rb
230
230
  - lib/activerecord-bixformer/attribute/enumerize.rb
231
231
  - lib/activerecord-bixformer/attribute/formatted_foreign_key.rb
232
+ - lib/activerecord-bixformer/attribute/integer.rb
232
233
  - lib/activerecord-bixformer/attribute/override.rb
233
234
  - lib/activerecord-bixformer/attribute/string.rb
234
235
  - lib/activerecord-bixformer/attribute/time.rb