Skip to content

Commit

Permalink
Merge pull request #1920 from MushroomObserver/nimmo-observations-loc…
Browse files Browse the repository at this point in the history
…ations-controller

`Observations::LocationsController`, not `Locations::MergesController`
  • Loading branch information
nimmolo authored Feb 20, 2024
2 parents 6644519 + 10797a6 commit 3286363
Show file tree
Hide file tree
Showing 14 changed files with 209 additions and 202 deletions.
36 changes: 0 additions & 36 deletions app/controllers/locations/merges_controller.rb

This file was deleted.

54 changes: 0 additions & 54 deletions app/controllers/locations/observations_controller.rb

This file was deleted.

1 change: 0 additions & 1 deletion app/controllers/locations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ def edit

params[:location] ||= {}
@display_name = @location.display_name
update if request.method == "POST"
end

def update
Expand Down
127 changes: 127 additions & 0 deletions app/controllers/observations/locations_controller.rb
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
17 changes: 0 additions & 17 deletions app/helpers/tabs/locations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,22 +138,5 @@ def location_search_tabs(name)
link_array << search_tab_for(site.first, search_string)
end
end

# these are from the observations form
def define_location_tab(query)
[:list_observations_location_define.l,
add_query_param(new_location_path(
where: query.params[:user_where]
)),
{ class: tab_id(__method__.to_s) }]
end

def merge_locations_tab(query)
[:list_observations_location_merge.l,
add_query_param(location_merges_form_path(
where: query.params[:user_where]
)),
{ class: tab_id(__method__.to_s) }]
end
end
end
17 changes: 16 additions & 1 deletion app/helpers/tabs/observations_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,26 @@ def observations_at_where_tabs(query)

[
define_location_tab(query),
merge_locations_tab(query),
assign_undefined_location_tab(query),
locations_index_tab
]
end

# these are from the observations form
def define_location_tab(query)
[:list_observations_location_define.l,
add_query_param(new_location_path(where: query.params[:user_where])),
{ class: tab_id(__method__.to_s) }]
end

def assign_undefined_location_tab(query)
[:list_observations_location_merge.l,
add_query_param(matching_locations_for_observations_path(
where: query.params[:user_where]
)),
{ class: tab_id(__method__.to_s) }]
end

def observations_index_sorts
[
["rss_log", :sort_by_activity.l],
Expand Down
12 changes: 7 additions & 5 deletions app/views/controllers/locations/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%
@container = :wide
@container = :full
add_index_title(@query, no_hits: "")
add_tab_set(locations_index_tabs(query: @query))
all_links = if (params[:id].present? ||
Expand All @@ -23,7 +23,7 @@ flash_error(@error) if @error && @known_pages.empty? && @undef_pages.empty?
</div>

<div class="row mt-3">
<div class="col-md-7">
<div class="col-md-6">
<% if @known_pages.any? && @known_data.any? %>
<div class="h4">
<%= :list_place_names_known.t %>
Expand All @@ -42,7 +42,7 @@ flash_error(@error) if @error && @known_pages.empty? && @undef_pages.empty?
<% end %>
</div>

<div class="col-md-5">
<div class="col-md-6">
<% if @undef_pages.any? && @undef_data.any? %>
<div class="h4">
<%= :list_place_names_undef.t %>
Expand All @@ -53,8 +53,10 @@ flash_error(@error) if @error && @known_pages.empty? && @undef_pages.empty?
<% @undef_data.each do |location_name, count| %>
<div class="list-group-item">
<%= location_link(location_name, nil, count) %>
<%= link_to(:list_place_names_merge.t,
location_merges_form_path(where: location_name)) %>
<%= link_to(
:list_place_names_merge.t,
matching_locations_for_observations_path(where: location_name)
) %>
</div>
<% end %>
</div>
Expand Down
9 changes: 0 additions & 9 deletions app/views/controllers/locations/merges/_row.html.erb

This file was deleted.

38 changes: 0 additions & 38 deletions app/views/controllers/locations/merges/new.html.erb

This file was deleted.

29 changes: 29 additions & 0 deletions app/views/controllers/observations/locations/edit.html.erb
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 %>
Loading

0 comments on commit 3286363

Please sign in to comment.