elevation_event_calendar 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.rdoc ADDED
@@ -0,0 +1,27 @@
1
+ == 2009-12-09 / Version 2.1
2
+ * Added the 'use_all_day' option. If this is set to true (by default it is false), it will check for an 'all_day' boolean field when displaying an event. If it is an all day event, or the event is multiple days, then it will display as usual. Otherwise it will display without a background color bar.
3
+ * Added a --use-all-day option to the generator
4
+ * Note, if updating, you will need to slightly change your helper. The block parameters passed to the calendar method are now in an argument hash. (More flexible, and future changes won't have this problem.) See the example in README for more.
5
+ calendar event_calendar_options do |args|
6
+ event = args[:event]
7
+ ...
8
+ end
9
+ * Javascript handles highlighting non-all-day events
10
+ * Re-factored javascript to be unobtrusive
11
+ * JQuery works the same as the Prototype version
12
+ * Minor changes to the stylesheet as a result of the new display option
13
+ * A helper method for displaying an event's time
14
+
15
+ == 2009-11-23 / Version 2
16
+ * Complete rewrite of the calendar's HTML generation and styling
17
+ * Event rows are now table cells which span the desired number of columns
18
+ * The width can be set arbitrarily, or it can be left to resize with the containing element
19
+ * Removes the use of background images for displaying the grid
20
+ * Increased ability to style calendar days
21
+ * Removed legacy options, comments, and code
22
+ * Caution, not backwards compatible with some of Version 1's options
23
+
24
+ == 2009-11-11 / Version 1
25
+ * The initial EventCalendar Rails Plugin
26
+ * Most bugs and issues have been resolved
27
+ * Works, but isn't as flexible as desired
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Elevation
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,247 @@
1
+ ==EventCalendar
2
+
3
+ Easily show multiple, overlapping events across calendar days and rows.
4
+
5
+ See http://dev.elevationblog.com/2009/7/23/event-calendar-rails-plugin for a screenshot.
6
+
7
+ After install, the "calendar" method will be available within your views.
8
+
9
+ To customize the look, modify the included stylesheet and/or change the default options.
10
+
11
+
12
+ ==Install
13
+
14
+ script/plugin install git://github.com/elevation/event_calendar.git
15
+
16
+ To generate the necessary static files AND the example below:
17
+
18
+ script/generate event_calendar
19
+
20
+ Options:
21
+
22
+ * --static-only: Only generate the stylesheet and javascript
23
+ * --use-jquery: Generate jquery javascript
24
+ * --use-all-day: Include an 'all_day' field on events, and display appropriately
25
+
26
+ You can change the default event model name (Event) and view name (Calendar) by passing in two name arguments:
27
+
28
+ script/generate event_calendar EventModel ViewName
29
+
30
+
31
+ ==Generated Files
32
+
33
+ Make sure to include the stylesheet and javascript in your layout/view.
34
+
35
+ ====Static files
36
+
37
+ public/stylesheets/event_calendar.css
38
+ public/javascripts/event_calendar.js
39
+
40
+ Unless the --static-only option is given, the following will be generated. Names will differ if name arguments were passed to the generator.
41
+
42
+ ====db/migrate/XXXX_create_events.rb
43
+
44
+ class CreateEvents < ActiveRecord::Migration
45
+ def self.up
46
+ create_table :events do |t|
47
+ t.string :name
48
+ t.datetime :start_at
49
+ t.datetime :end_at
50
+
51
+ t.timestamps
52
+ end
53
+ end
54
+
55
+ def self.down
56
+ drop_table :events
57
+ end
58
+ end
59
+
60
+ At minimum we need to have start_at and end_at fields.
61
+
62
+ If the '--use-all-day' option is passed to the generator, it will also add a boolean all_day field.
63
+
64
+ An event can also have a *color* field (hex value stored as a string) which determines the color of the event.
65
+ Or simply override the default virtual attribute on the model. For example, if events are associated to a calendar model, then the events can get their color from the calendar.
66
+
67
+ ====app/models/event.rb
68
+
69
+ class Event < ActiveRecord::Base
70
+ has_event_calendar
71
+ end
72
+
73
+ ====config/routes.rb
74
+
75
+ map.calendar "/calendar/:year/:month", :controller => "calendar", :action => "index", :year => Time.now.year, :month => Time.now.month
76
+
77
+ ====app/controllers/calendar_controller.rb
78
+
79
+ class CalendarController < ApplicationController
80
+
81
+ def index
82
+ @month = params[:month].to_i
83
+ @year = params[:year].to_i
84
+
85
+ @shown_month = Date.civil(@year, @month)
86
+
87
+ @event_strips = Event.event_strips_for_month(@shown_month)
88
+ end
89
+
90
+ end
91
+
92
+ ====app/helpers/calendar_helper.rb
93
+
94
+ Some helper methods are created, but you could put this in the view. The key is our calendar method, which takes some options.
95
+
96
+ module CalendarHelper
97
+ def month_link(month_date)
98
+ link_to(I18n.localize(month_date, :format => "%B"), {:month => month_date.month, :year => month_date.year})
99
+ end
100
+
101
+ # custom options for this calendar
102
+ def event_calendar_options
103
+ {
104
+ :year => @year,
105
+ :month => @month,
106
+ :event_strips => @event_strips,
107
+ :month_name_text => I18n.localize(@shown_month, :format => "%B %Y"),
108
+ :previous_month_text => "<< " + month_link(@shown_month.last_month),
109
+ :next_month_text => month_link(@shown_month.next_month) + " >>"
110
+ }
111
+ end
112
+
113
+ def event_calendar
114
+ calendar event_calendar_options do |args|
115
+ event = args[:event]
116
+ %(<a href="/events/#{event.id}" title="#{h(event.name)}">#{h(event.name)}</a>)
117
+ end
118
+ end
119
+ end
120
+
121
+ Notice you can pass in a block to the calendar method. In this example I'm passing a link to the event details, and displaying the event's name.
122
+
123
+ ====app/views/calendar/index.html.erb
124
+
125
+ Then in calendar view, simply:
126
+
127
+ <%= event_calendar %>
128
+
129
+
130
+ ==Default Options
131
+
132
+ The default options for the calendar are:
133
+
134
+ defaults = {
135
+ :year => Time.zone.now.year,
136
+ :month => Time.zone.now.month,
137
+ :abbrev => true,
138
+ :first_day_of_week => 0, # See note below when setting this
139
+ :show_today => true,
140
+ :month_name_text => Time.zone.now.strftime("%B %Y"),
141
+ :previous_month_text => nil,
142
+ :next_month_text => nil,
143
+ :event_strips => [],
144
+
145
+ # it would be nice to have these in the CSS file
146
+ # but they are needed to perform height calculations
147
+ :width => nil,
148
+ :height => 500,
149
+ :day_names_height => 18,
150
+ :day_nums_height => 18,
151
+ :event_height => 18,
152
+ :event_margin => 1,
153
+ :event_padding_top => 1,
154
+
155
+ :use_all_day => false,
156
+ :use_javascript => true,
157
+ :link_to_day_action => false
158
+ }
159
+
160
+ You can override any of these by passing your options to the calendar method. In the above example, update the event_calendar_options helper method.
161
+
162
+ ====Details
163
+
164
+ * See the notes in the plugin's calendar_helper.rb for more info.
165
+
166
+ * *width*: Optional, if none is given it will stretch to the containing element.
167
+
168
+ * *height*: Defaults to 500px. This is the approx minimum total height of the calendar. It could be greater if a calendar row(s) need to stretch to fit additional events.
169
+
170
+ * *use_all_day*: If set to true, will check for an 'all_day' boolean field when displaying an event. If it is an all day event, or the event is multiple days, then it will display as usual. Otherwise it will display without a background color bar.
171
+
172
+ Helper/View:
173
+ :use_all_day => true
174
+
175
+ If using this option, you probably want to also show times for non-all-day events:
176
+ calendar event_calendar_options do |args|
177
+ event, day = args[:event], args[:day]
178
+ html = %(<a href="/events/#{event.id}" title="#{h(event.name)}">)
179
+ html << display_event_time(event, day)
180
+ html << %(#{h(event.name)}</a>)
181
+ html
182
+ end
183
+
184
+ * *use_javascript*: If set to false, it won't add custom HTML data attributes that are needed for javascript highlighting. (Note, custom attributes will be valid in HTML 5.) The actual javascript is unobtrusive and found in the event_calendar.js file.
185
+
186
+ * *link_to_day_action*: Will make the calendar's day numbers links to the given Rails action. Note, you'll probably want a corresponding route, controller action, and view to go with this action. Example:
187
+
188
+ Helper/View calendar option:
189
+ :link_to_day_action => "day"
190
+
191
+ Route (the controller is the same as your other calendar route):
192
+ map.calendar_day "/calendar/:year/:month/:day", :controller => "calendar", :action => "day"
193
+
194
+
195
+ ==Notes
196
+
197
+ * If you want to change the <b>first day of the week</b> from the default of Sunday (0), then set the new value in an instance variable and pass it to event_strips_for_month (in the controller), and to the event calendar options (in the helper/view).
198
+
199
+ Controller:
200
+ @first_day_of_week = 1
201
+ @event_strips = Event.event_strips_for_month(@shown_month, @first_day_of_week)
202
+
203
+ Helper/View calendar options:
204
+ :first_day_of_week => @first_day_of_week
205
+
206
+ * If you need access to the events, not just the event strips, then instead of calling event_strips_for_month call these 3 methods:
207
+
208
+ start_d, end_d = Event.get_start_and_end_dates(@shown_month) # optionally pass in @first_day_of_week
209
+ @events = Event.events_for_date_range(start_d, end_d)
210
+ @event_strips = Event.create_event_strips(start_d, end_d, @events)
211
+
212
+ * The event <b>select color</b> is set in the event_calendar.js file.
213
+
214
+ ==il8n
215
+
216
+ To localize month and day names, add the following to your localization file(s) in config/locales:
217
+
218
+ date -> formats, day_names, abbr_day_names, month_names, abbr_month_names
219
+
220
+ For example, in es.yml:
221
+
222
+ es:
223
+ date:
224
+ formats:
225
+ default: "%e/%m/%Y"
226
+
227
+ day_names: [Domingo, Lunes, Martes, Miércoles, Jueves, Viernes, Sábado]
228
+ abbr_day_names: [Dom, Lun, Mar, Mie, Jue, Vie, Sab]
229
+
230
+ # Don't forget the nil at the beginning; there's no such thing as a 0th month
231
+ month_names: [~, Enero, Febrero, Marzo, Abril, Mayo, Junio, Julio, Agosto, Septiembre, Octubre, Noviembre, Diciembre]
232
+ abbr_month_names: [~, Ene, Feb, Mar, Abr, May, Jun, Jul, Ago, Sep, Oct, Nov, Dic]
233
+
234
+
235
+ ==Contributors
236
+
237
+ * Jeff Schuil
238
+ * See commit history for list of additional contributors.
239
+ * Thanks to those who have added features, fixed bugs, and/or reported issues.
240
+
241
+ == History
242
+
243
+ * Though EventCalendar has diverged greatly, it was...
244
+ * Originally based off of James Urquhart's http://www.cuppadev.co.uk/webdev/making-a-real-calendar-in-rails/
245
+ * This in turn started as Geoffrey Grosenbach's CalendarHelper.
246
+
247
+ Copyright (c) 2009 Elevation, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the event_calendar plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the event_calendar plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'EventCalendar'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,20 @@
1
+ Usage:
2
+
3
+ script/generate event_calendar [EVENT_MODEL VIEW_NAME]
4
+
5
+ This will create:
6
+
7
+ # static files
8
+ public/stylesheets/event_calendar.css
9
+ public/javascripts/event_calendar.js
10
+
11
+ # Unless --static-only option is given
12
+ # MVC and supporting files (depending on model and view name)
13
+ app/models/event.rb
14
+ app/controllers/calendar_controller.rb
15
+ app/views/calendar
16
+ app/views/calendar/index.html.erb
17
+ app/helpers/calendar_helper.rb
18
+ db/migrate
19
+ db/migrate/XXXX_create_events.rb
20
+ route map.calendar
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/lib/insert_routes.rb")
2
+
3
+ class EventCalendarGenerator < Rails::Generator::Base
4
+ default_options :static_only => false,
5
+ :use_jquery => false,
6
+ :use_all_day => false
7
+
8
+ attr_reader :class_name, :view_name
9
+
10
+ def initialize(args, runtime_options = {})
11
+ super
12
+ usage if args.length > 0 and args.length < 2
13
+ @class_name = (args.shift || "event").underscore
14
+ @view_name = (args.shift || "calendar").underscore
15
+ end
16
+
17
+ def manifest
18
+ record do |m|
19
+ # static files
20
+ m.file "stylesheet.css", "public/stylesheets/event_calendar.css"
21
+
22
+ script = options[:use_jquery] ? 'jq_javascript.js' : 'javascript.js'
23
+ m.file script, "public/javascripts/event_calendar.js"
24
+
25
+ # MVC and other supporting files
26
+ unless options[:static_only]
27
+ m.template "model.rb.erb", File.join("app/models", "#{@class_name}.rb")
28
+ m.template "controller.rb.erb", File.join("app/controllers", "#{@view_name}_controller.rb")
29
+ m.directory File.join("app/views", @view_name)
30
+ m.template "view.html.erb", File.join("app/views", @view_name, "index.html.erb")
31
+ m.template "helper.rb.erb", File.join("app/helpers", "#{@view_name}_helper.rb")
32
+ m.migration_template "migration.rb.erb", "db/migrate", :migration_file_name => "create_#{@class_name.pluralize}"
33
+ m.route_name(@view_name, "/#{@view_name}/:year/:month", ":controller => '#{@view_name}', :action => 'index', :year => Time.zone.now.year, :month => Time.zone.now.month")
34
+ end
35
+ end
36
+ end
37
+
38
+ protected
39
+
40
+ def add_options!(opt)
41
+ opt.separator ''
42
+ opt.separator 'Options:'
43
+ opt.on("--static-only",
44
+ "Only generate the static files. (stylesheet, javascript, and images)") { |v| options[:static_only] = v }
45
+ opt.on("--use-jquery",
46
+ "Use jquery template file when generating the javascript.") { |v| options[:use_jquery] = v }
47
+ opt.on("--use-all-day",
48
+ "Include an 'all_day' field on events, and display appropriately.") { |v| options[:use_all_day] = v }
49
+ end
50
+ end
@@ -0,0 +1,55 @@
1
+ # Adapted from Restful Authentication generator
2
+ Rails::Generator::Commands::Create.class_eval do
3
+ def route_resource(*resources)
4
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
5
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
6
+
7
+ logger.route "map.resource #{resource_list}"
8
+ unless options[:pretend]
9
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
10
+ "#{match}\n map.resource #{resource_list}\n"
11
+ end
12
+ end
13
+ end
14
+
15
+ def route_name(name, path, route_options)
16
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
17
+
18
+ logger.route "map.#{name} '#{path}', #{route_options}"
19
+ unless options[:pretend]
20
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
21
+ "#{match}\n map.#{name} '#{path}', #{route_options}"
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ Rails::Generator::Commands::Destroy.class_eval do
28
+ def route_resource(*resources)
29
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
30
+ look_for = "\n map.resource #{resource_list}\n"
31
+ logger.route "map.resource #{resource_list}"
32
+ unless options[:pretend]
33
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
34
+ end
35
+ end
36
+
37
+ def route_name(name, path, route_options)
38
+ look_for = "\n map.#{name} '#{path}', #{route_options}"
39
+ logger.route "map.#{name} '#{path}', #{route_options}"
40
+ unless options[:pretend]
41
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
42
+ end
43
+ end
44
+ end
45
+
46
+ Rails::Generator::Commands::List.class_eval do
47
+ def route_resource(*resources)
48
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
49
+ logger.route "map.resource #{resource_list}"
50
+ end
51
+
52
+ def route_name(name, path, route_options)
53
+ logger.route "map.#{name} '#{path}', #{route_options}"
54
+ end
55
+ end