Files
growstuff/app/controllers/member_locations_controller.rb
T
Brenda Wallace be38339b02 Refactor member location updates and improve error handling
- Updated MemberLocationsController to use a hash for location updates, ensuring coordinates are not geocoded again.
- Changed response status codes in member_locations_spec to use symbols for better readability.
- Modified PlacesController to use parentheses for defining MEMBER_FIELDS.
- Enhanced SuggestionsController to disable Rails/SkipsModelValidations for update_columns.
- Updated SeasonsHelper to use compact_blank for cleaner output.
- Refactored MemberDashboard service for improved readability and performance.
- Adjusted migration files to use parentheses for index definitions.
- Updated schema.rb to maintain consistency in index definitions.
- Added SampleData class to generate sample data for development.
- Cleaned up sample_data.rake task to utilize the new SampleData class.
- Updated tests in member_dashboard_spec and place_suggester_spec for consistency and readability.
2026-10-12 19:30:21 +13:00

34 lines
1.4 KiB
Ruby

# frozen_string_literal: true
# Saves the place a signed-in member says they garden in. The dialog on the
# home page sends the name and coordinates of a place it found with
# PlacesController#suggest, so there is no second geocode here.
class MemberLocationsController < ApplicationController
skip_authorize_resource
before_action :authenticate_member!
respond_to :json
def update
place = location_params
# Straight to the columns: saving would geocode the name again, and the place already has coordinates.
columns = { location: place[:name], latitude: place[:latitude], longitude: place[:longitude] }
current_member.update_columns(columns) # rubocop:disable Rails/SkipsModelValidations
current_member.gardens.where(location: [nil, '']).update_all(columns) # rubocop:disable Rails/SkipsModelValidations
render json: { location: place[:name] }
rescue ArgumentError, ActionController::ParameterMissing
render json: { errors: { location: ['is not a place we can use'] } }, status: :unprocessable_content
end
private
def location_params
raw = params.require(:location).permit(:name, :latitude, :longitude)
name = raw[:name].to_s.squish
latitude = Float(raw[:latitude])
longitude = Float(raw[:longitude])
raise ArgumentError unless name.present? && name.length <= 255 && latitude.between?(-90, 90) && longitude.between?(-180, 180)
{ name:, latitude:, longitude: }
end
end