Sha256: 5fa80807fee529ccb18592529d2426ac30c87f39da84fb1ce917ee219cbed452

Contents?: true

Size: 1.91 KB

Versions: 43

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

require 'active_support/concern'

module Satis
  module Forms
    module Concerns
      module Required
        extend ActiveSupport::Concern

        included do
          def required?(method)
            result = if !options[:required].nil?
              options[:required]
            elsif @object.respond_to?("#{method}_required?".to_sym)
              @object.send("#{method}_required?".to_sym)
            elsif has_validators?(method)
              required_by_validators?(method)
            end

            result = required?(method.to_s.sub(/_id$/, '')) if !result && method.match(/_id$/)
            result
          end

          def has_validators?(method)
            @has_validators ||= method && @object.class.respond_to?(:validators_on)
          end

          def required_by_validators?(method)
            (attribute_validators(method) + reflection_validators(method)).any? do |v|
              v.kind == :presence && valid_validator?(v)
            end
          end

          def attribute_validators(method)
            @object.class.validators_on(method)
          end

          def reflection_validators(_method)
            []
            # reflection ? object.class.validators_on(reflection.name) : []
          end

          def valid_validator?(validator)
            !conditional_validators?(validator) && action_validator_match?(validator)
          end

          def conditional_validators?(validator)
            validator.options.include?(:if) || validator.options.include?(:unless)
          end

          def action_validator_match?(validator)
            return true unless validator.options.include?(:on)

            case validator.options[:on]
            when :save
              true
            when :create
              !object.persisted?
            when :update
              object.persisted?
            end
          end
        end
      end
    end
  end
end

Version data entries

43 entries across 43 versions & 1 rubygems

Version Path
satis-2.1.20 lib/satis/forms/concerns/required.rb
satis-2.1.19 lib/satis/forms/concerns/required.rb
satis-2.1.18 lib/satis/forms/concerns/required.rb
satis-2.1.11 lib/satis/forms/concerns/required.rb
satis-2.1.10 lib/satis/forms/concerns/required.rb
satis-2.1.9 lib/satis/forms/concerns/required.rb
satis-2.1.8 lib/satis/forms/concerns/required.rb
satis-2.1.6 lib/satis/forms/concerns/required.rb
satis-2.1.5 lib/satis/forms/concerns/required.rb
satis-2.1.4 lib/satis/forms/concerns/required.rb
satis-2.1.3 lib/satis/forms/concerns/required.rb
satis-2.1.1 lib/satis/forms/concerns/required.rb
satis-2.1.0 lib/satis/forms/concerns/required.rb
satis-2.0.9 lib/satis/forms/concerns/required.rb
satis-2.0.8 lib/satis/forms/concerns/required.rb
satis-2.0.7 lib/satis/forms/concerns/required.rb
satis-1.0.75 lib/satis/forms/concerns/required.rb
satis-1.0.74 lib/satis/forms/concerns/required.rb
satis-1.0.70 lib/satis/forms/concerns/required.rb
satis-1.0.69 lib/satis/forms/concerns/required.rb