Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Occurrence over night #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ PATH
specs:
schedule_attributes (0.3.0)
activesupport
ice_cube (>= 0.10.0)
ice_cube (>= 0.12)
tzinfo

GEM
Expand All @@ -25,7 +25,7 @@ GEM
coderay (1.0.9)
diff-lcs (1.2.4)
i18n (0.6.1)
ice_cube (0.10.0)
ice_cube (0.12.1)
method_source (0.8.1)
multi_json (1.7.3)
pry (0.9.12.1)
Expand Down
6 changes: 4 additions & 2 deletions lib/schedule_attributes/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def schedule_attributes
atts[:ends] = 'never'
end

if months = rule.validations_for(:month_of_year).map(&:month)
months = rule.validations_for(:month_of_year).map(&:month)
if months.present?
atts[:yearly_start_month] = months.first
atts[:yearly_end_month] = months.last

Expand All @@ -127,7 +128,8 @@ def schedule_attributes
end
end
end

else
rule.replace_validations_for(:month_of_year, nil)
end
else
atts[:repeat] = 0
Expand Down
10 changes: 9 additions & 1 deletion lib/schedule_attributes/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ def start_time
def end_time
return nil if @params[:all_day]
return nil unless @params[:end_time].present?
parse_date_time(date_input, @params[:end_time])
parse_date_time(end_time_date, @params[:end_time])
end

# if end_time < start_time, the schedule occurs over night
def end_time_date
return date_input unless @params[:start_time].present?
return nil if @params[:all_day]
return nil unless @params[:end_time].present?
parse_date_time(date_input, @params[:end_time]) <= start_time ? (Time.parse(date_input) + 1.day).strftime('%Y-%m-%d') : date_input
end

def start_date
Expand Down
15 changes: 10 additions & 5 deletions lib/schedule_attributes/time_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ module ScheduleAttributes
module TimeHelpers
def self.parse_in_zone(str)
if Time.respond_to?(:zone) && Time.zone
return str.in_time_zone if str.is_a?(Time)
str.is_a?(Date) ? str.to_time_in_current_zone : Time.zone.parse(str)
# Rails 4.1 removes Date.to_time_in_current_zone in favour of Date.in_time_zone
if str.respond_to?(:in_time_zone)
str.in_time_zone
else
str.is_a?(Date) ? str.to_time_in_current_zone : Time.zone.parse(str)
end
else
return str if str.is_a?(Time)
Time.parse(str)
str.is_a?(Time) ? str : Time.parse(str)
end
end

def self.today
if Time.respond_to?(:zone) && Time.zone
Date.current.to_time_in_current_zone
# Rails 4.1 removes Date.to_time_in_current_zone in favour of Date.in_time_zone
current_date = Date.current
current_date.respond_to?(:in_time_zone) ? current_date.in_time_zone : current_date.to_time_in_current_zone
else
Date.today.to_time
end
Expand Down
2 changes: 1 addition & 1 deletion schedule_attributes.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency 'ice_cube', '>= 0.10.0'
s.add_dependency 'ice_cube', '>= 0.12'
s.add_dependency 'activesupport'
s.add_dependency 'tzinfo' # this should be an activesupport dependency!

Expand Down
15 changes: 15 additions & 0 deletions spec/active_record_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ def hourly
end
model.schedule.should == expected
end

it "should deal with all_occurrences with no infinite loops" do
model.schedule_attributes = {
repeat: 1, interval: 7, interval_unit: "day",
start_date: "11/03/2000", end_date: "11/04/2000",
all_day: true
}
schedule = model.schedule

# Calling schedule_attributes should not change Schedule::Rule.
model.schedule_attributes

model.schedule.should == schedule
model.schedule.all_occurrences.size.should == 5
end
end

end
8 changes: 8 additions & 0 deletions spec/schedule_attributes/input_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@
context args: {date: '2000-12-31', end_time: '14:00'} do
it { should == Time.new(2000,12,31,14,0,0) }
end

context args: {start_date: '2000-06-06', end_date: '2000-12-31', start_time: '06:00', end_time: '14:00'} do
it { should == Time.new(2000,6,6,14,0,0) }
end

context args: {start_date: '2000-06-06', end_date: '2000-12-31', start_time: '20:00', end_time: '14:00'} do
it { should == Time.new(2000,6,7,14,0,0) }
end
end

describe "#duration" do
Expand Down
9 changes: 8 additions & 1 deletion spec/schedule_attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@

context args: {repeat: "1", start_date: "1-1-1985", interval_unit: "day", interval: "3", end_date: "29-12-1985", ends: "eventually"} do
its(:start_time) { should == Date.new(1985,1,1).to_time }
its(:rrules) { should == [ IceCube::Rule.daily(3).until(Date.new(1985,12,29).to_time_in_current_zone) ] }
# Rails 4.1 removes Date.current.to_time_in_current_zone in favour of Date.in_time_zone
its(:rrules) do
if Date.current.respond_to?(:in_time_zone)
should == [ IceCube::Rule.daily(3).until(Date.new(1985,12,29).in_time_zone) ]
else
should == [ IceCube::Rule.daily(3).until(Date.new(1985,12,29).to_time_in_current_zone) ]
end
end
specify { schedule.first(3).should == [Date.civil(1985,1,1), Date.civil(1985,1,4), Date.civil(1985,1,7)].map(&:to_time) }
end

Expand Down
5 changes: 1 addition & 4 deletions spec/support/scheduled_active_record_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
class ActiveRecord::Base
extend ScheduleAttributes::ActiveRecord::Sugar

establish_connection(
adapter: "sqlite3",
database: ":memory:"
)
establish_connection( adapter: "sqlite3", database: ":memory:" )
end

ActiveRecord::Migration.create_table :calendars do |t|
Expand Down