eac_rails_utils 0.9.0 → 0.11.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper.rb +5 -1
  3. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder.rb +10 -2
  4. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/association_select_field.rb +1 -1
  5. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/common_text_fields.rb +1 -1
  6. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/currency_field.rb +1 -1
  7. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/date_field.rb +1 -1
  8. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/fields_for.rb +1 -1
  9. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/file_field.rb +1 -1
  10. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/radio_select_field.rb +1 -1
  11. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/searchable_association_field.rb +1 -1
  12. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/select_field.rb +3 -2
  13. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/time_field.rb +1 -1
  14. data/{lib/eac → app/helpers/eac_rails_utils}/common_form_helper/form_builder/year_month_field.rb +1 -1
  15. data/app/helpers/eac_rails_utils/data_table_helper.rb +13 -0
  16. data/{lib/eac → app/helpers/eac_rails_utils}/data_table_helper/column.rb +1 -1
  17. data/{lib/eac → app/helpers/eac_rails_utils}/data_table_helper/data_table.rb +2 -2
  18. data/{lib/eac → app/helpers/eac_rails_utils}/data_table_helper/setup.rb +2 -2
  19. data/app/helpers/eac_rails_utils/links_helper.rb +8 -1
  20. data/{lib/eac → app/helpers/eac_rails_utils}/menus_helper.rb +5 -1
  21. data/{lib/eac → app/helpers/eac_rails_utils}/menus_helper/bootstrap_gui_builder.rb +1 -1
  22. data/{lib/eac → app/helpers/eac_rails_utils}/menus_helper/data_builder.rb +1 -1
  23. data/{lib/eac → app/helpers/eac_rails_utils}/menus_helper/gui_builder.rb +1 -1
  24. data/{lib/eac → app/validators/eac_rails_utils}/cpf_validator.rb +1 -1
  25. data/{lib/eac → app/validators/eac_rails_utils}/no_presence_validator.rb +1 -1
  26. data/lib/assets/javascripts/jMenu.jquery.min.js +12 -12
  27. data/lib/eac_rails_utils.rb +2 -39
  28. data/lib/eac_rails_utils/engine.rb +2 -0
  29. data/lib/{eac → eac_rails_utils}/htmlbeautifier.rb +1 -1
  30. data/lib/eac_rails_utils/models.rb +9 -0
  31. data/lib/eac_rails_utils/models/attribute_required.rb +43 -0
  32. data/lib/eac_rails_utils/models/fetch_errors.rb +92 -0
  33. data/lib/eac_rails_utils/models/inequality_queries.rb +39 -0
  34. data/lib/eac_rails_utils/models/tableless.rb +97 -0
  35. data/lib/eac_rails_utils/models/test_utils.rb +65 -0
  36. data/lib/eac_rails_utils/patches.rb +9 -0
  37. data/lib/eac_rails_utils/patches/action_controller_base.rb +2 -0
  38. data/lib/eac_rails_utils/version.rb +1 -1
  39. data/test/dummy/app/models/user.rb +4 -0
  40. data/test/{lib/eac → helpers/eac_rails_utils}/common_form_helper_test.rb +2 -2
  41. data/test/{lib/eac → helpers/eac_rails_utils}/data_table_test_helper_test.rb +2 -2
  42. data/test/{app/helpers → helpers}/eac_rails_utils/formatter_helper_test.rb +0 -0
  43. data/test/lib/eac_rails_utils/{patches/model_attribute_required_test.rb → models/attribute_required_test.rb} +1 -0
  44. data/test/lib/eac_rails_utils/models/fetch_errors_test.rb +80 -0
  45. data/test/lib/eac_rails_utils/models/tableless_test.rb +25 -0
  46. data/test/{lib/eac → validators}/cpf_validator_test.rb +2 -2
  47. metadata +68 -89
  48. data/lib/eac/data_table_helper.rb +0 -13
  49. data/lib/eac/inequality_queries.rb +0 -37
  50. data/lib/eac/model.rb +0 -90
  51. data/lib/eac/test_utils.rb +0 -61
  52. data/lib/eac_rails_utils/patches/model_attribute_required.rb +0 -34
  53. data/lib/eac_rails_utils/patches/ofx_parser.rb +0 -43
  54. data/lib/eac_rails_utils/tableless_model.rb +0 -92
  55. data/test/lib/eac/model_test.rb +0 -78
  56. data/test/lib/eac/source_target_fixtures_test_files/a.source.html +0 -1
  57. data/test/lib/eac/source_target_fixtures_test_files/a.target.yaml +0 -1
  58. data/test/lib/eac/source_target_fixtures_test_files/b.source.html +0 -1
  59. data/test/lib/eac/source_target_fixtures_test_files/c.target.yaml +0 -1
  60. data/test/lib/eac_rails_utils/tableless_model_test.rb +0 -23
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_dependency 'eac/data_table_helper/column'
4
- require_dependency 'eac/data_table_helper/setup'
5
- require_dependency 'eac/data_table_helper/data_table'
6
-
7
- module Eac
8
- module DataTableHelper
9
- def data_table(dataset, &block)
10
- ::Eac::DataTableHelper::DataTable.new(self, dataset, &block).output
11
- end
12
- end
13
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eac
4
- # == Example:
5
- #
6
- # Note: model Product has a attribute "foo" Date, Time or Number:
7
- #
8
- # class Product
9
- # include ::Eac::InequalityQueries
10
- #
11
- # add_inequality_queries(:foo)
12
- # end
13
- #
14
- # This add the following scopes:
15
- #
16
- # Product.by_foo_gt(value) # Equivalent to Product.where("foo > ?", value)
17
- # Product.by_foo_gteq(value) # Equivalent to Product.where("foo >= ?", value)
18
- # Product.by_foo_lt(value) # Equivalent to Product.where("foo < ?", value)
19
- # Product.by_foo_lteq(value) # Equivalent to Product.where("foo <= ?", value)
20
- module InequalityQueries
21
- class << self
22
- def included(base)
23
- base.extend(ClassMethods)
24
- end
25
- end
26
-
27
- module ClassMethods
28
- def add_inequality_queries(attribute)
29
- %w[gt gteq lt lteq].each do |ineq|
30
- scope "by_#{attribute}_#{ineq}", lambda { |v|
31
- where(arel_table[attribute].send(ineq, v))
32
- }
33
- end
34
- end
35
- end
36
- end
37
- end
@@ -1,90 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eac
4
- module Model
5
- # Adiciona as mensagens de erro de record. As mensagens de uma coluna X em record
6
- # serão adicionadas na coluna X em self. Se options[:default_column] for especificado
7
- # as mensagens da coluna X de record em que X não existe em self serão adicionadas
8
- # na coluna options[:default_column].
9
- # Um array de colunas pode ser passado em options[:skip] de colunas em record que não
10
- # terão suas falhas adicionadas.
11
- def fetch_record_errors(record, options = {})
12
- record.errors.keys.each do |column|
13
- fetch_column_errors(record, column, column, options)
14
- end
15
- end
16
-
17
- # Similar a fetch_record_errors, mas torna possível especificar, através de mapping,
18
- # colunas-alvo em self com nomes diferentes das colunas-fonte em record.
19
- # mapping tem o formato { record_column => self_column }.
20
- def fetch_record_errors_by_mapping(record, mapping, options = {})
21
- mapping.each do |record_column, self_column|
22
- fetch_column_errors(record, record_column, self_column, options)
23
- end
24
- end
25
-
26
- def fetch_column_errors(record, record_column, self_column, options = {})
27
- return if options[:skip]&.include?(record_column)
28
-
29
- record.errors[record_column].each do |message|
30
- fetch_error_column_message(self_column, message, options[:default_column],
31
- "#{record.class.human_attribute_name(record_column)}: ")
32
- end
33
- end
34
-
35
- # Verifica se uma coluna existe.
36
- def column?(column)
37
- respond_to?(column) && respond_to?("#{column}=")
38
- end
39
-
40
- def save_or_raise
41
- raise "Falha ao tentar salvar #{self.class.name}: #{errors_to_string}" unless save
42
-
43
- self
44
- end
45
-
46
- private
47
-
48
- def fetch_error_column_message(column, message, default_column, default_column_message_prefix)
49
- build_self_columns_messages(column, message, default_column,
50
- default_column_message_prefix).each do |k, v|
51
- if column?(k)
52
- add_error_message(k, v)
53
- break
54
- end
55
- end
56
- end
57
-
58
- # Adiciona uma mensagem de erro a uma coluna somente se a coluna
59
- # ainda não a possui
60
- def add_error_message(column, message)
61
- return if errors[column].include?(message)
62
-
63
- errors.add(column, message)
64
- end
65
-
66
- # Produz uma lista de campos-mensagens, em ordem de preferência,
67
- # que podem receber uma mensagem de falha.
68
- def build_self_columns_messages(column, message, default_column,
69
- default_column_message_prefix)
70
- r = { column => message }
71
- m = /^(.+)_id$/.match(column)
72
- if m
73
- r[m[1]] = message
74
- else
75
- r["#{column}_id"] = message
76
- end
77
- r[default_column] = "#{default_column_message_prefix}#{message}" if default_column
78
- r
79
- end
80
-
81
- def errors_to_string
82
- b = ''
83
- errors.messages.each do |field, messages|
84
- b += ' / ' if b != ''
85
- b += field.to_s + ': ' + messages.to_s
86
- end
87
- b
88
- end
89
- end
90
- end
@@ -1,61 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Eac
4
- module TestUtils
5
- # Add more helper methods to be used by all tests here...
6
- def valid_invalid_column_values_test(record, column, valid_values, invalid_values)
7
- valid_values.each do |v|
8
- record.send("#{column}=", v)
9
- assert record.valid?, "#{record.errors.messages}, #{column} = #{v.inspect} should be valid"
10
- end
11
- invalid_values.each do |v|
12
- record.send("#{column}=", v)
13
- assert_not record.valid?, "#{column} = #{v.inspect} should be invalid"
14
- end
15
- end
16
-
17
- # Verifica falhas em campos específicos de um record
18
- def assert_record_errors(record, fields_without_error, fields_with_error)
19
- fields_without_error.each do |c|
20
- assert record.errors[c].empty?, "Column: #{c} should not have errors (#{record.errors[c]})"
21
- end
22
- fields_with_error. each do |c|
23
- assert_not record.errors[c].empty?, "Column: #{c} should have errors"
24
- end
25
- end
26
-
27
- # Verifica, campo por campo, se invalida o registro.
28
- def assert_column_changes(ppp, expected_valid_result, changes)
29
- changes.each do |k, v|
30
- ppp.send("#{k}=", v)
31
- assert_equal expected_valid_result, ppp.valid?,
32
- "\"#{k}\" change should be " + (expected_valid_result ? 'valid' : 'invalid')
33
- assert_not ppp.errors[k].empty? unless expected_valid_result
34
- ppp.restore_attributes
35
- end
36
- end
37
-
38
- # Ex.: attrs = {a: 1, b: 2} resulta em
39
- # [{a: nil, b: nil}, {a: 1, b: nil}, {a: nil, b: 2}, {a: 1, b: 2}].
40
- def all_combinations(attrs)
41
- combs = [{}]
42
- attrs.each do |attr_name, value|
43
- combs = all_combinations_new_combination(attr_name, value, combs)
44
- end
45
- combs
46
- end
47
-
48
- def all_combinations_new_combination(attr_name, value, combs)
49
- new_comb = []
50
- assert_not value.nil?, "#{attr_name}=#{value}"
51
- [nil, value].each do |vv|
52
- combs.each do |c|
53
- cc = c.dup
54
- cc[attr_name] = vv
55
- new_comb << cc
56
- end
57
- end
58
- new_comb
59
- end
60
- end
61
- end
@@ -1,34 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'active_record'
4
-
5
- module EacRailsUtils
6
- module Patches
7
- module ModelAttributeRequired
8
- def self.included(base)
9
- base.extend ClassMethods
10
- base.include InstanceMethods
11
- end
12
-
13
- module ClassMethods
14
- def column_required?(column)
15
- m = new
16
- m.validate
17
- m.errors.key?(column.to_sym)
18
- end
19
- end
20
-
21
- module InstanceMethods
22
- def attribute_required?(column)
23
- self.class.column_required?(column)
24
- end
25
- end
26
- end
27
- end
28
- end
29
-
30
- [::ActiveRecord::Base, ::ActiveModel::Model].each do |c|
31
- next if c.included_modules.include? ::EacRailsUtils::Patches::ModelAttributeRequired
32
-
33
- c.include ::EacRailsUtils::Patches::ModelAttributeRequired
34
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'ofx-parser'
4
-
5
- module EacRailsUtils
6
- module Patches
7
- module OfxParser
8
- module OfxParser
9
- def self.included(base)
10
- base.class_eval do
11
- class << self
12
- prepend ClassMethods
13
- end
14
- end
15
- end
16
-
17
- module ClassMethods
18
- def build_transaction(transaction)
19
- r = super
20
- r.currate = (transaction / 'CURRENCY/CURRATE').inner_text
21
- r
22
- end
23
- end
24
- end
25
-
26
- module Transaction
27
- attr_accessor :currate, :cursym
28
- end
29
- end
30
- end
31
- end
32
-
33
- unless ::OfxParser::OfxParser.included_modules.include?(
34
- ::EacRailsUtils::Patches::OfxParser::OfxParser
35
- )
36
- ::OfxParser::OfxParser.include ::EacRailsUtils::Patches::OfxParser::OfxParser
37
- end
38
-
39
- unless ::OfxParser::Transaction.included_modules.include?(
40
- ::EacRailsUtils::Patches::OfxParser::Transaction
41
- )
42
- ::OfxParser::Transaction.include ::EacRailsUtils::Patches::OfxParser::Transaction
43
- end
@@ -1,92 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module EacRailsUtils
4
- class TablelessModel
5
- include ActiveModel::Model
6
- include Virtus.model
7
- include ActiveModel::Associations
8
-
9
- def initialize(values = {})
10
- super(build_attributes(values))
11
- end
12
-
13
- def attributes=(values)
14
- super(build_attributes(values))
15
- end
16
-
17
- # need hash like accessor, used internal Rails
18
- def [](attr)
19
- send(attr)
20
- end
21
-
22
- # need hash like accessor, used internal Rails
23
- def []=(attr, value)
24
- send("#{attr}=", value)
25
- end
26
-
27
- def save!
28
- save || raise("#{self.class}.save failed: #{errors.messages}")
29
- end
30
-
31
- private
32
-
33
- def build_attributes(values)
34
- AttributesBuilder.new(self.class, values).to_attributes
35
- end
36
-
37
- class AttributesBuilder
38
- DATE_TIME_FIELDS = %i[year month day hour min sec].freeze
39
-
40
- def initialize(model_class, values)
41
- @model_class = model_class
42
- @values = {}
43
- values.each { |k, v| add(k, v) }
44
- end
45
-
46
- def to_attributes
47
- @values
48
- end
49
-
50
- private
51
-
52
- def add(key, value)
53
- array_attr = parse_array_attr_key(key)
54
- if array_attr
55
- array_value_set(array_attr, value)
56
- else
57
- @values[key] = value
58
- end
59
- end
60
-
61
- def parse_array_attr_key(key)
62
- m = /\A(.+)\(([0-9]+)(.)\)\z/.match(key)
63
- return unless m
64
-
65
- ::OpenStruct.new(key: m[1], index: m[2].to_i - 1, converter: array_value_converter(m[3]))
66
- end
67
-
68
- def array_value_set(array_attr, value)
69
- @values[array_attr.key] ||= {}
70
- @values[array_attr.key].merge!(
71
- DATE_TIME_FIELDS[array_attr.index] => value.send(array_attr.converter)
72
- )
73
- end
74
-
75
- def array_value_converter(str_type)
76
- case str_type
77
- when 'i'
78
- 'to_i'
79
- else
80
- raise "Unknown array type: \"#{str_type}\""
81
- end
82
- end
83
-
84
- def date_time_attribute?(key)
85
- attr = @model_class.attributes[key]
86
- return false unless attr
87
-
88
- raise attr.to_s
89
- end
90
- end
91
- end
92
- end
@@ -1,78 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- module Eac
6
- class ModelTest < ActiveSupport::TestCase
7
- class M1
8
- include ActiveModel::Model
9
- include Eac::Model
10
- attr_accessor :name, :age, :account, :country_id, :other
11
- end
12
-
13
- class M2
14
- include ActiveModel::Model
15
- attr_accessor :name, :myage, :account_id, :country
16
-
17
- validate :my_validate
18
-
19
- def my_validate
20
- errors.add(:name, 'NAME_ERROR')
21
- errors.add(:myage, 'MYAGE_ERROR')
22
- errors.add(:account_id, 'ACCOUNT_ID_ERROR')
23
- errors.add(:country, 'COUNTRY_ERROR')
24
- end
25
- end
26
-
27
- def setup
28
- reset_m1
29
- reset_m2
30
- end
31
-
32
- def test_fetch_column_errors
33
- @m1.fetch_column_errors(@m2, :myage, :age)
34
- assert_equal @m2.errors[:myage], @m1.errors[:age]
35
- end
36
-
37
- def test_fetch_record_errors
38
- @m1.fetch_record_errors(@m2)
39
- { name: :name, account_id: :account, country: :country_id }.each do |c2, c1|
40
- assert_equal @m2.errors[c2], @m1.errors[c1], "c2: #{c2}, c1: #{c1}"
41
- end
42
- assert @m1.errors[:age].empty?
43
- end
44
-
45
- def test_fetch_record_errors_with_default_column
46
- @m1.fetch_record_errors(@m2, default_column: :other)
47
- assert_equal @m2.errors[:name], @m1.errors[:name]
48
- assert @m1.errors[:age].empty?
49
- assert_equal ['Myage: MYAGE_ERROR'], @m1.errors[:other]
50
- end
51
-
52
- def test_fetch_record_errors_with_skip_option
53
- @m1.fetch_record_errors(@m2, skip: [:name])
54
- assert @m1.errors[:name].empty?
55
- assert @m1.errors[:age].empty?
56
- end
57
-
58
- def test_fetch_record_errors_by_mapping
59
- @m1.fetch_record_errors_by_mapping(@m2, name: :name, myage: :age)
60
- assert_equal @m2.errors[:name], @m1.errors[:name]
61
- assert_equal @m2.errors[:myage], @m1.errors[:age]
62
- end
63
-
64
- private
65
-
66
- def reset_m1
67
- @m1 = M1.new
68
- assert @m1.errors.empty?
69
- end
70
-
71
- def reset_m2
72
- @m2 = M2.new
73
- assert_not @m2.valid?
74
- assert_not @m2.errors[:name].empty?
75
- assert_not @m2.errors[:myage].empty?
76
- end
77
- end
78
- end