eac_rails_utils 0.6.0 → 0.7.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
  SHA256:
3
- metadata.gz: 926390e40752868353a97b85802a2f3626ed5b9206c570ce88931c4fde036836
4
- data.tar.gz: 56fa6a845cedfc2afda247787dd6c94a62013f883def7e2c7dcf06f2cb348260
3
+ metadata.gz: f71207400c361bf17bc680e3d653a9f9f7b57cb8342dda2e7d27adc757714a22
4
+ data.tar.gz: f79f986ac9e8096ed31c7096436083f67cf2951f1e676e51001055b755d2d644
5
5
  SHA512:
6
- metadata.gz: 68dd63e8acea983a4716bd9b9e349d4320358349fe3845b9e4961c5b6005e505362c8c3f7b0c7c121fce3b8d6445687760230c790b4345c88c1ac8d1d33975fc
7
- data.tar.gz: ccb1f573919f77aee1b7eb14eb8db72eea4e969b6ea465b6561fc632995af978b9bc1c3a46281a89da1e46bce3227ab396541b15170073d9287cb63222e1e488
6
+ metadata.gz: af66a26c332e71970051b281bf1c4dc9998699cb9db894d9fdf687e02815da431075ca627a489f3eb1bc0c6e4dc7ca8c8eabfba9d4f4fdc58401ad345fbc304a
7
+ data.tar.gz: 6cbc2b7d6368112a81b7758b90a0f4e927d05b4ff176e99d9f37c352dbd5027ea2bbc41b2bb80960a6486cd60a61aca02d01e501da661e62bb7681115a91987e
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+ module EacRailsUtils
3
+ module FormatterHelper
4
+ include ActionView::Helpers::NumberHelper
5
+
6
+ def value_or_sign(value, sign = '-', &block)
7
+ return sign if value.blank?
8
+ return yield(value) if block
9
+ value
10
+ end
11
+
12
+ def format_real(value)
13
+ number_to_currency(
14
+ value,
15
+ unit: 'R$ ',
16
+ separator: ',',
17
+ delimiter: '.',
18
+ raise: true
19
+ )
20
+ end
21
+
22
+ def format_percentage(float_value)
23
+ number_to_percentage(float_value * 100, precision: 0)
24
+ end
25
+
26
+ def brl_currency_to_float(currency)
27
+ currency.to_s.gsub(/[R$ .]/, '').tr(',', '.').to_f
28
+ end
29
+
30
+ def format_cep(cep)
31
+ "#{cep[0, 5]}-#{cep[5, 3]}"
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+ module EacRailsUtils
3
+ module LinksHelper
4
+ def short_delete_link(object)
5
+ short_object_link object, '', class: 'delete_link', method: :delete, target: '_blank',
6
+ title: ::I18n.t('eac_rails_base0.links.delete_object',
7
+ label: object.to_s),
8
+ data: {
9
+ confirm: ::I18n.t('eac_rails_base0.links.delete_confirm',
10
+ label: object.to_s)
11
+ }
12
+ end
13
+
14
+ def short_edit_link(object)
15
+ short_object_link object, 'edit', class: 'edit_link', target: '_blank',
16
+ title: ::I18n.t('eac_rails_base0.links.edit_object',
17
+ label: object.to_s)
18
+ end
19
+
20
+ def short_goto_link(url)
21
+ value_or_sign(url, '') do |value|
22
+ link_to '', value, class: 'goto_link', target: '_blank',
23
+ title: ::I18n.t('eac_rails_base0.links.goto_url', url: value.to_s)
24
+ end
25
+ end
26
+
27
+ def short_show_link(object)
28
+ short_detail_show_link(object, false)
29
+ end
30
+
31
+ def short_detail_link(object)
32
+ short_detail_show_link(object, true)
33
+ end
34
+
35
+ def object_path(object, action = nil)
36
+ current_class = object.class
37
+ tried_paths = []
38
+ while current_class
39
+ path = object_path_by_class(current_class, action)
40
+ return send(path, object) if respond_to?(path)
41
+ tried_paths << path
42
+ current_class = current_class.superclass
43
+ end
44
+ raise "Path not found for {object: #{object.class}, action: \"#{action}\"}" \
45
+ "(Tried: #{tried_paths})"
46
+ end
47
+
48
+ private
49
+
50
+ def short_detail_show_link(object, detail)
51
+ short_object_link object,
52
+ detail ? 'detail' : nil,
53
+ class: 'show_link', target: '_blank',
54
+ title: ::I18n.t('eac_rails_base0.links.show_object', label: object.to_s)
55
+ end
56
+
57
+ def short_object_link(object, action = nil, options = {})
58
+ value_or_sign(object, '') do |value|
59
+ link_to '', object_path(value, action), options
60
+ end
61
+ end
62
+
63
+ def object_path_by_class(klass, action)
64
+ path = "#{klass.name.underscore.tr('/', '_')}_url"
65
+ path = "#{action}_#{path}" if action.present?
66
+ path
67
+ end
68
+ end
69
+ end
@@ -7,7 +7,6 @@ module EacRailsUtils
7
7
  require 'ofx-parser'
8
8
  require 'virtus'
9
9
 
10
- require_dependency 'eac_rails_utils/helpers/formatter'
11
10
  require 'eac_rails_utils/patches/action_controller_base'
12
11
  require 'eac_rails_utils/patches/model_attribute_required'
13
12
  require 'eac_rails_utils/patches/ofx_parser'
@@ -41,7 +40,6 @@ module EacRailsUtils
41
40
  require 'eac/source_target_fixtures'
42
41
  require 'eac/test_utils'
43
42
 
44
- ActionView::Base.send :include, ::EacRailsUtils::Helpers::Formatter
45
43
  ActionView::Base.send :include, Eac::CommonFormHelper
46
44
  ActionView::Base.send :include, Eac::DataTableHelper
47
45
  ActionView::Base.send :include, Eac::MenusHelper
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module EacRailsUtils
3
- VERSION = '0.6.0'
3
+ VERSION = '0.7.0'
4
4
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ require 'test_helper'
3
+
4
+ module EacRailsUtils
5
+ class FormatterHelperTest < ActionView::TestCase
6
+ include ::EacRailsUtils::FormatterHelper
7
+
8
+ test 'should convert BRL currency to float' do
9
+ brl_currency = { a: '1', b: '1,2', c: '1,23', d: '123.456.789,01',
10
+ e: 'R$1,23', f: 'R$ 123.4,56' }
11
+ float_currency = { a: 1, b: 1.2, c: 1.23, d: 123_456_789.01,
12
+ e: 1.23, f: 123_4.56 }
13
+
14
+ brl_currency.each do |k, v|
15
+ assert_equal float_currency[k], brl_currency_to_float(v),
16
+ "#{v} should be #{float_currency[k]}"
17
+ end
18
+ end
19
+ end
20
+ 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.6.0
4
+ version: 0.7.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: 2019-12-09 00:00:00.000000000 Z
11
+ date: 2019-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel-associations
@@ -135,6 +135,8 @@ extensions: []
135
135
  extra_rdoc_files: []
136
136
  files:
137
137
  - Rakefile
138
+ - app/helpers/eac_rails_utils/formatter_helper.rb
139
+ - app/helpers/eac_rails_utils/links_helper.rb
138
140
  - app/helpers/eac_rails_utils/open_graph_protocol_helper.rb
139
141
  - lib/assets/javascripts/currency_field.js
140
142
  - lib/assets/javascripts/eac_rails_utils.js
@@ -179,12 +181,12 @@ files:
179
181
  - lib/eac/test_utils.rb
180
182
  - lib/eac_rails_utils.rb
181
183
  - lib/eac_rails_utils/engine.rb
182
- - lib/eac_rails_utils/helpers/formatter.rb
183
184
  - lib/eac_rails_utils/patches/action_controller_base.rb
184
185
  - lib/eac_rails_utils/patches/model_attribute_required.rb
185
186
  - lib/eac_rails_utils/patches/ofx_parser.rb
186
187
  - lib/eac_rails_utils/tableless_model.rb
187
188
  - lib/eac_rails_utils/version.rb
189
+ - test/app/helpers/eac_rails_utils/formatter_helper_test.rb
188
190
  - test/dummy/Rakefile
189
191
  - test/dummy/app/models/job.rb
190
192
  - test/dummy/app/models/user.rb
@@ -216,7 +218,6 @@ files:
216
218
  - test/lib/eac/source_target_fixtures_test_files/a.target.yaml
217
219
  - test/lib/eac/source_target_fixtures_test_files/b.source.html
218
220
  - test/lib/eac/source_target_fixtures_test_files/c.target.yaml
219
- - test/lib/eac_rails_utils/helpers/formatter_test.rb
220
221
  - test/lib/eac_rails_utils/patches/model_attribute_required_test.rb
221
222
  - test/lib/eac_rails_utils/tableless_model_test.rb
222
223
  - test/test_helper.rb
@@ -245,7 +246,6 @@ specification_version: 4
245
246
  summary: Código reutilizável para as aplicações Rails da E.A.C..
246
247
  test_files:
247
248
  - test/lib/eac_rails_utils/tableless_model_test.rb
248
- - test/lib/eac_rails_utils/helpers/formatter_test.rb
249
249
  - test/lib/eac_rails_utils/patches/model_attribute_required_test.rb
250
250
  - test/lib/eac/source_target_fixtures_test.rb
251
251
  - test/lib/eac/listable_test.rb
@@ -278,4 +278,5 @@ test_files:
278
278
  - test/dummy/db/migrate/20160415125333_create_users.rb
279
279
  - test/dummy/db/migrate/20160415143123_create_jobs.rb
280
280
  - test/dummy/db/migrate/20160415143229_add_job_to_users.rb
281
+ - test/app/helpers/eac_rails_utils/formatter_helper_test.rb
281
282
  - test/test_helper.rb
@@ -1,36 +0,0 @@
1
- # frozen_string_literal: true
2
- module EacRailsUtils
3
- module Helpers
4
- module Formatter
5
- include ActionView::Helpers::NumberHelper
6
-
7
- def value_or_sign(value, sign = '-', &block)
8
- return sign if value.blank?
9
- return yield(value) if block
10
- value
11
- end
12
-
13
- def format_real(value)
14
- number_to_currency(
15
- value,
16
- unit: 'R$ ',
17
- separator: ',',
18
- delimiter: '.',
19
- raise: true
20
- )
21
- end
22
-
23
- def format_percentage(float_value)
24
- number_to_percentage(float_value * 100, precision: 0)
25
- end
26
-
27
- def brl_currency_to_float(currency)
28
- currency.to_s.gsub(/[R$ .]/, '').tr(',', '.').to_f
29
- end
30
-
31
- def format_cep(cep)
32
- "#{cep[0, 5]}-#{cep[5, 3]}"
33
- end
34
- end
35
- end
36
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
- require 'test_helper'
3
-
4
- module EacRailsUtils
5
- module Helpers
6
- class FormatterTest < ActionView::TestCase
7
- include ::EacRailsUtils::Helpers::Formatter
8
-
9
- test 'should convert BRL currency to float' do
10
- brl_currency = { a: '1', b: '1,2', c: '1,23', d: '123.456.789,01',
11
- e: 'R$1,23', f: 'R$ 123.4,56' }
12
- float_currency = { a: 1, b: 1.2, c: 1.23, d: 123_456_789.01,
13
- e: 1.23, f: 123_4.56 }
14
-
15
- brl_currency.each do |k, v|
16
- assert_equal float_currency[k], brl_currency_to_float(v),
17
- "#{v} should be #{float_currency[k]}"
18
- end
19
- end
20
- end
21
- end
22
- end