micalender 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 88cbadbf2982860f10b64e81633aa89c1bbec4e7
4
- data.tar.gz: b46bbe0ec0b7500b89ae2dc8d3457910ec1a697e
3
+ metadata.gz: e3428558f7c85c8f265c55ac41f77508b88f1ae3
4
+ data.tar.gz: e7329bca577e0407f0cd144f632428d08336f02e
5
5
  SHA512:
6
- metadata.gz: b116e4c9ce55412b60d7d5d5ddc058bbcc0e10c209a4c150fcdb963e17c285f310c59291c647de7be4c9758f24701e049f924716051cbb0f8a00426bb61b034d
7
- data.tar.gz: d6bb41bb59cb0774cdbf66f49e0b5c66a268173a6c685d7e2e4c44b9afab6a567412996d03db0c3007c903822d03428220cdb1ddb4c12325c73c2c73bc332f75
6
+ metadata.gz: c21d39bd7d18a6943d5270b5d8c1748504fdf5c276deb45cca3a71c2854dbe255157f693f40c1a228f21e5820e3d0b231793e9883d9b5a4fb370ab1e51488279
7
+ data.tar.gz: 0acca9dda4da859b070eceaef61bfdea0ad7762970baecaee2c5b23c8cab497e5a83af806cc62828995ff3c10c011c7b2a659c0c88df18ffae22db820d47d880
@@ -0,0 +1,78 @@
1
+ .micalendar {
2
+ table {
3
+ -webkit-border-horizontal-spacing: 0px;
4
+ -webkit-border-vertical-spacing: 0px;
5
+ background-color: rgba(0, 0, 0, 0);
6
+ border: 1px solid rgb(221, 221, 221);
7
+ border-collapse: collapse;
8
+ box-sizing: border-box;
9
+ max-width: 100%;
10
+ width: 100%;
11
+ }
12
+
13
+ tr {
14
+ border-collapse: collapse;
15
+ }
16
+
17
+ th {
18
+ padding: 6px;
19
+ border-bottom: 2px solid rgb(221, 221, 221);
20
+ border-collapse: collapse;
21
+ border-left: 1px solid rgb(221, 221, 221);
22
+ border-right: 1px solid rgb(221, 221, 221);
23
+ border-top: 0px none rgb(51, 51, 51);
24
+ box-sizing: border-box;
25
+ text-align: left;
26
+ }
27
+
28
+ td {
29
+ padding: 6px;
30
+ vertical-align: top;
31
+ width: 14%;
32
+
33
+ border: 1px solid #ddd;
34
+ border-top-color: rgb(221, 221, 221);
35
+ border-top-style: solid;
36
+ border-top-width: 1px;
37
+ border-right-color: rgb(221, 221, 221);
38
+ border-right-style: solid;
39
+ border-right-width: 1px;
40
+ border-bottom-color: rgb(221, 221, 221);
41
+ border-bottom-style: solid;
42
+ border-bottom-width: 1px;
43
+ border-left-color: rgb(221, 221, 221);
44
+ border-left-style: solid;
45
+ border-left-width: 1px;
46
+ }
47
+
48
+ .day {
49
+ height: 80px;
50
+ }
51
+
52
+ .wday-0 {}
53
+ .wday-1 {}
54
+ .wday-2 {}
55
+ .wday-3 {}
56
+ .wday-4 {}
57
+ .wday-5 {}
58
+ .wday-6 {}
59
+
60
+ .today {
61
+ background: #FFFFC0
62
+ }
63
+
64
+ .past {}
65
+ .future {}
66
+
67
+ .start-date {}
68
+
69
+ .prev-month {
70
+ background: #DDD;
71
+ }
72
+ .next-month {
73
+ background: #DDD;
74
+ }
75
+ .current-month {}
76
+
77
+ .has-events {}
78
+ }
@@ -0,0 +1,33 @@
1
+ <div class="micalendar">
2
+ <div class="calendar-heading">
3
+ <%= link_to t('micalendar.previous', default: 'Previous'), calendar.url_for_previous_view %>
4
+ <span class="calendar-title"><%= t('date.month_names')[start_date.month] %> <%= start_date.year %></span>
5
+ <%= link_to t('micalendar.next', default: 'Next'), calendar.url_for_next_view %>
6
+ </div>
7
+
8
+ <table class="table table-striped">
9
+ <thead>
10
+ <tr>
11
+ <% date_range.slice(0, 7).each do |day| %>
12
+ <th><%= t('date.abbr_day_names')[day.wday] %></th>
13
+ <% end %>
14
+ </tr>
15
+ </thead>
16
+
17
+ <tbody>
18
+ <% date_range.each_slice(7) do |week| %>
19
+ <%= content_tag :tr, class: calendar.tr_classes_for(week) do %>
20
+ <% week.each do |day| %>
21
+ <%= content_tag :td, class: calendar.td_classes_for(day) do %>
22
+ <% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
23
+ <% capture_haml(day, sorted_events.fetch(day, []), &block) %>
24
+ <% else %>
25
+ <% block.call day, sorted_events.fetch(day, []) %>
26
+ <% end %>
27
+ <% end %>
28
+ <% end %>
29
+ <% end %>
30
+ <% end %>
31
+ </tbody>
32
+ </table>
33
+ </div>
@@ -0,0 +1,33 @@
1
+ <div class="micalendar">
2
+ <div class="calendar-heading">
3
+ <%= link_to t('micalendar.previous', default: 'Previous'), calendar.url_for_previous_view %>
4
+ <span class="calendar-title"><%= t('date.month_names')[start_date.month] %> <%= start_date.year %></span>
5
+ <%= link_to t('micalendar.next', default: 'Next'), calendar.url_for_next_view %>
6
+ </div>
7
+
8
+ <table class="table table-striped">
9
+ <thead>
10
+ <tr>
11
+ <% date_range.slice(0, 7).each do |day| %>
12
+ <th><%= t('date.abbr_day_names')[day.wday] %></th>
13
+ <% end %>
14
+ </tr>
15
+ </thead>
16
+
17
+ <tbody>
18
+ <% date_range.each_slice(7) do |week| %>
19
+ <tr>
20
+ <% week.each do |day| %>
21
+ <%= content_tag :td, class: calendar.td_classes_for(day) do %>
22
+ <% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
23
+ <% capture_haml(day, sorted_events.fetch(day, []), &block) %>
24
+ <% else %>
25
+ <% block.call day, sorted_events.fetch(day, []) %>
26
+ <% end %>
27
+ <% end %>
28
+ <% end %>
29
+ </tr>
30
+ <% end %>
31
+ </tbody>
32
+ </table>
33
+ </div>
@@ -0,0 +1,37 @@
1
+ <div class="micalendar">
2
+ <div class="calendar-heading">
3
+ <%= link_to t('micalendar.previous', default: 'Previous'), calendar.url_for_previous_view %>
4
+ <% if calendar.number_of_weeks == 1 %>
5
+ <span class="calendar-title">Week <%= calendar.week_number %></span>
6
+ <%else%>
7
+ <span class="calendar-title">Week <%= calendar.week_number %> - <%= calendar.end_week %></span>
8
+ <%end%>
9
+ <%= link_to t('micalendar.next', default: 'Next'), calendar.url_for_next_view %>
10
+ </div>
11
+
12
+ <table class="table table-striped">
13
+ <thead>
14
+ <tr>
15
+ <% date_range.slice(0, 7).each do |day| %>
16
+ <th><%= t('date.abbr_day_names')[day.wday] %></th>
17
+ <% end %>
18
+ </tr>
19
+ </thead>
20
+
21
+ <tbody>
22
+ <% date_range.each_slice(7) do |week| %>
23
+ <tr>
24
+ <% week.each do |day| %>
25
+ <%= content_tag :td, class: calendar.td_classes_for(day) do %>
26
+ <% if defined?(Haml) && respond_to?(:block_is_haml?) && block_is_haml?(block) %>
27
+ <% capture_haml(day, sorted_events.fetch(day, []), &block) %>
28
+ <% else %>
29
+ <% block.call day, sorted_events.fetch(day, []) %>
30
+ <% end %>
31
+ <% end %>
32
+ <% end %>
33
+ </tr>
34
+ <% end %>
35
+ </tbody>
36
+ </table>
37
+ </div>
@@ -0,0 +1,13 @@
1
+ require 'rails/generators'
2
+
3
+ module Micalender
4
+ module Generators
5
+ class ViewsGenerator < Rails::Generators::Base
6
+ source_root File.expand_path("../../../..", __FILE__)
7
+
8
+ def copy_views
9
+ directory 'app/views/micalender', 'app/views/micalender'
10
+ end
11
+ end
12
+ end
13
+ end
data/lib/micalender.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require "micalender/version"
2
2
  require_relative "./micalender/view_helpers"
3
3
  require_relative "./micalender/railtie"
4
+ require_relative "./micalender/calendar"
5
+ require_relative "./micalender/month_calendar"
6
+ require_relative "./micalender/week_calendar"
4
7
 
5
8
  module Micalender
6
9
  class Engine < Rails::Engine
@@ -0,0 +1,127 @@
1
+ require 'rails'
2
+
3
+ module Micalender
4
+ class Calendar
5
+ PARAM_KEY_BLACKLIST = :authenticity_token, :commit, :utf8, :_method, :script_name
6
+
7
+ attr_accessor :view_context, :options
8
+
9
+ def initialize(view_context, opts={})
10
+ @view_context = view_context
11
+ @options = opts
12
+
13
+ # Next and previous view links should use the same params as the current view
14
+ @params = @view_context.respond_to?(:params) ? @view_context.params : Hash.new
15
+ @params = @params.to_unsafe_h if @params.respond_to?(:to_unsafe_h)
16
+ @params = @params.with_indifferent_access.except(*PARAM_KEY_BLACKLIST)
17
+
18
+ # Add in any additonal params the user passed in
19
+ @params.merge!(@options.fetch(:params, {}))
20
+ end
21
+
22
+ def render(&block)
23
+ view_context.render(
24
+ partial: partial_name,
25
+ locals: {
26
+ block: block,
27
+ calendar: self,
28
+ date_range: date_range,
29
+ start_date: start_date,
30
+ sorted_events: sorted_events
31
+ }
32
+ )
33
+ end
34
+
35
+ def td_classes_for(day)
36
+ today = Date.current
37
+
38
+ td_class = ['day']
39
+ td_class << "wday-#{day.wday.to_s}"
40
+ td_class << 'today' if today == day
41
+ td_class << 'past' if today > day
42
+ td_class << 'future' if today < day
43
+ td_class << 'start-date' if day.to_date == start_date.to_date
44
+ td_class << 'prev-month' if start_date.month != day.month && day < start_date
45
+ td_class << 'next-month' if start_date.month != day.month && day > start_date
46
+ td_class << 'current-month' if start_date.month == day.month
47
+ td_class << 'has-events' if sorted_events.fetch(day, []).any?
48
+
49
+ td_class
50
+ end
51
+
52
+ def tr_classes_for(week)
53
+ today = Date.current
54
+ tr_class = ['week']
55
+ tr_class << 'current-week' if week.include?(today)
56
+
57
+ tr_class
58
+ end
59
+
60
+ def url_for_next_view
61
+ view_context.url_for(@params.merge(start_date_param => date_range.last + 1.day))
62
+ end
63
+
64
+ def url_for_previous_view
65
+ view_context.url_for(@params.merge(start_date_param => date_range.first - 1.day))
66
+ end
67
+
68
+ def date_range
69
+ (start_date..(start_date + additional_days.days)).to_a
70
+ end
71
+
72
+ private
73
+
74
+ def partial_name
75
+ @options[:partial] || self.class.name.underscore
76
+ end
77
+
78
+ def attribute
79
+ options.fetch(:attribute, :start_time).to_sym
80
+ end
81
+
82
+ def end_attribute
83
+ options.fetch(:end_attribute, :end_time).to_sym
84
+ end
85
+
86
+ def start_date_param
87
+ options.fetch(:start_date_param, :start_date).to_sym
88
+ end
89
+
90
+ def sorted_events
91
+ @sorted_events ||= begin
92
+ events = options.fetch(:events, []).reject { |e| e.send(attribute).nil? }.sort_by(&attribute)
93
+ group_events_by_date(events)
94
+ end
95
+ end
96
+
97
+ def group_events_by_date(events)
98
+ events_grouped_by_date = Hash.new {|h,k| h[k] = [] }
99
+
100
+ events.each do |event|
101
+ event_start_date = event.send(attribute).to_date
102
+ event_end_date = (event.respond_to?(end_attribute) && !event.send(end_attribute).nil?) ? event.send(end_attribute).to_date : event_start_date
103
+ (event_start_date..event_end_date.to_date).each do |enumerated_date|
104
+ events_grouped_by_date[enumerated_date] << event
105
+ end
106
+ end
107
+
108
+ events_grouped_by_date
109
+ end
110
+
111
+ def start_date
112
+ if options.has_key?(:start_date)
113
+ options.fetch(:start_date).to_date
114
+ else
115
+ view_context.params.fetch(start_date_param, Date.current).to_date
116
+ end
117
+ end
118
+
119
+ def end_date
120
+ date_range.last
121
+ end
122
+
123
+ def additional_days
124
+ options.fetch(:number_of_days, 4) - 1
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,7 @@
1
+ module Micalender
2
+ class MonthCalendar < Micalender::Calendar
3
+ def date_range
4
+ (start_date.beginning_of_month.beginning_of_week..start_date.end_of_month.end_of_week).to_a
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,3 @@
1
1
  module Micalender
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,150 +1,18 @@
1
1
  module Micalender
2
2
  module ViewHelpers
3
- def month_calendar(param_name=:start_date, options={}, &block)
4
- start_date = (params[param_name] || Time.zone.now).to_date
3
+ def calendar(options={}, &block)
4
+ raise 'calendar requires a block' unless block_given?
5
+ Micalender::Calendar.new(self, options).render(&block)
6
+ end
5
7
 
6
- options.reverse_merge!(
7
- td: day_classes,
8
- prev_link: ->(range) { link_to raw("&laquo;"), param_name => range.first - 1.day },
9
- header: month_header,
10
- next_link: ->(range) { link_to raw("&raquo;"), param_name => range.last + 1.day },
11
- start_date: start_date,
12
- )
13
- render_calendar month_range(start_date), options, &block
8
+ def month_calendar(options={}, &block)
9
+ raise 'month_calendar requires a block' unless block_given?
10
+ Micalender::MonthCalendar.new(self, options).render(&block)
14
11
  end
15
12
 
16
- def week_calendar(param_name=:start_date, options={}, &block)
17
- start_date = (params[param_name] || Time.zone.now).to_date
18
- number_of_weeks = options.fetch(:number_of_weeks, 1)
19
-
20
- options.reverse_merge!(
21
- td: day_classes,
22
- prev_link: ->(range) { link_to raw("&laquo;"), param_name => range.first - 1.day },
23
- header: false,
24
- next_link: ->(range) { link_to raw("&raquo;"), param_name => range.last + 1.day },
25
- start_date: start_date,
26
- )
27
- render_calendar week_range(start_date, number_of_weeks), options, &block
28
- end
29
-
30
- def calendar(param_name=:start_date, options={}, &block)
31
- start_date = (params[param_name] || Time.zone.now).to_date
32
- number_of_days_to_advance = options.fetch(:number_of_days, 4) - 1
33
-
34
- options.reverse_merge!(
35
- td: day_classes,
36
- prev_link: ->(range) { link_to raw("&laquo;"), param_name => range.first - 1.day },
37
- header: false,
38
- next_link: ->(range) { link_to raw("&raquo;"), param_name => range.last + 1.day },
39
- start_date: start_date,
40
- )
41
- render_calendar calendar_range(start_date, number_of_days_to_advance), options, &block
42
- end
43
-
44
- def month_range(start_date)
45
- start_date.beginning_of_month.beginning_of_week.to_date..start_date.end_of_month.end_of_week.to_date
46
- end
47
-
48
- def week_range(start_date, number_of_weeks)
49
- number_of_days_to_advance = (number_of_weeks * 7) - 1
50
- starting_day = start_date.beginning_of_week.to_date
51
- ending_day = starting_day + number_of_days_to_advance.days
52
- starting_day..ending_day
53
- end
54
-
55
- def calendar_range(start_date, number_of_days_to_advance)
56
- start_date..(start_date + number_of_days_to_advance.days)
57
- end
58
-
59
- def render_calendar(range, options, &block)
60
- raise 'SimpleCalendar requires a block' unless block_given?
61
-
62
- @block = block
63
- @options = options.reverse_merge default_options
64
- @start_date = options.fetch(:start_date)
65
-
66
- capture do
67
- concat render_header(@start_date, range)
68
- concat render_table(range)
69
- end
70
- end
71
-
72
- def render_header(start_date, range)
73
- capture do
74
- content_tag :div do
75
- concat get_options(@options[:prev_link], range)
76
- concat get_options(@options[:header], start_date)
77
- concat get_options(@options[:next_link], range)
78
- end
79
- end
80
- end
81
-
82
- def render_table(range)
83
- content_tag(:table, get_options(@options[:table])) do
84
- content_tag(:tbody) do
85
- render_weeks(range)
86
- end
87
- end
88
- end
89
-
90
-
91
- def render_weeks(range)
92
- weeks = []
93
- range.each_slice(7) do |week|
94
- weeks << content_tag(:tr, get_options(@options[:tr], week)) do
95
- render_week(week)
96
- end
97
- end
98
- safe_join weeks
99
- end
100
-
101
- def render_week(week)
102
- results = week.map do |day|
103
- content_tag :td, get_options(@options[:td], @start_date, day) do
104
- @block.call(day)
105
- end
106
- end
107
- safe_join results
108
- end
109
-
110
- def get_options(options, *params)
111
- case options
112
- when Hash
113
- options
114
- when String
115
- send(options, *params)
116
- when Symbol
117
- send(options, *params)
118
- else
119
- options.call(*params) if options.respond_to? :call
120
- end
121
- end
122
-
123
- def default_options
124
- { table: {}, tr: {}, td: {}, }
125
- end
126
-
127
- def day_classes
128
- ->(start_date, current_calendar_date) {
129
- today = Time.zone.now.to_date
130
- td_class = ["day"]
131
-
132
- td_class << "today" if today == current_calendar_date
133
- td_class << "past" if today > current_calendar_date
134
- td_class << "future" if today < current_calendar_date
135
- td_class << "prev-month" if start_date.month != current_calendar_date.month && current_calendar_date < start_date
136
- td_class << "next-month" if start_date.month != current_calendar_date.month && current_calendar_date > start_date
137
- td_class << "current-month" if start_date.month == current_calendar_date.month
138
- td_class << "wday-#{current_calendar_date.wday.to_s}"
139
-
140
- { class: td_class.join(" ") }
141
- }
142
- end
143
-
144
- def month_header
145
- ->(start_date) {
146
- content_tag :span, "#{I18n.t("date.month_names")[start_date.month]} #{start_date.year}", class: "calendar-header"
147
- }
13
+ def week_calendar(options={}, &block)
14
+ raise 'week_calendar requires a block' unless block_given?
15
+ Micalender::WeekCalendar.new(self, options).render(&block)
148
16
  end
149
17
  end
150
18
  end
@@ -0,0 +1,23 @@
1
+ module Micalender
2
+ class WeekCalendar < Micalender::Calendar
3
+ def week_number
4
+ format = (Date.beginning_of_week == :sunday) ? "%U" : "%W"
5
+ start_date.beginning_of_week.strftime(format).to_i
6
+ end
7
+
8
+ def number_of_weeks
9
+ options.fetch(:number_of_weeks, 1)
10
+ end
11
+
12
+ def end_week
13
+ week_number + number_of_weeks - 1
14
+ end
15
+
16
+ def date_range
17
+ starting = start_date.beginning_of_week
18
+ ending = (starting + (number_of_weeks - 1).weeks).end_of_week
19
+
20
+ (starting..ending).to_a
21
+ end
22
+ end
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: micalender
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mukesh Kumar
@@ -73,10 +73,18 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - app/assets/stylesheets/micalender.scss
77
+ - app/views/micalender/_calendar.html.erb
78
+ - app/views/micalender/_month_calendar.html.erb
79
+ - app/views/micalender/_week_calendar.html.erb
80
+ - lib/generators/micalender/views_generator.rb
76
81
  - lib/micalender.rb
82
+ - lib/micalender/calendar.rb
83
+ - lib/micalender/month_calendar.rb
77
84
  - lib/micalender/railtie.rb
78
85
  - lib/micalender/version.rb
79
86
  - lib/micalender/view_helpers.rb
87
+ - lib/micalender/week_calendar.rb
80
88
  homepage: https://github.com/karanraj9042/holiday_calender
81
89
  licenses:
82
90
  - MIT