Skip to content

Instantly share code, notes, and snippets.

View aldesantis's full-sized avatar

Alessandro Desantis aldesantis

View GitHub Profile
en:
errors:
messages:
not_divisible: 'is not a multiple of %{by}'
it:
errors:
messages:
not_divisible: 'non è un multiplo di %{by}'
class Shoe < ActiveRecord::Base
validates :size, divisibility: { by: 0.5, allow_blank: true }
end
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'
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
class Model < ActiveRecord::Base
validates :attribute1, :attribute2, divisibility: { by: 0.5 }
validates :attribute3, divisibility: { by: 0.75 }
end
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'
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?
Crowdster.configure do |config|
# The :: is needed to access the root namespace.
config.base_controller = '::ApplicationController'
end
require 'rails_helper'
RSpec.describe MyEngine::UsersController do
routes { MyEngine::Engine.routes }
# ...
end
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'