eac_rails_utils 0.14.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 7137f7331f9f361c5371306a1c2c607495dd3897bef11c887aad02535814c312
         | 
| 4 | 
            +
              data.tar.gz: b226cce4f9f3dc400a97426fa13a0d00d5fa2d157fd48bbda07873218832bd45
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 51cab365aec88b22c1a4765e3537f8b661eff6e06290210b5cd79e4d18f2a07a8de3c7ee059eb5b0809e71359c9c855cbee406f0d94c7a8381de78f45dbdebeb
         | 
| 7 | 
            +
              data.tar.gz: b2e42d61fbf1ed64f5297a7762e0bde11522682c12d02e4d9ca864d3000def5d037d512134bbae2ad9e998f4a3567f5326fb81db29e3fe5a9870b1f1926266ce
         | 
    
        data/app/helpers/eac_rails_utils/common_form_helper/form_builder/association_select_field.rb
    CHANGED
    
    | @@ -3,22 +3,51 @@ | |
| 3 3 | 
             
            module EacRailsUtils
         | 
| 4 4 | 
             
              module CommonFormHelper
         | 
| 5 5 | 
             
                class FormBuilder
         | 
| 6 | 
            -
                   | 
| 7 | 
            -
                     | 
| 8 | 
            -
                      options = options.dup
         | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
                       | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 6 | 
            +
                  class AssociationSelectField
         | 
| 7 | 
            +
                    common_constructor :builder, :field_name, :options, default: [{}] do
         | 
| 8 | 
            +
                      self.options = options.dup
         | 
| 9 | 
            +
                    end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                    delegate :model_instance, to: :builder
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                    def collection
         | 
| 14 | 
            +
                      extract_association_key(:collection, *(
         | 
| 15 | 
            +
                          ::Rails.version < '5' ? [:all] : %i[klass all]
         | 
| 16 | 
            +
                        ))
         | 
| 17 | 
            +
                    end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                    def foreign_key
         | 
| 20 | 
            +
                      extract_association_key(:foreign_key, (
         | 
| 21 | 
            +
                          ::Rails.version < '5' ? :association_foreign_key : :join_foreign_key
         | 
| 22 | 
            +
                        ))
         | 
| 23 | 
            +
                    end
         | 
| 24 | 
            +
             | 
| 25 | 
            +
                    def methods
         | 
| 26 | 
            +
                      extract_methods(options)
         | 
| 27 | 
            +
                    end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                    def output
         | 
| 30 | 
            +
                      builder.send(:field, field_name, options) do
         | 
| 31 | 
            +
                        builder.form.collection_select(foreign_key, collection, methods[:value], methods[:text],
         | 
| 32 | 
            +
                                                       select_options, class: 'form-control')
         | 
| 17 33 | 
             
                      end
         | 
| 18 34 | 
             
                    end
         | 
| 19 35 |  | 
| 36 | 
            +
                    def select_options
         | 
| 37 | 
            +
                      extract_select_options(options)
         | 
| 38 | 
            +
                    end
         | 
| 39 | 
            +
             | 
| 20 40 | 
             
                    private
         | 
| 21 41 |  | 
| 42 | 
            +
                    def association_reflection
         | 
| 43 | 
            +
                      if model_instance.class.respond_to?(:reflect_on_association)
         | 
| 44 | 
            +
                        model_instance.class.reflect_on_association(field_name)
         | 
| 45 | 
            +
                      else
         | 
| 46 | 
            +
                        raise "#{model_instance.class} não possui um método \"reflect_on_association\". " \
         | 
| 47 | 
            +
                          "Defina explicitamente a opção :#{key}"
         | 
| 48 | 
            +
                      end
         | 
| 49 | 
            +
                    end
         | 
| 50 | 
            +
             | 
| 22 51 | 
             
                    def extract_methods(options)
         | 
| 23 52 | 
             
                      { value: options.delete(:value_method) || :id, text:
         | 
| 24 53 | 
             
                          options.delete(:text_method) || :to_s }
         | 
| @@ -28,14 +57,12 @@ module EacRailsUtils | |
| 28 57 | 
             
                      options.extract!(:prompt, :include_blank)
         | 
| 29 58 | 
             
                    end
         | 
| 30 59 |  | 
| 31 | 
            -
                    def extract_association_key( | 
| 32 | 
            -
                       | 
| 33 | 
            -
             | 
| 34 | 
            -
             | 
| 60 | 
            +
                    def extract_association_key(key, *methods)
         | 
| 61 | 
            +
                      if options.key?(key)
         | 
| 62 | 
            +
                        options.fetch(key)
         | 
| 63 | 
            +
                      else
         | 
| 64 | 
            +
                        methods.inject(association_reflection) { |a, e| a.send(e) }
         | 
| 35 65 | 
             
                      end
         | 
| 36 | 
            -
             | 
| 37 | 
            -
                      raise "#{model_instance.class} não possui um método \"reflect_on_association\". " \
         | 
| 38 | 
            -
                        "Defina explicitamente a opção :#{key}"
         | 
| 39 66 | 
             
                    end
         | 
| 40 67 | 
             
                  end
         | 
| 41 68 | 
             
                end
         | 
| @@ -8,7 +8,6 @@ module EacRailsUtils | |
| 8 8 | 
             
                class FormBuilder
         | 
| 9 9 | 
             
                  ::EacRubyUtils.require_sub __FILE__
         | 
| 10 10 |  | 
| 11 | 
            -
                  include AssociationSelectField
         | 
| 12 11 | 
             
                  include CommonTextFields
         | 
| 13 12 | 
             
                  include CurrencyField
         | 
| 14 13 | 
             
                  include DateField
         | 
| @@ -27,6 +26,11 @@ module EacRailsUtils | |
| 27 26 | 
             
                    @field_errors_showed = Set.new
         | 
| 28 27 | 
             
                  end
         | 
| 29 28 |  | 
| 29 | 
            +
                  def association_select_field(field_name, options = {})
         | 
| 30 | 
            +
                    ::EacRailsUtils::CommonFormHelper::FormBuilder::AssociationSelectField
         | 
| 31 | 
            +
                      .new(self, field_name, options).output
         | 
| 32 | 
            +
                  end
         | 
| 33 | 
            +
             | 
| 30 34 | 
             
                  def model_instance
         | 
| 31 35 | 
             
                    form.object
         | 
| 32 36 | 
             
                  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.14. | 
| 4 | 
            +
              version: 0.14.1
         | 
| 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: 2021- | 
| 11 | 
            +
            date: 2021-12-23 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: activemodel-associations
         |