How to translate with parameter in rails errors? - ruby-on-rails

I have an error message that needs to be translated along with parameters. What is the best way to do it in rails?
dates = '12th June - 14th June'
errors.add(:base, :reserved_dates)
From en.yml
activemodel:
errors:
models:
reserve_changes:
reserved_dates: "Dates changes from %{dates}"
Expected output:
Dates changes from 12th June - 14th June
When running this way I only get Dates changes from without the passed parameters.

You need to pass in the dates as an option to the errors.add:
dates = '12th June - 14th June'
errors.add(:base, :reserved_dates, dates: dates)
Otherwise the translator has no access to the string you want to substitute.

Related

Ruby date time parse to get 'th'

I've got date as string '2020-02-10 8,00' which I want to convert into Monday, 10th of February. I'm aware of this old topic however I cannot find (or use) any related information.
All I have is just parsed string to date - Date.parse '2020-02-10 8,00'
You are halfway there! Date.parse '2020-02-10 8,00' produces a ruby Date object, as you have noted. You now have to apply strftime. However strftime doesn't have any ordinalization so that piece has to be done manually.
date = Date.parse('2020-02-10 8,00')
date.strftime("%A, #{date.day.ordinalize} of %B") #=> Monday, 10th of February
the ordinalize method is provided by ActiveSupport.
If this format will be used multiple times in your app, you may wish to add an app-wide format:
# in config/initializers/time_formats.rb
Date::DATE_FORMATS(:ordinalized_day) = lambda{|date| date.strftime("%A, #{date.day.ordinalize} of %B")}
# anywhere in the app
Date.today.to_formatted_s(:ordinalized_day)

Rails - how to throw explicit error when setting / persisting invalid date [duplicate]

This question already has answers here:
How do I validate a date in rails?
(10 answers)
Closed 7 years ago.
Okay so September has 30 days right.
This is what rails does when you set an invalid date like 31 September:
s = Student.first
s.date_of_birth = "2015-09-31"
s.save!
> true
s.date_of_birth
> Thu, 01 Oct 2015 00:00:00 UTC +00:00
It sets the date to 1st of October.
What I want is something like this
s.save!
(0.4ms) BEGIN
(0.5ms) ROLLBACK
ActiveRecord::RecordInvalid: Validation failed: Date is not a valid date
Is there no way to force Rails to explicitly error out when setting such a crap date? The standard rails date_select does not dynamically change the number of days in the days dropdown when you change to a different month. So you can select 31 September with the dropdowns, and the date will be saved as 1st of October. But I want to display an error message.
I don't want people to accidentally put the wrong dates into my system.
I think one of the solutions would be to change the datatype of date_of_birth from datetime to date. This way Rails would validate the date and would not allow the situation, you've shown.
It is also a good practice to store dates in date type, because it leads to significant decrease in potential issues, when working with those dates.
Try adding this to your model:
def date_of_birth=(d)
date = Date.strptime(d.to_s)
send(:write_attribute, :date_of_birth, date)
end
Now if you try setting an invalid date, it should raise an exception:
s.date_of_birth = "2015-09-31" # raises an exception
However, I don't think that doing this is a good idea. It's probably a better idea to do as Andrey Deineko said and store it in a date type.

Date format bug when date is in Portuguese?

I'm seeing some odd behavior in our application coming from a user working in what I believe to be Portuguese.
I realized that Rails seems to be mis-interpreting the dates she is submitting. Here's some examples:
Date.parse("ter, 30 abr 2013 07:00:00 GMT-07:00")
=> Thu, 30 May 2013
Date.parse("dom, 5 mai 2013 07:00:00 GMT-07:00 -07:00")
ArgumentError: invalid date
Is there something extra I need to do correctly identify dates being submitted in other languages?
Date are parsed with Ruby Date class with the following documentation
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#method-c-parse
I'm not sure it's a great idea to allow your users to type in the full date by themselves - they can mistype or assume different format. I suggest to use datepicker that with localized presentation based on the users location? One of the the many date pickers that allow this is http://docs.jquery.com/UI/Datepicker/Localization

It is possible to change or disable Date validator in jira

I change calendar javascript to Hijri, But another problem comes up. It seems that a class (DueDateValidator.class) try to validate input date, while the number of days in month is different to georgian calendar. for example we have 31 days in second month of year but in georgian (february) it is 28 or 29.
I don't think there is an out-of-the-box option to cancel Jira's date validation. You could overwrite Jira's date validation, but I think it will be easier to use a Free Text Field custom field that will hold the date as a string. Then, add to the field js code that will hide this field and copy it's date to the date field.
To keep the right order when searching for issues, you can either use date mapping to keep the georgian calendar up to date, or store the dates in a sortable way (for example YYYY/MM/DD/HH/MM - 201302161334) and order the results according to this field.
UPDATE
Simple example for Jira version 5.2.6, this will copy the text from field id customfield_10001 to the created field:
AJS.$("#create-date").text(AJS.$("#customfield_10001-val").text().trim())
To search easily keep another text field and save the date in the following format :
year month day hour minute
All in digits. for example, today's Gregorian date would be:
2013 02 267 10 26
than, when searching for issues, for example to find issues created after today's date:
custom_filed > "2013 02 267 10 26"
that sorting will work since it will sort issues first by year, than month, day, hour, minute.
I stored date in text field by creating new custom field in system-customfieldtypes-plugin.xml filed but In search it only accept exact text not > or <
custom_filed ~ "2013 02 267 10 26" It is acceptable but
custom_filed > "2013 02 267 10 26" shows jQL error.

Validations for excluded dates.

My Code works. The problem I have here is that dates like November 31 and April 31 can occur every year from 2010 down to 1995 which means I have to type in all these dates e.g [Date.new(2012,09,31),Date.new(2011,09,31)] and so on. I am trying to make this validation work for only the month and the day to avoid too much typing or in other words shorten my code please see a description of my
Model/profile.rb
validate :excluded_dates
private
def excluded_dates
exclusion_dates = [Date.new(2012,9,31), Date.new(2012,2,1)]
if exclusion_dates.include?(self.next_shoeing)
self.errors.add(:next_shoeing, "cannot be on a reserved date.")
end
First of all doing Date.new(2012,9,31) will throw you an ArgumentError: invalid date. IMO there is no need to validate this as no one will be able to construct such date (just handling this exception outside this model).
So if next_shoeing is an instance of Date it won't be invalid anyway.

Resources