Skip to content

Instantly share code, notes, and snippets.

@sporto
Last active August 29, 2015 14:15
Show Gist options
  • Save sporto/2a04dff19424decb6a10 to your computer and use it in GitHub Desktop.
Save sporto/2a04dff19424decb6a10 to your computer and use it in GitHub Desktop.
Refactor curations show
def show
start = Time.now
previous_centre_future = nil
self.class.trace_execution_scoped(['Custom/CurationsController/show-api-calls']) do
if flash[:previous_centre_id].present?
previous_centre_future = Concurrent::Future.execute{CentreService.fetch flash[:previous_centre_id]}
previous_centre_future.add_observer(FutureObserver.new('previous_centre_future', start))
end
show_curations_future.add_observer(FutureObserver.new('curation_future', start))
show_centre_future.add_observer(FutureObserver.new('centre_future', start))
@curation = show_curation
if show_in_centre?
@centre = show_centre_future.value!
else
centres = show_centre_future.value!
if centres
@centres_by_state = centres.by_state
if @curation.present?
@availability_centres = build_availability_centres(@curation, centres)
end
end
end
end
@previous_centre = previous_centre_future.value! if previous_centre_future
end
def show_in_centre?
params[:centre_id]
end
def show_curations_future
@show_curations_future ||= if show_in_centre?
Concurrent::Future.execute{
CurationService.find(code: params[:slug], centre: params[:centre_id]).try{|curation|
curation.load_products(centre: params[:centre_id])
}
}
else
Concurrent::Future.execute{CurationService.find(code: params[:slug], centre: params[:centre_id]).try(:load_products)}
end
end
def show_curation
begin
show_curations_future.value!
rescue Service::API::Errors::NotFound => e
# @curation will just be nil
end
end
def show_centre_future
@show_centre_future ||= if show_in_centre?
Concurrent::Future.execute{CentreService.find(params[:centre_id])}
else
Concurrent::Future.execute{CentreService.find(:all, country: 'au')}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment