Skip to content

Instantly share code, notes, and snippets.

@zxiest
Created April 14, 2011 11:42
Show Gist options
  • Save zxiest/919311 to your computer and use it in GitHub Desktop.
Save zxiest/919311 to your computer and use it in GitHub Desktop.
OperationInterval
class OperationInterval < ActiveRecord::Base
include OperationIntervalsHelper
before_save :fix_values
before_update :fix_values
AMPM = [ "am", "pm" ]
HOURS = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12" ]
MINUTES = ["00", "15", "30", "45"]
DAYS = ["M", "T", "W", "Th", "F", "S", "Su"]
has_many :operation_intervals_locations
has_many :locations, :through => :operation_intervals_locations
attr_accessor :start_ampm, :end_ampm, :start_day, :end_day, :old_record
attr_accessible :start_day, :end_day, :days, :start_hour, :start_minute, :end_hour, :end_minute, :closed, :start_ampm, :end_ampm, :f_start_hour, :f_end_hour, :old_record
def start_ampm
unless start_hour.nil?
if start_hour >= 12
return "pm"
else
return "am"
end
end
end
def start_ampm=(val)
@start_ampm = val
end
def end_ampm
unless end_hour.nil?
if end_hour >= 12
return "pm"
else
return "am"
end
end
end
def end_ampm=(val)
@end_ampm = val
end
def f_start_hour
unless new_record? || start_hour.nil?
if (start_hour - 12 > 0)
(start_hour - 12)
else
start_hour
end
end
end
def f_start_hour=(val)
@f_start_hour = val
end
def f_end_hour
unless new_record? || end_hour.nil?
if (end_hour - 12 > 0)
(end_hour - 12)
else
end_hour
end
end
end
def f_end_hour=(val)
@f_end_hour = val
end
def start_day
unless new_record? || days.nil?
days.split("-").first
end
end
def start_day=(val)
@start_day = val
end
def end_day
unless new_record? || days.nil?
days.split("-").second
end
end
def end_day=(val)
@end_day = val
end
def fix_values
unless empty?
self.start_hour = (@f_start_hour.to_s + " " + @start_ampm).to_24.to_i
self.end_hour = (@f_end_hour.to_s + " " + @end_ampm).to_24.to_i
self.days = @start_day + (!@end_day.empty?? "-" + @end_day : "")
end
end
def empty?
start_day.nil? || start_day.empty?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment