simple_calendar 0.1.11 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b46fa1a91f987c5e308a4bf435365dd6ae4bee1b
4
- data.tar.gz: 71cf6c5d9b33d48cad89d779fff7df6603506c45
3
+ metadata.gz: fc702d5bfc9216617da9921539291ed03e61fd97
4
+ data.tar.gz: 4eec246dd685d28eea8f822483380c9121e53912
5
5
  SHA512:
6
- metadata.gz: 6d9d0a0aea3af9d70a2d29a421bca603c18cbb24d4ab4516e3aa9ad9213303a2b406bba420607e9a96a117682e3f101964093ac41d013900c9bed18c193c0b40
7
- data.tar.gz: 8ce4d3ebcf32cf966c69e92391e5518152d83ce91a6ae602063fad7ede933b289b76a5374856744e286d0e619c88b1c83944b20f1af1fa65af29d380516f25d2
6
+ metadata.gz: 45d62759c32d0a1ec5dadf6ebe3d819f8604bef0c4d99f5e45832d4e0d5815d001f788155d39d5067ee66979298ec23ac035b37940b571c7c00ca0c909bd8aa0
7
+ data.tar.gz: b0ece55d4cac8003ca070a9bd8803e02c7ce6f9c6ca6f7357f65eaa4db12540e2a5f0de00544589b4ac1446ce5c91a489c131f945da852086376a24bdd26b7fe
data/.gitignore CHANGED
@@ -2,4 +2,3 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
- vendor/bundle
data/Gemfile CHANGED
@@ -1,9 +1,3 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- # Specify your gem's dependencies in simple_calendar.gemspec
4
3
  gemspec
5
-
6
- group :test do
7
- gem 'rspec'
8
- gem 'pry'
9
- end
data/README.md CHANGED
@@ -1,158 +1,172 @@
1
1
  Simple Calendar
2
2
  ===============
3
3
 
4
- This is a small Rails 3.2 gem for creating a quick and clean table calendar.
5
- Theming is up to you, but it works nicely with Twitter Bootstrap. It's
6
- compatible with pure Ruby classes, ActiveRecord, Mongoid, and any other
7
- ORM.
4
+ Simple Calendar is design to do one thing really really well: render a
5
+ calendar. It lets you render a calendar of any size. Maybe you want a
6
+ day view, a 4 day agenda, a week view, a month view, or a 6 week
7
+ calendar. You can do all of that with the new gem, just give it a range
8
+ of dates to render.
8
9
 
9
- Thanks to all of the awesome contributors!
10
+ It doesn't depend on any ORM so you're free to use it with ActiveRecord,
11
+ Mongoid, any other ORM, or pure Ruby objects.
12
+
13
+ Thanks to all contributors for your wonderful help!
10
14
 
11
15
  Installation
12
16
  ------------
13
17
 
14
18
  Just add this into your Gemfile followed by a bundle install:
15
19
 
16
- gem "simple_calendar", "~> 0.1.10"
20
+ gem "simple_calendar", "~> 1.0.0"
17
21
 
18
22
  Usage
19
23
  -----
20
24
 
21
- #### Model
22
-
23
- SimpleCalendar will look for a method on your model called `start_time`.
24
- This is used to determine the day and time of the event. This should be
25
- a `DateTime` object or at least respond similarly.
25
+ Generating calendars is extremely simple with simple_calendar in version 1.0.
26
26
 
27
- The simplest way to use SimpleCalendar is to have an attribute on your
28
- model called `start_time` and you won't need to make any changes to your
29
- model at all. For example, I used `rails g model Event name:string
30
- start_time:datetime` to generate this class:
27
+ The first parameter is a symbol that looks up the current date in
28
+ `params`. If no date is found, it will use the current date.
31
29
 
32
- ```ruby
33
- class Event < ActiveRecord::Base
34
- attr_accessible :name, :start_time
35
- end
36
- ```
30
+ In these examples, we're using `:start_date` which is the default.
37
31
 
38
- If you don't have an attribute called `start_time` on your model, you
39
- can simply delegate like so:
32
+ ### Month Calendar
40
33
 
41
- ```ruby
42
- class Event < ActiveRecord::Base
43
- attr_accessible :name, :event_start_time
34
+ You can generate a calendar for the month with the `month_calendar`
35
+ method.
36
+ This will use `params[:start_date]` to render the calendar.
44
37
 
45
- def start_time
46
- event_start_time
47
- end
48
- end
38
+ ```erb
39
+ <%= month_calendar :start_date do |day| %>
40
+ <%= day %>
41
+ <% end %>
49
42
  ```
50
43
 
51
- As long as `start_time` returns a `DateTime` object, you're good to go.
52
- This means SimpleCalendar is now compatible with any class, whether it's
53
- ORM backed like ActiveRecord, Mongoid, or it's just a pure Ruby class.
54
- (Yay!)
44
+ ### Week Calendar
55
45
 
56
- ##### Querying
46
+ You can generate a week calendar with the `week_calendar` method.
47
+ This will use `params[:start_date]` to render the calendar.
57
48
 
58
- SimpleCalendar uses `params[:month]` and `params[:year]` to determine
59
- which month of the calendar to render. You can use these to make your
60
- database queries more efficient.
49
+ ```erb
50
+ <%= week_calendar :start_date, number_of_weeks: 2 do |day| %>
51
+ <%= day %>
52
+ <% end %>
53
+ ```
61
54
 
62
- #### Views
55
+ Setting `number_of_weeks` is optional and defaults to 1.
63
56
 
64
- SimpleCalendar just accepts an array of events and a block. The block
65
- will be executed for each event so you can provide your own logic for
66
- displaying the events.
57
+ ### Custom Length Calendar
67
58
 
68
- Here's an example that uses SimpleCalendar to simply render a link to
69
- each event on its own line inside the table. You would simply query for
70
- the `@events` as discussed above in the querying section.
59
+ You can generate calendars of any length by passing in the number of days you want to render.
60
+ This will use `params[:start_date]` to render the calendar.
71
61
 
72
62
  ```erb
73
- <%= calendar @events do |event| %>
74
- <div><%= link_to event.title, event %></div>
63
+ <%= calendar :start_date, number_of_days: 4 do |day| %>
64
+ <%= day %>
75
65
  <% end %>
76
66
  ```
77
67
 
78
- When the calendar is rendering, it yields to the block to allow you to
79
- render whatever you like for the item. In this example, I use the title
80
- attribute on the event with a link to the event.
68
+ Setting `number_of_days` is optional and defaults to 4.
81
69
 
82
- You may even pass options to calendar renderer to customize it's behavior
70
+ ## Customizing The Calendar
83
71
 
84
- ```erb
85
- <%= calendar @events, {:prev_text=>"prev", :next_text=>"next"} do |event| %>
86
- <div><%= link_to event.title, event %></div>
87
- <% end %>
72
+ You can change a couple of global options that will affect how the
73
+ calendars are generated:
74
+
75
+ ```ruby
76
+ Time.zone = "Central Time (US & Canada)"
88
77
  ```
89
78
 
90
- This time calendar will use prev and next as labels for previous and next month
91
- links (which are normally set to &amp;laquo; (&laquo;) and &amp;raquo; (&raquo;)
79
+ Setting `Time.zone` will make sure the calendar start days are correctly computed
80
+ in the right timezone. You can set this globally in your `application.rb` file or
81
+ if you have a User model with a time_zone attribute, you can set it on every request by using
82
+ a before_filter like the following example.
92
83
 
93
- Possible options:
84
+ This code example uses [Devise](https://github.com/plataformatec/devise)'s
85
+ `current_user` and `user_signed_in?` methods to retrieve the user's timezone and set it for the duration of the request.
86
+ Make sure to change the `:user_signed_in?` and `current_user` methods if you are
87
+ using some other method of authentication.
94
88
 
95
- :year # current year, default: from params or current year
96
- :month # current month, default: from params or current month
97
- :prev_text # previous month link text, default: &laquo;
98
- :next_text # next month link text, default: &raquo;
99
- :start_day # starting day of week, default: :sunday
100
- :empty_date # block called when a date is empty
101
- :class # HTML class attribute for the calendar
102
- :params # Any extra params you'd like in the URL (automatically includes month and year)
103
- :dont_display_year #Set to true to remove the year in the table header
89
+ ```ruby
90
+ class ApplicationController < ActionController::Base
91
+ before_filter :set_time_zone, if: :user_signed_in?
104
92
 
105
- If you wish to have Monday as the first day of the week, you'll need to
106
- change a couple things. First, when rendering the calendar, use the
107
- `:start_day => :monday` option like so:
93
+ private
108
94
 
109
- ```erb
110
- <%= calendar @events, :start_day => :monday do |event| %>
111
- <%= link_to event.title, event %>
112
- <% end %>
95
+ def set_time_zone
96
+ Time.zone = current_user.time_zone
97
+ end
98
+ end
113
99
  ```
114
100
 
115
- And the second step is to make sure you've set your `I18n.locale` to the
116
- correct one. There is a lot of information here regarding use of locales in Rails:
117
- https://github.com/svenfuchs/rails-i18n
118
-
119
- The `empty_date` option accepts a block that will be called when a day
120
- in the calendar is empty. It will be called with the date object that
121
- represents the day that has no events.
101
+ You can also change the beginning day of the week. If you want to set
102
+ this globally, you can put this line in
103
+ `config/initializers/simple_calendar.rb`:
122
104
 
123
- ```erb
124
- <%= calendar @events, empty_date: lambda{ |date| "hello from #{date}" } do |event| %>
125
- <%= event.name %>
126
- <% end %>
105
+ ```ruby
106
+ Date.beginning_of_week = :sunday
127
107
  ```
128
- The `not_empty_date` option accepts a block that will be called when a day
129
- in the calendar has at least one event. It will be called with the date object that
130
- represents the day that has some events.
131
108
 
132
- ```erb
133
- <%= calendar @events, not_empty_date: lambda{ |date| "hello from #{date}" } do |event| %>
134
- <%= event.name %>
135
- <% end %>
109
+ Setting classes on the table and elements are pretty:
110
+
111
+ ```ruby
112
+
113
+ <%= calendar :start_date,
114
+ table: {class: "table table-bordered"},
115
+ tr: {class: "row"},
116
+ td: {class: "day"}, do |day| %>
117
+ <% end %>
136
118
  ```
137
119
 
120
+ This will set the class of `table table-bordered` on the `table` HTML
121
+ element.
122
+
123
+ Each of the `table`, `tr`, and `td`, options are passed directly to the
124
+ the `content_tag` method so each of them **must** be a hash.
125
+
126
+ ### Custom Header Links
138
127
 
139
- CSS
140
- ---
128
+ Each of the calendar methods will generate a header with links to the
129
+ previous and next views. The `month_calendar` also includes a header
130
+ that tells you the current month and year that you are viewing.
141
131
 
142
- You will probably want to customize the height of the calendar so that
143
- all the rows are the same heights and widths. You can do this by adding
144
- the following line to your css:
132
+ To change these, you can pass in the `prev_link`, `header`, and
133
+ `next_link` options into the calendar methods.
145
134
 
146
- ```css
147
- .calendar td { height: 100px; width: 14.28%; }
135
+ The default `month_calendar` look like this:
136
+
137
+ ```erb
138
+ <%= month_calendar :start_date,
139
+ prev_link: ->(range) { link_to raw("&laquo;"), param_name => range.first - 1.day },
140
+ header: ->{ content_tag :span, "#{I18n.t("date.month_names")[start_date.month]} #{start_date.year}", class: "calendar-header" },
141
+ next_link: ->(range) { link_to raw("&raquo;"), param_name => range.last + 1.day } do |day| %>
142
+
143
+ <% end %>
148
144
  ```
149
145
 
150
- By default simple_calendar will set the calendar to use .table
151
- .table-bordered .table-striped and .calendar classes.
146
+ The `prev_link` option is a standard `link_to` that is a left arrow and
147
+ with the current url having `?start_date=2014-04-30` appended to it as
148
+ a date in the previous view of the calendar.
149
+
150
+ The `next_link` option is a standard `link_to` that is a right arrow and
151
+ with the current url having `?start_date=2014-06-01` appended to it as
152
+ a date in the next view of the calendar.
152
153
 
153
- You can also override the class by passing in the `class` option.
154
+ The `header` option is just a simple span tag with the month and year
155
+ inside of it.
156
+
157
+ If you wish to disable any of these partsof the header, just pass in
158
+ `false` and that will hide it:
154
159
 
155
160
  ```erb
156
- <%= calendar @events, class: "simple-calendar" do |event| %>
157
- <% end %>
161
+ <%= month_calendar :start_date, header: false do |day| %>
162
+ <% end %>
158
163
  ```
164
+
165
+
166
+ ## Author
167
+
168
+ Chris Oliver <chris@gorails.com>
169
+
170
+ [https://gorails.com](https://gorails.com)
171
+
172
+ [@excid3](https://twitter.com/excid3)
@@ -1,3 +1,3 @@
1
1
  module SimpleCalendar
2
- VERSION = "0.1.11"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,119 +1,120 @@
1
1
  module SimpleCalendar
2
2
  module ViewHelpers
3
+ def month_calendar(param_name=:start_date, options={}, &block)
4
+ start_date = (params[param_name] || Time.zone.now).to_date
5
+
6
+ options.reverse_merge!(
7
+ prev_link: ->(range) { link_to raw("&laquo;"), param_name => range.first - 1.day },
8
+ header: ->{ content_tag :span, "#{I18n.t("date.month_names")[start_date.month]} #{start_date.year}", class: "calendar-header" },
9
+ next_link: ->(range) { link_to raw("&raquo;"), param_name => range.last + 1.day },
10
+ )
11
+ render_calendar month_range(start_date), options, &block
12
+ end
13
+
14
+ def week_calendar(param_name=:start_date, options={}, &block)
15
+ start_date = (params[param_name] || Time.zone.now).to_date
16
+ number_of_weeks = options.fetch(:number_of_weeks, 1)
3
17
 
4
- def calendar(events, options={}, &block)
5
- raise 'SimpleCalendar requires a block to be passed in' unless block_given?
18
+ options.reverse_merge!(
19
+ prev_link: ->(range) { link_to raw("&laquo;"), param_name => range.first - 1.day },
20
+ header: false,
21
+ next_link: ->(range) { link_to raw("&raquo;"), param_name => range.last + 1.day },
22
+ )
23
+ render_calendar week_range(start_date, number_of_weeks), options, &block
24
+ end
6
25
 
7
- opts = default_options
8
- options.reverse_merge! opts
9
- events ||= []
10
- selected_month = Date.new(options[:year], options[:month])
11
- current_date = Time.zone.now.to_date
12
- range = build_range selected_month, options
13
- month_array = range.each_slice(7).to_a
26
+ def calendar(param_name=:start_date, options={}, &block)
27
+ start_date = (params[param_name] || Time.zone.now).to_date
28
+ number_of_days_to_advance = options.fetch(:number_of_days, 4) - 1
14
29
 
15
- draw_calendar(selected_month, month_array, current_date, events, options, block)
30
+ options.reverse_merge!(
31
+ prev_link: ->(range) { link_to raw("&laquo;"), param_name => range.first - 1.day },
32
+ header: false,
33
+ next_link: ->(range) { link_to raw("&raquo;"), param_name => range.last + 1.day },
34
+ )
35
+ render_calendar calendar_range(start_date, number_of_days_to_advance), options, &block
16
36
  end
17
37
 
18
- private
19
- def default_options
20
- {
21
- :year => (params[:year] || Time.zone.now.year).to_i,
22
- :month => (params[:month] || Time.zone.now.month).to_i,
23
- :prev_text => raw("&laquo;"),
24
- :next_text => raw("&raquo;"),
25
- :start_day => :sunday,
26
- :class => "table table-bordered table-striped calendar",
27
- :params => {},
28
- :time_selector => "start_time",
29
- :dont_display_year => false
30
- }
38
+ def month_range(start_date)
39
+ start_date.beginning_of_month.beginning_of_week.to_date..start_date.end_of_month.end_of_week.to_date
31
40
  end
32
- # Returns array of dates between start date and end date for selected month
33
- def build_range(selected_month, options)
34
- start_date = selected_month.beginning_of_month.beginning_of_week(options[:start_day])
35
- end_date = selected_month.end_of_month.end_of_week(options[:start_day])
36
41
 
37
- (start_date..end_date).to_a
42
+ def week_range(start_date, number_of_weeks)
43
+ number_of_days_to_advance = (number_of_weeks * 7) - 1
44
+ starting_day = start_date.beginning_of_week.to_date
45
+ ending_day = starting_day + number_of_days_to_advance.days
46
+ starting_day..ending_day
38
47
  end
39
48
 
40
- # Renders the calendar table
41
- def draw_calendar(selected_month, month, current_date, events, options, block)
42
- tags = []
43
- today = Time.zone.now.to_date
44
- content_tag(:table, :class => options[:class]) do
45
- tags << month_header(selected_month, options)
46
- day_names = I18n.t("date.abbr_day_names")
47
- day_names = day_names.rotate((Date::DAYS_INTO_WEEK[options[:start_day]] + 1) % 7)
48
- tags << content_tag(:thead, content_tag(:tr, day_names.collect { |name| content_tag :th, name, :class => (selected_month.month == today.month && today.strftime("%a") == name ? "current-day" : nil)}.join.html_safe))
49
- tags << content_tag(:tbody, :'data-month'=>selected_month.month, :'data-year'=>selected_month.year) do
50
-
51
- month.collect do |week|
52
- content_tag(:tr, :class => (week.include?(today) ? "current-week week" : "week")) do
53
-
54
- week.collect do |date|
55
- td_class = ["day"]
56
- td_class << "today" if today == date
57
- td_class << "not-current-month" if selected_month.month != date.month
58
- td_class << "past" if today > date
59
- td_class << "future" if today < date
60
- td_class << "wday-#{date.wday.to_s}" # <- to enable different styles for weekend, etc
61
-
62
- cur_events = day_events(date, events, options[:time_selector])
63
-
64
- td_class << (cur_events.any? ? "events" : "no-events")
65
-
66
- content_tag(:td, :class => td_class.join(" "), :'data-date-iso'=>date.to_s, 'data-date'=>date.to_s.gsub('-', '/')) do
67
- content_tag(:div) do
68
- divs = []
69
- concat content_tag(:div, date.day.to_s, :class=>"day_number")
70
-
71
- if cur_events.empty? && options[:empty_date]
72
- concat options[:empty_date].call(date)
73
- else
74
- if options[:not_empty_date]
75
- concat options[:not_empty_date].call(date)
76
- end
77
- divs << cur_events.collect{ |event| block.call(event) }
78
- end
79
-
80
- divs.join.html_safe
81
- end #content_tag :div
82
- end #content_tag :td
83
-
84
- end.join.html_safe
85
- end #content_tag :tr
86
-
87
- end.join.html_safe
88
- end #content_tag :tbody
89
-
90
- tags.join.html_safe
91
- end #content_tag :table
49
+ def calendar_range(start_date, number_of_days_to_advance)
50
+ start_date..(start_date + number_of_days_to_advance.days)
92
51
  end
93
52
 
94
- # Returns an array of events for a given day
95
- def day_events(date, events, time_selector)
96
- events.select { |e| e.send(time_selector).to_date == date }.sort_by { |e| e.send(time_selector) }
53
+ def render_calendar(range, options, &block)
54
+ raise 'SimpleCalendar requires a block' unless block_given?
55
+
56
+ @block = block
57
+ @options = options.reverse_merge default_options
58
+
59
+ capture do
60
+ concat render_header(range)
61
+ concat render_table(range)
62
+ end
97
63
  end
98
64
 
99
- # Generates the header that includes the month and next and previous months
100
- def month_header(selected_month, options)
101
- content_tag :h2 do
102
- previous_month = selected_month.advance :months => -1
103
- next_month = selected_month.advance :months => 1
104
- tags = []
65
+ def render_header(range)
66
+ capture do
67
+ content_tag :div do
68
+ concat get_options(@options[:prev_link], range)
69
+ concat get_options(@options[:header])
70
+ concat get_options(@options[:next_link], range)
71
+ end
72
+ end
73
+ end
105
74
 
106
- tags << month_link(options[:prev_text], previous_month, options[:params], {:class => "previous-month"})
107
- tags << "#{I18n.t("date.month_names")[selected_month.month]} #{selected_month.year unless options[:dont_display_year]}".strip
108
- tags << month_link(options[:next_text], next_month, options[:params], {:class => "next-month"})
75
+ def render_table(range)
76
+ content_tag(:table, get_options(@options[:table])) do
77
+ content_tag(:tbody) do
78
+ render_weeks(range)
79
+ end
80
+ end
81
+ end
82
+
83
+
84
+ def render_weeks(range)
85
+ weeks = []
86
+ range.each_slice(7) do |week|
87
+ weeks << content_tag(:tr, get_options(@options[:tr], week)) do
88
+ render_week(week)
89
+ end
90
+ end
91
+ safe_join weeks
92
+ end
109
93
 
110
- tags.join.html_safe
94
+ def render_week(week)
95
+ results = week.map do |day|
96
+ content_tag :td, get_options(@options[:td], day) do
97
+ @block.call(day)
98
+ end
111
99
  end
100
+ safe_join results
112
101
  end
113
102
 
114
- # Generates the link to next and previous months
115
- def month_link(text, date, params, opts={})
116
- link_to(text, params.merge({:month => date.month, :year => date.year}), opts)
103
+ def get_options(options, *params)
104
+ case options
105
+ when Hash
106
+ options
107
+ when String
108
+ send(options, *params)
109
+ when Symbol
110
+ send(options, *params)
111
+ else
112
+ options.call(*params) if options.respond_to? :call
113
+ end
114
+ end
115
+
116
+ def default_options
117
+ { table: {}, tr: {}, td: {}, }
117
118
  end
118
119
  end
119
120
  end
@@ -8,8 +8,8 @@ Gem::Specification.new do |s|
8
8
  s.authors = ["Chris Oliver"]
9
9
  s.email = ["excid3@gmail.com"]
10
10
  s.homepage = "https://github.com/excid3/simple_calendar"
11
- s.summary = %q{A simple Rails 3 calendar}
12
- s.description = %q{A simple Rails 3 calendar}
11
+ s.summary = %q{A simple Rails 3 and Rails 4 calendar}
12
+ s.description = %q{A simple Rails 3 and Rails 4 calendar}
13
13
 
14
14
  s.rubyforge_project = "simple_calendar"
15
15
 
@@ -18,5 +18,5 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_dependency('rails', '>= 3.0')
21
+ s.add_dependency 'rails', '>= 3.0'
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_calendar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
- description: A simple Rails 3 calendar
27
+ description: A simple Rails 3 and Rails 4 calendar
28
28
  email:
29
29
  - excid3@gmail.com
30
30
  executables: []
@@ -41,9 +41,6 @@ files:
41
41
  - lib/simple_calendar/version.rb
42
42
  - lib/simple_calendar/view_helpers.rb
43
43
  - simple_calendar.gemspec
44
- - spec/helper.rb
45
- - spec/simple_calendar_spec.rb
46
- - spec/support/railtie.rb
47
44
  homepage: https://github.com/excid3/simple_calendar
48
45
  licenses: []
49
46
  metadata: {}
@@ -66,8 +63,5 @@ rubyforge_project: simple_calendar
66
63
  rubygems_version: 2.2.2
67
64
  signing_key:
68
65
  specification_version: 4
69
- summary: A simple Rails 3 calendar
70
- test_files:
71
- - spec/helper.rb
72
- - spec/simple_calendar_spec.rb
73
- - spec/support/railtie.rb
66
+ summary: A simple Rails 3 and Rails 4 calendar
67
+ test_files: []
data/spec/helper.rb DELETED
@@ -1,24 +0,0 @@
1
- require 'pathname'
2
- require 'rubygems'
3
- require 'bundler'
4
- require 'pry'
5
- require 'active_support/all'
6
- require 'action_view'
7
-
8
- root_path = Pathname(__FILE__).dirname.join('..').expand_path
9
- lib_path = root_path.join('lib')
10
-
11
- Dir[root_path.join("spec/support/*.rb")].each { |f| require f }
12
- Dir[lib_path.join("simple_calendar/*.rb")].each { |f| require f }
13
-
14
- Bundler.setup(:default)
15
-
16
- RSpec.configure do |config|
17
- config.fail_fast = true
18
-
19
- config.filter_run :focused => true
20
- config.alias_example_to :fit, :focused => true
21
- config.alias_example_to :xit, :pending => true
22
- config.run_all_when_everything_filtered = true
23
- end
24
- Time.zone = "UTC"
@@ -1,102 +0,0 @@
1
- require 'helper'
2
-
3
- class View
4
- include SimpleCalendar::ViewHelpers
5
- include ActionView::Helpers
6
- include ActionView::Context
7
-
8
- def raw(argument)
9
- argument
10
- end
11
-
12
- end
13
-
14
- describe "SimpleCalendar" do
15
- subject { View.new }
16
-
17
- before do
18
- subject.stub(:params => {:year => 2013, :month => 5})
19
- end
20
-
21
- describe "#calendar" do
22
- context "with invalid arguments" do
23
- it "should raise an error" do
24
- expect { subject.calendar("foo") }.to raise_error(StandardError, /block/)
25
- end
26
- end
27
-
28
- context "with valid arguments" do
29
- it "should draw a calendar" do
30
- subject.should_receive(:draw_calendar)
31
- subject.calendar("foo") { "test" }
32
- end
33
-
34
- it "should not overwrite passed in arguments" do
35
- subject.should_receive(:build_range).and_return([2,2,3])
36
- subject.should_receive(:draw_calendar)
37
- subject.calendar("foo") { "test" }
38
- end
39
- end
40
- end
41
-
42
- describe "#build_range" do
43
- it "should return an array representing the days of the month" do
44
- selected_month = Date.new(2013, 1)
45
- options = {:start_day => :tuesday}
46
- result = subject.send(:build_range, selected_month, options)
47
- result.class.should == Array
48
- result.first.class.should == Date
49
- result.last.class.should == Date
50
- end
51
- end
52
-
53
- describe "#draw_calendar" do
54
- it "should render a calendar table" do
55
- #TODO: refactor draw_calendar to not require so much build up
56
- subject.should_receive(:month_header)
57
- selected_month = Date.new(2013, 1)
58
- month = []
59
- current_date = Date.new(2013, 1, 14)
60
- events = {}
61
- options = {:start_day => :monday}
62
- block = Proc.new {}
63
- subject.send(:draw_calendar, selected_month, month, current_date, events, options, block).should match(/^<table/)
64
- end
65
- end
66
-
67
- describe "#day_events" do
68
- it "should return an array of events for a given day" do
69
- date = Date.new(2013, 1, 14)
70
- matching_event = stub(:start_time => Date.new(2013, 1, 14))
71
- other_event = stub(:start_time => Date.new(2013,1,15))
72
- events = [matching_event, other_event]
73
- subject.send(:day_events, date, events, "start_time").should == [matching_event]
74
- end
75
- end
76
-
77
- describe "#month_header" do
78
- it "should generate html including the month, next and previous month" do
79
- subject.should_receive(:month_link).exactly(2)
80
- selected_month = Date.new(2013, 1)
81
- options = {}
82
- subject.send(:month_header, selected_month, options).should match(/^<h2>January 2013<\/h2>/)
83
- end
84
-
85
- it "should allow the display_year option to be set to false and not show the year" do
86
- subject.should_receive(:month_link).exactly(2)
87
- selected_month = Date.new(2014, 1)
88
- options = {:dont_display_year => true}
89
- str = subject.send(:month_header, selected_month, options)
90
- str.should match(/^<h2>January<\/h2>/)
91
- end
92
-
93
- end
94
-
95
- describe "#month_link" do
96
- it "should return a link" do
97
- #TODO: test even needed?
98
- subject.should_receive(:link_to)
99
- subject.send(:month_link, "previous", Date.new(2013, 1), {})
100
- end
101
- end
102
- end
@@ -1,9 +0,0 @@
1
- module SimpleCalendar
2
- class Rails
3
- class Railtie
4
- def self.initializer(object)
5
- end
6
- end
7
- end
8
- end
9
-