Sha256: c6c6e2cb8be104b5f27a35743e68d6cfe6758d1f7b133230105ebf2bdb90346f

Contents?: true

Size: 1.44 KB

Versions: 12

Compression:

Stored size: 1.44 KB

Contents

require 'delegate'

module UnitTests
  class RecordBuilderWithI18nValidationMessage < SimpleDelegator
    def initialize(builder, options = {})
      super(builder)
      @options = default_options.merge(options)
      builder.message = validation_message_key
    end

    def validation_message_key
      options[:validation_message_key]
    end

    protected

    attr_reader :builder, :options

    private

    def model
      @_model ||= super.tap do |model|
        stub_validation_messages
      end
    end

    def stub_validation_messages
      stub_default_validation_message
      stub_attribute_specific_validation_message
    end

    def stub_default_validation_message
      keys = [
        'activerecord.errors.messages',
        validation_message_key
      ]

      I18nFaker.stub_translation(keys, default_message)
    end

    def stub_attribute_specific_validation_message
      keys = [
        'activerecord.errors',
        "models.#{builder.model_name.to_s.underscore}",
        "attributes.#{builder.attribute_that_receives_error}",
        validation_message_key
      ]

      I18nFaker.stub_translation(
        keys,
        message_for_attribute_that_receives_error
      )
    end

    def default_message
      'the wrong message'
    end

    def message_for_attribute_that_receives_error
      'the right message'
    end

    def default_options
      {
        validation_message_key: :validation_message_key
      }
    end
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
shoulda-matchers-3.1.3 spec/support/unit/record_builder_with_i18n_validation_message.rb
shoulda-matchers-4.0.0.rc1 spec/support/unit/record_builder_with_i18n_validation_message.rb
shoulda-matchers-3.1.2 spec/support/unit/record_builder_with_i18n_validation_message.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_builder_with_i18n_validation_message.rb
shoulda-matchers-3.1.1 spec/support/unit/record_builder_with_i18n_validation_message.rb
shoulda-matchers-3.1.0 spec/support/unit/record_builder_with_i18n_validation_message.rb
shoulda-matchers-3.0.1 spec/support/unit/record_builder_with_i18n_validation_message.rb
shoulda-matchers-3.0.0 spec/support/unit/record_builder_with_i18n_validation_message.rb
shoulda-matchers-3.0.0.rc1 spec/support/unit/record_builder_with_i18n_validation_message.rb
shoulda-matchers-2.8.0 spec/support/unit/record_builder_with_i18n_validation_message.rb
shoulda-matchers-2.8.0.rc2 spec/support/unit/record_builder_with_i18n_validation_message.rb
shoulda-matchers-2.8.0.rc1 spec/support/unit/record_builder_with_i18n_validation_message.rb