Skip to content

Commit f23d1f0

Browse files
committed
Refactor /calendars into it's own controller
Spec it out too
1 parent 6e276b2 commit f23d1f0

File tree

7 files changed

+128
-74
lines changed

7 files changed

+128
-74
lines changed

.rubocop_todo.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,9 @@ Metrics/AbcSize:
535535
# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns, inherit_mode.
536536
# AllowedMethods: refine
537537
Metrics/BlockLength:
538-
Max: 202
538+
Exclude:
539+
- 'config/routes.rb'
540+
Max: 100
539541

540542
# Offense count: 1
541543
# Configuration parameters: CountBlocks, CountModifierForms.
@@ -1435,7 +1437,7 @@ Rails/RedundantPresenceValidationOnBelongsTo:
14351437
# Offense count: 2
14361438
Rails/RenderInline:
14371439
Exclude:
1438-
- 'app/controllers/conferences_controller.rb'
1440+
- 'app/controllers/calendars_controller.rb'
14391441
- 'app/controllers/schedules_controller.rb'
14401442

14411443
# Offense count: 10
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
class CalendarsController < ApplicationController
2+
skip_authorization_check only: :index
3+
4+
# GET /calendars
5+
def index
6+
calendar = Icalendar::Calendar.new
7+
8+
Conference.all.each do |conference|
9+
if params[:full]
10+
event_schedules = conference.program.selected_event_schedules(
11+
includes: [{ event: %i[event_type speakers submitter] }]
12+
)
13+
calendar = build_icalendar_from_proposals(calendar, event_schedules.map(&:event), conference)
14+
else
15+
calendar = build_icalender_from_conference(calendar, conference)
16+
end
17+
end
18+
19+
respond_to do |format|
20+
format.ics do
21+
calendar.publish
22+
render inline: calendar.to_ical
23+
end
24+
end
25+
end
26+
27+
private
28+
29+
def build_icalender_from_conference(calendar, conference)
30+
calendar.event do |event|
31+
event.dtstart = conference.start_date
32+
event.dtstart.ical_params = { 'VALUE'=>'DATE' }
33+
event.dtend = conference.end_date
34+
event.dtend.ical_params = { 'VALUE'=>'DATE' }
35+
event.duration = "P#{(conference.end_date - conference.start_date + 1).floor}D"
36+
event.created = conference.created_at
37+
event.last_modified = conference.updated_at
38+
event.summary = conference.title
39+
event.description = conference.description
40+
event.uid = conference.guid
41+
event.url = conference_url(conference.short_title)
42+
43+
venue = conference.venue
44+
if venue
45+
event.geo = venue.latitude, venue.longitude if venue.latitude && venue.longitude
46+
47+
location = ''
48+
location += "#{venue.street}, " if venue.street
49+
location += "#{venue.postalcode} #{venue.city}, " if venue.postalcode && venue.city
50+
location += venue.country_name if venue.country_name
51+
event.location = location if location
52+
end
53+
end
54+
calendar
55+
end
56+
57+
# adds events to icalendar for proposals in a conference
58+
def build_icalendar_from_proposals(calendar, proposals, conference)
59+
proposals.each do |proposal|
60+
calendar.event do |event|
61+
event.dtstart = proposal.time
62+
event.dtend = proposal.time + (proposal.event_type.length * 60)
63+
event.duration = "PT#{proposal.event_type.length}M"
64+
event.created = proposal.created_at
65+
event.last_modified = proposal.updated_at
66+
event.summary = proposal.title
67+
event.description = proposal.abstract
68+
event.uid = proposal.guid
69+
event.url = conference_program_proposal_url(conference.short_title, proposal.id)
70+
venue = conference.venue
71+
if venue
72+
event.geo = venue.latitude, venue.longitude if venue.latitude && venue.longitude
73+
74+
location = ''
75+
location += "#{proposal.room.name} - " if proposal.room.name
76+
location += " - #{venue.street}, " if venue.street
77+
location += "#{venue.postalcode} #{venue.city}, " if venue.postalcode && venue.city
78+
location += "#{venue.country_name}, " if venue.country_name
79+
event.location = location
80+
end
81+
if proposal.difficulty_level && proposal.track
82+
event.categories = proposal.title, "Difficulty: #{proposal.difficulty_level.title}", "Track: #{proposal.track.name}"
83+
end
84+
end
85+
end
86+
calendar
87+
end
88+
end

app/controllers/conferences_controller.rb

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -67,47 +67,6 @@ def show
6767
end
6868
end
6969

70-
def calendar
71-
respond_to do |format|
72-
format.ics do
73-
calendar = Icalendar::Calendar.new
74-
Conference.all.each do |conf|
75-
if params[:full]
76-
event_schedules = conf.program.selected_event_schedules(
77-
includes: [{ event: %i[event_type speakers submitter] }]
78-
)
79-
calendar = icalendar_proposals(calendar, event_schedules.map(&:event), conf)
80-
else
81-
calendar.event do |e|
82-
e.dtstart = conf.start_date
83-
e.dtstart.ical_params = { 'VALUE'=>'DATE' }
84-
e.dtend = conf.end_date
85-
e.dtend.ical_params = { 'VALUE'=>'DATE' }
86-
e.duration = "P#{(conf.end_date - conf.start_date + 1).floor}D"
87-
e.created = conf.created_at
88-
e.last_modified = conf.updated_at
89-
e.summary = conf.title
90-
e.description = conf.description
91-
e.uid = conf.guid
92-
e.url = conference_url(conf.short_title)
93-
v = conf.venue
94-
if v
95-
e.geo = v.latitude, v.longitude if v.latitude && v.longitude
96-
location = ''
97-
location += "#{v.street}, " if v.street
98-
location += "#{v.postalcode} #{v.city}, " if v.postalcode && v.city
99-
location += v.country_name if v.country_name
100-
e.location = location if location
101-
end
102-
end
103-
end
104-
end
105-
calendar.publish
106-
render inline: calendar.to_ical
107-
end
108-
end
109-
end
110-
11170
def code_of_conduct; end
11271

11372
private

app/helpers/conference_helper.rb

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,4 @@ def sponsorship_mailto(conference)
2121
'%20Sponsorship'
2222
].join
2323
end
24-
25-
# adds events to icalendar for proposals in a conference
26-
def icalendar_proposals(calendar, proposals, conference)
27-
proposals.each do |proposal|
28-
calendar.event do |e|
29-
e.dtstart = proposal.time
30-
e.dtend = proposal.time + proposal.event_type.length * 60
31-
e.duration = "PT#{proposal.event_type.length}M"
32-
e.created = proposal.created_at
33-
e.last_modified = proposal.updated_at
34-
e.summary = proposal.title
35-
e.description = proposal.abstract
36-
e.uid = proposal.guid
37-
e.url = conference_program_proposal_url(conference.short_title, proposal.id)
38-
v = conference.venue
39-
if v
40-
e.geo = v.latitude, v.longitude if v.latitude && v.longitude
41-
location = ''
42-
location += "#{proposal.room.name} - " if proposal.room.name
43-
location += " - #{v.street}, " if v.street
44-
location += "#{v.postalcode} #{v.city}, " if v.postalcode && v.city
45-
location += "#{v.country_name}, " if v.country_name
46-
e.location = location
47-
end
48-
e.categories = conference.title, "Difficulty: #{proposal.difficulty_level.title}", "Track: #{proposal.track.name}"
49-
end
50-
end
51-
calendar
52-
end
5324
end

app/views/conferences/index.html.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
%p
2222
Add the events to your calendar:
2323
%span.btn-group
24-
= link_to("Days only", calendar_url(protocol: 'webcal', format: 'ics'), class: 'btn btn-default')
25-
= link_to("Detailed", calendar_url(protocol: 'webcal', format: 'ics', full: true), class: 'btn btn-default')
24+
= link_to("Days only", calendars_url(protocol: 'webcal', format: 'ics'), class: 'btn btn-default')
25+
= link_to("Detailed", calendars_url(protocol: 'webcal', format: 'ics', full: true), class: 'btn btn-default')
2626

2727
-content_for :script_body do
2828
:javascript

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@
198198
end
199199
end
200200

201+
resources :calendars, only: :index
202+
201203
namespace :api, defaults: {format: 'json'} do
202204
namespace :v1 do
203205
resources :conferences, only: [ :index, :show ] do
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
describe CalendarsController do
6+
render_views
7+
8+
let!(:conference) { create(:conference, splashpage: create(:splashpage, public: true), venue: create(:venue)) }
9+
let!(:events) { create_list(:event_scheduled, 2, program: conference.program) }
10+
11+
describe 'GET #index' do
12+
before :each do
13+
conference.program.update(schedule_public: true)
14+
get :index, format: :ics
15+
end
16+
17+
it 'renders a calendar from conference' do
18+
expect(response.body).to match(/#{conference.title}/im)
19+
end
20+
end
21+
22+
describe 'GET #index with full format' do
23+
before :each do
24+
conference.program.update(schedule_public: true)
25+
get :index, format: :ics, params: { full: 1 }
26+
end
27+
28+
it 'renders a calendar from events' do
29+
expect(response.body).to match(/#{events.first.title}/im)
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)