simple_calendar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in simple_calendar.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ Simple Calendar
2
+ ===============
3
+
4
+ This is a small gem for creating a quick and clean table calendar.
5
+ Theming is up to you, but it works nicely with Twitter Bootstrap.
6
+
7
+ Installation
8
+ ------------
9
+
10
+ Just add this into your Gemfile followed by a bundle install:
11
+
12
+ gem "simple_calendar", "~> 0.0.1"
13
+
14
+ Usage
15
+ -----
16
+
17
+ Here we have a model called Event with the start_time attribute that we
18
+ will be using with simple_calendar.
19
+
20
+ class Event < ActiveRecord::Base
21
+ has_calendar
22
+ end
23
+
24
+ We query the events we want to display as usual, and then render the
25
+ calendar in the view like so:
26
+
27
+ <%= calendar @events %>
28
+
29
+ has_calendar has options that can be passed to it for configuration:
30
+
31
+ has_calendar :title => :whatever, :start_time => :my_start_column
32
+
33
+ def whatever
34
+ title + " ohai"
35
+ end
36
+
37
+ Start_time is the field for the start time of the event. Title is the
38
+ text that will be displayed for the event on the calendar.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ require "simple_calendar/version"
2
+ require "simple_calendar/view_helpers"
3
+ require "simple_calendar/model_additions"
4
+ require "simple_calendar/railtie" if defined?(Rails)
@@ -0,0 +1,19 @@
1
+ module SimpleCalendar
2
+ module ModelAdditions
3
+ def has_calendar(options={})
4
+ config = { :title => "title", :start_time => "start_time"}
5
+ config.update(options) if options.is_a?(Hash)
6
+
7
+ class_eval <<-EOV
8
+ def title_column
9
+ #{config[:title]}
10
+ end
11
+
12
+ def start_time_column
13
+ #{config[:start_time]}
14
+ end
15
+ EOV
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,12 @@
1
+ module SimpleCalendar
2
+ class Railtie < Rails::Railtie
3
+ initializer "simple_calendar.view_helpers" do
4
+ ActionView::Base.send :include, ViewHelpers
5
+ end
6
+ initializer "simple_calendar.model_additions" do
7
+ ActiveSupport.on_load :active_record do
8
+ extend ModelAdditions
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleCalendar
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,77 @@
1
+ module SimpleCalendar
2
+ module ViewHelpers
3
+ def calendar(events)
4
+ day = Date.civil((params[:year] || Time.zone.now.year).to_i, (params[:month] || Time.zone.now.month).to_i)
5
+
6
+ content_tag :table, :class => "bordered-table calendar" do
7
+ month_header(day) + day_header + body(day, events)
8
+ end
9
+ end
10
+
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
16
+
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
22
+
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("<", calendar_path(:month => previous_month.month, :year => previous_month.year))
29
+ tags << day.strftime("%B %Y")
30
+ tags << link_to(">", calendar_path(:month => next_month.month, :year => next_month.year))
31
+ tags.join.html_safe
32
+ end
33
+ end
34
+
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
41
+ end
42
+
43
+ def body(day, events)
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)
53
+ current_date = current_date.tomorrow
54
+ end
55
+ tags << day(current_date, events)
56
+ current_date = current_date.tomorrow
57
+ tags.join.html_safe
58
+ end
59
+ end
60
+ weeks.join.html_safe
61
+ end
62
+ end
63
+
64
+ def day(date, events)
65
+ content_tag :td do
66
+ events = day_events(date, events)
67
+ tags = [content_tag(:div, date.day, :class => "day")]
68
+ tags += events.map { |e| content_tag(:div, e.title_column) } unless events.empty?
69
+ tags.join.html_safe
70
+ end
71
+ end
72
+
73
+ def day_events(date, events)
74
+ events.select { |e| e.start_time_column.to_date == date }
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "simple_calendar/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "simple_calendar"
7
+ s.version = SimpleCalendar::VERSION
8
+ s.authors = ["Chris Oliver"]
9
+ s.email = ["excid3@gmail.com"]
10
+ s.homepage = "https://github.com/excid3/simple_calendar"
11
+ s.summary = %q{A simple Rails 3 calendar}
12
+ s.description = %q{A simple Rails 3 calendar}
13
+
14
+ s.rubyforge_project = "simple_calendar"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_calendar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Oliver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-04 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: A simple Rails 3 calendar
15
+ email:
16
+ - excid3@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - README.md
24
+ - Rakefile
25
+ - lib/simple_calendar.rb
26
+ - lib/simple_calendar/model_additions.rb
27
+ - lib/simple_calendar/railtie.rb
28
+ - lib/simple_calendar/version.rb
29
+ - lib/simple_calendar/view_helpers.rb
30
+ - simple_calendar.gemspec
31
+ homepage: https://github.com/excid3/simple_calendar
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project: simple_calendar
51
+ rubygems_version: 1.8.10
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: A simple Rails 3 calendar
55
+ test_files: []