simple_calendar 0.0.8 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,41 +2,61 @@ Simple Calendar
2
2
  ===============
3
3
 
4
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.
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.
6
8
 
7
- Thanks to Josh Chernoff for an early rewrite of the calendar generation
8
- code.
9
+ Thanks to Josh Chernoff and all other contributors.
9
10
 
10
11
  Installation
11
12
  ------------
12
13
 
13
14
  Just add this into your Gemfile followed by a bundle install:
14
15
 
15
- gem "simple_calendar", "~> 0.0.7"
16
+ gem "simple_calendar", "~> 0.1.1"
16
17
 
17
18
  Usage
18
19
  -----
19
20
 
20
- ####Model
21
+ #### Model
21
22
 
22
- Here we have a model called Event with the start_time attribute that we
23
- will be using with simple_calendar.
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.
24
26
 
25
- class Event < ActiveRecord::Base
26
- has_calendar
27
- end
27
+ If you don't have an attribute called `start_time` on your model, you
28
+ can simply delegate like so:
28
29
 
29
- has_calendar has options that can be passed to it for configuration:
30
+ ```ruby
31
+ class Event < ActiveRecord::Base
32
+ attr_accessible :name, :event_start_time
30
33
 
31
- has_calendar :start_time => :my_start_column
34
+ def start_time
35
+ event_start_time
36
+ end
37
+ end
38
+ ```
32
39
 
33
- The `start_time` option is the field for the start time of the event. This will use
34
- `my_start_column` to determine which day to render the event on.
40
+ As long as `start_time` returns a `DateTime` object, you're good to go.
41
+ This means SimpleCalendar is now compatible with any class, whether it's
42
+ ORM backed like ActiveRecord, Mongoid, or it's just a pure Ruby class.
43
+ (Yay!)
35
44
 
36
- ####Views
45
+ ##### Querying
37
46
 
38
- We query the events we want to display as usual, and then render the
39
- calendar in the view like so:
47
+ SimpleCalendar uses `params[:month]` and `params[:year]` to determine
48
+ which month of the calendar to render. You can use these to make your
49
+ database queries more efficient.
50
+
51
+ #### Views
52
+
53
+ SimpleCalendar just accepts an array of events and a block. The block
54
+ will be executed for each event so you can provide your own logic for
55
+ displaying the events.
56
+
57
+ Here's an example that uses SimpleCalendar to simply render a link to
58
+ each event on its own line inside the table. You would simply query for
59
+ the `@events` as discussed above in the querying section.
40
60
 
41
61
  <%= calendar @events do |event| %>
42
62
  <div><%= link_to event.title, event %></div>
@@ -58,15 +78,15 @@ Possible options:
58
78
 
59
79
  :year # current year, default: from params or current year
60
80
  :month # current month, default: from params or current month
61
- :prev_text # previous month link text, default: &laquo;
62
- :next_text # next month link text, default: &raquo;
81
+ :prev_text # previous month link text, default: &laquo;
82
+ :next_text # next month link text, default: &raquo;
63
83
 
64
84
  CSS
65
85
  ---
66
86
 
67
87
  You will probably want to customize the height of the calendar so that
68
- all the rows are the same. You can do this by adding the following line
69
- to your css:
88
+ all the rows are the same heights and widths. You can do this by adding
89
+ the following line to your css:
70
90
 
71
91
  .calendar td { height: 100px; width: 14.28%; }
72
92
 
@@ -76,7 +96,4 @@ and .calendar classes.
76
96
  TODO
77
97
  ====
78
98
 
79
- * Add query helpers to grab events for a current month and the days into
80
- the next and previous months for efficiency
81
99
  * Customizable starting day of week
82
- * More customization?
@@ -1,4 +1,3 @@
1
1
  require "simple_calendar/version"
2
2
  require "simple_calendar/view_helpers"
3
- require "simple_calendar/model_additions"
4
3
  require "simple_calendar/railtie"
@@ -3,10 +3,5 @@ module SimpleCalendar
3
3
  initializer "simple_calendar.view_helpers" do
4
4
  ActionView::Base.send :include, ViewHelpers
5
5
  end
6
- initializer "simple_calendar.model_additions" do
7
- ActiveSupport.on_load :active_record do
8
- extend ModelAdditions
9
- end
10
- end
11
6
  end
12
7
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleCalendar
2
- VERSION = "0.0.8"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -2,6 +2,9 @@ module SimpleCalendar
2
2
  module ViewHelpers
3
3
 
4
4
  def calendar(events, options={}, &block)
5
+ raise 'SimpleCalendar requires a block to be passed in' unless block_given?
6
+
7
+
5
8
  opts = {
6
9
  :year => (params[:year] || Time.zone.now.year).to_i,
7
10
  :month => (params[:month] || Time.zone.now.month).to_i,
@@ -9,6 +12,7 @@ module SimpleCalendar
9
12
  :next_text => raw("&raquo;")
10
13
  }
11
14
  options.reverse_merge! opts
15
+ events ||= []
12
16
  selected_month = Date.civil(options[:year], options[:month])
13
17
  current_date = Date.today
14
18
  range = build_range selected_month
@@ -26,7 +30,7 @@ module SimpleCalendar
26
30
  end_date = selected_month.end_of_month
27
31
  end_date = end_date.saturday? ? end_date : end_date.end_of_week(:sunday)
28
32
 
29
- date_range = (start_date..end_date).to_a
33
+ (start_date..end_date).to_a
30
34
  end
31
35
 
32
36
  def build_month(date_range)
@@ -73,7 +77,7 @@ module SimpleCalendar
73
77
  divs = []
74
78
 
75
79
  concat content_tag(:div, date.day.to_s, :class=>"day_number")
76
- divs << day_events(date, events).collect {|event| block.call(event) }
80
+ divs << day_events(date, events).collect { |event| block.call(event) }
77
81
  divs.join.html_safe
78
82
  end #content_tag :div
79
83
  end #content_tag :td
@@ -90,7 +94,7 @@ module SimpleCalendar
90
94
 
91
95
  # Returns an array of events for a given day
92
96
  def day_events(date, events)
93
- events.select { |e| e.start_time_column.to_date == date }
97
+ events.select { |e| e.start_time.to_date == date }
94
98
  end
95
99
 
96
100
  # Generates the header that includes the month and next and previous months
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.0.8
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-27 00:00:00.000000000 Z
12
+ date: 2012-12-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -39,7 +39,6 @@ files:
39
39
  - README.md
40
40
  - Rakefile
41
41
  - lib/simple_calendar.rb
42
- - lib/simple_calendar/model_additions.rb
43
42
  - lib/simple_calendar/railtie.rb
44
43
  - lib/simple_calendar/version.rb
45
44
  - lib/simple_calendar/view_helpers.rb
@@ -56,12 +55,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
55
  - - ! '>='
57
56
  - !ruby/object:Gem::Version
58
57
  version: '0'
58
+ segments:
59
+ - 0
60
+ hash: 1074299040452234765
59
61
  required_rubygems_version: !ruby/object:Gem::Requirement
60
62
  none: false
61
63
  requirements:
62
64
  - - ! '>='
63
65
  - !ruby/object:Gem::Version
64
66
  version: '0'
67
+ segments:
68
+ - 0
69
+ hash: 1074299040452234765
65
70
  requirements: []
66
71
  rubyforge_project: simple_calendar
67
72
  rubygems_version: 1.8.24
@@ -1,15 +0,0 @@
1
- module SimpleCalendar
2
- module ModelAdditions
3
- def has_calendar(options={})
4
- config = { :start_time => "start_time"}
5
- config.update(options) if options.is_a?(Hash)
6
-
7
- class_eval <<-EOV
8
- def start_time_column
9
- #{config[:start_time]}
10
- end
11
- EOV
12
- end
13
- end
14
- end
15
-