]> git.openstreetmap.org Git - rails.git/commitdiff
Use imported scope to simplify trace controller methods
authorTom Hughes <tom@compton.nu>
Thu, 28 Mar 2024 17:26:04 +0000 (17:26 +0000)
committerTom Hughes <tom@compton.nu>
Thu, 28 Mar 2024 17:26:04 +0000 (17:26 +0000)
app/controllers/traces/icons_controller.rb
app/controllers/traces/pictures_controller.rb
app/models/trace.rb

index ec67a6bb1336585735388d96709d69653c248b77..c86e391e4ddec5a973856bbb2493b57eb75b6317 100644 (file)
@@ -6,21 +6,17 @@ module Traces
     authorize_resource :trace
 
     def show
-      trace = Trace.visible.find(params[:trace_id])
+      trace = Trace.visible.imported.find(params[:trace_id])
 
-      if trace.inserted?
-        if trace.public? || (current_user && current_user == trace.user)
-          if trace.icon.attached?
-            redirect_to rails_blob_path(trace.icon, :disposition => "inline")
-          else
-            expires_in 7.days, :private => !trace.public?, :public => trace.public?
-            send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
-          end
+      if trace.public? || (current_user && current_user == trace.user)
+        if trace.icon.attached?
+          redirect_to rails_blob_path(trace.icon, :disposition => "inline")
         else
-          head :forbidden
+          expires_in 7.days, :private => !trace.public?, :public => trace.public?
+          send_file(trace.icon_picture_name, :filename => "#{trace.id}_icon.gif", :type => "image/gif", :disposition => "inline")
         end
       else
-        head :not_found
+        head :forbidden
       end
     rescue ActiveRecord::RecordNotFound
       head :not_found
index 0e0d588cb7410a8c781d41380f0fa316fc039fbb..e190621674df094aabc4bca32c1c6ac1d5ec16c0 100644 (file)
@@ -6,21 +6,17 @@ module Traces
     authorize_resource :trace
 
     def show
-      trace = Trace.visible.find(params[:trace_id])
+      trace = Trace.visible.imported.find(params[:trace_id])
 
-      if trace.inserted?
-        if trace.public? || (current_user && current_user == trace.user)
-          if trace.icon.attached?
-            redirect_to rails_blob_path(trace.image, :disposition => "inline")
-          else
-            expires_in 7.days, :private => !trace.public?, :public => trace.public?
-            send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
-          end
+      if trace.public? || (current_user && current_user == trace.user)
+        if trace.icon.attached?
+          redirect_to rails_blob_path(trace.image, :disposition => "inline")
         else
-          head :forbidden
+          expires_in 7.days, :private => !trace.public?, :public => trace.public?
+          send_file(trace.large_picture_name, :filename => "#{trace.id}.gif", :type => "image/gif", :disposition => "inline")
         end
       else
-        head :not_found
+        head :forbidden
       end
     rescue ActiveRecord::RecordNotFound
       head :not_found
index 818cc363b8629595bed5a4d842033f708598f14c..d1f917571a21287f5de4e0a7a290e8e8337babb3 100644 (file)
@@ -38,6 +38,7 @@ class Trace < ApplicationRecord
   scope :visible_to, ->(u) { visible.where(:visibility => %w[public identifiable]).or(visible.where(:user => u)) }
   scope :visible_to_all, -> { where(:visibility => %w[public identifiable]) }
   scope :tagged, ->(t) { joins(:tags).where(:gpx_file_tags => { :tag => t }) }
+  scope :imported, -> { where(:inserted => true) }
 
   has_one_attached :file, :service => Settings.trace_file_storage
   has_one_attached :image, :service => Settings.trace_image_storage