simple_calendar 2.2.7 → 2.4.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,7 @@
1
1
  module SimpleCalendar
2
2
  class MonthCalendar < SimpleCalendar::Calendar
3
- private
4
-
5
- def date_range
6
- (start_date.beginning_of_month.beginning_of_week..start_date.end_of_month.end_of_week).to_a
7
- end
3
+ def date_range
4
+ (start_date.beginning_of_month.beginning_of_week..start_date.end_of_month.end_of_week).to_a
5
+ end
8
6
  end
9
7
  end
10
-
@@ -1,7 +1,9 @@
1
1
  module SimpleCalendar
2
2
  class Engine < Rails::Engine
3
3
  initializer "simple_calendar.view_helpers" do
4
- ActionView::Base.send :include, ViewHelpers
4
+ ActiveSupport.on_load(:action_view) do
5
+ include ViewHelpers
6
+ end
5
7
  end
6
8
  end
7
9
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleCalendar
2
- VERSION = "2.2.7"
2
+ VERSION = "2.4.3"
3
3
  end
@@ -1,17 +1,17 @@
1
1
  module SimpleCalendar
2
2
  module ViewHelpers
3
- def calendar(options={}, &block)
4
- raise 'calendar requires a block' unless block_given?
3
+ def calendar(options = {}, &block)
4
+ raise "calendar requires a block" unless block
5
5
  SimpleCalendar::Calendar.new(self, options).render(&block)
6
6
  end
7
7
 
8
- def month_calendar(options={}, &block)
9
- raise 'month_calendar requires a block' unless block_given?
8
+ def month_calendar(options = {}, &block)
9
+ raise "month_calendar requires a block" unless block
10
10
  SimpleCalendar::MonthCalendar.new(self, options).render(&block)
11
11
  end
12
12
 
13
- def week_calendar(options={}, &block)
14
- raise 'week_calendar requires a block' unless block_given?
13
+ def week_calendar(options = {}, &block)
14
+ raise "week_calendar requires a block" unless block
15
15
  SimpleCalendar::WeekCalendar.new(self, options).render(&block)
16
16
  end
17
17
  end
@@ -1,7 +1,7 @@
1
1
  module SimpleCalendar
2
2
  class WeekCalendar < SimpleCalendar::Calendar
3
3
  def week_number
4
- format = (Date.beginning_of_week == :sunday) ? "%U" : "%W"
4
+ format = Date.beginning_of_week == :sunday ? "%U" : "%V"
5
5
  start_date.beginning_of_week.strftime(format).to_i
6
6
  end
7
7
 
@@ -13,13 +13,11 @@ module SimpleCalendar
13
13
  week_number + number_of_weeks - 1
14
14
  end
15
15
 
16
- private
16
+ def date_range
17
+ starting = start_date.beginning_of_week
18
+ ending = (starting + (number_of_weeks - 1).weeks).end_of_week
17
19
 
18
- def date_range
19
- starting = start_date.beginning_of_week
20
- ending = (starting + (number_of_weeks - 1).weeks).end_of_week
21
-
22
- (starting..ending).to_a
23
- end
20
+ (starting..ending).to_a
21
+ end
24
22
  end
25
23
  end
@@ -1,23 +1,22 @@
1
- # -*- encoding: utf-8 -*-
2
1
  $:.push File.expand_path("../lib", __FILE__)
3
2
  require "simple_calendar/version"
4
3
 
5
4
  Gem::Specification.new do |s|
6
- s.name = "simple_calendar"
7
- s.version = SimpleCalendar::VERSION
8
- s.authors = ["Chris Oliver"]
9
- s.email = ["excid3@gmail.com"]
10
- s.homepage = "https://github.com/excid3/simple_calendar"
11
- s.summary = %q{A simple Rails calendar}
12
- s.description = %q{A simple Rails calendar}
13
- s.license = "MIT"
5
+ s.name = "simple_calendar"
6
+ s.version = SimpleCalendar::VERSION
7
+ s.authors = ["Chris Oliver"]
8
+ s.email = ["excid3@gmail.com"]
9
+ s.homepage = "https://github.com/excid3/simple_calendar"
10
+ s.summary = "A simple Rails calendar"
11
+ s.description = "A simple Rails calendar"
12
+ s.license = "MIT"
14
13
 
15
14
  s.rubyforge_project = "simple_calendar"
16
15
 
17
- s.files = `git ls-files`.split("\n")
18
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
18
  s.require_paths = ["lib"]
20
19
 
21
- s.add_dependency 'rails', '>= 3.0'
22
- s.add_development_dependency 'rspec'
20
+ s.add_dependency "rails", ">= 3.0"
21
+ s.add_development_dependency "rspec"
23
22
  end
@@ -1,53 +1,38 @@
1
- require 'spec_helper'
2
- require 'action_controller'
3
- require 'simple_calendar/calendar'
4
-
5
- class ViewContext
6
- attr_accessor :start_date, :start_date_param
7
-
8
- def initialize(start_date=nil, options={})
9
- @start_date = start_date
10
- @start_date_param = options.fetch(:start_date_param, :start_date)
11
- end
12
-
13
- def params
14
- if @start_date.present?
15
- ActionController::Parameters.new({start_date_param => @start_date})
16
- else
17
- ActionController::Parameters.new
18
- end
19
- end
20
- end
1
+ require "spec_helper"
2
+ require "action_controller"
3
+ require "simple_calendar/calendar"
4
+ require_relative "support/fake_event"
5
+ require_relative "support/view_context"
21
6
 
22
7
  describe SimpleCalendar::Calendar do
23
8
  let(:calendar) { SimpleCalendar::Calendar.new(ViewContext.new) }
24
9
 
25
- it 'renders a partial with the same name as the class' do
10
+ it "renders a partial with the same name as the class" do
26
11
  expect(calendar.send(:partial_name)).to eq("simple_calendar/calendar")
27
12
  end
28
13
 
29
- context 'event sorting attribute' do
30
- it 'has start_time as the default attribute' do
14
+ context "event sorting attribute" do
15
+ it "has start_time as the default attribute" do
31
16
  expect(calendar.send(:attribute)).to eq(:start_time)
32
17
  end
33
18
 
34
- it 'allows you to override the default attribute' do
19
+ it "allows you to override the default attribute" do
35
20
  expect(SimpleCalendar::Calendar.new(ViewContext.new, attribute: :starts_at).send(:attribute)).to eq(:starts_at)
36
21
  end
37
22
 
38
23
  it "set a default when `partial` option isn't present" do
39
- expect(SimpleCalendar::Calendar.new(ViewContext.new).send(:partial_name)).to eq('simple_calendar/calendar')
40
- expect(SimpleCalendar::MonthCalendar.new(ViewContext.new).send(:partial_name)).to eq('simple_calendar/month_calendar')
41
- expect(SimpleCalendar::WeekCalendar.new(ViewContext.new).send(:partial_name)).to eq('simple_calendar/week_calendar')
24
+ expect(SimpleCalendar::Calendar.new(ViewContext.new).send(:partial_name)).to eq("simple_calendar/calendar")
25
+ expect(SimpleCalendar::MonthCalendar.new(ViewContext.new).send(:partial_name)).to eq("simple_calendar/month_calendar")
26
+ expect(SimpleCalendar::WeekCalendar.new(ViewContext.new).send(:partial_name)).to eq("simple_calendar/week_calendar")
42
27
  end
43
28
 
44
- it 'allows to override the default partial' do
45
- expect(SimpleCalendar::Calendar.new(ViewContext.new, partial: 'simple_calendar/custom_calendar').send(:partial_name)).to eq('simple_calendar/custom_calendar')
29
+ it "allows to override the default partial" do
30
+ expect(SimpleCalendar::Calendar.new(ViewContext.new, partial: "simple_calendar/custom_calendar").send(:partial_name)).to eq("simple_calendar/custom_calendar")
46
31
  end
47
32
  end
48
33
 
49
34
  describe "#sorted_events" do
50
- it 'converts an array of events to a hash sorted by days' do
35
+ it "converts an array of events to a hash sorted by days" do
51
36
  today, tomorrow = Date.today, Date.tomorrow
52
37
 
53
38
  event1 = double(start_time: today.at_midnight)
@@ -63,7 +48,7 @@ describe SimpleCalendar::Calendar do
63
48
  expect(sorted_events[tomorrow]).to eq([event3])
64
49
  end
65
50
 
66
- it 'converts an array of multi-day events to a hash sorted by days' do
51
+ it "converts an array of multi-day events to a hash sorted by days" do
67
52
  today, tomorrow = Date.today, Date.tomorrow
68
53
 
69
54
  event1 = double(start_time: today.at_midnight, end_time: tomorrow.at_midnight)
@@ -79,17 +64,17 @@ describe SimpleCalendar::Calendar do
79
64
  expect(sorted_events[tomorrow]).to eq([event1, event3])
80
65
  end
81
66
 
82
- it 'handles events without a start time' do
67
+ it "handles events without a start time" do
83
68
  event = double(start_time: nil)
84
69
  calendar = SimpleCalendar::Calendar.new(ViewContext.new, events: [event])
85
70
 
86
- expect{calendar.send(:sorted_events)}.not_to raise_error
71
+ expect { calendar.send(:sorted_events) }.not_to raise_error
87
72
  end
88
73
  end
89
74
 
90
75
  describe "#start_date" do
91
76
  it "defaults to today's date" do
92
- view_context = ViewContext.new()
77
+ view_context = ViewContext.new
93
78
  calendar = SimpleCalendar::Calendar.new(view_context)
94
79
  expect(calendar.send(:start_date)).to eq(Date.today)
95
80
  end
@@ -113,32 +98,79 @@ describe SimpleCalendar::Calendar do
113
98
  end
114
99
  end
115
100
 
116
- describe 'current week class' do
117
- it 'should have the current week' do
101
+ describe "current week class" do
102
+ it "should have the current week" do
118
103
  calendar = SimpleCalendar::Calendar.new(ViewContext.new)
119
- week = calendar.send(:date_range).each_slice(7).to_a[0]
120
- expect(calendar.send(:tr_classes_for, week)).to include('current-week')
104
+ week = calendar.date_range.each_slice(7).to_a[0]
105
+ expect(calendar.send(:tr_classes_for, week)).to include("current-week")
121
106
  end
122
107
 
123
- it 'should not have the current week if it does not contain today' do
124
- calendar = SimpleCalendar::MonthCalendar.new(ViewContext.new)
125
- week = calendar.send(:date_range).each_slice(7).to_a[0]
126
- expect(calendar.send(:tr_classes_for, week)).to_not include('current-week')
108
+ it "should not have the current week if it does not contain today" do
109
+ calendar = SimpleCalendar::MonthCalendar.new(ViewContext.new(6.months.ago))
110
+ week = calendar.date_range.each_slice(7).to_a[0]
111
+ expect(calendar.send(:tr_classes_for, week)).to_not include("current-week")
127
112
  end
128
113
  end
129
114
 
130
- it 'has a param that determines the start date of the calendar'
131
- it 'generates a default date if no start date is present'
132
- it 'has a range of dates'
115
+ it "has a param that determines the start date of the calendar" do
116
+ calendar = SimpleCalendar::Calendar.new(ViewContext.new)
117
+
118
+ rendering_variables = calendar.render[:locals]
119
+
120
+ expect(rendering_variables[:start_date]).not_to be_nil
121
+ end
122
+
123
+ it "generates a default date if no start date is present" do
124
+ calendar = SimpleCalendar::Calendar.new(ViewContext.new)
133
125
 
134
- it 'can split the range of dates into weeks'
135
- it 'has a title'
136
- it 'has a next view link'
137
- it 'has a previous view link'
126
+ calendar_start_date = calendar.render[:locals][:start_date]
138
127
 
139
- it 'accepts an array of events'
140
- it 'sorts the events'
141
- it 'yields the events for each day'
128
+ expect(calendar_start_date).not_to be_nil
129
+ expect(calendar_start_date).to be_a(Date)
130
+ end
131
+
132
+ it "has a range of dates" do
133
+ calendar = SimpleCalendar::Calendar.new(ViewContext.new)
134
+
135
+ calendar_date_range = calendar.date_range
136
+
137
+ expect(calendar_date_range).to be_an(Array)
138
+ expect(calendar_date_range).to all(be_an(Date))
139
+ end
140
+
141
+ it "can split the range of dates into weeks"
142
+ it "has a title"
143
+ it "has a next view link"
144
+ it "has a previous view link"
145
+
146
+ it "accepts an array of events" do
147
+ first_event = FakeEvent.new("event1", Date.today)
148
+ second_event = FakeEvent.new("event2", Date.today + 1.day)
149
+ events = [first_event, second_event]
150
+ calendar = SimpleCalendar::Calendar.new(ViewContext.new, events: events)
151
+
152
+ calendar_sorted_events = calendar.render[:locals][:sorted_events]
153
+
154
+ expect(calendar_sorted_events.length).to eq(2)
155
+ end
156
+
157
+ it "sorts the events" do
158
+ first_event = FakeEvent.new("event1", Date.today + 2.days)
159
+ second_event = FakeEvent.new("event2", Date.today + 1.day)
160
+ third_event = FakeEvent.new("event3", Date.today)
161
+ events = [first_event, third_event, second_event]
162
+ calendar = SimpleCalendar::Calendar.new(ViewContext.new, events: events)
163
+
164
+ calendar_sorted_events = calendar.render[:locals][:sorted_events]
165
+ first_key = calendar_sorted_events.keys[0]
166
+ second_key = calendar_sorted_events.keys[1]
167
+ third_key = calendar_sorted_events.keys[2]
168
+
169
+ expect(calendar_sorted_events[first_key][0]).to eq(third_event)
170
+ expect(calendar_sorted_events[second_key][0]).to eq(second_event)
171
+ expect(calendar_sorted_events[third_key][0]).to eq(first_event)
172
+ end
142
173
 
174
+ it "yields the events for each day"
143
175
  it "doesn't crash when an event has a nil start_time"
144
176
  end
@@ -1,7 +1,25 @@
1
- require 'spec_helper'
2
- require 'simple_calendar/month_calendar'
1
+ require "spec_helper"
2
+ require "action_controller"
3
+ require "simple_calendar/calendar"
4
+ require "simple_calendar/month_calendar"
5
+ require "support/view_context"
3
6
 
4
7
  describe SimpleCalendar::MonthCalendar do
5
- it 'renders a full calendar month'
6
- it 'render the days of next and previous months on the edges of the calendar'
8
+ describe "#date_range" do
9
+ it "renders a full calendar month" do
10
+ today = Date.today
11
+ calendar = SimpleCalendar::MonthCalendar.new(ViewContext.new, start_date: Date.today)
12
+
13
+ expect(calendar.date_range.min).to be <= today.beginning_of_month
14
+ expect(calendar.date_range.max).to be >= today.end_of_month
15
+ end
16
+
17
+ it "render the days of next and previous months on the edges of the calendar" do
18
+ month = Date.new(2018, 8, 1)
19
+ calendar = SimpleCalendar::MonthCalendar.new(ViewContext.new, start_date: month)
20
+
21
+ expect(calendar.date_range.first).to eq Date.new(2018, 7, 30)
22
+ expect(calendar.date_range.last).to eq Date.new(2018, 9, 2)
23
+ end
24
+ end
7
25
  end
@@ -1,9 +1,9 @@
1
- require 'spec_helper'
2
- require 'rails'
3
- require 'simple_calendar'
1
+ require "spec_helper"
2
+ require "rails"
3
+ require "simple_calendar"
4
4
 
5
5
  describe SimpleCalendar do
6
- it 'has a version number' do
6
+ it "has a version number" do
7
7
  expect(SimpleCalendar::VERSION).not_to be nil
8
8
  end
9
9
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "active_support/all"
2
+
1
3
  # This file was generated by the `rspec --init` command. Conventionally, all
2
4
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
5
  # The generated `.rspec` file contains `--require spec_helper` which will cause
@@ -40,57 +42,55 @@ RSpec.configure do |config|
40
42
  mocks.verify_partial_doubles = true
41
43
  end
42
44
 
43
- # The settings below are suggested to provide a good initial experience
44
- # with RSpec, but feel free to customize to your heart's content.
45
- =begin
46
- # These two settings work together to allow you to limit a spec run
47
- # to individual examples or groups you care about by tagging them with
48
- # `:focus` metadata. When nothing is tagged with `:focus`, all examples
49
- # get run.
50
- config.filter_run :focus
51
- config.run_all_when_everything_filtered = true
52
-
53
- # Allows RSpec to persist some state between runs in order to support
54
- # the `--only-failures` and `--next-failure` CLI options. We recommend
55
- # you configure your source control system to ignore this file.
56
- config.example_status_persistence_file_path = "spec/examples.txt"
57
-
58
- # Limits the available syntax to the non-monkey patched syntax that is
59
- # recommended. For more details, see:
60
- # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
61
- # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
62
- # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
63
- config.disable_monkey_patching!
64
-
65
- # This setting enables warnings. It's recommended, but in some cases may
66
- # be too noisy due to issues in dependencies.
67
- config.warnings = true
68
-
69
- # Many RSpec users commonly either run the entire suite or an individual
70
- # file, and it's useful to allow more verbose output when running an
71
- # individual spec file.
72
- if config.files_to_run.one?
73
- # Use the documentation formatter for detailed output,
74
- # unless a formatter has already been configured
75
- # (e.g. via a command-line flag).
76
- config.default_formatter = 'doc'
77
- end
78
-
79
- # Print the 10 slowest examples and example groups at the
80
- # end of the spec run, to help surface which specs are running
81
- # particularly slow.
82
- config.profile_examples = 10
83
-
84
- # Run specs in random order to surface order dependencies. If you find an
85
- # order dependency and want to debug it, you can fix the order by providing
86
- # the seed, which is printed after each run.
87
- # --seed 1234
88
- config.order = :random
89
-
90
- # Seed global randomization in this process using the `--seed` CLI option.
91
- # Setting this allows you to use `--seed` to deterministically reproduce
92
- # test failures related to randomization by passing the same `--seed` value
93
- # as the one that triggered the failure.
94
- Kernel.srand config.seed
95
- =end
45
+ # The settings below are suggested to provide a good initial experience
46
+ # with RSpec, but feel free to customize to your heart's content.
47
+ # # These two settings work together to allow you to limit a spec run
48
+ # # to individual examples or groups you care about by tagging them with
49
+ # # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # # get run.
51
+ # config.filter_run :focus
52
+ # config.run_all_when_everything_filtered = true
53
+ #
54
+ # # Allows RSpec to persist some state between runs in order to support
55
+ # # the `--only-failures` and `--next-failure` CLI options. We recommend
56
+ # # you configure your source control system to ignore this file.
57
+ # config.example_status_persistence_file_path = "spec/examples.txt"
58
+ #
59
+ # # Limits the available syntax to the non-monkey patched syntax that is
60
+ # # recommended. For more details, see:
61
+ # # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
62
+ # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
63
+ # # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
64
+ # config.disable_monkey_patching!
65
+ #
66
+ # # This setting enables warnings. It's recommended, but in some cases may
67
+ # # be too noisy due to issues in dependencies.
68
+ # config.warnings = true
69
+ #
70
+ # # Many RSpec users commonly either run the entire suite or an individual
71
+ # # file, and it's useful to allow more verbose output when running an
72
+ # # individual spec file.
73
+ # if config.files_to_run.one?
74
+ # # Use the documentation formatter for detailed output,
75
+ # # unless a formatter has already been configured
76
+ # # (e.g. via a command-line flag).
77
+ # config.default_formatter = 'doc'
78
+ # end
79
+ #
80
+ # # Print the 10 slowest examples and example groups at the
81
+ # # end of the spec run, to help surface which specs are running
82
+ # # particularly slow.
83
+ # config.profile_examples = 10
84
+ #
85
+ # # Run specs in random order to surface order dependencies. If you find an
86
+ # # order dependency and want to debug it, you can fix the order by providing
87
+ # # the seed, which is printed after each run.
88
+ # # --seed 1234
89
+ # config.order = :random
90
+ #
91
+ # # Seed global randomization in this process using the `--seed` CLI option.
92
+ # # Setting this allows you to use `--seed` to deterministically reproduce
93
+ # # test failures related to randomization by passing the same `--seed` value
94
+ # # as the one that triggered the failure.
95
+ # Kernel.srand config.seed
96
96
  end