taq_simple_calendar 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 623a68c4b42abdaabf2334a76485658376383139
4
+ data.tar.gz: 605cb20386851b7ffc753ad477298d4c6105fede
5
+ SHA512:
6
+ metadata.gz: 0dd656744224b291e8639b08c15e0913be1b600d20012eb7eb9f33b20f5fa7f0c82af944a519063802574da434b65746e18face1d9d5c759805fc54247477c13
7
+ data.tar.gz: 88e998e76a41e650f34fa7cc61defa284a4e4aa089694111ab4110c6925c83272b405e2a646f669af1ecd28ae57c16586519bd33862e531492c7c4d311743529
@@ -0,0 +1,2 @@
1
+ .*.swp
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in taq_simple_calendar.gemspec
4
+ gemspec
@@ -0,0 +1,7 @@
1
+ # Simple Calendar
2
+
3
+ Just a gem used to create calendars from events of a certain app I use.
4
+ It includes some methods on Array and ActiveRelation to 'calendarize' events.
5
+ As it is used for a specific app here (and we need to keep the gem on the
6
+ rubygems repositories to install it) it is not made to a more global scope
7
+ and use.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,13 @@
1
+ require "#{File.expand_path(File.dirname(__FILE__))}/taq_simple_calendar/railties.rb" if defined?(Rails)
2
+ require "#{File.expand_path(File.dirname(__FILE__))}/taq_simple_calendar/record.rb"
3
+ require "#{File.expand_path(File.dirname(__FILE__))}/taq_simple_calendar/view_helper.rb"
4
+
5
+ class Array
6
+ include TaQSimpleCalendar::Record
7
+ end
8
+
9
+ if defined?(ActiveRecord::Relation)
10
+ class ActiveRecord::Relation
11
+ include TaQSimpleCalendar::Record
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ module Test
2
+ class Railtie < Rails::Railtie
3
+ initializer "taq_simple_calendar.helpers" do
4
+ ActionView::Base.send(:include,TaQSimpleCalendar::ViewHelper) if defined?(Rails)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,26 @@
1
+ module TaQSimpleCalendar
2
+ module Record
3
+ def calendarize
4
+ collection = self.sort_by {|c| c.start_at}
5
+ months = collection.inject({}) do |memo,col|
6
+ %w(start_at end_at).each do |date|
7
+ key, name = col.send(date).strftime("%Y-%m %B").split
8
+ if !memo.key?(key)
9
+ memo[key] = {}
10
+ memo[key][:name] = name
11
+ memo[key][:collection] = collection.select do |c|
12
+ c.start_at.strftime("%Y-%m") == key ||
13
+ c.end_at.strftime("%Y-%m") == key
14
+ end.map do |c|
15
+ {:day => c.send(:start_at).strftime("%02d"),
16
+ :title => c.send(:title),
17
+ :bin_period => c.send(:bin_period)
18
+ }
19
+ end
20
+ end
21
+ end
22
+ memo
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module TaQSimpleCalendar
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,80 @@
1
+ module TaQSimpleCalendar
2
+ module ViewHelper
3
+ def calendarize(calendar,options={})
4
+ str = ""
5
+ attrib = options[:attribute] ? options[:attribute] : "title"
6
+ url = options[:url] ? options[:url] : "#"
7
+ sep = options[:separator] ? options[:separator] : " "
8
+
9
+ calendar.each do |key,val|
10
+ date = Date.parse("#{key}-01")
11
+ pmon = date.beginning_of_month-1.day
12
+ nmon = date.end_of_month+1.day
13
+ pmld = pmon.end_of_month
14
+ cmfd = date.beginning_of_month
15
+ cmld = date.end_of_month
16
+ daysb = Array.new
17
+
18
+ (cmfd.wday-1).downto(0) {|d| daysb << (pmld-d.days)}
19
+ 0.upto(cmld.day-1) {|d| daysb << (cmfd+d.days)}
20
+
21
+ (7-(daysb.size%7)).times do |nxt|
22
+ daysb << nmon+nxt.days
23
+ end
24
+ day_groups = daysb.in_groups_of(7)
25
+ month = I18n.t("date.month_names")[Date::MONTHNAMES.index(val[:name])]
26
+
27
+ str += "<table class='calendar'><caption>#{month}</caption>"
28
+ str += "<thead><tr>"
29
+ dnam = I18n.t("date.abbr_day_names")
30
+
31
+ 7.times do |day|
32
+ str << "<th>#{dnam[day]}</th>"
33
+ end
34
+ str << "</tr></thead>"
35
+
36
+ str << "<tbody>"
37
+ for day_group in day_groups
38
+ str << "<tr>"
39
+ for day in day_group
40
+ cls = []
41
+ if day
42
+ day_s = day.strftime("%02d")
43
+ evt_s = ""
44
+ url_s = (url % "#{key}-#{day_s}") if url!="#"
45
+
46
+ cls << "calendar_other_month" if day.month!=date.month
47
+ cls << "calendar_weekend" if [0,6].include?(day.wday)
48
+
49
+ if day.month==date.month
50
+ evts = val[:collection].select {|v| v[:day]==day_s}
51
+ evt_s = evts.map {|e| e[:title]}.join(sep)
52
+ if evts.size>0
53
+ cls << "calendar_event_#{evts.size}"
54
+ if evts.first.key?(:bin_period)
55
+ cls << "bp" + evts.map {|e| e[:bin_period]}.inject("000") do |m,v|
56
+ (m.to_i(2) | v.to_i(2)).to_s(2)
57
+ end.rjust(3,"0")
58
+ end
59
+ end
60
+ end
61
+
62
+ str << "<td class=\"#{cls.join(' ')}\">"
63
+ if evt_s.strip.size>0
64
+ str << "<a href='#{url_s}' #{attrib}='#{evt_s}'>#{day_s}</a>"
65
+ else
66
+ str << day_s
67
+ end
68
+ str << "</td>"
69
+ else
70
+ str << "<td></td>"
71
+ end
72
+ end
73
+ str << "</tr>"
74
+ end
75
+ str << "</tbody></table>"
76
+ end
77
+ str.html_safe
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "taq_simple_calendar/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "taq_simple_calendar"
7
+ s.version = TaQSimpleCalendar::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Eustaquio Rangel"]
10
+ s.email = ["eustaquiorangel@gmail.com"]
11
+ s.homepage = "http://github.com/taq/taq_simple_calendar"
12
+ s.summary = %q{Simple calendar gem for Rails}
13
+ s.description = %q{Simple calendar gem for Rails}
14
+
15
+ s.rubyforge_project = "taq_simple_calendar"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,18 @@
1
+ require "date"
2
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/taq_simple_calendar.rb"
3
+
4
+ class Event
5
+ attr_reader :title, :url, :start_at, :end_at, :bin_period
6
+
7
+ def initialize(title,start_at,end_at,bin_period,url=nil)
8
+ @title, @start_at, @end_at, @bin_period, @url = title, start_at, end_at, bin_period, url
9
+ end
10
+
11
+ def self.all
12
+ @events = [
13
+ Event.new("This is event 1",DateTime.parse("2011-01-30 09:00"),DateTime.parse("2011-01-30 10:00"),"100","/events/1"),
14
+ Event.new("This is event 2",DateTime.parse("2011-01-31 13:00"),DateTime.parse("2011-01-31 14:00"),"010","/events/2"),
15
+ Event.new("This is event 3",DateTime.parse("2011-02-01 20:00"),DateTime.parse("2011-02-01 21:00"),"001","/events/3")
16
+ ]
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ require "test/unit"
2
+ require "#{File.expand_path(File.dirname(__FILE__))}/../lib/taq_simple_calendar.rb"
3
+ require "#{File.expand_path(File.dirname(__FILE__))}/event.rb"
4
+
5
+ class EventTest < Test::Unit::TestCase
6
+ def setup
7
+ @events = Event.all.calendarize
8
+ end
9
+
10
+ def test_months
11
+ keys = @events.keys
12
+ assert_equal keys.size, 2
13
+ assert_equal keys.first, "2011-01"
14
+ assert_equal keys.last , "2011-02"
15
+ end
16
+
17
+ def test_january_name
18
+ assert_equal @events["2011-01"][:name], "January"
19
+ end
20
+
21
+ def test_january_days
22
+ days = @events["2011-01"][:collection].map {|day| day[:day]}
23
+ assert_equal days, %w(30 31)
24
+ end
25
+
26
+ def test_february_name
27
+ assert_equal @events["2011-02"][:name], "February"
28
+ end
29
+
30
+ def test_february_days
31
+ days = @events["2011-02"][:collection].map {|day| day[:day]}
32
+ assert_equal days, %w(01)
33
+ end
34
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: taq_simple_calendar
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eustaquio Rangel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Simple calendar gem for Rails
14
+ email:
15
+ - eustaquiorangel@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - README.md
23
+ - Rakefile
24
+ - lib/taq_simple_calendar.rb
25
+ - lib/taq_simple_calendar/railties.rb
26
+ - lib/taq_simple_calendar/record.rb
27
+ - lib/taq_simple_calendar/version.rb
28
+ - lib/taq_simple_calendar/view_helper.rb
29
+ - taq_simple_calendar.gemspec
30
+ - test/event.rb
31
+ - test/test_events.rb
32
+ homepage: http://github.com/taq/taq_simple_calendar
33
+ licenses: []
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project: taq_simple_calendar
51
+ rubygems_version: 2.2.2
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Simple calendar gem for Rails
55
+ test_files: []