-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1920 from MushroomObserver/nimmo-observations-loc…
…ations-controller `Observations::LocationsController`, not `Locations::MergesController`
- Loading branch information
Showing
14 changed files
with
209 additions
and
202 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,127 @@ | ||
# frozen_string_literal: true | ||
|
||
# list_merge_options | ||
module Observations | ||
class LocationsController < ApplicationController | ||
before_action :login_required | ||
|
||
############################################################################ | ||
# | ||
# :section: Associate Observations with "undefined" +where+ strings | ||
# to Location db records as a batch. | ||
# | ||
############################################################################ | ||
|
||
# NOTE: This "form" is only accessed from one index flavor, | ||
# "OBSERVATIONS AT WHERE" | ||
# | ||
# It's a UI for Observations lacking a Location association in the db, | ||
# that share a particular "where" string. Multiple obs may share such a | ||
# "where" string. These are referred to as "undefined Locations" in the | ||
# code, but they're not really Locations at all, they're just strings that | ||
# have been entered by a User when creating an obs, that might help us find | ||
# a defined Location. Future Obs validations could force users to use known | ||
# locations, or to define a Location record before the obs, but we | ||
# currently allow this type of vagueness. | ||
# | ||
# What the list shows is a list of defined Locations that match the given | ||
# +where+ string, in order of closeness of match, in the following order: | ||
# 1) matches = match the string | ||
# 2) others that include everything in "where" up to the comma | ||
# 3) others that include the second-to-last segment in "where" | ||
# (for example, "Plaza Garibaldi, Ciudad de Guadalajara, México": | ||
# searches for "Ciudad de Guadalajara") | ||
# 4) others that include the last word of the first segment in "where" | ||
# (for example, "Estado de Guerrero, México": searches for "Guerrero") | ||
# 5) others that include the first word in "where": "Estado" | ||
# 6) doesn't try other segments, because the last one could be a country | ||
# | ||
# The form is not really a `form`, but the buttons do commit to "update". | ||
# It contains links to assign the observations sharing the +where+ string | ||
# to the given Location (AR record) | ||
|
||
def edit | ||
store_location | ||
# NOTE: Don't use or pass Location.user_format for @where. Needs "postal". | ||
# This is the string we're looking for in the db, in the `name` column, | ||
# and we're also assuming "postal" order when splitting the string. | ||
@where = params[:where].to_s | ||
@matches = locations_matching_where | ||
@pages = paginate_locations! | ||
end | ||
|
||
# Adds the Observation's associated with obs.where == params[:where] | ||
# into the given Location. | ||
def update | ||
location = find_or_goto_index(Location, params[:location]) | ||
return unless location | ||
|
||
where = begin | ||
params[:where].strip_squeeze | ||
rescue StandardError | ||
"" | ||
end | ||
if where.present? && | ||
update_observations_by_where(location, where) | ||
flash_notice( | ||
:runtime_location_merge_success.t( | ||
this: Location.user_format(@user, where), | ||
that: location.display_name | ||
) | ||
) | ||
end | ||
redirect_to(locations_path) | ||
end | ||
|
||
private | ||
|
||
def locations_matching_where | ||
matches = Location.name_includes(@where) | ||
|
||
# Try for segments: split by comma, or by space if no commas | ||
places = @where.split(",") | ||
words = @where.split | ||
return unless places.length > 1 || words.length > 1 | ||
|
||
matches += Location.name_includes(places.first) | ||
# Try for specific segment matches if we have enough of them. | ||
if places.length > 2 | ||
matches += Location.name_includes(places.second_to_last.strip) | ||
end | ||
if places.length >= 2 | ||
matches += Location.name_includes(places.second_to_last.split.last) | ||
end | ||
matches += Location.name_includes(words.first) | ||
|
||
matches.uniq | ||
end | ||
|
||
def paginate_locations! | ||
pages = paginate_numbers(:page, 100) | ||
pages.num_total = @matches.length | ||
@matches = @matches[pages.from..pages.to] | ||
pages | ||
end | ||
|
||
# Move all the Observation's with a given +where+ into a given Location. | ||
def update_observations_by_where(location, given_where) | ||
success = true | ||
observations = Observation.where(where: given_where) | ||
count = 3 | ||
observations.each do |o| | ||
count += 1 | ||
next if o.location_id | ||
|
||
o.location_id = location.id | ||
o.where = nil | ||
next if o.save | ||
|
||
flash_error( | ||
:runtime_location_merge_failed.t(name: o.unique_format_name) | ||
) | ||
success = false | ||
end | ||
success | ||
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
29 changes: 29 additions & 0 deletions
29
app/views/controllers/observations/locations/edit.html.erb
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,29 @@ | ||
<% | ||
@container = :wide | ||
add_page_title( | ||
:list_merge_options_title.t(where: Location.user_format(@user, @where)) | ||
) | ||
# NOTE: the "forms" are patch buttons. | ||
%> | ||
<% if @matches.any? %> | ||
<div class="h4"> | ||
<%= :list_merge_options_near_matches.t %> | ||
</div> | ||
<%= paginate_block(@pages) do %> | ||
<div class="list-group"> | ||
<% @matches.map do |location| %> | ||
<div class="list-group-item"> | ||
<%= patch_button(name: location.display_name.t, | ||
path: assign_location_to_observations_path( | ||
where: @where, location: location | ||
), | ||
class: "text-left", | ||
data: { | ||
turbo_confirm: :list_merge_are_you_sure.l | ||
}) %> | ||
</div> | ||
<% end %> | ||
</div> | ||
<% end %> | ||
<% end %> |
Oops, something went wrong.