Skip to content

Commit

Permalink
Refactor /calendars into it's own controller
Browse files Browse the repository at this point in the history
Spec it out too
  • Loading branch information
hennevogel committed Mar 8, 2021
1 parent 5e70690 commit 7733622
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 95 deletions.
88 changes: 88 additions & 0 deletions app/controllers/calendars_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
class CalendarsController < ApplicationController
skip_authorization_check only: :index

# GET /calendars
def index
calendar = Icalendar::Calendar.new

Conference.all.each do |conference|
if params[:full]
event_schedules = conference.program.selected_event_schedules(
includes: [{ event: %i[event_type speakers submitter] }]
)
calendar = build_icalendar_from_proposals(calendar, event_schedules.map(&:event), conference)
else
calendar = build_icalender_from_conference(calendar, conference)
end
end

respond_to do |format|
format.ics do
calendar.publish
render inline: calendar.to_ical
end
end
end

private

def build_icalender_from_conference(calendar, conference)
calendar.event do |event|
event.dtstart = conference.start_date
event.dtstart.ical_params = { 'VALUE'=>'DATE' }
event.dtend = conference.end_date
event.dtend.ical_params = { 'VALUE'=>'DATE' }
event.duration = "P#{(conference.end_date - conference.start_date + 1).floor}D"
event.created = conference.created_at
event.last_modified = conference.updated_at
event.summary = conference.title
event.description = conference.description
event.uid = conference.guid
event.url = conference_url(conference.short_title)

venue = conference.venue
if venue
event.geo = venue.latitude, venue.longitude if venue.latitude && venue.longitude

location = ''
location += "#{venue.street}, " if venue.street
location += "#{venue.postalcode} #{venue.city}, " if venue.postalcode && venue.city
location += venue.country_name if venue.country_name
event.location = location if location
end
end
calendar
end

# adds events to icalendar for proposals in a conference
def build_icalendar_from_proposals(calendar, proposals, conference)
proposals.each do |proposal|
calendar.event do |event|
event.dtstart = proposal.time
event.dtend = proposal.time + proposal.event_type.length * 60
event.duration = "PT#{proposal.event_type.length}M"
event.created = proposal.created_at
event.last_modified = proposal.updated_at
event.summary = proposal.title
event.description = proposal.abstract
event.uid = proposal.guid
event.url = conference_program_proposal_url(conference.short_title, proposal.id)
venue = conference.venue
if venue
event.geo = venue.latitude, venue.longitude if venue.latitude && venue.longitude

location = ''
location += "#{proposal.room.name} - " if proposal.room.name
location += " - #{venue.street}, " if venue.street
location += "#{venue.postalcode} #{venue.city}, " if venue.postalcode && venue.city
location += "#{venue.country_name}, " if venue.country_name
event.location = location
end
if proposal.difficulty_level && proposal.track
event.categories = proposal.title, "Difficulty: #{proposal.difficulty_level.title}", "Track: #{proposal.track.name}"
end
end
end
calendar
end
end
41 changes: 0 additions & 41 deletions app/controllers/conferences_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,47 +67,6 @@ def show
end
end

def calendar
respond_to do |format|
format.ics do
calendar = Icalendar::Calendar.new
Conference.all.each do |conf|
if params[:full]
event_schedules = conf.program.selected_event_schedules(
includes: [{ event: %i[event_type speakers submitter] }]
)
calendar = icalendar_proposals(calendar, event_schedules.map(&:event), conf)
else
calendar.event do |e|
e.dtstart = conf.start_date
e.dtstart.ical_params = { 'VALUE'=>'DATE' }
e.dtend = conf.end_date
e.dtend.ical_params = { 'VALUE'=>'DATE' }
e.duration = "P#{(conf.end_date - conf.start_date + 1).floor}D"
e.created = conf.created_at
e.last_modified = conf.updated_at
e.summary = conf.title
e.description = conf.description
e.uid = conf.guid
e.url = conference_url(conf.short_title)
v = conf.venue
if v
e.geo = v.latitude, v.longitude if v.latitude && v.longitude
location = ''
location += "#{v.street}, " if v.street
location += "#{v.postalcode} #{v.city}, " if v.postalcode && v.city
location += v.country_name if v.country_name
e.location = location if location
end
end
end
end
calendar.publish
render inline: calendar.to_ical
end
end
end

private

def conference_finder_conditions
Expand Down
29 changes: 0 additions & 29 deletions app/helpers/conference_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,4 @@ def sponsorship_mailto(conference)
'%20Sponsorship'
].join
end

# adds events to icalendar for proposals in a conference
def icalendar_proposals(calendar, proposals, conference)
proposals.each do |proposal|
calendar.event do |e|
e.dtstart = proposal.time
e.dtend = proposal.time + proposal.event_type.length * 60
e.duration = "PT#{proposal.event_type.length}M"
e.created = proposal.created_at
e.last_modified = proposal.updated_at
e.summary = proposal.title
e.description = proposal.abstract
e.uid = proposal.guid
e.url = conference_program_proposal_url(conference.short_title, proposal.id)
v = conference.venue
if v
e.geo = v.latitude, v.longitude if v.latitude && v.longitude
location = ''
location += "#{proposal.room.name} - " if proposal.room.name
location += " - #{v.street}, " if v.street
location += "#{v.postalcode} #{v.city}, " if v.postalcode && v.city
location += "#{v.country_name}, " if v.country_name
e.location = location
end
e.categories = conference.title, "Difficulty: #{proposal.difficulty_level.title}", "Track: #{proposal.track.name}"
end
end
calendar
end
end
4 changes: 2 additions & 2 deletions app/views/conferences/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
%p
Add the events to your calendar:
%span.btn-group
= link_to("Days only", calendar_url(protocol: 'webcal', format: 'ics'), class: 'btn btn-default')
= link_to("Detailed", calendar_url(protocol: 'webcal', format: 'ics', full: true), class: 'btn btn-default')
= link_to("Days only", calendars_url(protocol: 'webcal', format: 'ics'), class: 'btn btn-default')
= link_to("Detailed", calendars_url(protocol: 'webcal', format: 'ics', full: true), class: 'btn btn-default')

-content_for :script_body do
:javascript
Expand Down
43 changes: 22 additions & 21 deletions app/views/conferences/new_install.html.haml
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
.row
.col-md-12
.well
%h1
Welcome to your new
= link_to('https://osem.io') do
Open Source Event Manager
installation!
%p
The first user to
- if ENV['OSEM_ICHAIN_ENABLED'] == 'true'
= link_to(new_ichain_registration_path('user')) do
sign up
- else
= link_to(new_registration_path('user')) do
sign up
will the be administrator of it.
%p
We hope you enjoy using OSEM, if you have any question don't hesitate to
= link_to('https://osem.io/#contact') do
contact us!
.container
.row
.col-md-12
.well
%h1
Welcome to your new
= link_to('https://osem.io') do
Open Source Event Manager
installation!
%p
The first user to
- if ENV['OSEM_ICHAIN_ENABLED'] == 'true'
= link_to(new_ichain_registration_path('user')) do
sign up
- else
= link_to(new_registration_path('user')) do
sign up
will the be administrator of it.
%p
We hope you enjoy using OSEM, if you have any question don't hesitate to
= link_to('https://osem.io/#contact') do
contact us!
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@
end
end

resources :calendars, only: :index

namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :conferences, only: [ :index, :show ] do
Expand All @@ -219,8 +221,6 @@

get '/admin' => redirect('/admin/conferences')

get '/calendar' => 'conferences#calendar'

unless ENV['OSEM_ROOT_CONFERENCE'].blank?
root to: redirect("/conferences/#{ENV['OSEM_ROOT_CONFERENCE']}")
else
Expand Down
32 changes: 32 additions & 0 deletions spec/controllers/calendars_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require 'spec_helper'

describe CalendarsController do
render_views

let!(:conference) { create(:conference, splashpage: create(:splashpage, public: true), venue: create(:venue)) }
let!(:events) { create_list(:event_scheduled, 2, program: conference.program) }

describe 'GET #index' do
before :each do
conference.program.update_attributes(schedule_public: true)
get :index, format: :ics
end

it 'renders a calendar from conference' do
expect(response.body).to match(/#{conference.title}/im)
end
end

describe 'GET #index with full format' do
before :each do
conference.program.update_attributes(schedule_public: true)
get :index, format: :ics, params: { full: 1 }
end

it 'renders a calendar from events' do
expect(response.body).to match(/#{events.first.title}/im)
end
end
end

0 comments on commit 7733622

Please sign in to comment.