Skip to content

Commit

Permalink
no_details=2 flag for locations endpoint, geared toward app bottom sheet
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanTG committed Dec 3, 2024
1 parent ecb6635 commit 357b569
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
44 changes: 31 additions & 13 deletions app/controllers/api/v1/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,13 @@ def closest_by_lat_lon
def within_bounding_box
if params[:no_details] == '1'
except = %i[country last_updated_by_user_id description region_id zone_id website phone ic_active is_stern_army date_last_updated created_at]
except2 = %i[machine_names_first machine_ids num_machines]
includes = %i[machine_names_first machine_ids num_machines]
elsif params[:no_details] == '2'
except = %i[name street state zip country updated_at location_type_id operator_id country last_updated_by_user_id description region_id zone_id website phone ic_active is_stern_army date_last_updated created_at]
except2 = []
includes = []
else
except = []
except2 = %i[machine_names_first machine_ids num_machines]
includes = %i[machine_names_first machine_ids num_machines]
end

bounds = [params[:swlat], params[:swlon], params[:nelat], params[:nelon]]
Expand Down Expand Up @@ -254,7 +254,7 @@ def within_bounding_box

if !locations_within.empty?
respond_to do |format|
format.json { return_response(locations_within, 'locations', [], except2, 200, except) }
format.json { return_response(locations_within, 'locations', [], includes, 200, except) }
format.geojson { render json: container_geojson.to_json }
end
else
Expand Down Expand Up @@ -316,18 +316,36 @@ def closest_by_address
formats ['json']
def show
location = nil
if params[:no_details]
location = Location.includes(:machines, :last_updated_by_user).find(params[:id])

if params[:no_details] == '1'
location = Location.includes(:location_machine_xrefs, :machines, :last_updated_by_user).find(params[:id])
return_response(
location,
nil,
:location_machine_xrefs,
%i[last_updated_by_username num_machines],
200,
%i[zone_id created_at region_id is_stern_army country]
)
elsif params[:no_details] == '2'
location = Location.find(params[:id])
return_response(
location,
nil,
[],
%i[machine_names_first_no_year num_machines],
200,
%i[phone website updated_at region_id description operator_id date_last_updated last_updated_by_user_id ic_active zone_id created_at is_stern_army country]
)
else
location = Location.includes(location_machine_xrefs: [:user, :machine, { machine_conditions: :user }, { machine_score_xrefs: :user }]).find(params[:id])
return_response(
location,
nil,
[location_machine_xrefs: { include: { machine_conditions: { methods: :username }, machine_score_xrefs: { methods: :username } }, methods: :last_updated_by_username }],
%i[last_updated_by_username num_machines]
)
end

return_response(
location,
nil,
params[:no_details] ? :location_machine_xrefs : [location_machine_xrefs: { include: { machine_conditions: { methods: :username }, machine_score_xrefs: { methods: :username } }, methods: :last_updated_by_username }],
%i[last_updated_by_username num_machines]
)
rescue ActiveRecord::RecordNotFound
return_response('Failed to find location', 'errors')
end
Expand Down
29 changes: 23 additions & 6 deletions spec/requests/api/v1/locations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -850,10 +850,13 @@
end

describe '#show' do
it 'returns all regions within scope along with lmx data' do
lmx = FactoryBot.create(:location_machine_xref, location: @location, machine: FactoryBot.create(:machine, id: 777, name: 'Cleo'))
before(:each) do
lmx = FactoryBot.create(:location_machine_xref, location: @location, machine: FactoryBot.create(:machine, id: 7777, name: 'Cleo'))
FactoryBot.create(:machine_condition, location_machine_xref_id: lmx.id, comment: 'foo bar')
FactoryBot.create(:machine_score_xref, location_machine_xref: lmx, score: 567)
FactoryBot.create(:machine_score_xref, location_machine_xref: lmx, score: 567_890)
end

it 'returns all locations within region scope along with lmx data' do
get "/api/v1/region/#{@region.name}/locations/#{@location.id}.json"

expect(response.body).to include('Satchmo')
Expand All @@ -862,16 +865,30 @@
expect(response.body).to include('567')
end

it 'show location info plus comments and scores' do
get "/api/v1/locations/#{@location.id}.json"

expect(response.body).to include('Satchmo')
expect(response.body).to include('7777')
expect(response.body).to include('foo bar')
expect(response.body).to include('567890')
end

it 'respects no_details and shows fewer location fields' do
lmx = FactoryBot.create(:location_machine_xref, location: @location, machine: FactoryBot.create(:machine, id: 7777, name: 'Cleo'))
FactoryBot.create(:machine_condition, location_machine_xref_id: lmx.id, comment: 'foo bar')
FactoryBot.create(:machine_score_xref, location_machine_xref: lmx, score: 567_890)
get "/api/v1/locations/#{@location.id}.json", params: { no_details: 1 }

expect(response.body).to include('Satchmo')
expect(response.body).to include('7777')
expect(response.body).to_not include('foo bar')
expect(response.body).to_not include('567890')

get "/api/v1/locations/#{@location.id}.json", params: { no_details: 2 }

expect(response.body).to include('Satchmo')
expect(response.body).to include('Cleo')
expect(response.body).to_not include('7777')
expect(response.body).to_not include('foo bar')
expect(response.body).to_not include('567890')
end
end

Expand Down

0 comments on commit 357b569

Please sign in to comment.