This is not an ActiveRecord subclass.
Boilerplate code for Rails
# File app/models/timesheet_force_commit.rb, line 73 def initialize( attributes = {} ) attributes ||= {} attributes.each do | name, value | send( "#{name}=", value ) end end
We use a private coerce-to-date function rather than Rails’ built in “#to_date” because of the latter’s bizarre behaviour when faced with invalid strings (user-meaningless exceptions arising from the internal implementation, rather than Ruby’s “invalid date”).
The values used for limits end up always coerced to dates, while the user-specified “earliest” and “latest” are left to whatever the user set. That way, re-rendering a form complaining about e.g. an invalid date will make sense by presenting the user-set data, not parsed or cleared-out variation of it.
# File app/models/timesheet_force_commit.rb, line 39 def earliest_limit=( value ) @earliest_limit = to_date( value ) end
Neither NilClass (annoyingly) or Date (sensibly) provide “empty?” but we want to detect both empty strings, or true nil, while still allowing Date instances. So pass a value and get true/false if it is, or is not, effectively empty.
# File app/models/timesheet_force_commit.rb, line 56 def effectively_empty( value ) value.nil? || value.to_s.empty? end
# File app/models/timesheet_force_commit.rb, line 43 def latest_limit=( value ) @latest_limit = to_date( value ) end
# File app/models/timesheet_force_commit.rb, line 80 def persisted? false end
Pass a Date, DateTime, Time, string etc. and have it parsed to a Date. Returns the result (or throws an exception). If the value given is effectively empty (e.g. nil, empty string) then the method returns ‘nil’.
# File app/models/timesheet_force_commit.rb, line 65 def to_date( value ) effectively_empty( value ) ? nil : Date.parse( value.to_s ) end