Sha256: aa01cac5fc3c755964d2c2342582d1179c758099503514287a00597eb6a699b1

Contents?: true

Size: 1020 Bytes

Versions: 22

Compression:

Stored size: 1020 Bytes

Contents

module Carnival::PresenterValidators
  class FieldValidator
    EDIT_ACTIONS = [:new, :edit]
    SHOW_ACTIONS = [:index, :show]

    def initialize(presenter)
      @presenter = presenter
    end


    def validates
      fields.each do |field|
        validates_one_to_one_associations(field)
      end
    end

    def validates_one_to_one_associations(field)
      if field.specified_association?
        check_field_invalid_actions(field, EDIT_ACTIONS)
      elsif @presenter.is_one_to_one_relation?(field)
        check_field_invalid_actions(field, SHOW_ACTIONS)
      end
    end

    private
    def check_field_invalid_actions(field, actions)
      return if field.actions.nil?
      intersection = actions & field.actions
      if intersection.size > 0
        error = I18n.t("carnival.errors.invalid_field", actions: intersection, field: field.name, presenter: @presenter.presenter_name)
        raise ArgumentError.new(error)
      end
    end

    def fields
      @presenter.fields.values
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
carnival-0.3.2 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.3.1 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.3.0 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.2.8 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.2.7 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.2.6 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.2.5 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.2.4 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.2.3 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.1.10 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.2.2 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.2.1 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.2.0 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.1.9 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.1.8 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.1.7 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.1.6 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.1.5 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.1.3 app/presenters/carnival/presenter_validators/field_validator.rb
carnival-0.1.2 app/presenters/carnival/presenter_validators/field_validator.rb