From 50bce629d83acec28114ef9c12949ad75b129f28 Mon Sep 17 00:00:00 2001 From: Carlos Daniel Pohlod Date: Wed, 18 May 2022 17:46:37 -0300 Subject: [PATCH] refatored conferences_controller --- app/controllers/conferences_controller.rb | 127 ++++++++----------- app/services/conferences_calendar_service.rb | 57 +++++++++ app/services/conferences_service.rb | 89 +++++++++++++ 3 files changed, 200 insertions(+), 73 deletions(-) create mode 100644 app/services/conferences_calendar_service.rb create mode 100644 app/services/conferences_service.rb diff --git a/app/controllers/conferences_controller.rb b/app/controllers/conferences_controller.rb index a9dcdd3b..4a0ad8b8 100644 --- a/app/controllers/conferences_controller.rb +++ b/app/controllers/conferences_controller.rb @@ -3,6 +3,8 @@ class ConferencesController < ApplicationController protect_from_forgery with: :null_session before_action :respond_to_options + before_action :conference, only: [:show] + before_action :set_conferences_service, only: [:show] load_and_authorize_resource find_by: :short_title, except: :show def index @@ -14,14 +16,6 @@ class ConferencesController < ApplicationController end def show - # load conference with header content - @conference = Conference.unscoped.eager_load( - :splashpage, - :program, - :registration_period, - :contact, - venue: :commercial - ).find_by!(conference_finder_conditions) authorize! :show, @conference # TODO: reduce the 10 queries performed here splashpage = @conference.splashpage @@ -29,42 +23,7 @@ class ConferencesController < ApplicationController unless splashpage.present? redirect_to admin_conference_splashpage_path(@conference.short_title) && return end - - @image_url = "#{request.protocol}#{request.host}#{@conference.picture}" - - if splashpage.include_cfp - cfps = @conference.program.cfps - @call_for_events = cfps.find { |call| call.cfp_type == 'events' } - if @call_for_events.try(:open?) - @event_types = @conference.event_types.pluck(:title) - @track_names = @conference.confirmed_tracks.pluck(:name).sort - end - @call_for_tracks = cfps.find { |call| call.cfp_type == 'tracks' } - @call_for_booths = cfps.find { |call| call.cfp_type == 'booths' } - end - if splashpage.include_program - @highlights = @conference.highlighted_events.eager_load(:speakers) - if splashpage.include_tracks - @tracks = @conference.confirmed_tracks.eager_load( - :room - ).order('tracks.name') - end - if splashpage.include_booths - @booths = @conference.confirmed_booths.order('title') - end - end - if splashpage.include_registrations || splashpage.include_tickets - @tickets = @conference.tickets.order('price_cents') - end - if splashpage.include_lodgings - @lodgings = @conference.lodgings.order('name') - end - if splashpage.include_sponsors - @sponsorship_levels = @conference.sponsorship_levels.eager_load( - :sponsors - ).order('sponsorship_levels.position ASC', 'sponsors.name') - @sponsors = @conference.sponsors - end + handle_present_splash_page(splashpage) end def calendar @@ -72,35 +31,12 @@ class ConferencesController < ApplicationController 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 + service = ConferenceCalendarService.new(calendar, conf) + calendar = if params[:full] + service.full_calendar + else + service.not_full_calendar + end end calendar.publish render inline: calendar.to_ical @@ -110,6 +46,51 @@ class ConferencesController < ApplicationController private + def handle_present_splash_page(splashpage) + @image_url = @service.conference_image_url(request) + + if splashpage.include_cfp + cfp_variables + end + + if splashpage.include_program + program_variables + end + + @tickets = @service.if_include_registrations_or_tickets + @lodgings = @service.if_include_lodgings + + if splashpage.include_sponsors + sponsor_variables + end + end + + def program_variables + @highlights = @conference.highlighted_events.eager_load(:speakers) + @tracks = @service.if_include_tracks + @booths = @service.if_include_booths + end + + def cfp_variables + @call_for_events = @service.cfp_call_by_type('events') + @event_types, @track_names = @service.cfp_variables_if_event_open(@call_for_events) + @call_for_tracks = @service.cfp_call_by_type('tracks') + @call_for_booths = @service.cfp_call_by_type('booths') + end + + def sponsor_variables + @sponsorship_levels = @service.sponsorship_levels + @sponsors = @conference.sponsors + end + + def conference + @conference = ConferencesService.conference_by_filter(conference_finder_conditions) + end + + def set_conferences_service + @service = ConferencesService.new(@conference) + end + def conference_finder_conditions if params[:id] { short_title: params[:id] } diff --git a/app/services/conferences_calendar_service.rb b/app/services/conferences_calendar_service.rb new file mode 100644 index 00000000..4eb595b1 --- /dev/null +++ b/app/services/conferences_calendar_service.rb @@ -0,0 +1,57 @@ +class ConferencesCalendarService + def initialize(calendar, conference) + @calendar = calendar + @conference = conference + end + + def not_full_calendar + calendar.event do |e| + default_calendar_params(e) + if @conference.venue + venue_calendar_params(e) + end + end + calendar + end + + def full_calendar + icalendar_proposals(@calendar, event_schedules.map(&:event), @conference) + end + + private + + def default_calendar_params(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) + end + + def venue_calendar_params(event) + venue = @conference.venue + event.geo = venue.latitude, venue.longitude if venue.latitude && venue.longitude + location = location(venue) + event.location = location if location + end + + def location(venue) + 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 + location + end + + def event_schedules + @conference.program.selected_event_schedules( + includes: [{ event: %i[event_type speakers submitter] }] + ) + end +end diff --git a/app/services/conferences_service.rb b/app/services/conferences_service.rb new file mode 100644 index 00000000..64616278 --- /dev/null +++ b/app/services/conferences_service.rb @@ -0,0 +1,89 @@ +class ConferencesService + def initialize(conference = nil) + @conference = conference + @cfps = @conference&.program&.cfps + @splashpage = @conference&.splashpage + end + + def self.conference_by_filter(conference_finder_conditions) + Conference.unscoped.eager_load( + :splashpage, + :program, + :registration_period, + :contact, + venue: :commercial + ).find_by!(conference_finder_conditions) + end + + def conference_image_url(request) + "#{request.protocol}#{request.host}#{@conference.picture}" + end + + def cfp_variables_if_event_open(call_for_events) + if call_for_events.try(:open?) + [event_types, track_names] + end + end + + def cfp_call_by_type(type) + @cfps.find { |call| call.cfp_type == type } + end + + def if_include_tracks + if @splashpage.include_tracks + tracks + end + end + + def if_include_booths + if @splashpage.include_booths + booths + end + end + + def if_include_registrations_or_tickets + if @splashpage.include_registrations || @splashpage.include_tickets + tickets + end + end + + def if_include_lodgings + if @splashpage.include_lodgings + lodgings + end + end + + def sponsorship_levels + @conference.sponsorship_levels.eager_load( + :sponsors + ).order('sponsorship_levels.position ASC', 'sponsors.name') + end + + private + + def lodgings + @conference.lodgings.order('name') + end + + def track_names + @conference.confirmed_tracks.pluck(:name).sort + end + + def event_types + @conference.event_types.pluck(:title) + end + + def tracks + @conference.confirmed_tracks.eager_load( + :room + ).order('tracks.name') + end + + def booths + @conference.confirmed_booths.order('title') + end + + def tickets + @conference.tickets.order('price_cents') + end +end