-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring of event and event_series model completed
- Loading branch information
aditya-kapoor
committed
Jan 7, 2014
1 parent
e8a1c43
commit 1070ee4
Showing
2 changed files
with
53 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,58 @@ | ||
module FullcalendarEngine | ||
class EventSeries < ActiveRecord::Base | ||
attr_accessor :title, :description, :commit_button | ||
|
||
validates_presence_of :frequency, :period, :starttime, :endtime | ||
validates_presence_of :title, :description | ||
|
||
|
||
validates :frequency, :period, :starttime, :endtime, :title, :description, :presence => true | ||
|
||
has_many :events, :dependent => :destroy | ||
|
||
after_create :create_events_until_end_time | ||
|
||
def create_events_until_end_time(end_time=RECURRING_EVENTS_UPTO) | ||
st = starttime | ||
et = endtime | ||
p = r_period(period) | ||
nst, net = st, et | ||
|
||
while frequency.send(p).from_now(st) <= end_time | ||
# puts "#{nst} ::::::::: #{net}" if nst and net | ||
self.events.create(:title => title, :description => description, :all_day => all_day, :starttime => nst, :endtime => net) | ||
nst = st = frequency.send(p).from_now(st) | ||
net = et = frequency.send(p).from_now(et) | ||
|
||
old_start_time = starttime | ||
old_end_time = endtime | ||
frequency_period = recurring_period(period) | ||
new_start_time, new_end_time = old_start_time, old_end_time | ||
|
||
while frequency.send(frequency_period).from_now(old_start_time) <= end_time | ||
self.events.create( | ||
:title => title, | ||
:description => description, | ||
:all_day => all_day, | ||
:starttime => new_start_time, | ||
:endtime => new_end_time | ||
) | ||
new_start_time = old_start_time = frequency.send(frequency_period).from_now(old_start_time) | ||
new_end_time = old_end_time = frequency.send(frequency_period).from_now(old_end_time) | ||
|
||
if period.downcase == 'monthly' or period.downcase == 'yearly' | ||
begin | ||
nst = DateTime.parse("#{starttime.hour}:#{starttime.min}:#{starttime.sec}, #{starttime.day}-#{st.month}-#{st.year}") | ||
net = DateTime.parse("#{endtime.hour}:#{endtime.min}:#{endtime.sec}, #{endtime.day}-#{et.month}-#{et.year}") | ||
new_start_time = make_date_time(starttime, old_start_time) | ||
new_end_time = make_date_time(endtime, old_end_time) | ||
rescue | ||
nst = net = nil | ||
new_start_time = new_end_time = nil | ||
end | ||
end | ||
end | ||
end | ||
def r_period(period) | ||
|
||
def recurring_period(period) | ||
case period | ||
when 'Daily' | ||
p = 'days' | ||
when "Weekly" | ||
p = 'weeks' | ||
when "Monthly" | ||
p = 'months' | ||
when "Yearly" | ||
p = 'years' | ||
'days' | ||
when 'Weekly' | ||
'weeks' | ||
when 'Monthly' | ||
'months' | ||
when 'Yearly' | ||
'years' | ||
end | ||
return p | ||
end | ||
|
||
private | ||
|
||
def make_date_time(original_time, difference_time) | ||
DateTime.parse("#{original_time.hour}:#{original_time.min}:#{original_time.sec}, #{original_time.day}-#{difference_time.month}-#{difference_time.year}") | ||
end | ||
end | ||
end |