eac_rails_utils 0.1.15 → 0.2.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: e2bba119f4803e55e20c3a9bf915965ae821847d
4
- data.tar.gz: 1493145cd715c349b3f9debbccb1cf18e2cb34cf
3
+ metadata.gz: 48317e9de4d255c8f419eb298030496144d85a93
4
+ data.tar.gz: bba8b002895dcca4c34af057f612b34199226ade
5
5
  SHA512:
6
- metadata.gz: d97cbae07e0d8c6101324db441377322cebe01d6e191b38b9bc6a6d8a7b14b8175b61f716f7d5c060a753109101a0892adccf25c81478fcf64d9442061c0d19f
7
- data.tar.gz: 8cc81ac1dfc2f727b0dcb80fe64400bef7c573d9aa329a9d90d2ec26b0ff440529290bea53364b94a4ad90cf1d365c37561d961c679ab06c0df270b7914c2278
6
+ metadata.gz: f609cacab57ddc7427a207d419008678b8fe6e7efcf050f415ba461bd0e3c37c78b6ea162b25cbff58dc3074f6ddaeac2dea3b025fba8e295ea2899cc27195bf
7
+ data.tar.gz: 3598abd778ff91d1101dabe8a350f7afc240b41d47128a32f5cc28a78a44a6db3f2b3297408b692fefc7ab8becc955f75546a0f254f04f7cdce02180871f0ad9
@@ -9,6 +9,7 @@ module Eac
9
9
  include RadioSelectField
10
10
  include FieldsFor
11
11
  include SelectField
12
+ include TimeField
12
13
  include YearMonthField
13
14
 
14
15
  attr_reader :form, :helper, :field_errors_showed
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+ module Eac
3
+ module CommonFormHelper
4
+ class FormBuilder
5
+ module TimeField
6
+ def time_field(field_name, options = {})
7
+ field_options = options.extract!(:use_month_numbers, :use_two_digit_numbers,
8
+ :use_short_month, :add_month_numbers, :use_month_names,
9
+ :month_format_string, :date_separator, :start_year,
10
+ :end_year, :discard_day, :discard_month, :discard_year,
11
+ :order, :include_blank, :default, :selected, :disabled,
12
+ :prompt, :with_css_classes)
13
+ field(field_name, options) do
14
+ @helper.content_tag(:div, @form.time_select(field_name, field_options),
15
+ class: 'form-control-complex')
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -5,9 +5,11 @@ module EacRailsUtils
5
5
  require 'ofx-parser'
6
6
  require 'virtus'
7
7
 
8
+ require 'eac_rails_utils/patches/action_controller_base'
8
9
  require 'eac_rails_utils/patches/model_attribute_required'
9
10
  require 'eac_rails_utils/patches/ofx_parser'
10
11
  require 'eac_rails_utils/rails/engine'
12
+ require 'eac_rails_utils/tableless_model'
11
13
  require 'eac/cpf_validator'
12
14
  require 'eac/formatter_helper'
13
15
  require 'eac/common_form_helper/form_builder/association_select_field'
@@ -18,6 +20,7 @@ module EacRailsUtils
18
20
  require 'eac/common_form_helper/form_builder/radio_select_field'
19
21
  require 'eac/common_form_helper/form_builder/searchable_association_field'
20
22
  require 'eac/common_form_helper/form_builder/select_field'
23
+ require 'eac/common_form_helper/form_builder/time_field'
21
24
  require 'eac/common_form_helper/form_builder/year_month_field'
22
25
  require 'eac/common_form_helper/form_builder'
23
26
  require 'eac/common_form_helper'
@@ -37,7 +40,6 @@ module EacRailsUtils
37
40
  require 'eac/parsers/ofx'
38
41
  require 'eac/simple_cache'
39
42
  require 'eac/source_target_fixtures'
40
- require 'eac/tableless_model'
41
43
  require 'eac/test_utils'
42
44
 
43
45
  ActionView::Base.send :include, Eac::CommonFormHelper
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ module EacRailsUtils
3
+ module Patches
4
+ module ActionControllerBasePatch
5
+ def self.included(base)
6
+ base.include InstanceMethods
7
+ end
8
+
9
+ module InstanceMethods
10
+ def redirect_back(default_path, options = {})
11
+ redirect_to :back, options
12
+ rescue ActionController::RedirectBackError
13
+ redirect_to default_path, options
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+
20
+ patch = ::EacRailsUtils::Patches::ActionControllerBasePatch
21
+ target = ::ActionController::Base
22
+ target.send(:include, patch) unless target.included_modules.include?(patch)
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+ module EacRailsUtils
3
+ class TablelessModel
4
+ include ActiveModel::Model
5
+ include Virtus.model
6
+ include ActiveModel::Associations
7
+
8
+ def initialize(values = {})
9
+ super(build_attributes(values))
10
+ end
11
+
12
+ def attributes=(values)
13
+ super(build_attributes(values))
14
+ end
15
+
16
+ # need hash like accessor, used internal Rails
17
+ def [](attr)
18
+ send(attr)
19
+ end
20
+
21
+ # need hash like accessor, used internal Rails
22
+ def []=(attr, value)
23
+ send("#{attr}=", value)
24
+ end
25
+
26
+ def save!
27
+ save || raise("#{self.class}.save failed: #{errors.messages}")
28
+ end
29
+
30
+ private
31
+
32
+ def build_attributes(values)
33
+ AttributesBuilder.new(self.class, values).to_attributes
34
+ end
35
+
36
+ class AttributesBuilder
37
+ DATE_TIME_FIELDS = %i(year month day hour min sec).freeze
38
+
39
+ def initialize(model_class, values)
40
+ @model_class = model_class
41
+ @values = {}
42
+ values.each { |k, v| add(k, v) }
43
+ end
44
+
45
+ def to_attributes
46
+ @values
47
+ end
48
+
49
+ private
50
+
51
+ def add(key, value)
52
+ array_attr = parse_array_attr_key(key)
53
+ if array_attr
54
+ array_value_set(array_attr, value)
55
+ else
56
+ @values[key] = value
57
+ end
58
+ end
59
+
60
+ def parse_array_attr_key(key)
61
+ m = /\A(.+)\(([0-9]+)(.)\)\z/.match(key)
62
+ if m
63
+ ::OpenStruct.new(key: m[1], index: m[2].to_i - 1, converter: array_value_converter(m[3]))
64
+ end
65
+ end
66
+
67
+ def array_value_set(array_attr, value)
68
+ @values[array_attr.key] ||= {}
69
+ @values[array_attr.key].merge!(
70
+ DATE_TIME_FIELDS[array_attr.index] => value.send(array_attr.converter)
71
+ )
72
+ end
73
+
74
+ def array_value_converter(str_type)
75
+ case str_type
76
+ when 'i'
77
+ 'to_i'
78
+ else
79
+ raise "Unknown array type: \"#{str_type}\""
80
+ end
81
+ end
82
+
83
+ def date_time_attribute?(key)
84
+ attr = @model_class.attributes[key]
85
+ return false unless attr
86
+ raise attr.to_s
87
+ end
88
+ end
89
+ end
90
+ end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module EacRailsUtils
3
- VERSION = '0.1.15'
3
+ VERSION = '0.2.0'
4
4
  end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+ # frozen_string_literal: true
3
+ require 'test_helper'
4
+
5
+ module EacRailsUtils
6
+ class TablelessModelTest < ActiveSupport::TestCase
7
+ class Stub < ::EacRailsUtils::TablelessModel
8
+ attribute :tempo, DateTime
9
+ end
10
+
11
+ test 'date time array values' do
12
+ stub = Stub.new('tempo(1i)' => '9', 'tempo(2i)' => '10', 'tempo(3i)' => '11',
13
+ 'tempo(4i)' => '12', 'tempo(5i)' => '13', 'tempo(6i)' => '14')
14
+ assert stub.tempo.is_a?(DateTime), "Class: |#{stub.tempo.class}|, Value: |#{stub.tempo}|"
15
+ assert_equal 9, stub.tempo.year, 'Year'
16
+ assert_equal 10, stub.tempo.month, 'Month'
17
+ assert_equal 11, stub.tempo.day, 'Day'
18
+ assert_equal 12, stub.tempo.hour, 'Hour'
19
+ assert_equal 13, stub.tempo.minute, 'Minute'
20
+ assert_equal 14, stub.tempo.second, 'Second'
21
+ end
22
+ end
23
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_rails_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - E.A.C.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-27 00:00:00.000000000 Z
11
+ date: 2018-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel-associations
@@ -150,6 +150,7 @@ files:
150
150
  - lib/eac/common_form_helper/form_builder/radio_select_field.rb
151
151
  - lib/eac/common_form_helper/form_builder/searchable_association_field.rb
152
152
  - lib/eac/common_form_helper/form_builder/select_field.rb
153
+ - lib/eac/common_form_helper/form_builder/time_field.rb
153
154
  - lib/eac/common_form_helper/form_builder/year_month_field.rb
154
155
  - lib/eac/cpf_validator.rb
155
156
  - lib/eac/data_table_helper.rb
@@ -172,12 +173,13 @@ files:
172
173
  - lib/eac/parsers/ofx.rb
173
174
  - lib/eac/simple_cache.rb
174
175
  - lib/eac/source_target_fixtures.rb
175
- - lib/eac/tableless_model.rb
176
176
  - lib/eac/test_utils.rb
177
177
  - lib/eac_rails_utils.rb
178
+ - lib/eac_rails_utils/patches/action_controller_base.rb
178
179
  - lib/eac_rails_utils/patches/model_attribute_required.rb
179
180
  - lib/eac_rails_utils/patches/ofx_parser.rb
180
181
  - lib/eac_rails_utils/rails/engine.rb
182
+ - lib/eac_rails_utils/tableless_model.rb
181
183
  - lib/eac_rails_utils/version.rb
182
184
  - lib/tasks/eac_rails_utils.rake
183
185
  - test/dummy/Rakefile
@@ -214,6 +216,7 @@ files:
214
216
  - test/lib/eac/source_target_fixtures_test_files/b.source.html
215
217
  - test/lib/eac/source_target_fixtures_test_files/c.target.yaml
216
218
  - test/lib/eac_rails_utils/patches/model_attribute_required_test.rb
219
+ - test/lib/eac_rails_utils/tableless_model_test.rb
217
220
  - test/test_helper.rb
218
221
  homepage:
219
222
  licenses: []
@@ -257,6 +260,7 @@ test_files:
257
260
  - test/dummy/app/models/user.rb
258
261
  - test/test_helper.rb
259
262
  - test/lib/eac_rails_utils/patches/model_attribute_required_test.rb
263
+ - test/lib/eac_rails_utils/tableless_model_test.rb
260
264
  - test/lib/eac/common_form_helper_test.rb
261
265
  - test/lib/eac/source_target_fixtures_test.rb
262
266
  - test/lib/eac/parsers/files_test_test.rb
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
- module Eac
3
- class TablelessModel
4
- include ActiveModel::Model
5
- include Virtus.model
6
- include ActiveModel::Associations
7
-
8
- # need hash like accessor, used internal Rails
9
- def [](attr)
10
- send(attr)
11
- end
12
-
13
- # need hash like accessor, used internal Rails
14
- def []=(attr, value)
15
- send("#{attr}=", value)
16
- end
17
-
18
- def save!
19
- save || raise("#{self.class}.save failed: #{errors.messages}")
20
- end
21
- end
22
- end