eac_rails_utils 0.8.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +2 -0
  3. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper.rb +8 -1
  4. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder.rb +12 -2
  5. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/association_select_field.rb +5 -2
  6. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/common_text_fields.rb +4 -3
  7. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/currency_field.rb +14 -12
  8. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/date_field.rb +3 -1
  9. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/fields_for.rb +3 -1
  10. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/file_field.rb +2 -1
  11. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/radio_select_field.rb +5 -2
  12. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/searchable_association_field.rb +7 -3
  13. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/select_field.rb +3 -1
  14. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/time_field.rb +2 -1
  15. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/year_month_field.rb +2 -1
  16. data/app/helpers/eac_rails_utils/data_table_helper.rb +13 -0
  17. data/{lib/eac → app/helpers/eac_rails_utils}/data_table_helper/column.rb +6 -3
  18. data/{lib/eac → app/helpers/eac_rails_utils}/data_table_helper/data_table.rb +13 -11
  19. data/{lib/eac → app/helpers/eac_rails_utils}/data_table_helper/setup.rb +3 -2
  20. data/app/helpers/eac_rails_utils/formatter_helper.rb +2 -0
  21. data/app/helpers/eac_rails_utils/links_helper.rb +12 -2
  22. data/{lib/eac → app/helpers/eac_rails_utils}/menus_helper.rb +10 -2
  23. data/{lib/eac → app/helpers/eac_rails_utils}/menus_helper/bootstrap_gui_builder.rb +8 -5
  24. data/{lib/eac → app/helpers/eac_rails_utils}/menus_helper/data_builder.rb +7 -2
  25. data/{lib/eac → app/helpers/eac_rails_utils}/menus_helper/gui_builder.rb +6 -3
  26. data/app/helpers/eac_rails_utils/open_graph_protocol_helper.rb +2 -0
  27. data/{lib/eac → app/validators/eac_rails_utils}/cpf_validator.rb +8 -4
  28. data/{lib/eac → app/validators/eac_rails_utils}/no_presence_validator.rb +3 -3
  29. data/lib/assets/javascripts/jMenu.jquery.min.js +12 -12
  30. data/lib/eac_rails_utils.rb +3 -41
  31. data/lib/eac_rails_utils/engine.rb +3 -0
  32. data/lib/{eac → eac_rails_utils}/htmlbeautifier.rb +3 -1
  33. data/lib/eac_rails_utils/models.rb +9 -0
  34. data/lib/eac_rails_utils/models/attribute_required.rb +43 -0
  35. data/lib/eac_rails_utils/models/fetch_errors.rb +92 -0
  36. data/lib/eac_rails_utils/models/inequality_queries.rb +39 -0
  37. data/lib/eac_rails_utils/models/tableless.rb +97 -0
  38. data/lib/eac_rails_utils/models/test_utils.rb +65 -0
  39. data/lib/eac_rails_utils/patches.rb +9 -0
  40. data/lib/eac_rails_utils/patches/action_controller_base.rb +3 -0
  41. data/lib/eac_rails_utils/version.rb +2 -1
  42. data/test/dummy/Rakefile +3 -1
  43. data/test/dummy/app/models/job.rb +2 -0
  44. data/test/dummy/app/models/user.rb +6 -0
  45. data/test/dummy/config.ru +2 -0
  46. data/test/dummy/config/application.rb +3 -1
  47. data/test/dummy/config/boot.rb +3 -1
  48. data/test/dummy/config/environment.rb +3 -1
  49. data/test/dummy/config/environments/test.rb +2 -0
  50. data/test/dummy/config/routes.rb +2 -0
  51. data/test/dummy/db/migrate/20160415125333_create_users.rb +2 -0
  52. data/test/dummy/db/migrate/20160415143123_create_jobs.rb +2 -0
  53. data/test/dummy/db/migrate/20160415143229_add_job_to_users.rb +2 -0
  54. data/test/{lib/eac → helpers/eac_rails_utils}/common_form_helper_test.rb +4 -2
  55. data/test/{lib/eac/data_table_test_helper.rb → helpers/eac_rails_utils/data_table_test_helper_test.rb} +4 -3
  56. data/test/{app/helpers → helpers}/eac_rails_utils/formatter_helper_test.rb +1 -0
  57. data/test/lib/eac_rails_utils/{patches/model_attribute_required_test.rb → models/attribute_required_test.rb} +3 -0
  58. data/test/lib/eac_rails_utils/models/fetch_errors_test.rb +80 -0
  59. data/test/lib/eac_rails_utils/models/tableless_test.rb +25 -0
  60. data/test/test_helper.rb +3 -1
  61. data/test/{lib/eac → validators}/cpf_validator_test.rb +4 -2
  62. metadata +91 -98
  63. data/lib/eac/data_table_helper.rb +0 -12
  64. data/lib/eac/inequality_queries.rb +0 -36
  65. data/lib/eac/model.rb +0 -87
  66. data/lib/eac/parsers/files_test.rb +0 -63
  67. data/lib/eac/source_target_fixtures.rb +0 -66
  68. data/lib/eac/test_utils.rb +0 -56
  69. data/lib/eac_rails_utils/patches/model_attribute_required.rb +0 -31
  70. data/lib/eac_rails_utils/patches/ofx_parser.rb +0 -42
  71. data/lib/eac_rails_utils/tableless_model.rb +0 -90
  72. data/test/lib/eac/model_test.rb +0 -76
  73. data/test/lib/eac/parsers/files_test_test.rb +0 -27
  74. data/test/lib/eac/parsers/ok_test_files/a.source.yaml +0 -1
  75. data/test/lib/eac/parsers/ok_test_files/a.target.yaml +0 -1
  76. data/test/lib/eac/parsers/ok_test_files/b.source.yaml +0 -1
  77. data/test/lib/eac/parsers/ok_test_files/b.target.yaml +0 -1
  78. data/test/lib/eac/source_target_fixtures_test.rb +0 -41
  79. data/test/lib/eac/source_target_fixtures_test_files/a.source.html +0 -1
  80. data/test/lib/eac/source_target_fixtures_test_files/a.target.yaml +0 -1
  81. data/test/lib/eac/source_target_fixtures_test_files/b.source.html +0 -1
  82. data/test/lib/eac/source_target_fixtures_test_files/c.target.yaml +0 -1
  83. data/test/lib/eac_rails_utils/tableless_model_test.rb +0 -23
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'activemodel/associations'
4
+ require 'virtus'
5
+
6
+ module EacRailsUtils
7
+ module Models
8
+ class Tableless
9
+ include ActiveModel::Model
10
+ include Virtus.model
11
+ include ActiveModel::Associations
12
+
13
+ def initialize(values = {})
14
+ super(build_attributes(values))
15
+ end
16
+
17
+ def attributes=(values)
18
+ super(build_attributes(values))
19
+ end
20
+
21
+ # need hash like accessor, used internal Rails
22
+ def [](attr)
23
+ send(attr)
24
+ end
25
+
26
+ # need hash like accessor, used internal Rails
27
+ def []=(attr, value)
28
+ send("#{attr}=", value)
29
+ end
30
+
31
+ def save!
32
+ save || raise("#{self.class}.save failed: #{errors.messages}")
33
+ end
34
+
35
+ private
36
+
37
+ def build_attributes(values)
38
+ AttributesBuilder.new(self.class, values).to_attributes
39
+ end
40
+
41
+ class AttributesBuilder
42
+ DATE_TIME_FIELDS = %i[year month day hour min sec].freeze
43
+
44
+ def initialize(model_class, values)
45
+ @model_class = model_class
46
+ @values = {}
47
+ values.each { |k, v| add(k, v) }
48
+ end
49
+
50
+ def to_attributes
51
+ @values
52
+ end
53
+
54
+ private
55
+
56
+ def add(key, value)
57
+ array_attr = parse_array_attr_key(key)
58
+ if array_attr
59
+ array_value_set(array_attr, value)
60
+ else
61
+ @values[key] = value
62
+ end
63
+ end
64
+
65
+ def parse_array_attr_key(key)
66
+ m = /\A(.+)\(([0-9]+)(.)\)\z/.match(key)
67
+ return unless m
68
+
69
+ ::OpenStruct.new(key: m[1], index: m[2].to_i - 1, converter: array_value_converter(m[3]))
70
+ end
71
+
72
+ def array_value_set(array_attr, value)
73
+ @values[array_attr.key] ||= {}
74
+ @values[array_attr.key].merge!(
75
+ DATE_TIME_FIELDS[array_attr.index] => value.send(array_attr.converter)
76
+ )
77
+ end
78
+
79
+ def array_value_converter(str_type)
80
+ case str_type
81
+ when 'i'
82
+ 'to_i'
83
+ else
84
+ raise "Unknown array type: \"#{str_type}\""
85
+ end
86
+ end
87
+
88
+ def date_time_attribute?(key)
89
+ attr = @model_class.attributes[key]
90
+ return false unless attr
91
+
92
+ raise attr.to_s
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRailsUtils
4
+ module Models
5
+ module TestUtils
6
+ # Add more helper methods to be used by all tests here...
7
+ def valid_invalid_column_values_test(record, column, valid_values, invalid_values)
8
+ valid_values.each do |v|
9
+ record.send("#{column}=", v)
10
+ assert record.valid?,
11
+ "#{record.errors.messages}, #{column} = #{v.inspect} should be valid"
12
+ end
13
+ invalid_values.each do |v|
14
+ record.send("#{column}=", v)
15
+ assert_not record.valid?, "#{column} = #{v.inspect} should be invalid"
16
+ end
17
+ end
18
+
19
+ # Verifica falhas em campos específicos de um record
20
+ def assert_record_errors(record, fields_without_error, fields_with_error)
21
+ fields_without_error.each do |c|
22
+ assert record.errors[c].empty?,
23
+ "Column: #{c} should not have errors (#{record.errors[c]})"
24
+ end
25
+ fields_with_error. each do |c|
26
+ assert_not record.errors[c].empty?, "Column: #{c} should have errors"
27
+ end
28
+ end
29
+
30
+ # Verifica, campo por campo, se invalida o registro.
31
+ def assert_column_changes(ppp, expected_valid_result, changes)
32
+ changes.each do |k, v|
33
+ ppp.send("#{k}=", v)
34
+ assert_equal expected_valid_result, ppp.valid?,
35
+ "\"#{k}\" change should be " + (expected_valid_result ? 'valid' : 'invalid')
36
+ assert_not ppp.errors[k].empty? unless expected_valid_result
37
+ ppp.restore_attributes
38
+ end
39
+ end
40
+
41
+ # Ex.: attrs = {a: 1, b: 2} resulta em
42
+ # [{a: nil, b: nil}, {a: 1, b: nil}, {a: nil, b: 2}, {a: 1, b: 2}].
43
+ def all_combinations(attrs)
44
+ combs = [{}]
45
+ attrs.each do |attr_name, value|
46
+ combs = all_combinations_new_combination(attr_name, value, combs)
47
+ end
48
+ combs
49
+ end
50
+
51
+ def all_combinations_new_combination(attr_name, value, combs)
52
+ new_comb = []
53
+ assert_not value.nil?, "#{attr_name}=#{value}"
54
+ [nil, value].each do |vv|
55
+ combs.each do |c|
56
+ cc = c.dup
57
+ cc[attr_name] = vv
58
+ new_comb << cc
59
+ end
60
+ end
61
+ new_comb
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+
5
+ module EacRailsUtils
6
+ module Patches
7
+ ::EacRubyUtils.require_sub __FILE__
8
+ end
9
+ end
@@ -1,4 +1,7 @@
1
1
  # frozen_string_literal: true
2
+
3
+ require 'action_controller/base'
4
+
2
5
  module EacRailsUtils
3
6
  module Patches
4
7
  module ActionControllerBasePatch
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module EacRailsUtils
3
- VERSION = '0.8.0'
4
+ VERSION = '0.11.0'
4
5
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Add your own tasks in files placed in lib/tasks ending in .rake,
2
4
  # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
5
 
4
- require File.expand_path('../config/application', __FILE__)
6
+ require File.expand_path('config/application', __dir__)
5
7
 
6
8
  Rails.application.load_tasks
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Job < ActiveRecord::Base
2
4
  has_many :users
3
5
  accepts_nested_attributes_for :users, reject_if: :all_blank, allow_destroy: true
@@ -1,4 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_rails_utils/models/attribute_required'
4
+
1
5
  class User < ActiveRecord::Base
6
+ include ::EacRailsUtils::Models::AttributeRequired
7
+
2
8
  belongs_to :job
3
9
 
4
10
  validates :job, presence: true
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # This file is used by Rack-based servers to start the application.
2
4
 
3
5
  require ::File.expand_path('../config/environment', __FILE__)
@@ -1,4 +1,6 @@
1
- require File.expand_path('../boot', __FILE__)
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path('boot', __dir__)
2
4
 
3
5
  require 'rails/all'
4
6
 
@@ -1,4 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Set up gems listed in the Gemfile.
2
- ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../../Gemfile', __FILE__)
4
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__)
3
5
 
4
6
  require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Load the Rails application.
2
- require File.expand_path('../application', __FILE__)
4
+ require File.expand_path('application', __dir__)
3
5
 
4
6
  # Initialize the Rails application.
5
7
  Rails.application.initialize!
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.configure do
2
4
  # Settings specified here will take precedence over those in config/application.rb.
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Rails.application.routes.draw do
2
4
  resources :jobs do
3
5
  collection do
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CreateUsers < ActiveRecord::Migration
2
4
  def change
3
5
  create_table :users do |t|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class CreateJobs < ActiveRecord::Migration
2
4
  def change
3
5
  create_table :jobs do |t|
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class AddJobToUsers < ActiveRecord::Migration
2
4
  def change
3
5
  add_belongs_to :users, :job
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
- module Eac
5
+ module EacRailsUtils
4
6
  class CommonFormHelperTest < ActionView::TestCase
5
- include Eac::CommonFormHelper
7
+ include ::EacRailsUtils::CommonFormHelper
6
8
 
7
9
  setup do
8
10
  reset_test_database
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
 
4
- module Eac
5
- class DataTableTestHelper < ActionView::TestCase
6
- include ::Eac::DataTableHelper
5
+ module EacRailsUtils
6
+ class DataTableTestHelperTest < ActionView::TestCase
7
+ include ::EacRailsUtils::DataTableHelper
7
8
 
8
9
  class Person
9
10
  attr_reader :name, :age, :job
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require 'test_helper'
3
4
 
4
5
  module EacRailsUtils
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  module EacRailsUtils
@@ -30,6 +32,7 @@ module EacRailsUtils
30
32
 
31
33
  class ActiveModelStub
32
34
  include ActiveModel::Model
35
+ include ::EacRailsUtils::Models::AttributeRequired
33
36
 
34
37
  attr_accessor :name, :age
35
38
  validates :name, presence: true
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module EacRailsUtils
6
+ module Models
7
+ class FetchErrorsTest < ActiveSupport::TestCase
8
+ class M1
9
+ include ActiveModel::Model
10
+ include EacRailsUtils::Models::FetchErrors
11
+ attr_accessor :name, :age, :account, :country_id, :other
12
+ end
13
+
14
+ class M2
15
+ include ActiveModel::Model
16
+ attr_accessor :name, :myage, :account_id, :country
17
+
18
+ validate :my_validate
19
+
20
+ def my_validate
21
+ errors.add(:name, 'NAME_ERROR')
22
+ errors.add(:myage, 'MYAGE_ERROR')
23
+ errors.add(:account_id, 'ACCOUNT_ID_ERROR')
24
+ errors.add(:country, 'COUNTRY_ERROR')
25
+ end
26
+ end
27
+
28
+ def setup
29
+ reset_m1
30
+ reset_m2
31
+ end
32
+
33
+ def test_fetch_column_errors
34
+ @m1.fetch_column_errors(@m2, :myage, :age)
35
+ assert_equal @m2.errors[:myage], @m1.errors[:age]
36
+ end
37
+
38
+ def test_fetch_record_errors
39
+ @m1.fetch_record_errors(@m2)
40
+ { name: :name, account_id: :account, country: :country_id }.each do |c2, c1|
41
+ assert_equal @m2.errors[c2], @m1.errors[c1], "c2: #{c2}, c1: #{c1}"
42
+ end
43
+ assert @m1.errors[:age].empty?
44
+ end
45
+
46
+ def test_fetch_record_errors_with_default_column
47
+ @m1.fetch_record_errors(@m2, default_column: :other)
48
+ assert_equal @m2.errors[:name], @m1.errors[:name]
49
+ assert @m1.errors[:age].empty?
50
+ assert_equal ['Myage: MYAGE_ERROR'], @m1.errors[:other]
51
+ end
52
+
53
+ def test_fetch_record_errors_with_skip_option
54
+ @m1.fetch_record_errors(@m2, skip: [:name])
55
+ assert @m1.errors[:name].empty?
56
+ assert @m1.errors[:age].empty?
57
+ end
58
+
59
+ def test_fetch_record_errors_by_mapping
60
+ @m1.fetch_record_errors_by_mapping(@m2, name: :name, myage: :age)
61
+ assert_equal @m2.errors[:name], @m1.errors[:name]
62
+ assert_equal @m2.errors[:myage], @m1.errors[:age]
63
+ end
64
+
65
+ private
66
+
67
+ def reset_m1
68
+ @m1 = M1.new
69
+ assert @m1.errors.empty?
70
+ end
71
+
72
+ def reset_m2
73
+ @m2 = M2.new
74
+ assert_not @m2.valid?
75
+ assert_not @m2.errors[:name].empty?
76
+ assert_not @m2.errors[:myage].empty?
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+
5
+ module EacRailsUtils
6
+ module Models
7
+ class TablelessTest < ActiveSupport::TestCase
8
+ class Stub < ::EacRailsUtils::Models::Tableless
9
+ attribute :tempo, DateTime
10
+ end
11
+
12
+ test 'date time array values' do
13
+ stub = Stub.new('tempo(1i)' => '9', 'tempo(2i)' => '10', 'tempo(3i)' => '11',
14
+ 'tempo(4i)' => '12', 'tempo(5i)' => '13', 'tempo(6i)' => '14')
15
+ assert stub.tempo.is_a?(DateTime), "Class: |#{stub.tempo.class}|, Value: |#{stub.tempo}|"
16
+ assert_equal 9, stub.tempo.year, 'Year'
17
+ assert_equal 10, stub.tempo.month, 'Month'
18
+ assert_equal 11, stub.tempo.day, 'Day'
19
+ assert_equal 12, stub.tempo.hour, 'Hour'
20
+ assert_equal 13, stub.tempo.minute, 'Minute'
21
+ assert_equal 14, stub.tempo.second, 'Second'
22
+ end
23
+ end
24
+ end
25
+ end