Sha256: ba4d59362e7f0ade23812332ad8fd9fa1b1843a0e795bd252003093d6648f37b

Contents?: true

Size: 1.83 KB

Versions: 12

Compression:

Stored size: 1.83 KB

Contents

require_relative 'helpers/model_builder'

module UnitTests
  class RecordWithDifferentErrorAttributeBuilder
    include ModelBuilder

    def initialize(options)
      @options = options.reverse_merge(default_options)
    end

    def attribute_that_receives_error
      options[:attribute_that_receives_error]
    end

    def attribute_to_validate
      options[:attribute_to_validate]
    end

    def message
      options[:message]
    end

    def message=(message)
      options[:message] = message
    end

    def model
      @_model ||= create_model
    end

    def model_name
      'Example'
    end

    def record
      model.new
    end

    def valid_value
      'some value'
    end

    protected

    attr_reader :options

    private

    def context
      {
        validation_method_name: validation_method_name,
        valid_value: valid_value,
        attribute_to_validate: attribute_to_validate,
        attribute_that_receives_error: attribute_that_receives_error,
        message: message
      }
    end

    def create_model
      _context = context

      define_model model_name, model_columns do
        validate _context[:validation_method_name]

        define_method(_context[:validation_method_name]) do
          if self[_context[:attribute_to_validate]] != _context[:valid_value]
            self.errors.add(_context[:attribute_that_receives_error], _context[:message])
          end
        end
      end
    end

    def validation_method_name
      :custom_validation
    end

    def model_columns
      {
        attribute_to_validate => :string,
        attribute_that_receives_error => :string
      }
    end

    def default_options
      {
        attribute_that_receives_error: :attribute_that_receives_error,
        attribute_to_validate: :attribute_to_validate,
        message: 'some message'
      }
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
shoulda-matchers-3.1.3 spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-4.0.0.rc1 spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-3.1.2 spec/support/unit/record_with_different_error_attribute_builder.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/shoulda-matchers-2.8.0/spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-3.1.1 spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-3.1.0 spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-3.0.1 spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-3.0.0 spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-3.0.0.rc1 spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-2.8.0 spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-2.8.0.rc2 spec/support/unit/record_with_different_error_attribute_builder.rb
shoulda-matchers-2.8.0.rc1 spec/support/unit/record_with_different_error_attribute_builder.rb