class TimesheetForceCommit

This is not an ActiveRecord subclass.

railscasts.com/episodes/219-active-model

Attributes

earliest[RW]
earliest_limit[R]
latest[RW]
latest_limit[R]

Public Class Methods

new( attributes = {} ) click to toggle source

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

Public Instance Methods

earliest_limit=( value ) click to toggle source

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
effectively_empty( value ) click to toggle source

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
latest_limit=( value ) click to toggle source
# File app/models/timesheet_force_commit.rb, line 43
def latest_limit=( value )
  @latest_limit = to_date( value )
end
persisted?() click to toggle source
# File app/models/timesheet_force_commit.rb, line 80
def persisted?
  false
end
to_date( value ) click to toggle source

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