Validate Dates in Your Models 3

Posted by Bob Silva Fri, 08 Sep 2006 06:11:00 GMT

mojombo in #caboose, just released his first version of Chronic, a new Ruby Gem for natural language processing of Dates and Times.
gem install chronic

irb:>
Chronic.parse('tomorrow')
    #=> Mon Aug 28 12:00:00 PDT 2006

Chronic.parse('monday', :context => :past)
    #=> Mon Aug 21 12:00:00 PDT 2006

Chronic.parse('this tuesday 5:00')
    #=> Tue Aug 29 17:00:00 PDT 2006

Chronic.parse('this tuesday 5:00', :ambiguous_time_range => :none)
    #=> Tue Aug 29 05:00:00 PDT 2006

Chronic.parse('may 27th', :now => Time.local(2000, 1, 1))
    #=> Sat May 27 12:00:00 PDT 2000

Chronic.parse('may 27th', :guess => false)
    #=> Sun May 27 00:00:00 PDT 2007..Mon May 28 00:00:00 PDT 2007

The thing I find really nice is that if you pass it a malformed date, it returns nil. This means, in your model validations, you can add:
require 'chronic'

class Meeting < ActiveRecord::Base

  def validation
    errors.add :meeting_date, 'is not a valid date' if Chronic.parse(meeting_date.to_s).nil?
  end

end

ActiveRecord UML Diagrams 3

Posted by Bob Silva Sun, 30 Jul 2006 23:57:00 GMT

Boredom set in this weekend so while playing with my UML modeling software, I decided to model some pieces of ActiveRecord. I found that UML doesn't lend itself too well to the Ruby language. Since UML doesn't provide for mixins and modules, I modeled them as stereotyped dependencies and classes. Don't know if they are useful for anyone but they are nice to have for a quick reference to figure out how ActiveRecord is structured with all the mixins and modules.


ActiveRecord

Associations

ConnectionAdapters

Exceptions

Handling Invalid Dates with ActiveRecord Date Helpers 3

Posted by Bob Silva Thu, 23 Feb 2006 03:42:00 GMT

If you've worked with the model based date helpers in Rails (date_select and datetime_select), then you may know that selecting an invalid date throws an exception deep within ActiveRecord. A lot of users assume they can handle this in the model validation, but due to the way ActiveRecord handles multi-parameter input values, it will raise an exception if an invalid date is input. Here's some sample code of how you can catch and handle this exception without affecting the user experience too much.
In controller.rb:

def create
  begin
    @client = Client.new(params[:client])
  # Catch the exception from AR::Base
  rescue ActiveRecord::MultiparameterAssignmentErrors => ex
    # Iterarate over the exceptions and remove the invalid field components from the input
    ex.errors.each { |err| params[:client].delete_if { |key, value| key =~ /^#{err.attribute}/ } }
    # Recreate the Model with the bad input fields removed
    @client = Client.new(params[:client])      
  end
    
  if @client.save
    flash[:notice] = 'Client was successfully created.'
    redirect_to :action => 'list'
  else
    render :action => 'new'
  end
end

In model.rb:

validates_presence_of :date_field_name, :message => 'must be a valid date'