simple_calendar 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -9,7 +9,7 @@ Installation
9
9
 
10
10
  Just add this into your Gemfile followed by a bundle install:
11
11
 
12
- gem "simple_calendar", "~> 0.0.3"
12
+ gem "simple_calendar", "~> 0.0.4"
13
13
 
14
14
  Usage
15
15
  -----
@@ -1,3 +1,3 @@
1
1
  module SimpleCalendar
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,78 +1,112 @@
1
1
  module SimpleCalendar
2
2
  module ViewHelpers
3
+
3
4
  def calendar(events, &block)
4
- day = Date.civil((params[:year] || Time.zone.now.year).to_i, (params[:month] || Time.zone.now.month).to_i)
5
+ selected_month = Date.civil((params[:year] || Time.zone.now.year).to_i, (params[:month] || Time.zone.now.month).to_i)
6
+ current_date = Date.today
7
+ range = build_range selected_month
8
+ month_array = build_month range
5
9
 
6
- content_tag :table, :class => "bordered-table calendar" do
7
- month_header(day) + day_header + body(day, events, block)
8
- end
10
+ draw_calendar(selected_month, month_array, current_date, events, block)
9
11
  end
10
12
 
11
- def start_date(date)
12
- start_date = date.beginning_of_month
13
- start_date = start_date.beginning_of_week.advance(:days => -1) unless start_date.sunday?
14
- start_date
15
- end
13
+ private
16
14
 
17
- def end_date(date)
18
- end_date = date.end_of_month
19
- end_date = end_date.advance(:days => 1).end_of_week if end_date.sunday?
20
- end_date
21
- end
15
+ def build_range(selected_month)
16
+ start_date = selected_month.beginning_of_month
17
+ start_date = start_date.sunday? ? start_date : start_date.beginning_of_week.advance(:days => -1)
22
18
 
23
- def month_header(day)
24
- content_tag :h2 do
25
- previous_month = day.advance :months => -1
26
- next_month = day.advance :months => 1
27
- tags = []
28
- tags << link_to("<", request.fullpath.split('?').first + "?month=#{previous_month.month}&year=#{previous_month.year}")
29
- tags << day.strftime("%B %Y")
30
- tags << link_to(">", request.fullpath.split('?').first + "?month=#{next_month.month}&year=#{next_month.year}")
31
- tags.join.html_safe
32
- end
33
- end
19
+ end_date = selected_month.end_of_month
20
+ end_date = end_date.sunday? ? end_date : end_date.advance(:days => 1).end_of_week
34
21
 
35
- def day_header
36
- content_tag :thead do
37
- content_tag :tr do
38
- I18n.t(:"date.abbr_day_names").map{ |day| content_tag :th, day }.join.html_safe
39
- end
40
- end
22
+ date_range = (start_date..end_date).to_a
41
23
  end
42
24
 
43
- def body(day, events, block)
44
- current_date = start_date(day).dup
45
-
46
- content_tag :tbody do
47
- weeks = []
48
- while current_date < end_date(day)
49
- weeks << content_tag(:tr) do
50
- tags = []
51
- while not current_date.saturday?
52
- tags << day(current_date, events, block)
53
- current_date = current_date.tomorrow
54
- end
55
- tags << day(current_date, events, block)
56
- current_date = current_date.tomorrow
57
- tags.join.html_safe
58
- end
25
+ def build_month(date_range)
26
+ month = []
27
+ week = []
28
+ i = 0
29
+
30
+ date_range.each do |date|
31
+ week << date
32
+ if i == 6
33
+ i = 0
34
+ month << week
35
+ week = []
36
+ else
37
+ i += 1
59
38
  end
60
- weeks.join.html_safe
61
39
  end
40
+
41
+ month
62
42
  end
63
43
 
64
- def day(date, events, block)
65
- content_tag :td do
66
- concat content_tag(:div, date.day, :class => "day")
44
+ # Renders the calendar table
45
+ def draw_calendar(selected_month, month, current_date, events, block)
46
+ tags = []
67
47
 
68
- day_events(date, events).map do |event|
69
- block.call(event)
70
- end.join.html_safe
71
- end
48
+ content_tag(:table, :class => "table table-bordered table-striped calendar") do
49
+ tags << month_header(selected_month)
50
+ tags << content_tag(:thead, content_tag(:tr, I18n.t("date.abbr_day_names").collect { |name| content_tag :th, name, :class => (selected_month.month == Date.today.month && Date.today.strftime("%a") == name ? "current-day" : nil)}.join.html_safe))
51
+ tags << content_tag(:tbody) do
52
+
53
+ month.collect do |week|
54
+ content_tag(:tr, :class => (week.include?(Date.today) ? "current-week week" : "week")) do
55
+
56
+ week.collect do |date|
57
+ tb_class = []
58
+ tb_class << not_current_month = (date.month == selected_month.month ? "" : "not-currnet-month")
59
+ tb_class << (Date.today == date ? "today day" : "day")
60
+
61
+ content_tag(:td, :class => tb_class.join(" ")) do
62
+ content_tag(:div) do
63
+ divs = []
64
+
65
+ concat content_tag(:div, date.day.to_s)
66
+ divs << day_events(date, events).collect {|event| block.call(event) }
67
+ divs.join.html_safe
68
+ end #content_tag :div
69
+ end #content_tag :td
70
+
71
+ end.join.html_safe
72
+ end #content_tag :tr
73
+
74
+ end.join.html_safe
75
+ end #content_tag :tbody
76
+
77
+ tags.join.html_safe
78
+ end #content_tag :table
72
79
  end
73
80
 
81
+ # Returns an array of events for a given day
74
82
  def day_events(date, events)
75
83
  events.select { |e| e.start_time_column.to_date == date }
76
84
  end
85
+
86
+ # Generates the header that includes the month and next and previous months
87
+ def month_header(selected_month)
88
+ content_tag :h2 do
89
+ previous_month = selected_month.advance :months => -1
90
+ next_month = selected_month.advance :months => 1
91
+ tags = []
92
+
93
+ tags << month_link("&laquo;".html_safe, previous_month, :class => "previous-month")
94
+ tags << selected_month.strftime("%B %Y")
95
+ tags << month_link("&raquo;".html_safe, next_month, :class => "next-month")
96
+
97
+ tags.join.html_safe
98
+ end
99
+ end
100
+
101
+ # Generates the link to next and previous months
102
+ def month_link(text, month, opts={})
103
+ link_to(text, "#{simple_calendar_path}?month=#{month.month}&year=#{month.year}", opts)
104
+ end
105
+
106
+ # Returns the full path to the calendar
107
+ # This is used for generating the links to the next and previous months
108
+ def simple_calendar_path
109
+ request.fullpath.split('?').first
110
+ end
77
111
  end
78
112
  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.0.4
4
+ version: 0.0.5
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-03-27 00:00:00.000000000 Z
12
+ date: 2012-06-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
64
  version: '0'
65
65
  requirements: []
66
66
  rubyforge_project: simple_calendar
67
- rubygems_version: 1.8.21
67
+ rubygems_version: 1.8.23
68
68
  signing_key:
69
69
  specification_version: 3
70
70
  summary: A simple Rails 3 calendar