Skip to content

Commit

Permalink
Updated Event time parsing to use the Chronic gem
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesbrooks committed Oct 29, 2008
1 parent 2ac2657 commit b205992
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ paperclip
tiny_mce (check out http://github.com/kete/tiny_mce)

will_paginate (not required but strongly recommended, but will enhance post navigation functionality)

chronic (not required but enhanced time parsing for the calendar)

Example
=======
Expand Down
22 changes: 19 additions & 3 deletions lib/controllers/hush_cms_admin/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,25 @@ class HushCmsAdmin::EventsController < HushCmsAdminController


def index
paginate_method = if defined?(WillPaginate)
[ :paginate, { :per_page => 20, :page => params[:page] } ]
else
[ :all ]
end

@upcoming_events = true
@events = @calendar.events.upcoming
@events = @calendar.events.upcoming.send(*paginate_method)
end

def all
paginate_method = if defined?(WillPaginate)
[ :paginate, { :per_page => 20, :page => params[:page] } ]
else
[ :all ]
end

@all_events = true
@events = @calendar.events
@events = @calendar.events.send(*paginate_method)

render :template => 'hush_cms_admin/events/index'
end
Expand All @@ -23,10 +35,12 @@ def new
end

def create
chronisize params[:hush_cms_event][:start_time], params[:hush_cms_event][:finish_time]

@event = @calendar.events.build(params[:hush_cms_event])

if @event.save
redirect_to hush_cms_calendar_events_url(@calendar)
redirect_to hush_cms_admin_calendar_events_url(@calendar)
else
prepare_error_messages_for_javascript @event
render :action => 'new'
Expand All @@ -37,6 +51,8 @@ def edit
end

def update
chronisize params[:hush_cms_event][:start_time], params[:hush_cms_event][:finish_time]

if @event.update_attributes(params[:hush_cms_event])
redirect_to hush_cms_calendar_events_url(@calendar)
else
Expand Down
10 changes: 10 additions & 0 deletions lib/controllers/hush_cms_admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ def prepare_error_messages_for_javascript(obj)
@js_model_errors << "'#{attribute.gsub(/["']/) { |m| "\\#{m}" }}': '#{error.gsub(/["']/) { |m| "\\#{m}" }}'"
end
end

def chronisize(*attributes)
if defined?(Chronic)
attributes.each do |attribute|
if attribute && chronisized_value = Chronic.parse(attribute)
attribute.replace chronisized_value.to_s(:hush_time).gsub(/^0/, '').downcase
end
end
end
end

private
def authenticate
Expand Down
1 change: 1 addition & 0 deletions lib/hush_cms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def load
end

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge! :hush_date => "%b %d, %Y"
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge! :hush_time => "%I:%M%p"

validate_configuration
end
Expand Down
2 changes: 1 addition & 1 deletion lib/models/hush_cms/calendar.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class HushCMS::Calendar < ActiveRecord::Base
set_table_name 'hush_cms_calendars'

has_many :events, :class_name => 'HushCMS::Event', :order => 'start ASC'
has_many :events, :class_name => 'HushCMS::Event', :order => 'start_date ASC, start_time ASC'

validates_presence_of :name, :slug
validates_uniqueness_of :name, :slug
Expand Down

0 comments on commit b205992

Please sign in to comment.