Skip to content

Instantly share code, notes, and snippets.

@aaronmoodie
Forked from dnch/gist:5392985
Last active December 16, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronmoodie/5401127 to your computer and use it in GitHub Desktop.
Save aaronmoodie/5401127 to your computer and use it in GitHub Desktop.
class Show < ActiveRecord::Base
attr_accessible :artist_tokens, :cost, :booking_url, :door_charge, :venue_id, :date, :time, :starts_at
attr_reader :artist_tokens, :date, :time
validate :is_time
validate :is_date
before_save :save_date_and_time
has_many :performances
has_many :artists, :through => :performances
belongs_to :user
belongs_to :venue
def artist_tokens=(tokens)
self.artist_ids = Artist.ids_from_tokens(tokens)
end
def date
if @date
@date
else
starts_at ? starts_at.in_time_zone(Venue.get_venue_timezone(self.venue_id)).strftime("%A, %B #{starts_at.day.ordinalize}") : ""
end
end
def date=(d)
@date = d
end
def time
if @time
@time
else
starts_at ? starts_at.in_time_zone(Venue.get_venue_timezone(self.venue_id)).strftime("%l:%M %p") : ""
end
end
def time=(t)
@time = t
end
private
def is_time
errors.add(:time, 'must be a valid time') if ((DateTime.parse(@time) rescue ArgumentError) == ArgumentError)
end
def is_date
errors.add(:date, 'must be a valid date') if ((DateTime.parse(@date) rescue ArgumentError) == ArgumentError)
end
def save_date_and_time
time_zone = Venue.get_venue_timezone(venue_id)
self.starts_at = ActiveSupport::TimeZone[time_zone].parse(@date + " " + @time)
end
end
@aaronmoodie
Copy link
Author

When is date called? On form render on on submit? I don't understand why it would be called on new?

date and time in the save_date_and_time instances are blank, which makes me think they are being pulled from the DB (via the above methods) rather than using form params?

@dnch
Copy link

dnch commented Apr 17, 2013

Line 36 should probably use the instance variables, rather than the getter method:

starts_at = Chronic.parse(@date + @time)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment