Recent News
Time Parseable
Nobody likes dropdowns. datetime_select and date_select? Ugly troupes of unwieldy elements, each of them. They form a line and rudely spit lists of numbers at you. Who wants that? I know how to type a date. Rails knows how to parse a date. Time.parse, let’s be friends!
$ script/plugin install git://github.com/cementhorses/time_parseable.git
Now, let’s say we have this object filled with timestamp attributes that we need to manage…
# create_table do |t|
# t.timestamp :published_at, :archived_at
# end
class NewsItem < ActiveRecord::Base
time_parseable
end
What? NewsItem now has published_at_string and archived_at_string? Yup. And on assignment, they are automatically parsed with Time.parse.
news_item.published_at_string = 'April 30, 2008'
news_item.published_at # => Wed Apr 30 00:00:00 -0000 2008
If you’re only parsing the published_at column, and magic is looking a bit too expensive for you, scope things down.
time_parseable :published_at
Wonderful.
Wait a second! Time.parse, you’re a good friend, but what are you doing?
Time.parse('Cement Horses') == Time.now # => true
Yes, “Cement Horses” is now, but I only want dates for dates. time_parseable, please work this out.
news_item.archived_at_string = 'Cement Horses'
news_item.archived_at # => nil
news_item.errors.on(:archived_at) # => 'is invalid'
Let’s just throw things in a form_for, and time_parseable will do the rest.
<%= f.label :published_at_string %>
<%= f.text_field :published_at_string %>
The field, once assigned, will return a strftime-formatted result. We can choose the format (but we must choose wisely—it should be able to parse to the same result).
time_parseable :format => '%I:%M %p on %b %d, %Y'
That’s it for now. Cutting down the form fields, one time at a time.
written by scelis on July 23rd, 2008 @ 04:11 PM
RoR: Schedulable
What it is.
If you have data that must go live on a specific date; If you have a model object you would like to query for it’s published state; If you would like the functions of publishing and un-publishing content to be precise and understandable, the Schedulable plugin may be for you. In short, adding Schedulable to your models will allow you to manage the publishing of your data through the liberal use of semantic sugar. The goal of the Schedulable plugin is to be intuitive, concise and easy while solving an often necessary and crucial business need: Scheduling your content.
How it works.
Calling schedulable automatically hooks a few methods to a published_at column:
scheduled?(trueif the item is scheduled to be published but isn’t yet)published?(trueif the item is published)
The real power is when expiration is a question
schedulable :end => :archived_at
or
schedulable :published_at, :archived_at
does a little more:
scheduled? :archived_at(trueifarchived_atis set in the future)archived?(trueif the item has been archived)
with a note:
published?returnsfalsewhenarchived?returnstrue.
It’s all semantic sugar:
schedulable :activated_at, :terminated_at, :end_required => true
therefore creates:
activated?terminated?
And that last option? It just adds a validation requiring the end to be set if the start is.
We have your other validations taken care of, too. We’re just more comfortable with things when they’re chronological.
How can I get it
On Edge:
$ script/plugin install git://github.com/cementhorses/schedulable.git
…otherwise
$ git clone git@github.com:cementhorses/schedulable.git vendor/plugins/schedulable
written by dbora on May 29th, 2008 @ 08:30 PM
cementhorses.com
Recent
- Engineering happiness (August 15th)
- The future with gestures (August 14th)
- Debugging Kentico sites in vs.net 2008/05 improve compilation time (August 14th)
- Resistance ...is a good thing? (August 13th)
- Managing dashboard widgets on various external displays (August 1st)
- Counting in CSS (July 28th)
- Take control of your server configuration with Capistrano (July 23rd)
- Time Parseable (July 23rd)
- An SSH one-liner to authorize via public-key (June 23rd)
- MicroApps (or little ditties) with Sinatra (June 17th)
- RoR: Schedulable (May 29th)
- Less work, more be-ing with quick shell aliasing (May 8th)
- Interceptor Methods (May 7th)
Comments
- Samuel Pires on Engineering happiness
- Brock on Engineering happiness
- Colton Sanches on Engineering happiness
- Caleb Mathews on Engineering happiness
- Collins on Engineering happiness
- Struye on Engineering happiness
- Jordan on Engineering happiness
- Aidan McDonough on Engineering happiness
- Tremonti on Engineering happiness
- Mathews on Engineering happiness
- Aiden Jones on Engineering happiness
- Rachman on Engineering happiness
- Perle on Engineering happiness
- Veer on Engineering happiness
- Robinson on Engineering happiness
Categories
- Stuff You Already Know (6)
- Open Source (2)
Cement Horses Pics
Archives
- August 2008 (5)
- July 2008 (3)
- June 2008 (2)
- May 2008 (3)
