confusion over validates_with custom validations syntax shown in rails guide - ruby-on-rails

I am attempting to use the validates_with custom validations helper with Rails 4.
The following code is working in my application:
class Photo
validates_with CleanValidator
include ActiveModel::Validations
end
class CleanValidator < ActiveModel::Validator
def validate(record)
if record.title.include? "foo"
record.errors[:title] << "Photo failed! restricted word"
end
end
end
However I want to pass this helper to multiple attributes in multiple models, not just :title.
There is an example in validates_with section of guide that contains the following example:
class GoodnessValidator < ActiveModel::Validator
def validate(record)
if options[:fields].any?{|field| record.send(field) == "Evil" }
record.errors[:base] << "This person is evil"
end
end
end
class Person < ApplicationRecord
validates_with GoodnessValidator, fields: [:first_name, :last_name]
end
This is what I want to achieve, substituting [:fields] for [:title] in my code example so that I can use CleanValidator for multiple models and multiple attributes (User.name, Photo.title etc).

I think you want the other example from the guides, each validator. You should be able to do
class CleanValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless ["Evil", "Other", "Restricted", "Words"].include?(value)
record.errors[attribute] << (options[:message] || "is a restricted word")
end
end
end
class Photo
include ActiveModel::Validations
attr_accessor :title
validates :title, clean: true
end

Related

Rails - validation inside decorator

I'm struggling with some kind of issue. I have a rails model (mongoid).
class User
include Mongoid::Document
include ActiveModel::SecurePassword
validate :password_presence,
:password_confirmation_match,
:email_presence,
field :email
field :password_digest
def password_presence
end
def email_presence
end
def password_confirmation_match
end
end
My goal is to call validations depends on which decorator I will use. Let's say I've got two decorators:
class PasswordDecorator < Draper::Decorator
def initialize(user)
#user = user
end
end
def RegistraionDecorator < Draper::Decorator
def initialize(user)
#user = user
end
end
So now when I create/save/update my user object inside RegistraionDecorator I would like to perform all validation methods.
RegistraionDecorator.new(User.new(attrbiutes))
But when I will do it inside PasswordDecorator I want to call for example only password_presence method.
PasswordDecorator.new(User.first)
When I move validations to decorator it won't work cuz its different class than my model.
How can I achieve that?
Try to use a Form Object pattern instead.
Here is an example (from a real project) of how it could be done with reform.
class PromocodesController < ApplicationController
def new
#form = PromocodeForm.new(Promocode.new)
end
def create
#form = PromocodeForm.new(Promocode.new)
if #form.validate(promo_params)
Promocode.create!(promo_params)
redirect_to promocodes_path
else
render :edit
end
end
private
def promo_params
params.require(:promocode).
permit(:token, :promo_type, :expires_at, :usage_limit, :reusable)
end
end
class PromocodeForm < Reform::Form
model :promocode
property :token
property :promo_type
property :expires_at
property :usage_limit
property :reusable
validates_presence_of :token, :promo_type, :expires_at, :usage_limit, :reusable
validates_uniqueness_of :token
validates :usage_limit, numericality: { greater_or_equal_to: -1 }
validates :promo_type, inclusion: { in: Promocode::TYPES }
end
Bonus: The model does not trigger validations and much easy to use in tests.

Rails plug and play validations

Here is my example
class User < ActiveRecord::Base
validates_with EmailValidator
end
class EmailValidator < ActiveModel::Validator
def validate(record)
if record != someregex
record.errors.add(:email, 'invalid email')
end
end
end
Now I can use this EmailValidator for any model. But my requirement is to validate its uniqueness and presence also for that particular model.
If I can achieve this I can use this EmailValidator for any model email validation with functionalities unique, presence.
Then I can achieve more reusable Validator.
You can do this by rewriting your validator to be EachValidator:
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if attribute !~ someregex
record.errors.add attribute, (options[:message] || 'invalid email')
end
end
end
Then in your model:
validates :email, email: true, uniqueness: true, presence: true
Hope that helps.

Generic method for validation of paperclip files in different models

I have 2 models users , companies
User model:
has_attached_file :avatar,
...
:whiny=>false
validates_with ImageSizeValidator
validates_with ImageTypeValidator
validates_with ImageConvertionValidator
Company model:
has_attached_file :logo,
#the rest is similar
I have done validation for users and put it in validation_helper
class ImageSizeValidator < ActiveModel::Validator
def validate(record)
if record.avatar_file_name.present?
record.errors[:base] << (I18n.t :in_between, scope: "activerecord.errors.models.user.attributes.avatar_file_size") unless record.avatar_file_size.to_i < 200000
end
end
end
class ImageTypeValidator < ActiveModel::Validator
def validate(record)
if record.avatar_file_name.present?
record.errors[:base] << (I18n.t :file_type, scope: "activerecord.errors.models.user.attributes") unless ['image/jpeg', 'image/gif','image/png'].include?(record.avatar_content_type)
end
end
end
My problem is that the names will be different so avatar_file_name for users and logo for companies.
Do I have to do a specific method for each?
How can I work this around?
you just need to add options. If you take a look at documentation, you can pass arguments in block:
#model
validates_with ImageSizeValidator, paperclip_field_name: :avatar
#validator
def validate(record)
if record.send(options[:paperclip_field_name].to_s+"_file_name").present?
record.errors[:base] << (I18n.t :in_between, scope: "activerecord.errors.models.user.attributes.#{options[:paperclip_field_name]}_file_size") unless record.send(options[:paperclip_field_name].to_s+"_file_name").to_i < 200000
end
end
but much easier to use validate_each method
#model
validates :avatar, image_size: true, image_type: true, image_conversion: true
#validator
def validate_each(record, attribute, value)
if record.send(attribute.to_s+"_file_name").present?
record.errors[:base] << (I18n.t :in_between, scope: "activerecord.errors.models.user.attributes.#{attribute}_file_name)") unless record.send(attribute.to_s+"_file_name").to_i < 200000
end
end
Paperclip has built-in support for validations (https://github.com/thoughtbot/paperclip#validations). If their validation are not a fit for your problem you can look on how they are doing it: https://github.com/thoughtbot/paperclip/tree/master/lib/paperclip/validators

Why doesn't custom validation work in RoR 3

Here is my code for the Photo model class:
class Photo < ActiveRecord::Base
belongs_to :user
belongs_to :organization
validate :user_id_or_organization_id_cant_be_blank
...
def user_id_or_organization_id_cant_be_blank
if !:user_id? and !:organization_id?
errors.add(:user_id, "no user")
errors.add(:organisation_id, "no organization")
end
end
The problem is in the validation. It doesn't work, and i don't understand why.
I can create a photo with no user_id or organization_id which is not supposed to happen.
Please explain to me what i'm doing wrong?
You'd rather do:
def user_id_or_organization_id_cant_be_blank
if user_id.blank? && organization_id.blank?
errors.add(:user_id, "no user")
errors.add(:organisation_id, "no organization")
end
end
Rails provides the standard approach to check the presence:
validates :user_id, :organization_id, presence: true
If you need to do a complex validation, try to use ActiveModel::Validator
class Photo < ActiveRecord::Base
validates_with UserOrganizationIdsValidator
end
class UserOrganizationIdsValidator < ActiveModel::Validator
def validate(record)
if user_id.blank? && organisation_id.blank?
errors.add(:user_id, "no user")
errors.add(:organisation_id, "no organization")
end
# something custom else...
end
end

rails3 doesn't load my validators in lib

I put EmailValidator in lib/validators/email_validator and it's not workings (I put root/lib in the load_path)
here is the code.. I put the class in module validators as the parent folder name
class Validators::EmailValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /^([^#\s]+)#([a-z0-9]+\.)+[a-z]{2,}$/i
object.errors[attribute] << (options[:message] || "is not formatted properly")
end
end
end
I get the error Unknown validator: 'email'
You have two options:
Either put your custom validator under config/initializers.
Or add lib/validators to the autoload path in config/application.rb.
config.autoload_paths << "#{config.root}/lib/validators"
Personally I would go with the second option as lib/validators makes for good encapsulation.
Since you put your custom validator in the Validators:: in the lib/validators, you have to reference it with that namespace also.
validates :email, presence: true, :'validators/email' => true
UPDATE: You need this:
module Validators
class EmailValidator < ActiveModel::EachValidator
def validate(object, attribute, value)
unless value =~ /^([^#\s]+)#([a-z0-9]+\.)+[a-z]{2,}$/i
object.errors[attribute] << (options[:message] || "is not formatted properly")
end
end
end
end
class YourModel < ActiveRecord::Base
include Validators
validates :email, :presence => true, :email => true
end
Otherwise, you need to put your validator class under the ActiveModel::Validations namespace. When you namespace a class, ActiveRecord isn't going to see it, if that namespace isn't a namespace it has already included.

Resources