]> git.openstreetmap.org Git - rails.git/blob - app/controllers/traces/legacy_visibilities_controller.rb
Merge remote-tracking branch 'upstream/pull/7447'
[rails.git] / app / controllers / traces / legacy_visibilities_controller.rb
1 # frozen_string_literal: true
2
3 module Traces
4   # Bulk change of traces that still use a legacy visibility
5   class LegacyVisibilitiesController < ApplicationController
6     include PaginationMethods
7
8     layout :site_layout
9
10     before_action :authorize_web
11     before_action :set_locale
12     before_action :check_database_readable
13
14     authorize_resource :class => Trace
15
16     before_action :check_database_writable, :only => :update
17     before_action :offline_warning, :only => :edit
18     before_action :offline_redirect, :only => :update
19
20     def edit
21       @title = t ".title"
22       @params = params.permit(:visibility, :from, :to, :before, :after)
23
24       traces = legacy_traces
25       @count = traces.count
26       @traces = get_page_items(traces, :includes => [:user, :tags])
27     end
28
29     def update
30       visibility = params[:new_visibility]
31
32       if Trace::VISIBILITIES.include?(visibility)
33         # the traces are legacy and the new visibility is a current one, so no validation can fail
34         # rubocop:disable-next Rails/SkipsModelValidations
35         count = legacy_traces.update_all(:visibility => visibility)
36         flash[:notice] = t ".updated", :count => count
37       else
38         flash[:error] = t ".invalid_visibility"
39       end
40
41       redirect_to edit_traces_legacy_visibility_path(:visibility => params[:visibility],
42                                                      :from => params[:from],
43                                                      :to => params[:to])
44     end
45
46     private
47
48     ##
49     # the current user's legacy traces, with the optional filters applied
50     def legacy_traces
51       traces = current_user.traces.visible.where(:visibility => Trace::LEGACY_VISIBILITIES)
52       traces = traces.where(:visibility => params[:visibility]) if Trace::LEGACY_VISIBILITIES.include?(params[:visibility])
53
54       from = filter_date(params[:from])
55       to = filter_date(params[:to])
56       traces = traces.where(:timestamp => from..) if from
57       # timestamp is a datetime, so the upper bound is the start of the next day
58       traces = traces.where(:timestamp => ...(to + 1)) if to
59
60       traces
61     end
62
63     def filter_date(value)
64       Date.parse(value) if value.present?
65     rescue ArgumentError
66       nil
67     end
68
69     def offline_warning
70       flash.now[:warning] = t "traces.offline_warning.message" if Settings.status == "gpx_offline"
71     end
72
73     def offline_redirect
74       render :template => "traces/offline" if Settings.status == "gpx_offline"
75     end
76   end
77 end