Build custom calendars with Phlex.
This project started as an experiment to see how we can use Phlex with Simple Calendar. Then, I realized how easy it would be to rebuild Simple Calendar with Phlex, as we can
just create a Phlex::View
instead of using separate files for partials and helpers.
This gem works like Simple Calendar. To generate a calendar, you need a model that responds to #start_time
and #end_time
(but you can use custom attributes).
Let's scaffold an event resource to demonstrate how it works:
rails g scaffold event name:string start_time:datetime end_time:datetime
rails db:migrate
Now let's seed some events:
# db/seeds.rb
Event.create name: "Interview 1", start_time: Time.now, end_time: Time.now.advance(minutes: 15)
Event.create name: "Interview 2", start_time: Time.now.advance(days: 1, minutes: 15), end_time: Time.now.advance(days: 1, minutes: 45)
Event.create name: "Interview 3", start_time: Time.now.advance(days: 2, minutes: 30), end_time: Time.now.advance(days: 2, minutes: 75)
rails db:seed
Include PhlexibleCalendar::Event
in your model:
class Event < ApplicationRecord
include PhlexibleCalendar::Event
end
Then, add this in app/views/events/index.html.erb
to generate a calendar:
<%= render PhlexibleCalendar::Views::Calendar.new(events: @events) %>
class Event < ApplicationRecord
include PhlexibleCalendar::Event
def start_attribute
:start
end
def end_attribute
:end
end
end
<%= render PhlexibleCalendar::Views::Calendar.new(events: @events, start_attribute: :start, end_attribute: :end) %>
You need to set session[:time_zone]
to specify the time zone per request:
class ApplicationController < ActionController::Base
before_action :set_time_zone
private
def set_time_zone
session[:time_zone] ||= "Paris"
Time.zone = session[:time_zone]
end
end
Add this line to your application's Gemfile:
gem "phlexible_calendar", github: "mansakondo/phlexible_calendar"
Execute:
$ bundle
Then, run the installer:
rails phlexible_calendar:install
Contribution directions go here.
The gem is available as open source under the terms of the MIT License.