Skip to content

Instantly share code, notes, and snippets.

@ryanjm
Created April 6, 2012 14:41
Show Gist options
  • Select an option

  • Save ryanjm/2320468 to your computer and use it in GitHub Desktop.

Select an option

Save ryanjm/2320468 to your computer and use it in GitHub Desktop.
Special variables for dates
# == Schema Information
#
# Table name: showings
#
# id :integer not null, primary key
# start_time :datetime
# end_time :datetime
class Showing < ActiveRecord::Base
attr_accessor :showing_date, :start_date, :end_date
validates :showing_date, :start_date, :end_date, :presence => true
before_save :set_dates
# used to set the instance variables for editing the form
after_find :set_times
def set_times
self.showing_date = self.start_time.strftime("%A, %B %d, %Y") unless self.start_time.nil?
self.start_date = self.start_time.strftime("%-I:%M%p") unless self.start_time.nil?
self.end_date = self.end_time.strftime("%-I:%M%p") unless self.end_time.nil?
end
private
def set_dates
self.start_time = DateTime.strptime("#{start_date} #{showing_date} #{Time.now.zone}", "%I:%M%p %A, %B %d, %Y %z")
self.end_time = DateTime.strptime("#{end_date} #{showing_date} #{Time.now.zone}", "%I:%M%p %A, %B %d, %Y %z")
end
end
@amnesia7
Copy link
Copy Markdown

amnesia7 commented Apr 7, 2012

Here's the basics of my showtime model now:

class Showtime < ActiveRecord::Base

  attr_accessible   :start_date, :start_time

  attr_accessor   :starting_at, :starting_time

  belongs_to :performance

  before_validation :construct_starting_at

  validates :start_date,  :presence => true,  :date => { :after_or_equal_to => Proc.new { Date.today } }
  validates :starting_at, :date => { :after_or_equal_to => Proc.new { Time.now } }, :if => "start_date.present? && start_time.present?"

  def construct_starting_at
    if start_date.present? && start_time.present?
      self.starting_time = self.start_time.strftime("%T")
      self.starting_at = DateTime.strptime("#{start_date} #{starting_time}", "%F %T")
    end
  end

end

In case it is required, here's the basics/relevant parts of the Performance model:

class Performance < ActiveRecord::Base

  has_many :showtimes, :dependent => :delete_all
  accepts_nested_attributes_for :showtimes, :allow_destroy => true, :reject_if => lambda { |a| a[:start_date].blank? }

end

Here's the failing test:

require 'spec_helper'

describe Showtime do

  before(:each) do
    @attr = {
        :name => "Performance 1",
        :showtimes_attributes => {
              "0" => {
                  :start_date => Date.today+15.days,
                  :start_time => Time.now
              }
        }
    }
  end

  it "should test that the start time is a valid time if it exists" do
    @attr_invalid = {
        "0" => {
            :start_date => Date.today+15.days,
            :start_time => "invalid"
        }
    }
    invalid = Performance.create(@attr.merge(:showtimes_attributes => @attr_invalid))
    invalid.should_not be_valid
  end

end

Hopefully I've not broken anything in the above code in my attempt to just show the necessary parts.

@ryanjm
Copy link
Copy Markdown
Author

ryanjm commented Apr 24, 2012

I'm sorry, I've been pretty busy lately. Did you ever get this figured out?

@amnesia7
Copy link
Copy Markdown

Hey, no not yet.

I keep adding updates to my SO post (http://stackoverflow.com/questions/9955977/validate-date-and-time-fields-together-in-rails-model/) but no joy yet.

@amnesia7
Copy link
Copy Markdown

amnesia7 commented May 2, 2012

I'm starting to give up on the idea of seperate date and time fields so I've started converting my app to use a single datetime field/db column to see if that is a better option for me.

I'm now on the lookout for a datetime picker since I need the datetime to be in a textfield but I don't think the date part and time part can be split easily. Before I was using a datepicker on a textfield for the date and dropdowns for the time.

@amnesia7
Copy link
Copy Markdown

In the end I used a merged datetime text_field and the code here: codegram/date_validator#25 (comment)

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