Merb and Chronic: Natural Language Date Parsing

This assumes you’ve already got started with merb, and that you are using datamapper as your orm.

NOTE: This DOESN’T validate the chronicfied date yet so strange words like gobblygoob will still get passed, and the form will get saved.

0.Install chronic gem

1 sudo gem install chronic
      

1 sudo gem install chronic

1. In init.rb make sure you include the chronic gem as well as the following gems.

1 dependencies "merb-more", "merb_helpers", "merb-assets", "dm-timestamps", "dm-validations", "chronic" 
      

1 dependencies "merb-more", "merb_helpers", "merb-assets", "dm-timestamps", "dm-validations", "chronic" 

2. In your model.rb chronicfy the date with the datamapper before_valid? hook

 1   class Post
       2   include DataMapper::Resource
       3   
       4   property :id, Integer, :serial => true # A :serial property is an auto-incrementing key
       5   property :date, Date
       6   property :title, :String
       7   property :description, String
       8   property :created_at, DateTime
       9   property :updated_at, DateTime
      10   
      11   # see http://datamapper.org/docs/validations.html though there must be a way to refactor this cleaner
      12   validates_with_method :must_be_chronic
      13   def must_be_chronic
      14     if Chronic.parse(self.date).nil?
      15       [false, "is not a valid date. Try removing any commas."]
      16     else
      17       return true
      18     end
      19   end
      20 
      21   before :valid?, :chronicfy
      22   def chronicfy 
      23     self.date = Chronic.parse(self.date)
      24   end
      25   
      26 end
      

 1   class Post
2 include DataMapper::Resource
3
4 property :id, Integer, :serial => true # A :serial property is an auto-incrementing key
5 property :date, Date
6 property :title, :String
7 property :description, String
8 property :created_at, DateTime
9 property :updated_at, DateTime
10
11 # see http://datamapper.org/docs/validations.html though there must be a way to refactor this cleaner
12 validates_with_method :must_be_chronic
13 def must_be_chronic
14 if Chronic.parse(self.date).nil?
15 [false, "is not a valid date. Try removing any commas."]
16 else
17 return true
18 end
19 end
20
21 before :valid?, :chronicfy
22 def chronicfy
23 self.date = Chronic.parse(self.date)
24 end
25
26 end

3. In your view, change the date field to a text_field.

 1 <h1>Posts controller, new action</h1>
       2 
       3 <p>Edit this file in <tt>app/views/posts/new.html.erb</tt></p>
       4 
       5 <%= form_for @post, :class => "post_form", :action => url(:posts) do %>
       6         <%= partial("posts/form") %>
       7 <% end =%>
       8 
       9 
      10 # here's the _form.rhtml partial
      11 <%= error_messages_for @post %>
      12 
      13 <p>
      14 <%= text_field :date, :label => "Date<br />" %>
      15 </p>
      16 
      17 <p>
      18 <%= text_field :title, :label => "Title<br />" %>
      19 </p>
      20 
      21 <p>
      22 <%= text_field :description, :label => "Description<br />" %>
      23 </p>
      24 
      

 1 <h1>Posts controller, new action</h1>
2
3 <p>Edit this file in <tt>app
/
views/posts/new.html.erb</tt></p>
4
5 <%= form_for @post, :class => "post_form", :action => url(:posts) do %>
6 <%= partial("posts/form") >

7 < end
=%>
8
9
10 # here’s the form.rhtml partial
11 <%= error_messages_for @post >

12
13 <p>
14 <= text_field :date, :label =
> "Date<br />" >
15 </p>
16
17 <p>
18 <
= text_field :title, :label => "Title<br
/
>" >
19 </p>
20
21 <p>
22 <
= text
field :description, :label => "Description<br />" %>
23 <
/
p>
24

Picture of Scott Motte

delicious facebook rss twitter

Spitfire Sky | github | archives | resume