Rails, DRY default validator for string length? - ruby-on-rails

So I am having an issue where length errors are rails exception because the model doesn't have a length validation on it.
I understand I can put a length validator on my string by using
validates :blah, length: { maximum: 255 }
However most of my strings are using the default size of 255, so I would have to repeat this validation in a lot of places.
Is there a DRY way to put a default validator on all strings to validate to the default database length of 255?

The gem schema_validations (https://github.com/SchemaPlus/schema_validations) does what you want.
Automatically creates validations basing on the database schema.
It inspect the database and automatically creates validations basing on the schema. After installing it your model is as simple as it can be.
class User < ActiveRecord::Base
end
Validations are there but they are created by schema_validations under the hood.

This probably isn't exactly the answer you are looking for but if you add multiple fields to the validator then you aren't really repeating the validator and seems fairly DRY to me.
validates :foo, :bar, :baz, length: { maximum: 255 }

class ModelName < ActiveRecord::Base
validates :name, length: { maximum: 255 }
end
Then use it by typing
ModelName.validators

Related

Is there something like .NET's data annotations in rails?

I was looking for something similar to .net's data annotations in rails, something like this.
What I want to achieve by this is: I have some fields (they could be nil also) for which I want to check the length and if the length exceeds I want to display an error message.
I want to club all the error messages related to, say all blog posts (which again have many separate fields) and then display them at once.
Rails uses ActiveRecord validations. In many cases the default validations are easy to set up. But if you want/or need customized validations that can all be done as well. Read the documentation here:
http://guides.rubyonrails.org/active_record_validations.html
In your case this type of validation is built in to rails so it's simple as adding 1 line to your model:
class MyModel
validates :my_field_name, length: { maximum: 3 }, allow_blank: true
end
This will validate the maximum length of your field. You can also customize the validation error message:
class MyModel
validates :name, presence: {message: "Title can't be blank." }, uniqueness: {message: "Title already exists."}, length: { maximum: 5, message: "Must be less than 5 characters"}
end

How to make the validation(only accepts alphanumeric) global for my model in ruby

I am currently new in ruby on rails and I have gem called rails admin. I want the validation not repeatable, i want it to save in one method and make it global so that in my model I can call the validation format.
I added a def in my application controller and inside of it i declare it as global but when I type special characters, it will be add.
Note: My validation is only allowed alphanumeric or underscore..
Expected Output: It wont add unless the input is alphanumeric or underscore
Question: Is my global variable wrong? How could I make a global variable so that I will just call it
Model
class ActivityType < ApplicationRecord
has_many :activities
validates :name, presence: true, length: { maximum: 20 },
:uniqueness => true,
format: { with: /\A[\w\_]+\z/ }
validates :description, presence: true,
format: { with: /\A[\w]+\z/ }
end
RegexValidations
module RegexValidations
def self.alphanumeric_underscore
{ with: /\A[\w\_]+\z/ }
end
end
Validation are done at model level for their respective attributes.
Defining methods in application_controller doesn't make it global, but accessible to all controllers inheriting from it.
Your model validation validates should be enough to validate format of the string in column.
Edit/Improvisation:
If you want to make the regex dry/not repeat for every model. Create a module in your lib folder say:
module RegexValidations
def self.alphanumeric_underscore
{ with: /\A[\w\_]+\z/ }
end
end
And in your model include it and say:
include RegexValidations
validates :description, presence: true, format: RegexValidations.alphanumeric_underscore
This way you can write multiple regex in one file and use it in every model keeping it DRY.
PS: unfortunately i couldn't test it..but it should work. Let me know if you encounter any error.

Rails 4 - Validate Elements in Array

One of my models contains a virtual attribute called things. This attribute is an array, and I'd like every element within that array to be validated against a set of rules. Here is my current attempt at validation:
validates :things, presence: true, length: { minimum: 2, maximum: 255 }
The problem with this code is that it validates the entire array itself, not each individual element within the array. I know I can write a custom validator, but is there any way to use the existing validation options to run these validations against every element in the array? The other topics I found on this are for older versions of Rails, so I'm not sure if Rails 4 has something new that could help with this.
Thanks in advance for any help!
Have you tried a custom validation? You can create a custom validation inside your model.
Something like this, where "things" is the name of the attribute that you want to validate:
model.rb
validate :check_each_thing
def check_each_thing
things.each do |thing|
if thing.present?
if thing.size < 2 || thing.size > 255
errors.add(:things, 'It should be longer than 2 and shorter than 255.')
end
else
errors.add(:things, 'It should be present.')
end
end
end
Hope it helps :)

Selecting all current attributes of a model for validations in Rails

When setting up my models I often find myself having to write out all of its attributes when setting up certain validations. A common example is when I use the presence parameter:
validates :first_name, :last_name, :username, :email, presence: true
Is there a clever way to select all of its attributes without explicitly writing them all out similar to how you can retrieve them in the rails console?
User.columns
And pass it as an argument in the validates method?
ALL_ATTRIBUTES = User.columns
validates ALL_ATTRIBUTES, presence: true
Trying something like this out I got this error undefined method 'to_sym'
I will NOT encourage you or anyone to do this. Reason being when you run into issues, when an object of your model doesn't get saved and throw errors because of a new column which was added to application after some time in future, and you or new developers will wonder WHY?!?!.
However, if you must do then here you go:
validates *self.column_names.map(&:to_sym), presence: true
Here, * in Ruby is known as splat operator and here's the explanation on &:.
This is an awful idea. But you can do it this way:
attrs = column_names.map { |column| column.to_sym }
validates *attrs, presence: true
Why is it a bad idea? Because it's not very clear what is being validated it. It makes debugging hard, and could cause you have strange bugs. If you add a column in the future that does not require presence validation, you will trip up. Also, some things my not require presence. For example, an email field will need a regex validation, which automatically knows that a blank string is invalid. So a presence validator is redundant.
Beware of being too clever, as it's sometimes not so clever after all.

RSpec crashing with multiple Rails validation helpers

I have two different validations for the :website attribute on my Customer model. One is the build in length helper, with the maximum set to 255, while the other is a custom validation. They both work individually, and the appropriate tests pass, but for some reason, when I run my tests with both validations, RSpec crashes to the point I have to complete exit out of Guard and restart it.
Here is my code, any way they are some how conflicting with each other? I have never experienced this before:
class Customer < Active Record::Base
...
URL_REGEX = /(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*#)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|#)|\/|\?)*)?$/i
validates :website, length: { maximum: 255 }
validate :valid_urls
private
def valid_urls
["website", "blog", "contact"].each do |attribute|
errors.add(attribute, "needs to be a valid url") if send(attribute).present? && URL_REGEX.match(send(attribute)).nil?
end
end
end
UPDATE: Thanks for the help, turned out the whole issue was just a bad regex. I had copied the regex from a stackoverflow thread, which had escaped some of the ampersands, producing a bad regex. I just now copied it from the jQuery validate source and it worked, sorry for the trouble.
Mackshkatz, can you try removing custom validation to use those provided by rails? As such:
class Customer < ActiveRecord::Base
validates :website, format: { with: URL_REGEX }, allow_blank: true, length: { maximum: 255 }
validates :blog, format: { with: URL_REGEX }, allow_blank: true
validates :contact, format: { with: URL_REGEX }, allow_blank: true
end
And see if it passes? It seems like the problem may be in complex regexp you are using.

Resources