Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions app/controllers/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ def certify
end

def destroy
resource = Resource.find params[:id]
if resource.approved?
resource.inactive!
remove_from_algolia(resource)
render status: :ok
else
render status: :precondition_failed
end
puts params[:id]
Services::Resources.deactivate params[:id]
render status: :ok
rescue Errors::PreconditionFailed
render status: :precondition_failed
end

# Return the total number of active (i.e., approved) resources
Expand Down
18 changes: 4 additions & 14 deletions app/controllers/services_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,10 @@ def reject
end

def destroy
service = Service.find params[:id]
if service.approved?
service.inactive!
remove_from_algolia(service)
render status: :ok
else
render status: :precondition_failed
end
Services::Services.deactivate params[:id]
render status: :ok
rescue Errors::PreconditionFailed
render status: :precondition_failed
end

def count
Expand All @@ -103,12 +99,6 @@ def count

private

def remove_from_algolia(service)
service.remove_from_index!
rescue StandardError
puts 'failed to remove rservice ' + service.id.to_s + ' from algolia index'
end

def services
Service.includes(:notes, :categories, :eligibilities, :addresses, schedule: :schedule_days)
end
Expand Down
5 changes: 5 additions & 0 deletions app/errors/errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module Errors
class PreconditionFailed < StandardError; end
end
24 changes: 24 additions & 0 deletions app/services/resources/resources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

module Services
module Resources
Copy link
Contributor

@cliffcrosland cliffcrosland Jun 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be cool to add some rspec tests for these services, but if their functionality is already covered by the Postman tests, there is probably no need.

include Errors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rails might auto-load this module, so I think it might be possible to delete this include Errors line here. In development mode, Rails lazily loads modules, so when it encounters raise Errors::PreconditionFailed, it will go look for the appropriate file and load it into scope.


def self.deactivate(id)
resource = Resource.find id
raise Errors::PreconditionFailed unless resource.approved?

resource.inactive!
remove_from_algolia(resource)
resource.services.each do |service|
Services.deactivate service
end
end

def self.remove_from_algolia(resource)
resource.remove_from_index!
rescue StandardError
puts 'failed to remove resource ' + resource.id.to_s + ' from algolia index'
end
end
end
21 changes: 21 additions & 0 deletions app/services/services/services.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Services
module Services
include Errors

def self.deactivate(id)
service = Service.find id
raise Errors::PreconditionFailed unless service.approved?

service.inactive!
remove_from_algolia(service)
end

def self.remove_from_algolia(service)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this can be factored out as a common utility since it is used in both services? Might be able to put it in app/services/utils.rb? Perhaps the error log message can be generalized as follows:

def self.remove_from_algolia(record)
  record.remove_from_index!
rescue StandardError
  puts "failed to remove #{record.class} #{record.id} from algolia index"
end

service.remove_from_index!
rescue StandardError
puts 'failed to remove service ' + service.id.to_s + ' from algolia index'
end
end
end