ecm_calendar_helper 0.0.3 → 1.0.0

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.
Files changed (25) hide show
  1. checksums.yaml +4 -4
  2. data/README.rdoc +53 -1
  3. data/app/assets/javascripts/ecm/calendar_helper/application/remote_calendar.js.coffee +4 -0
  4. data/app/assets/javascripts/ecm/calendar_helper/application.js +2 -0
  5. data/app/assets/javascripts/ecm_calendar_helper.js +1 -0
  6. data/app/assets/stylesheets/ecm/calendar_helper/application/basic.css +7 -1
  7. data/app/concerns/ecm/calendar_helper/controller/calendar_concern.rb +92 -0
  8. data/app/helpers/ecm/calendar_helper.rb +23 -4
  9. data/app/views/ecm/calendar_helper/_month_calendar.haml +13 -5
  10. data/app/views/ecm/calendar_helper/_month_calendar_pagination.haml +4 -4
  11. data/app/views/ecm/calendar_helper/calendar_concern/_calendar.html.haml +2 -0
  12. data/app/views/ecm/calendar_helper/calendar_concern/index.html.haml +1 -0
  13. data/app/views/ecm/calendar_helper/calendar_concern/index.js.erb +1 -0
  14. data/config/initializers/assets.rb +1 -1
  15. data/config/locales/de.yml +3 -0
  16. data/config/locales/en.yml +3 -0
  17. data/lib/ecm/calendar_helper/version.rb +1 -1
  18. data/lib/generators/ecm/calendar_helper/controller/controller_generator.rb +49 -0
  19. data/lib/generators/ecm/calendar_helper/controller/templates/controller.rb +18 -0
  20. data/lib/generators/ecm/calendar_helper/controller/templates/initializer.rb +42 -0
  21. data/lib/generators/ecm/calendar_helper/controller/templates/routes.source +5 -0
  22. data/lib/generators/ecm/calendar_helper/controller/templates/views/_calendar.html.haml +2 -0
  23. data/lib/generators/ecm/calendar_helper/controller/templates/views/index.html.haml +1 -0
  24. data/lib/generators/ecm/calendar_helper/controller/templates/views/index.js.erb +1 -0
  25. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3e5f21214725193d52d1c15380ed1c03c04b8e6
4
- data.tar.gz: 15278f09109d889796d8c3f91eddf1628230c0b9
3
+ metadata.gz: 8464ada0852fc7dd950d6325a45f6af8f32feb7a
4
+ data.tar.gz: cbebb41ffb1b5fa22ce730a1b2bafb47bf6c5e4a
5
5
  SHA512:
6
- metadata.gz: 8a05e3299d4059ab13376b008a7b8f1745cb7406c541ef4ceec4f4012c3eec063a22e90a07f2844ab90d82329bed3e645df8bd239a84346bc9c6a39f0f7fe659
7
- data.tar.gz: ac222cd53b256fea99960dc1562aa81e9b1797824dacafdeb7c695474aeae3da6b60e4c4cd0a1bbd5ee7bbcc286ec8dbd38d565d9d3bcffa5630a8f1972fea5e
6
+ metadata.gz: f5ab3329ee346a7873fc30d317a4c41a44895bd70b3448553e0edf787d7deed668b02b1dd74b9c3017ac34fc6bcf6463b3626c13ec162c6b632c7bc881e0f1d9
7
+ data.tar.gz: 69ea1e0f58d5ad611170f90fd6358bf4cd902f4ddaef3ee9ad4f2934e527c9ebf35760f67c3c9e38ba3c08ec7ee5f0d6c62870bd81c18f32e6c249d23a952baf
data/README.rdoc CHANGED
@@ -1,3 +1,55 @@
1
1
  = EcmCalendarHelper
2
2
 
3
- This project rocks and uses MIT-LICENSE.
3
+ This project rocks and uses MIT-LICENSE.
4
+
5
+ == Installation
6
+
7
+ Add it to your Gemfile.
8
+
9
+ # Gemfile
10
+ gem 'ecm_calendar_helper'
11
+
12
+ == Usage
13
+
14
+ Add the helper to your controller:
15
+
16
+ # app/controllers/application_controller.rb
17
+ class ApplicationController < ActionController::Base
18
+ helper Ecm::CalendarHelper
19
+ end
20
+
21
+ # app/controllers/reservations_controller.rb
22
+ class ApplicationController < ActionController::Base
23
+ before_action :initialize_calendar
24
+
25
+ private
26
+
27
+ def initialize_calendar
28
+ @year = params[:year] ||= Time.zone.now.year
29
+ @month = params[:month] ||= Time.zone.now.month
30
+
31
+ @date = Date.strptime("#{@month}-#{@year}", "%m-%Y")
32
+
33
+ @collection = Reservation.in_month(@date).all
34
+ end
35
+ end
36
+
37
+ Render the calendar:
38
+
39
+ # i.e.app/views/reservations/index.html.haml
40
+ = month_calendar @date, @collection, display_method: :name, start_day: :monday
41
+
42
+ == Pagination
43
+
44
+ Add routes for nice year and month params:
45
+
46
+ # config/routes.rb
47
+ Rails.application.routes.draw do
48
+ resources :reservations do
49
+ get "(/:year/:month)", action: :index, on: :collection
50
+ end
51
+ end
52
+
53
+ == Remote rendering
54
+
55
+ @TODO
@@ -0,0 +1,4 @@
1
+ $ ->
2
+ $('[data-calendar]').each ->
3
+ url = $(this).attr('data-calendar')
4
+ $(this).load(url)
@@ -0,0 +1,2 @@
1
+ //= require_self
2
+ //= require_tree ./application
@@ -0,0 +1 @@
1
+ //= require ecm/calendar_helper/application
@@ -1,9 +1,15 @@
1
1
  @media screen and (max-width: 991px) {
2
- .calendar .calendar-weekday-name {
2
+ .calendar .calendar-weekday {
3
3
  display: none;
4
4
  }
5
5
  }
6
6
 
7
+ @media screen and (min-width: 991px) {
8
+ .calendar .calendar-day-container {
9
+ height: 7vw;
10
+ }
11
+ }
12
+
7
13
  .calendar-item {
8
14
  border: 1px solid transparent;
9
15
  border-radius: 4px;
@@ -0,0 +1,92 @@
1
+ module Ecm
2
+ module CalendarHelper
3
+ module Controller
4
+ # == Usage:
5
+ #
6
+ # Add javascripts:
7
+ #
8
+ # # app/assets/javascripts/application.js
9
+ # //= require ecm_calendar_helper
10
+ #
11
+ # Add stylesheets:
12
+ #
13
+ # # app/assets/stylesheets/application.css
14
+ # /*
15
+ # *= require ecm_calendar_helper
16
+ # */
17
+ #
18
+ # Add routes:
19
+ #
20
+ # # config/routes.rb
21
+ # Rails.application.routes.draw do
22
+ # resources :calendars, only: [:index], constraints: ->(r) { r.xhr? } do
23
+ # get "(/:year/:month)", action: :index, on: :collection
24
+ # end
25
+ # end
26
+ #
27
+ # Add controller:
28
+ #
29
+ # # app/controllers/calendars_controller.rb
30
+ # class CalendarsController < ApplicationController
31
+ # include EcmCalendarHelper::Controller::CalendarConcern
32
+ #
33
+ # private
34
+ #
35
+ # def load_collection_for_calendar
36
+ # @collection = Posts.for_calendar.in_month(@date).all
37
+ # end
38
+ # end
39
+ #
40
+ # Add views:
41
+ #
42
+ # Copy Ecm::CalendarHelper::Engine.root.join('app/views/ecm/calendar_helper/calendar_concern/*') => app/views/calendars/
43
+ #
44
+ # Render the calendar somewhere in your views:
45
+ #
46
+ # # app/views/*.html.haml
47
+ # #calendar{ data: { calendar: calendars_url } }
48
+ #
49
+ #
50
+ module CalendarConcern
51
+ extend ActiveSupport::Concern
52
+
53
+ included do
54
+ before_action :initialize_calendar_date, only: [:index]
55
+ before_action :load_collection_for_calendar, only: [:index]
56
+
57
+ helper Ecm::CalendarHelper
58
+ helper_method :calendar_options
59
+ end
60
+
61
+ def index
62
+ if request.xhr?
63
+ render layout: nil
64
+ else
65
+ render
66
+ end
67
+ end
68
+
69
+ private
70
+
71
+ def initialize_calendar_date
72
+ @year = params[:year] ||= Time.zone.now.year
73
+ @month = params[:month] ||= Time.zone.now.month
74
+
75
+ @date = Date.strptime("#{@month}-#{@year}", "%m-%Y")
76
+ end
77
+
78
+ def load_collection_for_calendar
79
+ raise 'not implemented'
80
+ end
81
+
82
+ def calendar_options
83
+ default_calendar_options
84
+ end
85
+
86
+ def default_calendar_options
87
+ {}
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -1,11 +1,23 @@
1
1
  module Ecm
2
2
  module CalendarHelper
3
3
  # renders a calendar table
4
+ #
5
+ # Example with elements that span more than 1 day:
6
+ #
7
+ # = month_calendar @date, @reservations, start_date_method: :start_at, end_date_method: :end_at
8
+ #
9
+ # Example of using a lamda as display method:
10
+ #
11
+ # = month_calendar(display_method: ->(context, resource) { context.link_to(resource.booker.human, resource.booker) })
12
+ #
4
13
  def month_calendar(date = Time.zone.now.to_date, elements = [], options = {})
5
- options.reverse_merge! :date_method => :start_at, :display_method => :to_s, :link_elements => true, :start_day => :sunday
14
+ options.reverse_merge! :date_method => :start_at, :display_method => :to_s, :link_elements => true, :start_day => :sunday, start_date_method: nil, end_date_method: nil
6
15
 
7
16
  display_method = options.delete(:display_method)
8
17
  link_elements = options.delete(:link_elements)
18
+
19
+ start_date_method = options.delete(:start_date_method)
20
+ end_date_method = options.delete(:end_date_method)
9
21
 
10
22
  # calculate beginning and end of month
11
23
  beginning_of_month = date.beginning_of_month.to_date
@@ -35,7 +47,13 @@ module Ecm
35
47
  last_day = end_of_month.end_of_week
36
48
 
37
49
  days = (first_day..last_day).each_with_object({}).with_index do |(day, memo), index|
38
- memo[day.to_date] = elements.find_all { |e| e.send(options[:date_method]).to_date == day.to_date } || {}
50
+ memo[day.to_date] = elements.find_all do |e|
51
+ if start_date_method.present? && end_date_method.present?
52
+ day.to_date.between?(e.send(start_date_method), e.send(end_date_method))
53
+ else
54
+ e.send(options[:date_method]).to_date == day.to_date
55
+ end
56
+ end || {}
39
57
  end
40
58
 
41
59
  days_by_week = days.each_with_object({}) { |(k, v), m| (m[k.cweek] ||= {})[k] = v }
@@ -44,15 +62,16 @@ module Ecm
44
62
  end
45
63
 
46
64
  def month_calendar_pagination(date, options = {})
47
- options.reverse_merge!(show_today_link: true)
65
+ options.reverse_merge!(show_today_link: true, remote: false)
48
66
 
49
67
  show_today_link = options.delete(:show_today_link)
68
+ remote = options.delete(:remote)
50
69
 
51
70
  actual_month = Time.zone.now.beginning_of_month.to_date
52
71
  previous_month = date.beginning_of_month.to_date - 1.month
53
72
  next_month = date.beginning_of_month.to_date + 1.months
54
73
 
55
- render partial: 'ecm/calendar_helper/month_calendar_pagination', locals: { actual_month: actual_month, previous_month: previous_month, next_month: next_month, show_today_link: show_today_link, date: date }
74
+ render partial: 'ecm/calendar_helper/month_calendar_pagination', locals: { actual_month: actual_month, previous_month: previous_month, next_month: next_month, show_today_link: show_today_link, date: date, remote: remote }
56
75
  end
57
76
  end
58
77
  end
@@ -1,8 +1,10 @@
1
1
  .calendar
2
2
  .flex-row.calendar-weekday-names
3
3
  - localized_day_names.each do |day|
4
- .column.calendar-weekday-name
5
- %h1= day
4
+ .column
5
+ .calendar-weekday
6
+ .column.calendar-weekday-name-container.border.mb-3.p-2
7
+ %h1= day
6
8
 
7
9
  - days_by_week.each do |week, days|
8
10
  .flex-row{ class: "week-#{week}" }
@@ -13,13 +15,19 @@
13
15
  - td_classes << 'with-elements' if elements.any?
14
16
  - td_classes << 'not-actual-month' if day.month != Time.zone.now.month
15
17
  .column{ class: td_classes.join(" ") }
16
- .calendar-day-container
18
+ .calendar-day-container.border.mb-3.p-2
17
19
  %h1.calendar-day-title= day.day
18
20
  %div.calendar-entries
19
21
  - elements.each do |element|
20
22
  %div.calendar-day-content
21
23
  %span.calendar-item.calendar-item-default
24
+ - if display_method.nil?
25
+ - label = nil
26
+ - elsif display_method.respond_to?(:call)
27
+ - label = display_method.call(self, element)
28
+ - elsif element.respond_to?(display_method)
29
+ - label = element.send(display_method)
22
30
  - if link_elements
23
- = link_to(element.send(display_method), element)
31
+ = link_to(label, element)
24
32
  - else
25
- = element.send(display_method)
33
+ = label
@@ -1,11 +1,11 @@
1
- .calendar-pagination
1
+ .calendar-pagination.text-center
2
2
  - if show_today_link
3
- = link_to(url_for(month: actual_month.month, year: actual_month.year), class: 'btn btn-default calendar-pagination-today') do
3
+ = link_to(url_for(month: actual_month.month, year: actual_month.year), class: 'btn btn-default btn-link calendar-pagination-today', remote: remote) do
4
4
  = t('.today')
5
5
  .btn-group
6
- = link_to(url_for(month: previous_month.month, year: previous_month.year), class: 'btn btn-primary calendar-pagination-back') do
6
+ = link_to(url_for(month: previous_month.month, year: previous_month.year), class: 'btn btn-primary calendar-pagination-back', remote: remote) do
7
7
  = t('.previous')
8
8
  = button_tag(type: 'button', class: 'btn btn-disabled calendar-pagination-actual') do
9
9
  = I18n.l(date, format: :month_with_year)
10
- = link_to(url_for(month: next_month.month, year: next_month.year), class: 'btn btn-primary calendar-pagination-next') do
10
+ = link_to(url_for(month: next_month.month, year: next_month.year), class: 'btn btn-primary calendar-pagination-next', remote: remote) do
11
11
  = t('.next')
@@ -0,0 +1,2 @@
1
+ = month_calendar_pagination(date, remote: request.xhr?)
2
+ = month_calendar date, collection, options
@@ -0,0 +1 @@
1
+ = render partial: 'calendar', locals: { date: @date, collection: @collection, options: calendar_options }
@@ -0,0 +1 @@
1
+ $('#bookings-calendar').html("<%= escape_javascript(render(partial: 'calendar', locals: { date: @date, collection: @collection, options: calendar_options })) %>");
@@ -1 +1 @@
1
- Rails.application.config.assets.precompile += %w(ecm_calendar_helper.css)
1
+ Rails.application.config.assets.precompile += %w(ecm_calendar_helper.css ecm_calendar_helper.js)
@@ -1,4 +1,7 @@
1
1
  de:
2
+ date:
3
+ formats:
4
+ month_with_year: '%B %Y'
2
5
  ecm:
3
6
  calendar_helper:
4
7
  month_calendar_pagination:
@@ -1,4 +1,7 @@
1
1
  en:
2
+ date:
3
+ formats:
4
+ month_with_year: '%B %Y'
2
5
  ecm:
3
6
  calendar_helper:
4
7
  month_calendar_pagination:
@@ -1,5 +1,5 @@
1
1
  module Ecm
2
2
  module CalendarHelper
3
- VERSION = "0.0.3"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -0,0 +1,49 @@
1
+ module Ecm
2
+ module CalendarHelper
3
+ module Generators
4
+ class ControllerGenerator < Rails::Generators::Base
5
+ desc 'Generates a calendar controller (with views)'
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ attr_reader :base_controller_class_name
10
+ attr_reader :controller_class_name
11
+
12
+ def initialize(*args)
13
+ super
14
+ @base_controller_class_name = ENV.fetch('BASE_CONTROLLER_CLASS_NAME') { '::FrontendController' }
15
+ @controller_class_name = ENV.fetch('CONTROLLER_CLASS_NAME') { 'CalendarsController' }
16
+ end
17
+
18
+ def generate_routes
19
+ route File.read(File.join(File.expand_path('../templates', __FILE__), 'routes.source'))
20
+ end
21
+
22
+ def generate_controller
23
+ binding.pry
24
+ template 'controller.rb', "app/controllers/#{controller_path_name}.rb"
25
+ end
26
+
27
+ def generate_views
28
+ template 'views/index.html.haml', "app/views/#{view_path}/index.html.haml"
29
+ template 'views/index.js.erb', "app/views/#{view_path}/index.js.erb"
30
+ template 'views/_calendar.html.haml', "app/views/#{view_path}/_calendar.html.haml"
31
+ end
32
+
33
+ private
34
+
35
+ def controller_path_name
36
+ @controller_class_name.underscore
37
+ end
38
+
39
+ def view_path
40
+ @controller_class_name.underscore.gsub('_controller', '')
41
+ end
42
+
43
+ def route_name
44
+ @controller_class_name.demodulize.underscore.pluralize
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,18 @@
1
+ class <%= controller_class_name %> < <%= base_controller_class_name %>
2
+ include Ecm::CalendarHelper::Controller::CalendarConcern
3
+
4
+ private
5
+
6
+ def load_collection_for_calendar
7
+ @collection = {}
8
+ end
9
+
10
+ # def calendar_options
11
+ # default_calendar_options.merge(
12
+ # display_method: nil,
13
+ # start_day: :monday,
14
+ # start_date_method: :start_at,
15
+ # end_date_method: :end_at
16
+ # )
17
+ # end
18
+ end
@@ -0,0 +1,42 @@
1
+ Ecm::Blog.configure do |config|
2
+ # Set the base controller for the page controller
3
+ #
4
+ # Default: config.base_controller = '<%= base_controller_class_name %>'
5
+ #
6
+ config.base_controller = '<%= base_controller_class_name %>'
7
+
8
+ # Class to use for creators and updaters.
9
+ #
10
+ # default: config.creator_class_name = 'User'
11
+ #
12
+ config.creator_class_name = 'User'
13
+
14
+ # Set the page title for the posts index page. Useful for when you use
15
+ # the blog posts index as main app root page.
16
+ #
17
+ # example: config.posts_index_page_title_proc = ->(view) { view.t('.welcome') }
18
+ #
19
+ # default: config.posts_index_page_title_proc = ->(view) { view.resource_class.model_name.human(count: :other) }
20
+ #
21
+ config.posts_index_page_title_proc = ->(view) { view.resource_class.model_name.human(count: :other) }
22
+
23
+ # Set options that will be passed to the paginate call.
24
+ #
25
+ # example for bootstrap 3 (You will need to add the bootstrap3-kaminari-views gem to your Gemfile):
26
+ #
27
+ # config.pagination_options_proc = ->(view) { { theme: 'twitter-bootstrap-3' }
28
+ #
29
+ # example for bootstrap 4 (You will need to add the bootstrap4-kaminari-views gem to your Gemfile):
30
+ #
31
+ # config.pagination_options_proc = ->(view) { { theme: 'twitter-bootstrap-4' }
32
+ #
33
+ # default: config.pagination_options_proc = ->(view) { { theme: 'twitter-bootstrap-4', pagination_class: 'justify-content-center' } }
34
+ #
35
+ config.pagination_options_proc = ->(view) { { theme: 'twitter-bootstrap-4', pagination_class: 'justify-content-center' } }
36
+
37
+ # Options for rendering the ActiveStorage preview picture in the posts index view.
38
+ #
39
+ # Default: config.preview_picture_asset_variant_options = { resize: '320x240' }
40
+ #
41
+ config.preview_picture_asset_variant_options = { resize: '320x240' }
42
+ end
@@ -0,0 +1,5 @@
1
+
2
+
3
+ <%= whitespace_from_nesting %>resources :<%= route_name %>, only: [:index], constraints: ->(r) { r.xhr? } do
4
+ <%= whitespace_from_nesting %> get "(/:year/:month)", action: :index, on: :collection
5
+ <%= whitespace_from_nesting %>end
@@ -0,0 +1,2 @@
1
+ = month_calendar_pagination(date, remote: request.xhr?)
2
+ = month_calendar date, collection, options
@@ -0,0 +1 @@
1
+ = render partial: 'calendar', locals: { date: @date, collection: @collection, options: calendar_options }
@@ -0,0 +1 @@
1
+ $('#bookings-calendar').html("<%%= escape_javascript(render(partial: 'calendar', locals: { date: @date, collection: @collection, options: calendar_options })) %>");
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ecm_calendar_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Vasquez Angel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-25 00:00:00.000000000 Z
11
+ date: 2018-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -48,20 +48,34 @@ files:
48
48
  - MIT-LICENSE
49
49
  - README.rdoc
50
50
  - Rakefile
51
+ - app/assets/javascripts/ecm/calendar_helper/application.js
52
+ - app/assets/javascripts/ecm/calendar_helper/application/remote_calendar.js.coffee
53
+ - app/assets/javascripts/ecm_calendar_helper.js
51
54
  - app/assets/stylesheets/ecm/calendar_helper/application.css
52
55
  - app/assets/stylesheets/ecm/calendar_helper/application/basic.css
53
56
  - app/assets/stylesheets/ecm/calendar_helper/application/bootstrap-extensions.css
54
57
  - app/assets/stylesheets/ecm/calendar_helper/application/calendar-pagination.css
55
58
  - app/assets/stylesheets/ecm_calendar_helper.css
59
+ - app/concerns/ecm/calendar_helper/controller/calendar_concern.rb
56
60
  - app/helpers/ecm/calendar_helper.rb
57
61
  - app/views/ecm/calendar_helper/_month_calendar.haml
58
62
  - app/views/ecm/calendar_helper/_month_calendar_pagination.haml
63
+ - app/views/ecm/calendar_helper/calendar_concern/_calendar.html.haml
64
+ - app/views/ecm/calendar_helper/calendar_concern/index.html.haml
65
+ - app/views/ecm/calendar_helper/calendar_concern/index.js.erb
59
66
  - config/initializers/assets.rb
60
67
  - config/locales/de.yml
61
68
  - config/locales/en.yml
62
69
  - lib/ecm/calendar_helper/engine.rb
63
70
  - lib/ecm/calendar_helper/version.rb
64
71
  - lib/ecm_calendar_helper.rb
72
+ - lib/generators/ecm/calendar_helper/controller/controller_generator.rb
73
+ - lib/generators/ecm/calendar_helper/controller/templates/controller.rb
74
+ - lib/generators/ecm/calendar_helper/controller/templates/initializer.rb
75
+ - lib/generators/ecm/calendar_helper/controller/templates/routes.source
76
+ - lib/generators/ecm/calendar_helper/controller/templates/views/_calendar.html.haml
77
+ - lib/generators/ecm/calendar_helper/controller/templates/views/index.html.haml
78
+ - lib/generators/ecm/calendar_helper/controller/templates/views/index.js.erb
65
79
  - lib/tasks/ecm_calendar_helper_tasks.rake
66
80
  homepage: https://github.com/robotex82/ecm_calendar_helper.git
67
81
  licenses: []