-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored conferences controller, dividing responsabilities using an…
… service and an query
- Loading branch information
1 parent
fd01351
commit 7b48dab
Showing
6 changed files
with
355 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
module Conferences | ||
class ShowVariablesFetcher | ||
def initialize(conference: nil) | ||
@conference = conference | ||
@cfps = @conference&.program&.cfps | ||
@splashpage = @conference&.splashpage | ||
end | ||
|
||
def conference_image_url(request) | ||
"#{request.protocol}#{request.host}#{conference.picture}" | ||
end | ||
|
||
def event_types_and_track_names(call_for_events) | ||
[event_types, track_names] if call_for_events.try(:open?) | ||
end | ||
|
||
def cfp_call_by_type(type) | ||
@cfps.find { |call| call.cfp_type == type } | ||
end | ||
|
||
def fetch_tracks | ||
tracks if @splashpage.include_tracks | ||
end | ||
|
||
def fetch_booths | ||
booths if @splashpage.include_booths | ||
end | ||
|
||
def fetch_tickets | ||
if @splashpage.include_registrations || @splashpage.include_tickets | ||
tickets | ||
end | ||
end | ||
|
||
def fetch_lodgings | ||
lodgings if @splashpage.include_lodgings | ||
end | ||
|
||
def sponsorship_levels | ||
Queries::Conferences.new(conference: conference).sponsorship_levels | ||
end | ||
|
||
private | ||
|
||
attr :conference | ||
|
||
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 | ||
Queries::Conferences.new(conference: conference).confirmed_tracks | ||
end | ||
|
||
def booths | ||
conference.confirmed_booths.order('title') | ||
end | ||
|
||
def tickets | ||
conference.tickets.order('price_cents') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class Queries::Conferences | ||
def initialize(conference: nil) | ||
@conference = conference | ||
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 sponsorship_levels | ||
conference.sponsorship_levels.eager_load( | ||
:sponsors | ||
).order('sponsorship_levels.position ASC', 'sponsors.name') | ||
end | ||
|
||
def confirmed_tracks | ||
conference.confirmed_tracks.eager_load( | ||
:room | ||
).order('tracks.name') | ||
end | ||
|
||
private | ||
|
||
attr :conference | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
spec/services/conferences/show_variables_fetcher_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
describe Conferences::ShowVariablesFetcher, type: :service do | ||
describe '.conference_image_url' do | ||
let(:request) do | ||
double(protocol: 'protocol', host: 'host') | ||
end | ||
let(:conference) do | ||
build(:conference) | ||
end | ||
|
||
it 'returns templete string builded' do | ||
result = described_class.new(conference: conference) | ||
.conference_image_url(request) | ||
|
||
expect(result).to eq( | ||
"#{request.protocol}#{request.host}#{conference.picture}" | ||
) | ||
end | ||
end | ||
|
||
describe '.event_types_and_track_names' do | ||
let!(:conference) do | ||
create :conference | ||
end | ||
let(:call_for_events) { double(open?: true) } | ||
|
||
it 'returns an array with event_types and track_names' do | ||
result = described_class.new(conference: conference) | ||
.event_types_and_track_names(call_for_events) | ||
|
||
expect(result).to eq([ | ||
conference.event_types.pluck(:title), | ||
conference.confirmed_tracks.pluck(:name).sort | ||
]) | ||
end | ||
end | ||
|
||
describe '.cfp_call_by_type' do | ||
let!(:conference) do | ||
create :full_conference | ||
end | ||
let(:cfp_type) do | ||
conference.program&.cfps.first.cfp_type | ||
end | ||
|
||
it 'returns finded cfp by your type' do | ||
result = described_class.new(conference: conference) | ||
.cfp_call_by_type(cfp_type) | ||
|
||
expect(result.cfp_type).to eq(cfp_type) | ||
end | ||
end | ||
|
||
describe '.fetch_tracks' do | ||
let!(:conference) do | ||
create :full_conference | ||
end | ||
let(:tracks) { double(:tracks) } | ||
let(:conferences_query) do | ||
instance_double(Queries::Conferences, confirmed_tracks: tracks) | ||
end | ||
|
||
setup do | ||
allow(Queries::Conferences) | ||
.to receive(:new) | ||
.and_return(conferences_query) | ||
end | ||
|
||
it 'returns tracks' do | ||
result = described_class.new(conference: conference) | ||
.fetch_tracks | ||
|
||
expect(result).to eq(tracks) | ||
end | ||
|
||
it 'calls Queries::Conferences with correct params' do | ||
expect(Queries::Conferences) | ||
.to receive(:new) | ||
.with(conference: conference) | ||
|
||
described_class.new(conference: conference).fetch_tracks | ||
end | ||
end | ||
|
||
describe '.fetch_booths' do | ||
let!(:conference) do | ||
create :full_conference | ||
end | ||
|
||
setup do | ||
conference.splashpage.update!(include_booths: true) | ||
end | ||
|
||
it 'returns confirmed_booths' do | ||
result = described_class.new(conference: conference).fetch_booths | ||
|
||
expect(result).to eq(conference.confirmed_booths) | ||
end | ||
end | ||
|
||
describe '.fetch_tickets' do | ||
let!(:conference) { create :full_conference } | ||
let(:splashpage) { conference.splashpage } | ||
let(:tickets) { double(:tickets) } | ||
|
||
context 'when splashpage#include_registrations is true' do | ||
setup do | ||
splashpage.update!(include_registrations: true) | ||
splashpage.update!(include_tickets: false) | ||
end | ||
|
||
it 'returns confirmed tickets' do | ||
result = described_class.new(conference: conference) | ||
.fetch_tickets | ||
|
||
expect(tickets) | ||
end | ||
end | ||
|
||
context 'when splashpage#include_tickets is true' do | ||
setup do | ||
splashpage.update!(include_registrations: false) | ||
splashpage.update!(include_tickets: true) | ||
end | ||
|
||
it 'returns confirmed tickets' do | ||
result = described_class.new(conference: conference) | ||
.fetch_tickets | ||
|
||
expect(tickets) | ||
end | ||
end | ||
end | ||
|
||
describe '.fetch_lodgings' do | ||
let!(:conference) { create :full_conference } | ||
|
||
it 'returns lodgings' do | ||
result = described_class.new(conference: conference).fetch_lodgings | ||
|
||
expect(result).to eq( | ||
conference.lodgings.order('name') | ||
) | ||
end | ||
end | ||
|
||
describe '.sponsorship_levels' do | ||
let(:sponsorship_levels) { double(:sponsorship_levels) } | ||
let(:query_instance) do | ||
instance_double(Queries::Conferences, sponsorship_levels: sponsorship_levels) | ||
end | ||
let(:conference) { build(:full_conference) } | ||
setup do | ||
allow(Queries::Conferences) | ||
.to receive(:new) | ||
.and_return(query_instance) | ||
end | ||
|
||
it 'returns sponsorship_levels' do | ||
result = described_class.new(conference: conference).sponsorship_levels | ||
|
||
expect(result).to eq(sponsorship_levels) | ||
end | ||
|
||
it 'calls Queries::Conferences with correct params' do | ||
expect(Queries::Conferences) | ||
.to receive(:new) | ||
.with(conference: conference) | ||
|
||
described_class.new(conference: conference).sponsorship_levels | ||
end | ||
end | ||
end |
Oops, something went wrong.