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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5d1603f1c60e9d64e4d2acc9e399fbef95660bde8e737a4c739481b6455733c
4
- data.tar.gz: baa588fd3f6095129486e021671f8a934d6d6728e1bbea4a2742b375dd8b5572
3
+ metadata.gz: 68b43bfb10790f9960831364c2ba228b22131e3f1fa24f84d7074ba784ff580d
4
+ data.tar.gz: 6bb8f25d260a942086d89c4f33ace3888ec99605d38b72c6ba023529b759bfd5
5
5
  SHA512:
6
- metadata.gz: d9c2b12877f921a3fa1c5cf23a36a0b6dae7e048e1a6814cc062c3238d3abad9e14e714f82d1415b3a706fe4f9c1bbcd10da0274ce28acc90bdb7cb09b0b0ba4
7
- data.tar.gz: 6e83568342b4b9257d3c2c87959256b3c728e8e259b17991f71709a1ed10e80a42d9d4e1b4a7b21231a285dcb2013cc45ef7e609d32eb0d035edb7e99fa6124c
6
+ metadata.gz: e51d8cb33ff34bf9698bb99f767d7e6adb69b2b2255a5cd787f5e1febd009bd25df5f0399e3a5e4dd031820580ca8a2942edacfd68a980c88ca94019ed9851de
7
+ data.tar.gz: 6353e5e12d10396ed143717e2b18c6844ffc192215e28dd3fc1da498ee7af6b18593097dec712a714d09914e23ffe4d5375a3db2c47df3984e4ef5ee0d50c731
@@ -1,7 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ require 'eac_ruby_utils/require_sub'
4
+
5
+ module EacRailsUtils
4
6
  module CommonFormHelper
7
+ ::EacRubyUtils.require_sub __FILE__
8
+
5
9
  def common_form(model_instance, options = {}, &block)
6
10
  submit_label = options.delete(:submit_label)
7
11
  options[:html] ||= {}
@@ -1,8 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ require 'eac_ruby_utils/require_sub'
4
+ require 'eac_rails_utils/models/attribute_required'
5
+
6
+ module EacRailsUtils
4
7
  module CommonFormHelper
5
8
  class FormBuilder
9
+ ::EacRubyUtils.require_sub __FILE__
10
+
6
11
  include AssociationSelectField
7
12
  include CommonTextFields
8
13
  include CurrencyField
@@ -87,7 +92,10 @@ module Eac
87
92
  end
88
93
 
89
94
  def field_label(field_name, label, required)
90
- required = model_instance.attribute_required?(field_name) if required.nil?
95
+ if required.nil?
96
+ required = ::EacRailsUtils::Models::AttributeRequired
97
+ .required?(model_instance, field_name)
98
+ end
91
99
  @form.label(field_name, label, class: required ? 'required' : 'optional')
92
100
  end
93
101
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module AssociationSelectField
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module CommonTextFields
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module CurrencyField
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module DateField
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module FieldsFor
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module FileField
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module RadioSelectField
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  class SearchableAssociationField
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module SelectField
7
7
  def select_field(field_name, options = {})
8
8
  options = options.dup
9
9
  field(field_name, options) do
10
- form.select(field_name, select_options(field_name, options))
10
+ form.select(field_name, select_options(field_name, options),
11
+ options, options[:html] || {})
11
12
  end
12
13
  end
13
14
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module TimeField
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module CommonFormHelper
5
5
  class FormBuilder
6
6
  module YearMonthField
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+
5
+ module EacRailsUtils
6
+ module DataTableHelper
7
+ ::EacRubyUtils.require_sub __FILE__
8
+
9
+ def data_table(dataset, &block)
10
+ ::EacRailsUtils::DataTableHelper::DataTable.new(self, dataset, &block).output
11
+ end
12
+ end
13
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module DataTableHelper
5
5
  class Column
6
6
  EMPTY_VALUE = '-'
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module DataTableHelper
5
5
  class DataTable
6
6
  def initialize(view, dataset)
7
7
  @view = view
8
8
  @dataset = dataset
9
- @setup = ::Eac::DataTableHelper::Setup.new
9
+ @setup = ::EacRailsUtils::DataTableHelper::Setup.new
10
10
  yield(@setup)
11
11
  end
12
12
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module DataTableHelper
5
5
  class Setup
6
6
  attr_reader :columns
@@ -12,7 +12,7 @@ module Eac
12
12
  end
13
13
 
14
14
  def column(label, path = nil, &block)
15
- @columns << ::Eac::DataTableHelper::Column.new(label, path, block)
15
+ @columns << ::EacRailsUtils::DataTableHelper::Column.new(label, path, block)
16
16
  end
17
17
  end
18
18
  end
@@ -35,7 +35,7 @@ module EacRailsUtils
35
35
  end
36
36
 
37
37
  def object_path(object, action = nil)
38
- current_class = object.class
38
+ current_class = object_class(object)
39
39
  tried_paths = []
40
40
  while current_class
41
41
  path = object_path_by_class(current_class, action)
@@ -68,5 +68,12 @@ module EacRailsUtils
68
68
  path = "#{action}_#{path}" if action.present?
69
69
  path
70
70
  end
71
+
72
+ def object_class(object)
73
+ return object.entity_class if object.respond_to?(:entity_class)
74
+ return object.__getobj__.class if object.respond_to?(:__getobj__)
75
+
76
+ object.class
77
+ end
71
78
  end
72
79
  end
@@ -1,7 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ require 'eac_ruby_utils/require_sub'
4
+
5
+ module EacRailsUtils
4
6
  module MenusHelper
7
+ ::EacRubyUtils.require_sub __FILE__
8
+
5
9
  def dropdown_menu(entries)
6
10
  entries = DataBuilder.new(self).build(entries)
7
11
  return nil unless entries
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module MenusHelper
5
5
  # http://bootsnipp.com/snippets/featured/multi-level-dropdown-menu-bs3
6
6
  class BootstrapGuiBuilder
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module MenusHelper
5
5
  class DataBuilder
6
6
  def initialize(view)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  module MenusHelper
5
5
  class GuiBuilder
6
6
  def initialize(view)
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  class CpfValidator < ActiveModel::EachValidator
5
5
  def validate_each(record, attribute, value)
6
6
  return if Cpf.new(value).valid?
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Eac
3
+ module EacRailsUtils
4
4
  # https://stackoverflow.com/questions/10070786/rails-3-validation-presence-false
5
5
  class NoPresenceValidator < ActiveModel::EachValidator
6
6
  def validate_each(record, attribute, _value)
@@ -1,12 +1,12 @@
1
- /************************************************************************
2
- *************************************************************************
3
- @Name : jMenu - jQuery Plugin
4
- @Revison : 2.0
5
- @Date : 08/2013
6
- @Author : ALPIXEL - (www.myjqueryplugins.com - www.alpixel.fr)
7
- @Support : FF, IE7, IE8, MAC Firefox, MAC Safari
8
- @License : Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
9
-
10
- **************************************************************************
11
- *************************************************************************/
12
- (function(e){e.jMenu={defaults:{ulWidth:"auto",absoluteTop:33,absoluteLeft:0,TimeBeforeOpening:100,TimeBeforeClosing:100,animatedText:true,paddingLeft:7,openClick:false,effects:{effectSpeedOpen:150,effectSpeedClose:150,effectTypeOpen:"slide",effectTypeClose:"slide",effectOpen:"swing",effectClose:"swing"}},init:function(t){opts=e.extend({},e.jMenu.defaults,t);if(opts.ulWidth=="auto")$width=e(".fNiv").outerWidth(false);else $width=opts.ulWidth;e(".jMenu li").each(function(){var t=e(this).find("a:first"),n=e(this).find("ul");if(e.jMenu._IsParent(t)){t.addClass("isParent");var r=t.next(),i=t.position();if(e(this).hasClass("jmenu-level-0"))r.css({top:i.top+opts.absoluteTop,left:i.left+opts.absoluteLeft,width:$width});else r.css({top:i.top,left:i.left+$width,width:$width});if(!opts.openClick)e(this).bind({mouseenter:function(){if(e(this).hasClass("jmenu-level-0")){i=e(this).position();r.css({left:i.left+opts.absoluteLeft,top:i.top+opts.absoluteTop})}e.jMenu._show(r)},mouseleave:function(){e.jMenu._closeList(r)}});else e(this).bind({click:function(t){t.preventDefault();e.jMenu._show(r)},mouseleave:function(){e.jMenu._closeList(r)}})}})},_show:function(e){switch(opts.effects.effectTypeOpen){case"slide":e.stop(true,true).delay(opts.TimeBeforeOpening).slideDown(opts.effects.effectSpeedOpen,opts.effects.effectOpen);break;case"fade":e.stop(true,true).delay(opts.TimeBeforeOpening).fadeIn(opts.effects.effectSpeedOpen,opts.effects.effectOpen);break;default:e.stop(true,true).delay(opts.TimeBeforeOpening).show()}},_closeList:function(e){switch(opts.effects.effectTypeClose){case"slide":e.stop(true,true).delay(opts.TimeBeforeClosing).slideUp(opts.effects.effectSpeedClose,opts.effects.effectClose);break;case"fade":e.stop(true,true).delay(opts.TimeBeforeClosing).fadeOut(opts.effects.effectSpeedClose,opts.effects.effectClose);break;default:e.delay(opts.TimeBeforeClosing).hide()}},_animateText:function(t){var n=parseInt(t.css("padding-left"));t.hover(function(){e(this).stop(true,false).animate({paddingLeft:n+opts.paddingLeft},100)},function(){e(this).stop(true,false).animate({paddingLeft:n},100)})},_IsParent:function(e){if(e.next().is("ul"))return true;else return false},_isReadable:function(){if(e(".jmenu-level-0").length>0)return true;else return false},_error:function(){alert("jMenu plugin can't be initialized. Please, check you have the '.jmenu-level-0' class on your first level <li> elements.")}};jQuery.fn.jMenu=function(t){e(this).addClass("jMenu");e(this).children("li").addClass("jmenu-level-0").children("a").addClass("fNiv");if(e.jMenu._isReadable()){e.jMenu.init(t)}else{e.jMenu._error()}}})(jQuery)
1
+ /************************************************************************
2
+ *************************************************************************
3
+ @Name : jMenu - jQuery Plugin
4
+ @Revison : 2.0
5
+ @Date : 08/2013
6
+ @Author : ALPIXEL - (www.myjqueryplugins.com - www.alpixel.fr)
7
+ @Support : FF, IE7, IE8, MAC Firefox, MAC Safari
8
+ @License : Open Source - MIT License : http://www.opensource.org/licenses/mit-license.php
9
+
10
+ **************************************************************************
11
+ *************************************************************************/
12
+ (function(e){e.jMenu={defaults:{ulWidth:"auto",absoluteTop:33,absoluteLeft:0,TimeBeforeOpening:100,TimeBeforeClosing:100,animatedText:true,paddingLeft:7,openClick:false,effects:{effectSpeedOpen:150,effectSpeedClose:150,effectTypeOpen:"slide",effectTypeClose:"slide",effectOpen:"swing",effectClose:"swing"}},init:function(t){opts=e.extend({},e.jMenu.defaults,t);if(opts.ulWidth=="auto")$width=e(".fNiv").outerWidth(false);else $width=opts.ulWidth;e(".jMenu li").each(function(){var t=e(this).find("a:first"),n=e(this).find("ul");if(e.jMenu._IsParent(t)){t.addClass("isParent");var r=t.next(),i=t.position();if(e(this).hasClass("jmenu-level-0"))r.css({top:i.top+opts.absoluteTop,left:i.left+opts.absoluteLeft,width:$width});else r.css({top:i.top,left:i.left+$width,width:$width});if(!opts.openClick)e(this).bind({mouseenter:function(){if(e(this).hasClass("jmenu-level-0")){i=e(this).position();r.css({left:i.left+opts.absoluteLeft,top:i.top+opts.absoluteTop})}e.jMenu._show(r)},mouseleave:function(){e.jMenu._closeList(r)}});else e(this).bind({click:function(t){t.preventDefault();e.jMenu._show(r)},mouseleave:function(){e.jMenu._closeList(r)}})}})},_show:function(e){switch(opts.effects.effectTypeOpen){case"slide":e.stop(true,true).delay(opts.TimeBeforeOpening).slideDown(opts.effects.effectSpeedOpen,opts.effects.effectOpen);break;case"fade":e.stop(true,true).delay(opts.TimeBeforeOpening).fadeIn(opts.effects.effectSpeedOpen,opts.effects.effectOpen);break;default:e.stop(true,true).delay(opts.TimeBeforeOpening).show()}},_closeList:function(e){switch(opts.effects.effectTypeClose){case"slide":e.stop(true,true).delay(opts.TimeBeforeClosing).slideUp(opts.effects.effectSpeedClose,opts.effects.effectClose);break;case"fade":e.stop(true,true).delay(opts.TimeBeforeClosing).fadeOut(opts.effects.effectSpeedClose,opts.effects.effectClose);break;default:e.delay(opts.TimeBeforeClosing).hide()}},_animateText:function(t){var n=parseInt(t.css("padding-left"));t.hover(function(){e(this).stop(true,false).animate({paddingLeft:n+opts.paddingLeft},100)},function(){e(this).stop(true,false).animate({paddingLeft:n},100)})},_IsParent:function(e){if(e.next().is("ul"))return true;else return false},_isReadable:function(){if(e(".jmenu-level-0").length>0)return true;else return false},_error:function(){alert("jMenu plugin can't be initialized. Please, check you have the '.jmenu-level-0' class on your first level <li> elements.")}};jQuery.fn.jMenu=function(t){e(this).addClass("jMenu");e(this).children("li").addClass("jmenu-level-0").children("a").addClass("fNiv");if(e.jMenu._isReadable()){e.jMenu.init(t)}else{e.jMenu._error()}}})(jQuery)
@@ -1,44 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'active_support/dependencies'
3
+ require 'eac_ruby_utils/require_sub'
4
4
 
5
5
  module EacRailsUtils
6
- require 'activemodel/associations'
7
- require 'nested_form_fields'
8
- require 'ofx-parser'
9
- require 'virtus'
10
-
11
- require 'eac_rails_utils/patches/action_controller_base'
12
- require 'eac_rails_utils/patches/model_attribute_required'
13
- require 'eac_rails_utils/patches/ofx_parser'
14
- require 'eac_rails_utils/engine'
15
- require 'eac_rails_utils/tableless_model'
16
- require 'eac/cpf_validator'
17
- require 'eac/common_form_helper/form_builder/association_select_field'
18
- require 'eac/common_form_helper/form_builder/common_text_fields'
19
- require 'eac/common_form_helper/form_builder/currency_field'
20
- require 'eac/common_form_helper/form_builder/date_field'
21
- require 'eac/common_form_helper/form_builder/fields_for'
22
- require 'eac/common_form_helper/form_builder/file_field'
23
- require 'eac/common_form_helper/form_builder/radio_select_field'
24
- require 'eac/common_form_helper/form_builder/searchable_association_field'
25
- require 'eac/common_form_helper/form_builder/select_field'
26
- require 'eac/common_form_helper/form_builder/time_field'
27
- require 'eac/common_form_helper/form_builder/year_month_field'
28
- require 'eac/common_form_helper/form_builder'
29
- require 'eac/common_form_helper'
30
- require 'eac/data_table_helper'
31
- require 'eac/htmlbeautifier'
32
- require 'eac/inequality_queries'
33
- require 'eac/menus_helper'
34
- require 'eac/menus_helper/bootstrap_gui_builder'
35
- require 'eac/menus_helper/data_builder'
36
- require 'eac/menus_helper/gui_builder'
37
- require 'eac/model'
38
- require 'eac/no_presence_validator'
39
- require 'eac/test_utils'
40
-
41
- ActionView::Base.include Eac::CommonFormHelper
42
- ActionView::Base.include Eac::DataTableHelper
43
- ActionView::Base.include Eac::MenusHelper
6
+ ::EacRubyUtils.require_sub __FILE__
44
7
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'nested_form_fields'
4
+
3
5
  module EacRailsUtils
4
6
  class Engine < ::Rails::Engine; end
5
7
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'htmlbeautifier'
4
4
 
5
- module Eac
5
+ module EacRailsUtils
6
6
  class Htmlbeautifier
7
7
  def self.beautify(string)
8
8
  ::HtmlBeautifier.beautify(string, tab_stops: 2) + "\n"
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/require_sub'
4
+
5
+ module EacRailsUtils
6
+ module Models
7
+ ::EacRubyUtils.require_sub __FILE__
8
+ end
9
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
5
+ module EacRailsUtils
6
+ module Models
7
+ module AttributeRequired
8
+ extend ::ActiveSupport::Concern
9
+
10
+ class << self
11
+ def required?(model_class_or_record, attribute)
12
+ model = model_class_or_record_to_model(model_class_or_record)
13
+ record = model.new
14
+ record.validate
15
+ record.errors.key?(attribute.to_sym)
16
+ end
17
+
18
+ private
19
+
20
+ def model_class_or_record_to_model(model_class_or_record)
21
+ model_class_or_record.is_a?(::Class) ? model_class_or_record : model_class_or_record.class
22
+ end
23
+ end
24
+
25
+ included do
26
+ extend ClassMethods
27
+ include InstanceMethods
28
+ end
29
+
30
+ module ClassMethods
31
+ def column_required?(column)
32
+ ::EacRailsUtils::Models::AttributeRequired.required?(self, column)
33
+ end
34
+ end
35
+
36
+ module InstanceMethods
37
+ def attribute_required?(column)
38
+ self.class.column_required?(column)
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EacRailsUtils
4
+ module Models
5
+ module FetchErrors
6
+ # Adiciona as mensagens de erro de record. As mensagens de uma coluna X em record
7
+ # serão adicionadas na coluna X em self. Se options[:default_column] for especificado
8
+ # as mensagens da coluna X de record em que X não existe em self serão adicionadas
9
+ # na coluna options[:default_column].
10
+ # Um array de colunas pode ser passado em options[:skip] de colunas em record que não
11
+ # terão suas falhas adicionadas.
12
+ def fetch_record_errors(record, options = {})
13
+ record.errors.keys.each do |column|
14
+ fetch_column_errors(record, column, column, options)
15
+ end
16
+ end
17
+
18
+ # Similar a fetch_record_errors, mas torna possível especificar, através de mapping,
19
+ # colunas-alvo em self com nomes diferentes das colunas-fonte em record.
20
+ # mapping tem o formato { record_column => self_column }.
21
+ def fetch_record_errors_by_mapping(record, mapping, options = {})
22
+ mapping.each do |record_column, self_column|
23
+ fetch_column_errors(record, record_column, self_column, options)
24
+ end
25
+ end
26
+
27
+ def fetch_column_errors(record, record_column, self_column, options = {})
28
+ return if options[:skip]&.include?(record_column)
29
+
30
+ record.errors[record_column].each do |message|
31
+ fetch_error_column_message(self_column, message, options[:default_column],
32
+ "#{record.class.human_attribute_name(record_column)}: ")
33
+ end
34
+ end
35
+
36
+ # Verifica se uma coluna existe.
37
+ def column?(column)
38
+ respond_to?(column) && respond_to?("#{column}=")
39
+ end
40
+
41
+ def save_or_raise
42
+ raise "Falha ao tentar salvar #{self.class.name}: #{errors_to_string}" unless save
43
+
44
+ self
45
+ end
46
+
47
+ private
48
+
49
+ def fetch_error_column_message(column, message, default_column, default_column_message_prefix)
50
+ build_self_columns_messages(column, message, default_column,
51
+ default_column_message_prefix).each do |k, v|
52
+ if column?(k)
53
+ add_error_message(k, v)
54
+ break
55
+ end
56
+ end
57
+ end
58
+
59
+ # Adiciona uma mensagem de erro a uma coluna somente se a coluna
60
+ # ainda não a possui
61
+ def add_error_message(column, message)
62
+ return if errors[column].include?(message)
63
+
64
+ errors.add(column, message)
65
+ end
66
+
67
+ # Produz uma lista de campos-mensagens, em ordem de preferência,
68
+ # que podem receber uma mensagem de falha.
69
+ def build_self_columns_messages(column, message, default_column,
70
+ default_column_message_prefix)
71
+ r = { column => message }
72
+ m = /^(.+)_id$/.match(column)
73
+ if m
74
+ r[m[1]] = message
75
+ else
76
+ r["#{column}_id"] = message
77
+ end
78
+ r[default_column] = "#{default_column_message_prefix}#{message}" if default_column
79
+ r
80
+ end
81
+
82
+ def errors_to_string
83
+ b = ''
84
+ errors.messages.each do |field, messages|
85
+ b += ' / ' if b != ''
86
+ b += field.to_s + ': ' + messages.to_s
87
+ end
88
+ b
89
+ end
90
+ end
91
+ end
92
+ end