weekly_calendar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in weekly_calendar.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Colin Young
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # WeeklyCalendar
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'weekly_calendar'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install weekly_calendar
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ require "week" # Require the week gem
2
+
3
+ require "weekly_calendar/version"
4
+ require "weekly_calendar/railtie"
5
+ require "weekly_calendar/calendar"
6
+ require "weekly_calendar/helpers"
7
+
8
+ module WeeklyCalendar
9
+
10
+ # Converts active record objects into hashed date format for WC
11
+ def self.to_events(array, date_method=:start_at_date)
12
+
13
+ Hash.new.tap do |h|
14
+ array.each do |object|
15
+ k = object.send(date_method).try(:to_date)
16
+ return if k.nil?
17
+ h[k] ||= []
18
+ h[k] << object
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,134 @@
1
+ module WeeklyCalendar
2
+
3
+ class Calendar
4
+
5
+ attr_accessor :output_buffer
6
+
7
+ def self.as_html(options)
8
+ self.new(options).to_html
9
+ end
10
+
11
+ def initialize(options)
12
+ @options = options
13
+
14
+ @view = options[:view]
15
+ @date = options[:for] || options[:date] || Date.current
16
+ @events = options[:events] || {}
17
+ end
18
+
19
+ def to_s
20
+ @days = days
21
+
22
+ content_tag :table, class: "weekly-calendar #{@options[:class]}", id: @options[:id] do
23
+ content_tag(:thead, headings) + content_tag(:tbody, body)
24
+ end
25
+ end
26
+
27
+ def days
28
+ start_week = (@date - 1.week)
29
+ if ![0,6].include?(@date.wday) # is a weekday
30
+ start_week -= 1.week
31
+ end
32
+ if @options[:weekend]
33
+ start_week.week
34
+ else
35
+ start_week.week[1..5]
36
+ end
37
+ end
38
+
39
+ def headings
40
+ content_tag :tr do
41
+ String.new.html_safe.tap do |s|
42
+ @days.each do |d|
43
+ # @todo Localize the month/day order
44
+ others = [ class: date_classes(d) ]
45
+ s << content_tag(:th, "#{Date::ABBR_DAYNAMES[d.wday]} #{d.month}/#{d.mday}", *others)
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def body
52
+ content_tag :tr do
53
+ String.new.html_safe.tap do |s|
54
+ @days.each do |date|
55
+ others = [ class: date_classes(date), :'data-date' => date.to_date ]
56
+ s << content_tag(:td,
57
+ date_box(date) + events_ending_this_week(days, date) + events_for_date(date),
58
+ *others)
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def date_box(date)
65
+ content_tag :div, date.mday, class: 'wc-date'
66
+ end
67
+
68
+ def events_for_date(date)
69
+ events = @events.delete(date.to_date)
70
+ return if events.nil?
71
+
72
+ content_tag :div, class: 'wc-events' do
73
+ safe_str do |s|
74
+ events.each do |event|
75
+ next if event.nil?
76
+
77
+ s << render_event(event, on: date.to_date)
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ def events_ending_this_week(week, date)
84
+ unless (events = events_ending_on(date)).empty?
85
+ content_tag :div, class: 'wc-events-ending' do
86
+ safe_str do |s|
87
+ events.compact.each do |event|
88
+ next if event.start_at_date >= week.first.to_date
89
+ s << render_event(event, on: date.to_date)
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+
96
+ def to_html
97
+ to_s.html_safe
98
+ end
99
+
100
+ def safe_str(&block)
101
+ String.new.html_safe.tap(&block)
102
+ end
103
+
104
+ def content_tag(*args, &block)
105
+ @view.content_tag(*args, &block)
106
+ end
107
+
108
+ protected
109
+
110
+ def render_event(event, options={})
111
+ content_tag(:div, class: "wc-event-container wc-days-#{[event.days(options[:on] || options[:date]), 5].min}") do
112
+ @view.render 'weekly_calendar/event', event: event
113
+ end
114
+ end
115
+
116
+ def events_ending_on(date)
117
+ Array.new.tap do |a|
118
+ @events.each do |date,events|
119
+ events.each do |event|
120
+ a << event if event.end_at_date == date.to_date
121
+ end
122
+ end
123
+ end
124
+ end
125
+
126
+ def date_classes(date)
127
+ Array.new.tap do |k|
128
+ k << "today" if date.same_day_as?(@date)
129
+ k << "past" if date.past? and !date.same_day_as?(@date)
130
+ end
131
+ end
132
+
133
+ end
134
+ end
@@ -0,0 +1,19 @@
1
+ class Date
2
+ def same_day_as?(date_or_time)
3
+ if date_or_time.is_a?(Time)
4
+ date_or_time.yday == self.yday && date_or_time.year == self.year
5
+ else
6
+ self == date_or_time
7
+ end
8
+ end
9
+ end
10
+
11
+ class Time
12
+ def same_day_as?(date_or_time)
13
+ if date_or_time.is_a?(Date)
14
+ date_or_time.yday == self.yday && date_or_time.year == self.year
15
+ else
16
+ self == date_or_time
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,8 @@
1
+ module WeeklyCalendar
2
+ class Railtie < Rails::Railtie
3
+ ActiveSupport.on_load :action_view do
4
+ require 'weekly_calendar/view_helper'
5
+ include WeeklyCalendar::ViewHelper
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module WeeklyCalendar
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ module WeeklyCalendar
2
+ module ViewHelper
3
+
4
+ def weekly_calendar(options)
5
+ WeeklyCalendar::Calendar.as_html(options.merge(view: self))
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/weekly_calendar/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Colin Young"]
6
+ gem.email = ["me@colinyoung.com"]
7
+ gem.description = %q{weekly calendar that supports tasks and multi-day events}
8
+ gem.summary = %q{weekly calendar that supports tasks and multi-day events}
9
+ gem.homepage = ""
10
+
11
+ gem.add_dependency 'week'
12
+
13
+ gem.files = `git ls-files`.split($\)
14
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
16
+ gem.name = "weekly_calendar"
17
+ gem.require_paths = ["lib"]
18
+ gem.version = WeeklyCalendar::VERSION
19
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weekly_calendar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Colin Young
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: week
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: weekly calendar that supports tasks and multi-day events
31
+ email:
32
+ - me@colinyoung.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - lib/weekly_calendar.rb
43
+ - lib/weekly_calendar/calendar.rb
44
+ - lib/weekly_calendar/helpers.rb
45
+ - lib/weekly_calendar/railtie.rb
46
+ - lib/weekly_calendar/version.rb
47
+ - lib/weekly_calendar/view_helper.rb
48
+ - weekly_calendar.gemspec
49
+ homepage: ''
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.23
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: weekly calendar that supports tasks and multi-day events
73
+ test_files: []