I'm using Reform gem. Let's say I have some form to which I pass two dates as two separate attributes in params. Now in form I want to instantiate another attribute (which is also existing model attribute) called interval to calculate interval between these two dates and then save it to model.
# some_form.rb
class SomeForm < Reform::Form
property: :starts_on
property: :ends_on
property: :interval
end
What is the best way to populate interval based on incoming dates, validate it and save it on model?
I tried different things, from overriding validate method and using populator/prepopulator options, but to no avail.
In best scenario, I would assume it would look like this:
class SomeForm < Reform::Form
property: :starts_on
property: :ends_on
property: :interval, populate: :calculate_interval
end
def calcute_interval
(starts_on..ends_on).count
end
populatorandprepopulatorassume that the relevant properties are present in the incoming data. In your case,intervalis not.Do you want to validate the
intervalvalue? If not, calculateintervalin anOperationstep and don't involvereform.If you want validation, your options are:
a
populatoron either timestamp:a custom predicate+rule combo: