This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
en: | |
errors: | |
messages: | |
not_divisible: 'is not a multiple of %{by}' | |
it: | |
errors: | |
messages: | |
not_divisible: 'non è un multiplo di %{by}' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Shoe < ActiveRecord::Base | |
validates :size, divisibility: { by: 0.5, allow_blank: true } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Shoe < ActiveRecord::Base | |
validate :validate_size | |
private | |
def validate_size | |
return if size.blank? | |
return if size % 0.5 == 0 | |
errors.add :size, 'is not a multiple of 0.5' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DivisibilityValidator < ActiveModel::EachValidator | |
attr_reader :base | |
def initialize(options) | |
super | |
@base = options[:base] | |
fail ArgumentError, 'You must pass the :base option!' unless base | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model < ActiveRecord::Base | |
validates :attribute1, :attribute2, divisibility: { by: 0.5 } | |
validates :attribute3, divisibility: { by: 0.75 } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RSpec.describe MyModel do | |
subject { MyModel.new } | |
%w(attribute1 attribute2 attribute3).each do |attribute| | |
it "adds an error when #{attribute} is not divisible by 0.5" do | |
subject.send("#{attribute}=", 1.3) | |
subject.validate | |
expect(subject.errors.messages[attribute]).to eq 'is not divisible by 0.5' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model < ActiveRecord::Base | |
validate :validate_multiples | |
private | |
def validate_multiples | |
%w(attribute1 attribute2 attribute3).each do |attribute| | |
value = send(attribute) | |
next if value.blank? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Crowdster.configure do |config| | |
# The :: is needed to access the root namespace. | |
config.base_controller = '::ApplicationController' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_helper' | |
RSpec.describe MyEngine::UsersController do | |
routes { MyEngine::Engine.routes } | |
# ... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV['RAILS_ENV'] ||= 'test' | |
ENGINE_ROOT = File.join(File.dirname(__FILE__), '../') | |
# Load environment.rb from the dummy app. | |
require File.expand_path('../dummy/config/environment', __FILE__) | |
abort("The Rails environment is running in production mode!") if Rails.env.production? | |
require 'spec_helper' | |
require 'rspec/rails' |