Rails 4+: where to store custom validation msgs used across models - validation

I have custom validation messages that I use in several different models:
# models/user.rb
class Person < ActiveRecord::Base
validates :name, presence: { message: "This is a required question" }
end
# models/pet.rb
class Pet < ActiveRecord::Base
validates :name, presence: { message: "This is a required question" }
end
I'm a little familiar with the en.yml config, but that specifies messages per model:
en:
activerecord:
errors:
models:
person:
attributes:
name:
blank: "This is a required question"
pet:
attributes:
name:
blank: "This is a required question"
And that just seems very verbose and not developer-friendly.
My solution was to create an initializer file and store my custom validation messages in a hash there:
# config/initializers/custom_validation_messages.rb
CUSTOM_VALIDATION_MESSAGES = {
required: "This is a required question"
}
And then to use them as:
validates :name, presence: { message: CUSTOM_VALIDATION_MESSAGES[:required] }
My solution works, but I'm not sure if this is a correct usage of an initializer. Is there a more appropriate Rails convention for cleaning up those duplicate validation messages?

I prefer the following approach, so I could also use the translations on other places within my app (like a hint message for instance):
class Pet < ActiveRecord::Base
validates :name, presence: { message: I18n.t('common_errors.required_question' }
end
and
en:
common_errors:
required_question: "This is a required question"

Related

Can't Use Validates Presence On Picture Rails 5.x

micropost.rb
class Micropost < ActiveRecord::Base
belongs_to :user
default_scope -> { order(created_at: :desc) }
mount_uploader :picture, PictureUploader
validates :user_id, presence: true
validates :tag1, presence: true, length: { maximum: 20 }
validates :tag2, presence: true, length: { maximum: 20 }
validates :tag3, presence: true, length: { maximum: 20 }
validates :picture, presence: true
validate :picture_size
validates :ispublic, inclusion: { in: [ true, false ] }
private
# Validates the size of an uploaded picture.
def picture_size
if picture.size > 5.megabytes
errors.add(:picture, "should be less than 5MB in size.")
end
end
end
microposts_controller.rb snippet:
def create
#micropost = current_user.microposts.build(micropost_params)
if #micropost.save!
flash[:success] = "Post created!"
redirect_to root_url
else
#feed_items = []
render 'static_pages/home'
end
end
def micropost_params
params.require(:micropost).permit(:tag1, :tag2, :tag3, :picture, :ispublic)
end
Test that is failing snippet:
tag1 = "This"
tag2 = "Is"
tag3 = "Sparta"
image = File.open("test/fixtures/p_avatar.png")
assert_difference 'Micropost.count', 1 do
post microposts_path, params: {micropost: { tag1: tag1, tag2: tag2, tag3: tag3, picture: "image",
ispublic: false }}
end
Test error:
Validation failed. Picture can't be blank.
I am using CarrierWave...based on other answers I was wondering if I can't use the presence: true validation with a picture, but I thought the #micropost.save would just store whatever as a string (varchar). For testing purposes, I just want to make sure a valid string is passed, but maybe that isn't even necessary if CarrierWave and picture_size validation is enough.
Found it. In the Rails spec, there is a function called fixture_file_upload() which does all the heavy lifting of creating the image object for me (some sort of upload to a cache it seems) and making the Model.save() method validate it and store the string in the db.
Thanks to this site it informed me a bit on image upload testing without going through FactoryBot: https://jeffkreeftmeijer.com/carrierwave-rails-test-fixtures/
Ultimately I decided I micropost doesn't require a picture (A la Twitter).

Rails 5 belongs_to scoped validation

lets say I have this model:
class Post < ApplicationRecord
enum post_type: { post: 0, page: 1 }
belongs_to :user
end
by default rails 5 will make the belongs_to :user association to be required. And If you pass optional: true will make this association to be optional. But what I want is the belongs_to :user association to be optional only when the post_type is page and when it is post to required.
How can I do it at the line belongs_to :user ?
At this moment I am doing this:
class Post < ApplicationRecord
enum post_type: { post: 0, page: 1 }
belongs_to :user, optional: true
validates :user_id, presence: { scope: post? }
end
But this will give me an error like:
NoMethodError: undefined method `post?' for #
Is this the correct way to do it? or there is another way?
The user presence can be validated using if option:
validates :user, presence: true, if: :post?

Uniqueness validation for polymorphic model fails

So I have this really weird problem. Here is my model:
class Clip < ApplicationRecord
belongs_to :owner, polymorphic: true
default_scope -> { order(created_at: :desc) }
validates :address, presence: true, uniqueness: { scope: [:owner_type, :owner_id] }
before_save { self.adress=adress.split("=").last }
validates :owner, presence: true
end
and the test
test "invalid creations" do
get new_clip_path
assert_no_difference "Clip.count" do
post clips_path, params: { clip: { address: "",
description: "bum bum bum" } }
end
assert_template "clips/new"
assert_match "blank", response.body
assert_no_difference "Clip.count" do
#clip allready in db, should refure
post clips_path, params: { clip: { address: "fWNaR-rxAic",
description: "bum bum bum" } }
end
assert_template "clips/new"
assert_no_match "blank", response.body
assert_match "Address has already been taken", response.body
end
The test is passing without a problem but I just realized by accident, that I can still create clips with the same address and owner, as often as I want to. This confuses me for two reasons: a) what is wrong with this line validates :address, presence: true, uniqueness: { scope: [:owner_type, :owner_id] } and b) why does the test not fail?

Unable to create correct Mongoid model from JSON

I'm trying to create a Mongoid model from the corresponding JSON structure.
However it does not include the embedded relation frameworks.
I'm using Mongoid 4.0. Am I doing something wrong or is this a bug?
If I store any embedded relation via store_as under a different name than the default serialization, it works as expected. Also, if I create the model in the database from JSON rather than initialize it everything's fine...
JSON input
{
"name": "MyName",
"frameworks": [
{
"name": "grails",
"runtime": "groovy",
"versions": []
}
]
}
Models
require 'mongoid'
class Vendor
include Mongoid::Document
include Mongoid::Attributes::Dynamic
# fields
field :name, type: String
# relations
embeds_many :frameworks
# validations
validates :name, presence: true
validates :frameworks, presence: true
end
class Framework
include Mongoid::Document
embedded_in :vendor
field :name, type: String
field :runtime, type: String
field :versions, type: Array
# validations
validates :name, presence: true
validates :runtime, presence: true
end
Test App
require 'json'
require 'require_relative'
require_relative 'vendor'
begin
json = JSON.parse(File.read('input.json'))
#profile = Vendor.new(json)
puts #profile.inspect
rescue JSON::ParserError => e
puts "Error: " << e.to_s
end

When is the validator of email uniqueness trigerred?

I have a question about this test from the Michael Hartl tutorial :
Model:
class User < ActiveRecord::Base
.
.
.
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: true
end
Test:
require 'spec_helper'
describe User do
before do
#user = User.new(name: "Example User", email: "user#example.com")
end
.
.
.
describe "when email address is already taken" do
before do
user_with_same_email = #user.dup
user_with_same_email.email = #user.email.upcase
user_with_same_email.save
end
it { should_not be_valid }
end
end
My understanding of the uniqueness validator for the email is that it cannot be added twice in the database. But in this test, the User is just instantiated with a new, not a create.
So here is what I think happens:
#user = User.new (just in memory)
...
user_with_same_email = #user.dup we have two users in memory
...
user_with_same_email.save we are inserting the first user in the db, so it should be valid, and yet the test it { should_not be_valid } passes.
What am I getting wrong ?
What really happens:
In before:
#user = User.new (just in memory)
In the describe:
user_with_same_email = #user.dup we have two users in memory
user_with_same_email.save we are inserting the first user in the db, so it should be valid, and it is ! But that's not what's being tested here
In the it:
should_not be_valid calls .valid? on #user, and since we've just inserted an user with the same email, #user is not valid. And so the test passes.

Resources