Skip to content

Commit

Permalink
Removed unused model image routes
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Mar 23, 2022
1 parent 7e6459a commit d62e543
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 212 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ tmp/model_images/*
tmp/restart.txt
tmp/rubycritic/*
tmp/profile-*
tmp/miniprofiler/*
tmp/test_content_blobs/*
tmp/rubycritic/*
tmp/rdf/
Expand Down
134 changes: 10 additions & 124 deletions app/controllers/model_images_controller.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,5 @@
class ModelImagesController < ApplicationController
before_action :check_model_specified
before_action :find_model_images, only: [:index]
before_action :find_model_image_auth, only: %i[show select edit update destroy]

def new
@model_image = ModelImage.new
end

def create
if params[:model_image].blank? || params[:model_image][:image_file].blank?
file_specified = false
else
file_specified = true
@model_image = ModelImage.new params[:model_image]
@model_image.model_id = params[:model_id]
@model_image.content_type = params[:model_image][:image_file].content_type
@model_image.original_filename = params[:model_image][:image_file].original_filename
end

respond_to do |format|
if file_specified && @model_image.save

if @model_image.model.model_image_id.nil?
@model_instance.update_attribute(:model_image_id, @model_image.id)
end

flash[:notice] = 'Image was successfully uploaded'
format.html { redirect_to params[:return_to] }
else
@model_image = ModelImage.new
if file_specified
flash.now[:error] = 'The image format is unreadable. Please try again or select a different image.'
else
flash.now[:error] = "You haven't specified the filename. Please choose the image file to upload."
end
format.html { render action: 'new' }
end
end
end
before_action :find_model_image_auth

def show
if params[:size]
Expand All @@ -58,95 +20,19 @@ def show
end
end

def filter_size(size)
max_size = 1500
matches = size.match(/([0-9]+)x([0-9]+).*/)
if matches
width = matches[1].to_i
height = matches[2].to_i
width = max_size if width > max_size
height = max_size if height > max_size
return "#{width}x#{height}"
else
matches = size.match(/([0-9]+)/)
if matches
width = matches[1].to_i
width = max_size if width > max_size
return width.to_s
else
return '900'
end
end
end

def index
respond_to do |format|
format.html
end
end

def destroy
model = @model_image.model
@model_image.destroy
respond_to do |format|
format.html { redirect_to model_model_images_url model.id }
end
end

def select
if @model_image.select!

@model_image.save
respond_to do |format|
flash[:notice] = "#{t('model')} image was successfully updated."
format.html { redirect_to model_model_image_url @model_instance.id }
end
else
respond_to do |format|
flash[:error] = 'Image was already selected.'
format.html { redirect_to url_for(@model_instance) }
end
end
end

private

def check_model_specified
@image_for = 'Model'
@image_for_id = params[:model_id]

begin
@model_instance = Model.find(@image_for_id)
rescue ActiveRecord::RecordNotFound
flash[:error] = "Could not find the #{@image_for.downcase} for this image."
redirect_to(root_path)
return false
end
end

def find_model_images
if @model_instance.can_view? current_user
@model_images = ModelImage.where model_id: @image_for_id
else
flash[:error] = "You can only view images for #{t('model').pluralize} you can access"
redirect_to root_path
end
end

def find_model_image_auth
action = Seek::Permissions::Translator.translate(action_name)
unless is_auth?(@model_instance, action)
flash[:error] = "You can only #{action} images for #{t('model').pluralize} you can access"
redirect_to root_path
return false
end

begin
@model_image = ModelImage.where(model_id: @image_for_id).find(params[:id])
rescue ActiveRecord::RecordNotFound
flash[:error] = 'Image not found or belongs to a different model.'
@model = Model.find(params[:model_id])
if is_auth?(@model, :view)
@model_image = @model.model_images.where(id: params[:id]).first
if @model_image.nil?
flash[:error] = 'Image not found or belongs to a different model.'
redirect_to root_path
end
else
flash[:error] = "You can only view images for #{I18n.t('model').pluralize} you can access"
redirect_to root_path
return false
end
end
end
8 changes: 0 additions & 8 deletions app/helpers/images_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ def flag_icon(country, text = country, margin_right = '0.3em')
end
end

def model_image_url(model_instance, model_image_id, size = nil)
basic_url = model_model_image_path(model_instance, model_image_id)

basic_url = append_size_parameter(basic_url, size)

basic_url
end

def append_size_parameter(url, size)
if size
url << "?size=#{size}"
Expand Down
50 changes: 0 additions & 50 deletions app/views/model_images/index.html.erb

This file was deleted.

22 changes: 0 additions & 22 deletions app/views/model_images/new.html.erb

This file was deleted.

9 changes: 1 addition & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,14 +508,7 @@
get :simulate
post :simulate
end
resources :model_images do
collection do
post :new
end
member do
post :select
end
end
resources :model_images, only: [:show]
resources :people, :programmes, :projects, :investigations, :assays, :studies, :publications, :events, :collections, only: [:index]
end

Expand Down

0 comments on commit d62e543

Please sign in to comment.