I'm saving some attributes of my model instances as floats, but it's necessary for my app to handle an input from a form with commas instead of points (ex. 10,99 should be saved as 10.99).
I'm trying to do that with before_validation callbacks, but I can't make it work properly - input with commas can't get through validations, I keep getting price/size2 is not a number error.
class Advert < ActiveRecord::Base
before_validation do
normalize(self.price) if is_number?(self.price)
normalize(self.size2) if is_number?(self.size2)
end
validates :price, presence: true, numericality: true
validates :size2, numericality: true
def is_number?(string)
true if Float(string) rescue false
end
def normalize(number)
number.to_s.gsub!(',', '.').to_f
end
Any help would be appreciated.
I havent tested it but i think you are not actually modifying price and size2 as you are gsub!ing number and not returning the value to the 2 attributes.
what about something like this:
class Advert < ActiveRecord::Base
before_validation :string_to_float
validates :price, presence: true, numericality: true
validates :size2, numericality: true
def is_number?(string)
true if Float(string) rescue false
end
def normalize(number)
number.to_s.gsub(',', '.').to_f
end
def string_to_float
self.price = normalize(self.price) if is_number?(self.price)
self.size2 = normalize(self.size2) if is_number?(self.size2)
end
end
Use a custom setter method instead:
class Advert
def price=(val)
#price = val.is_a?(Float) ? val : val.to_f
end
end
As I couldn't handle the decimal delimiter change via rails callbacks, I just wrote a simple jQuery workaround, maybe it'll help someone:
normalize_input = ->
$('form').submit ->
normalized_price = $('#advert_price').val().replace(',', '.') # advert_price is a form input
normalized_size2 = $('#size2input').val().replace(',', '.') # just like the size2input
$('#advert_price').val(normalized_price)
$('#size2input').val(normalized_size2)
Related
I'm writing a Redmine plugin that should check if some fields of an Issue are filled depending on values in other fields.
I've written a plugin that implements validate callback, but I don't know how to check field values which are going to be saved.
This is what I have so far:
module IssuePatch
def self.included(receiver)
receiver.class_eval do
unloadable
validate :require_comment_when_risk
protected
def require_comment_when_risk
risk_reduction = self.custom_value_for(3)
if risk_reduction.nil? || risk_reduction.value == 0
return true
end
comment2 = self.custom_value_for(4)
if comment2.nil? || comment2.value.empty?
errors.add(:comment2, "Comment2 is empty")
end
end
end
end
end
The problem here is that self.custom_value_for() returns the value already written to the DB, but not the one that is going to be written, so validation doesn't work. How do I check for the value that was passed from the web-form?
Any help will be greatly appreciated.
The nice thing about rails is that in your controller you don't have to validate anything. You are suppose to do all of this in your model. so in your model you should be doing something like
validates :value_that_you_care_about, :numericality => { :greater_than_or_equal_to => 0 }
or
validates :buyer_name, presence: true, :length => {:minimum => 4}
or
validates :delivery_location, presence: true
If any of these fail this will stop the object from being saved and if you are using rails scaffolding will actually highlight the field that is incorrect and give them and error message explaining what is wrong. You can also write your own validations such as
def enough_red_flowers inventory
if inventory.total_red_flowers-self.red_flower_quantity < 0
self.errors.add(:base, 'There are not enough Red Flowers Currently')
return false
end
inventory.total_red_flowers = inventory.total_red_flowers-self.red_flower_quantity
inventory.save
true
end
To write your own custom message just follow the example of self.errors.add(:base, 'your message')
You can find more validations here
Better way it's create custom validator
class FileValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
# some logic for validation
end
end
then in model:
validates :file, file: true
I need a conditional validation in some parts of my app. Right now I am using the following scheme:
User.create
User::WithPassword.create
User::WithPhone.create
It would be cool if I could change class behaviour on the fly like this:
User.with_phone.with_password.create
So I tried to do it like this:
class User < ActiveRecord::Base
validates :phone, presence: true, if: :phone_required?
def self.with_phone
define_method(:phone_required?) { true }
self
end
private
def phone_required?
false
end
end
So it can be used like this where needed:
User.with_phone.create(user_params)
The problem with this approach is that all instances of User get new behaviour since the actual class changes.
Is there a way to return only the modified copy of User class with new instance method phone_required? without affecting the "base" class?
Update
Thank you for the comments as this was more of an idea, the requirement is that I create users without certain validation automatically, and then when they edit profile, they are dealing with pristine User model. I create with_/without_ on the fly in method missing when needed.
Here's my next iteration:
class User < ActiveRecord::Base
validates :phone, presence: true, if: :phone_required?
def self.with_password
define_singleton_method(:password_required?) { true }
self
end
def password_required?
self.class.try :password_required?
end
end
Apparently it's not any better as the singleton method stays there all the time.
Why not simply use an instance variable initialized at creation time?
class User < ActiveRecord::Base
validates :phone, presence: true, if: :phone_required?
#phone_required = false
def self.create_with_phone(params)
obj = self.create(params)
obj.phone_required = true
end
private
def phone_required=(v)
#phone_required = v
end
def phone_required?
#phone_required
end
end
User.create_with_phone(user_params)
I'm having an issue stripping dollar signs and commas out of a currency before validations are run. For some reason the value is being set to 0 when saved.
class Property < ActiveRecord::Base
before_validation :format_currency
validates :street_name_1, presence: true
validates :city, presence: true
validates :state, presence: true
validates :postal_code, presence: true
validates :rate, presence: true, numericality: true
private
def format_currency
self.rate = rate.to_s.gsub(/[^0-9\.]/, '').to_i # Strip out non-numeric characters
end
end
When I do '$3,500'.to_s.gsub(/[^0-9\.]/, '').to_i in the rails console it correctly returns "3500". rate is being passed in to the model properly from the form as well.
For the life of me I can't figure out why it's not properly setting the value on save. Any help would be appreciated.
EDIT: I needed to override the default setter and do my gsub when the rate is set due to the database column being integer.
def rate=(rate)
write_attribute(:rate, rate.to_s.gsub(/[^0-9\.]/, '').to_i)
end
Since you are storing the rate column as an integer, and you are using Rails, I propose that your remove the format_currency method entirely. Rails has a method to deal with currencies already: number_to_currency.
When you want to output the price, simple use the helper:
number_to_currency #property.rate
This gives you greater flexibility too, since you can localise it and pass it a number of other options. Have a look at the API.
Edit, as per OP editing the question:
If you want to save something in that format, why don't you apply the dollar sine in the model?
before_save :set_rate
def set_rate
self.rate = "$#{rate}"
end
Assuming that I have a class such as the following:
class Book < ActiveRecord::Base
validates :title, :length => {:maximum => 10}
end
Is there a way (gem to install?) that I can have ActiveRecord automatically truncate values according to maximum length?
For instance, when I write:
b = Book.new
b.title = "123456789012345" # this is longer than maximum length of title 10
b.save
should save and return true?
If there is not such a way, how would you suggest that I proceed facing such a problem more generally?
Well, if you want the value truncated if its too long, you don't really need a validation, because it will always pass. I'd handle that like this:
class Book < ActiveRecord::Base
before_save :truncate_values
def truncate_values
self.title = self.title[0..9] if self.title.length > 10
end
end
I have come up with a new validator that does truncation. Here is how I did that:
I created the "validators" folder inside "app" folder and then created the file "length_truncate_validator.rb" with the following content:
class LengthTruncateValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
ml = options[:maximum]
record.send("#{attribute}=", value.mb_chars.slice(0,ml)) if value.mb_chars.length > ml unless value.nil? or ml.nil?
end
class << self
def maximum(record_class, attribute)
ltv = record_class.validators_on(attribute).detect { |v| v.is_a?(LengthTruncateValidator) }
ltv.options[:maximum] unless ltv.nil?
end
end
end
And inside my model class I have something like:
class Book < ActiveRecord::Base
validates :title, :length_truncate => {:maximum => 10}
end
which is quite handy and works the way I require.
But still, if you think that this one can be improved or done in another way, you are welcome.
This may not have been an option in 2011, but now there's a before_validation callback that will work.
class Book < ApplicationRecord
before_validation do
if self.params && self.params.length > 1000
self.params = self.title[0...10]
end
end
validate :title, length: { maximum: 10 }, allow_nil: true
end
I like the idea of using the before_validation callback. Here's my stab that automatically truncates all strings to within the database's limit
before_validation :truncate_strings
def truncate_strings
self.class.columns.each do |column|
next if column.type != :string
if self[column.name].length > column.limit
self[column.name] = self[column.name][0...column.limit]
end
end
end
I have a rails model which has 7 numeric attributes filled in by the user via a form.
I need to validate the presence of each of these attributes which is obviously easy using
validates :attribute1, :presence => true
validates :attribute2, :presence => true
# and so on through the attributes
However I also need to run a custom validator which takes a number of the attributes and does some calculations with them. If the result of these calculations is not within a certain range then the model should be declared invalid.
On it's own, this too is easy
validate :calculations_ok?
def calculations_ok?
errors[:base] << "Not within required range" unless within_required_range?
end
def within_required_range?
# check the calculations and return true or false here
end
However the problem is that the method "validate" always gets run before the method "validates". This means that if the user leaves one of the required fields blank, rails throws an error when it tries to do a calculation with a blank attribute.
So how can I check the presence of all the required attributes first?
I'm not sure it's guaranteed what order these validations get run in, as it might depend on how the attributes hash itself ends up ordered. You may be better off making your validate method more resilient and simply not run if some of the required data is missing. For example:
def within_required_range?
return if ([ a, b, c, d ].any?(&:blank?))
# ...
end
This will bail out if any of the variables a through d are blank, which includes nil, empty arrays or strings, and so forth.
An alternative for slightly more complex situations would be to create a helper method which runs the validations for the dependent attributes first. Then you can make your :calculations_ok? validation run conditionally.
validates :attribute1, :presence => true
validates :attribute2, :presence => true
...
validates :attribute7, :presence => true
validate :calculations_ok?, :unless => Proc.new { |a| a.dependent_attributes_valid? }
def dependent_attributes_valid?
[:attribute1, ..., :attribute7].each do |field|
self.class.validators_on(field).each { |v| v.validate(self) }
return false if self.errors.messages[field].present?
end
return true
end
I had to create something like this for a project because the validations on the dependent attributes were quite complex. My equivalent of :calculations_ok? would throw an exception if the dependent attributes didn't validate properly.
Advantages:
relatively DRY, especially if your validations are complex
ensures that your errors array reports the right failed validation instead of the macro-validation
automatically includes any additional validations on the dependent attributes you add later
Caveats:
potentially runs all validations twice
you may not want all validations to run on the dependent attributes
Check out http://railscasts.com/episodes/211-validations-in-rails-3
After implementing a custom validator, you'll simply do
validates :attribute1, :calculations_ok => true
That should solve your problem.
The James H solution makes the most sense to me. One extra thing to consider however, is that if you have conditions on the dependent validations, they need to be checked also in order for the dependent_attributes_valid? call to work.
ie.
validates :attribute1, presence: true
validates :attribute1, uniqueness: true, if: :attribute1?
validates :attribute1, numericality: true, unless: Proc.new {|r| r.attribute1.index("#") }
validates :attribute2, presence: true
...
validates :attribute7, presence: true
validate :calculations_ok?, unless: Proc.new { |a| a.dependent_attributes_valid? }
def dependent_attributes_valid?
[:attribute1, ..., :attribute7].each do |field|
self.class.validators_on(field).each do |v|
# Surely there is a better way with rails?
existing_error = v.attributes.select{|a| self.errors[a].present? }.present?
if_condition = v.options[:if]
validation_if_condition_passes = if_condition.blank?
validation_if_condition_passes ||= if_condition.class == Proc ? if_condition.call(self) : !!self.send(if_condition)
unless_condition = v.options[:unless]
validation_unless_condition_passes = unless_condition.blank?
validation_unless_condition_passes ||= unless_condition.class == Proc ? unless_condition.call(self) : !!self.send(unless_condition)
if !existing_error and validation_if_condition_passes and validation_unless_condition_passes
v.validate(self)
end
end
return false if self.errors.messages[field].present?
end
return true
end
I recall running into this issue quite some time ago, still unclear if validations order can be set and execution chain halted if a validation returns error.
I don't think Rails offers this option. It makes sense; we want to show all of the errors on the record (including those that come after a failing, due to invalid input, validation).
One possible approach is to validate only if the input to validate is present:
def within_required_range?
return unless [attribute1, attribute2, ..].all?(&:present?)
# check the calculations and return true or false here
end
Make it pretty & better structured (single responsibility) with Rails idiomatic validation options:
validates :attribute1, :presence => true
validates :attribute2, :presence => true
# and so on through the attributes
validate :calculations_ok?, if: :attributes_present?
private
def attributes_present?
[attribute1, attribute2, ..].all?(&:present?)
end
def calculations_ok?
errors[:base] << "Not within required range" unless within_required_range?
end
def within_required_range?
# check the calculations and return true or false here
end