Skip to content

Instantly share code, notes, and snippets.

@flyingoctopus
Created July 19, 2012 19:02
Show Gist options
  • Save flyingoctopus/3146029 to your computer and use it in GitHub Desktop.
Save flyingoctopus/3146029 to your computer and use it in GitHub Desktop.
event time fuckaduck
#event
= error_messages_for @event
.row
= form_for @event, :class => 'form-vertical' do |form|
.span8
.row
.span4
= form.label 'Date'
= form.date_select :start_date, {}, :class=>"span1"
.span4
= form.label 'Until (optional)'
= form.date_select :end_date, {:include_blank => true}, :class=>"span1"
.row
.span4
= form.label 'Time'
= form.time_select :start_time, {}, :class=>"span1"
.span4
= form.label 'Until (optional)'
= form.time_select :end_time, {:include_blank => true}, :class=>"span1"
= form.label :time_zone
= time_zone_select 'essentials_event', :time_zone, nil, {:default=>@event.time_zone}, :class=>"span4"
%div
= button_tag t('events.submit'), :class => 'btn', :style => 'margin-top:20px'
class Essentials::Event
include Mongoid::Document
include Mongoid::Timestamps
field :start_date, :type => Date
field :end_date, :type => Date
field :start_time, :type => Time
field :end_time, :type => Time
field :time_zone, :type => String
validates_presence_of :title, :body, :summary, :start_date, :start_time, :time_zone
validate :date_range
validate :time_range
def get_date_range
if start_date && end_date && (start_date != end_date)
"#{start_date.strftime('%m/%d/%Y')} - #{end_date.strftime('%m/%d/%Y')}"
else
"#{start_date.strftime('%m/%d/%Y')}"
end
end
def get_time_range
start_with_zone = ActiveSupport::TimeWithZone.new(start_time.getutc, ActiveSupport::TimeZone.new(time_zone))
if start_time && end_time
end_with_zone = ActiveSupport::TimeWithZone.new(end_time.getutc, ActiveSupport::TimeZone.new(time_zone))
"#{start_with_zone.strftime('%H:%M %Z')} - #{end_with_zone.strftime('%H:%M %Z')}"
else
"#{start_with_zone.strftime('%H:%M %Z')}"
end
end
private
def time_range
# Get rid of day and year!
self.start_time = Time.parse("#{self.start_time.hour}:#{self.start_time.min}") if self.start_time
self.end_time = Time.parse("#{self.end_time.hour}:#{self.end_time.min}") if self.end_time
if self.end_time
errors.add(:end_time, I18n.t('errors.days.time.invalid_range', {:start_time => start_time})) if (start_time && end_time && (start_date == end_date) && (end_time <= start_time))
end
end
end
ree-1.8.7-2011.03 :001 > e = Essentials::Event.create( {:start_date => "Thu Jul 19 00:00:00 UTC 2012", :group_name => "Boston Sites Program", :group => "4f84bec18d5362c8ba000001", :location => "borkbork", :end_time => nil, :created_at => "Thu Jul 19 18:28:36 UTC 2012", :body => " asdfasdf ", :title => "bork", :updated_at => "Thu Jul 19 18:39:20 UTC 2012", :user_type => "grantee", :highlighted => false, :time_zone => "Mountain Time (US & Canada)", :program => nil, :summary => "asdfasdf", :start_time => "Thu Jul 19 19:00:00 UTC 2012", :active => false} )
=> #<Essentials::Event _id: 5008594bc97d7d1561000001, start_date: Thu Jul 19 00:00:00 UTC 2012, group_name: "Boston Sites Program", group: "4f84bec18d5362c8ba000001", location: "borkbork", end_time: nil, created_at: Thu Jul 19 18:28:36 UTC 2012, body: " asdfasdf ", title: "bork", updated_at: Thu Jul 19 18:39:20 UTC 2012, user_type: "administrator", _type: "Essentials::Event", highlighted: false, time_zone: "Mountain Time (US & Canada)", program: nil, summary: "asdfasdf", end_date: nil, start_time: Thu Jul 19 19:00:00 UTC 2012, active: false, grantee_id: nil>
ree-1.8.7-2011.03 :002 > e.start_time.utc
=> Thu Jul 19 19:00:00 UTC 2012
ree-1.8.7-2011.03 :003 > e.time_zone
=> "Mountain Time (US & Canada)"
ree-1.8.7-2011.03 :004 > e.get_time_range
=> "13:00 MDT"
ree-1.8.7-2011.03 :005 > e.start_time
=> Thu Jul 19 12:00:00 -0700 2012
#event
.row
.span8
#event
.time
%b Event Date:
= @event.get_date_range
&nbsp;&nbsp;&nbsp;&nbsp;
%b Event Time:
= @event.get_time_range
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment