Sha256: 5581fd0ff77118d6270ea51288963995f62700a484d65af6442bdbbdd8c9d28b

Contents?: true

Size: 1.03 KB

Versions: 6

Compression:

Stored size: 1.03 KB

Contents

require 'active_support/concern'

module ActForm
  module Combinable
    extend ActiveSupport::Concern

    included do
      class_attribute :_forms, instance_writer: false
      self._forms = []
    end

    def valid?(context = nil)
      super
      combined_forms_valid?(context)
      errors.empty?
    end

    def combined_forms_valid?(context)
      return if _forms.empty?
      _forms.each do |form_class|
        form = form_class.new(attributes)
        form.valid?(context)
        form.errors.details.each do |attr_name, arr|
          arr.each do |error|
            next if error[:error] == :required
            errors.add(attr_name, error[:error])
          end
        end
      end
    end

    class_methods do

      def combine(*forms)
        forms.each do |form_class|
          raise ArgumentError, "can't combine itself" if form_class == self

          next if self._forms.include?(form_class)

          self.merge_attribute_set_from(form_class)
          self._forms << form_class
        end
      end

    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
act_form-0.4.1 lib/act_form/combinable.rb
act_form-0.4.0 lib/act_form/combinable.rb
act_form-0.3.2 lib/act_form/combinable.rb
act_form-0.3.1 lib/act_form/combinable.rb
act_form-0.3.0 lib/act_form/combinable.rb
act_form-0.2.0 lib/act_form/combinable.rb