ical 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ical.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 TODO: Write your name
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
+ # Ical
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ical'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ical
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 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ical/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ical"
8
+ spec.version = Ical::VERSION
9
+ spec.authors = ["ws70 team"]
10
+ spec.email = ["railscode@gmail.com"]
11
+ spec.description = "Modernization watu_table_builder with iCal calendar style for Rails App"
12
+ spec.summary = "Modernization watu_table_builder with iCal calendar style for Rails App"
13
+ spec.homepage = "https://github.com/ws70/ical"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,6 @@
1
+ require "ical/table_builder.rb"
2
+ require "ical/calendar_helper.rb"
3
+ require "ical/version.rb"
4
+
5
+ ActionView::Base.send :include, TableHelper
6
+ ActionView::Base.send :include, CalendarHelper
@@ -0,0 +1,173 @@
1
+ # coding: utf-8
2
+ module CalendarHelper
3
+ # Generates a calendar (as a table) for an array of objects placing each of them on the corresponding date.
4
+ # **TODO: fully document this method, the current documentation is far from done.**
5
+ # @param [Hash] options extra options
6
+ # :row_header if true, each row will have an extra cell at the beginning, as a row header. A typical usage would be
7
+ # to output week numbers. When the block is called, it will get the date that would normally be passed to the
8
+ # first day of the week (to give you some context) and a nil list of objects
9
+ # (and that's how you recognize it as a header, because empty days get an empty array, not nil).
10
+
11
+ def calendar_for(objects, *args)
12
+ raise ArgumentError, "Missing block" unless block_given?
13
+ options = args.last.is_a?(Hash) ? args.pop : {}
14
+ html_options = options[:html]
15
+ builder = options[:builder] || CalendarBuilder
16
+ calendar = options[:calendar] || Calendar
17
+ content_tag(:table, nil, html_options) do
18
+ yield builder.new(objects || [], self, calendar, options)
19
+ end
20
+ end
21
+
22
+ class CalendarBuilder < TableHelper::TableBuilder
23
+ def initialize(objects, template, calendar, options)
24
+ super(objects, template, options)
25
+ @calendar = calendar.new(options)
26
+ @today = options[:today] || Time.now
27
+ @row_header = options[:row_header] || false
28
+ end
29
+
30
+ def day(*args)
31
+ raise ArgumentError, "Missing block" unless block_given?
32
+ options = options_from_hash(args)
33
+ day_method = options.delete(:day_method) || :date
34
+ id_pattern = options.delete(:id) || 'day-%d'
35
+
36
+ # status
37
+ tbody do
38
+ @calendar.objects_for_days(@objects, day_method).to_a.sort{|a1, a2| a1.first <=> a2.first }.each do |o|
39
+ key, array = o
40
+ day, objects = array
41
+ status = objects.any? ? objects.collect {|o| o.try(:ical_klass) }.compact.join(" ") : "free"
42
+ concat(tag(:tr, options, true)) if(day.wday == @calendar.first_weekday)
43
+ if @row_header && day.wday == @calendar.first_weekday
44
+ row_header_options = td_options(day, id_pattern)
45
+ row_header_options[:class] ||= ""
46
+ row_header_options[:class] << " row_header"
47
+ concat(tag(:td, row_header_options, true))
48
+ yield(day, nil)
49
+ concat("</td>")
50
+ end
51
+ concat(tag(:td, td_options(day, id_pattern, status), true))
52
+ yield(day, objects)
53
+ concat('</td>')
54
+ concat('</tr>') if(day.wday == @calendar.last_weekday)
55
+ end
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def objects_for_days
62
+ @calendar.objects_for_days(@objects)
63
+ end
64
+
65
+ def td_options(day, id_pattern, status)
66
+ options = {}
67
+ css_classes = []
68
+ css_classes << 'today' if day.strftime("%Y-%m-%d") == @today.strftime("%Y-%m-%d")
69
+ css_classes << 'notmonth' if day.month != @calendar.month
70
+ css_classes << 'free' if day.month != @calendar.month
71
+ css_classes << 'weekend' if day.wday == 0 or day.wday == 6
72
+ css_classes << 'future' if day > @today.to_date
73
+ css_classes << status
74
+ options[:class] = css_classes.join(' ') unless css_classes.empty?
75
+ options[:id] = day.strftime(id_pattern) if id_pattern
76
+ options
77
+ end
78
+ end
79
+
80
+ class Calendar
81
+ attr_accessor :first_weekday, :last_weekday, :month
82
+
83
+ # :first lets you set the first day to start the calendar on
84
+ # (default is the first day of the given :month and :year).
85
+ # :first => :today will use Date.today
86
+ # :last lets you set the last day of the calendar
87
+ # (default is the last day of the given :month and :year).
88
+ # :last => :thirty will show 30 days from :first
89
+ # :last => :week will show one week
90
+
91
+ def initialize(options={})
92
+ @year = options[:year] || Time.now.year
93
+ @month = options[:month] || Time.now.month
94
+ @first_day_of_week = options[:first_day_of_week] || 0
95
+ @first_weekday = first_day_of_week(@first_day_of_week)
96
+ @last_weekday = last_day_of_week(@first_day_of_week)
97
+
98
+ @first = options[:first]==:today ? Date.today : options[:first] || Date.civil(@year, @month, 1)
99
+
100
+ if options[:last] == :thirty_days || options[:last] == :thirty
101
+ @last = @first + 30
102
+ elsif options[:last] == :one_week || options[:last] == :week
103
+ @last = @first
104
+ else
105
+ @last = options[:last] || Date.civil(@year, @month, -1)
106
+ end
107
+ end
108
+
109
+ def each_day
110
+ first_day.upto(last_day) do |day|
111
+ yield(day)
112
+ end
113
+ end
114
+
115
+ def last_day
116
+ last = @last
117
+ while(last.wday % 7 != @last_weekday % 7)
118
+ last = last.next
119
+ end
120
+ last
121
+ end
122
+
123
+ def first_day
124
+ first = @first - 6
125
+ while(first.wday % 7 != (@first_weekday) % 7)
126
+ first = first.next
127
+ end
128
+ first
129
+ end
130
+
131
+ def objects_for_days(objects, day_method)
132
+ unless @objects_for_days
133
+ @objects_for_days = {}
134
+ days.each{|day| @objects_for_days[day.strftime("%Y-%m-%d")] = [day, []]}
135
+ objects.each do |o|
136
+ date = o.send(day_method.to_sym).strftime("%Y-%m-%d")
137
+ if @objects_for_days[date]
138
+ @objects_for_days[date][1] << o
139
+ end
140
+ end
141
+ end
142
+ @objects_for_days
143
+ end
144
+
145
+ def days
146
+ unless @days
147
+ @days = []
148
+ each_day {|day| @days << day}
149
+ end
150
+ @days
151
+ end
152
+
153
+ def mjdays
154
+ unless @mjdays
155
+ @mdays = []
156
+ each_day {|day| @days << day}
157
+ end
158
+ @days
159
+ end
160
+
161
+ def first_day_of_week(day)
162
+ day
163
+ end
164
+
165
+ def last_day_of_week(day)
166
+ if day > 0
167
+ day - 1
168
+ else
169
+ 6
170
+ end
171
+ end
172
+ end
173
+ end
@@ -0,0 +1,124 @@
1
+ # coding: utf-8
2
+ module TableHelper
3
+ def table_for(objects, *args)
4
+ raise ArgumentError, "Missing block" unless block_given?
5
+ options = args.last.is_a?(Hash) ? args.pop : {}
6
+ html_options = options[:html]
7
+ builder = options[:builder] || TableBuilder
8
+ content_tag(:table, html_options) do
9
+ yield builder.new(objects || [], self, options)
10
+ end
11
+ end
12
+
13
+ class TableBuilder
14
+ include ::ActionView::Helpers::TagHelper
15
+
16
+ def initialize(objects, template, options)
17
+ raise ArgumentError, "TableBuilder expects an Enumerable object but found #{objects.inspect}" unless objects.respond_to? :each
18
+ @objects, @template, @options = objects, template, options
19
+ end
20
+
21
+ def head(*args)
22
+ if block_given?
23
+ concat(tag(:thead, options_from_hash(args), true))
24
+ yield
25
+ concat('</thead>')
26
+ else
27
+ @num_of_columns = args.size
28
+ content_tag(:thead,
29
+ content_tag(:tr,
30
+ args.collect { |c| content_tag(:th, c.html_safe)}.join('').html_safe
31
+ )
32
+ )
33
+ end
34
+ end
35
+
36
+ def head_r(*args)
37
+ raise ArgumentError, "Missing block" unless block_given?
38
+ options = options_from_hash(args)
39
+ head do
40
+ concat(tag(:tr, options, true))
41
+ yield
42
+ concat('</tr>')
43
+ end
44
+ end
45
+
46
+ def body(*args)
47
+ raise ArgumentError, "Missing block" unless block_given?
48
+ options = options_from_hash(args)
49
+ tbody do
50
+ @objects.each { |c| yield(c) }
51
+ end
52
+ end
53
+
54
+ def body_r(*args)
55
+ raise ArgumentError, "Missing block" unless block_given?
56
+ options = options_from_hash(args)
57
+ tbody do
58
+ @objects.each { |c|
59
+ concat(tag(:tr, options, true))
60
+ yield(c)
61
+ concat('</tr>'.html_safe)
62
+ }
63
+ end
64
+ end
65
+
66
+ def r(*args)
67
+ raise ArgumentError, "Missing block" unless block_given?
68
+ options = options_from_hash(args)
69
+ tr(options) do
70
+ yield
71
+ end
72
+ end
73
+
74
+ def h(*args)
75
+ if block_given?
76
+ concat(tag(:th, options_from_hash(args), true))
77
+ yield
78
+ concat('</th>')
79
+ else
80
+ content = args.shift
81
+ content_tag(:th, content, options_from_hash(args))
82
+ end
83
+ end
84
+
85
+ def d(*args)
86
+ if block_given?
87
+ concat(tag(:td, options_from_hash(args), true))
88
+ yield
89
+ concat('</td>')
90
+ else
91
+ content = args.shift
92
+ content_tag(:td, content, options_from_hash(args))
93
+ end
94
+ end
95
+
96
+ private
97
+
98
+ def options_from_hash(args)
99
+ args.last.is_a?(Hash) ? args.pop : {}
100
+ end
101
+
102
+ def concat(tag)
103
+ @template.safe_concat(tag)
104
+ ""
105
+ end
106
+
107
+ def content_tag(tag, content, *args)
108
+ options = options_from_hash(args)
109
+ @template.content_tag(tag, content, options)
110
+ end
111
+
112
+ def tbody
113
+ concat('<tbody>')
114
+ yield
115
+ concat('</tbody>')
116
+ end
117
+
118
+ def tr options
119
+ concat(tag(:tr, options, true))
120
+ yield
121
+ concat('</tr>')
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,3 @@
1
+ module Ical
2
+ VERSION = "0.0.12"
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ical
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.12
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ws70 team
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Modernization watu_table_builder with iCal calendar style for Rails App
47
+ email:
48
+ - railscode@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - ical.gemspec
58
+ - lib/ical.rb
59
+ - lib/ical/calendar_helper.rb
60
+ - lib/ical/table_builder.rb
61
+ - lib/ical/version.rb
62
+ homepage: https://github.com/ws70/ical
63
+ licenses:
64
+ - MIT
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.23
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Modernization watu_table_builder with iCal calendar style for Rails App
87
+ test_files: []