diff --git a/.gitignore b/.gitignore index 899c99ff3e..321ed327d3 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ tmp/model_images/* tmp/restart.txt tmp/rubycritic/* tmp/profile-* +tmp/miniprofiler/* tmp/test_content_blobs/* tmp/rubycritic/* tmp/rdf/ diff --git a/app/controllers/model_images_controller.rb b/app/controllers/model_images_controller.rb index 1c2821c8fc..82e814dea5 100644 --- a/app/controllers/model_images_controller.rb +++ b/app/controllers/model_images_controller.rb @@ -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] @@ -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 diff --git a/app/helpers/images_helper.rb b/app/helpers/images_helper.rb index 9a1f8af541..ac7138c4be 100644 --- a/app/helpers/images_helper.rb +++ b/app/helpers/images_helper.rb @@ -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}" diff --git a/app/views/model_images/index.html.erb b/app/views/model_images/index.html.erb deleted file mode 100644 index 05cf281426..0000000000 --- a/app/views/model_images/index.html.erb +++ /dev/null @@ -1,50 +0,0 @@ -<% image_can_be_edited_by_current_user = mine?(@model_instance) || @model_instance.can_edit?(current_user) -%> - - - - -

<%= t('model') %> Images: <%= link_to @model_instance.title, url_for(@model_instance) -%>

- -<% unless @model_images.empty? %> - -
- - -<% odd_row = false -%> -<% for model_image in @model_images -%> - "> - - - - -<% end -%> -
- <% if model_image.selected? -%> - <%= image_tag_for_key "tick", nil, nil, nil, "" -%>
- Current - <% end -%> -
- <%= image_tag model_image_url(@model_instance, model_image.id, 100), :alt => @model_instance.title, :class => 'framed' -%> - - <% if image_can_be_edited_by_current_user -%> - <% image_link = model_image_url(@model_instance, model_image.id) -%> - <% unless model_image.selected? -%> - <%= image_tag_for_key "image", image_link + "/select", nil, { :method => :post }, "Set as Main Image" -%> - <% end -%> - <% end -%> - <%= image_tag_for_key "show", model_image_url(@model_instance, model_image.id, "large"), nil, nil, "Enlarge" -%> - <% if image_can_be_edited_by_current_user -%> - <%= image_tag_for_key "destroy", image_link, nil, { data: { confirm: 'Are you sure?' }, :method => :delete }, "Delete" -%> - <% end -%> -
- -<% else -%> - <%= no_items_to_list_text %> -<% end -%> - -
diff --git a/app/views/model_images/new.html.erb b/app/views/model_images/new.html.erb deleted file mode 100644 index 446ca6ee1d..0000000000 --- a/app/views/model_images/new.html.erb +++ /dev/null @@ -1,22 +0,0 @@ -<% image_for_object = @image_for.find(@image_for_id) -%> -<% return_path = params[:return_to] ? params[:return_to] : polymorphic_path([image_for_object, :model_images]) -%> -

Upload <%= @image_for -%> Image: <%= link_to image_for_object.title, url_for(image_for_object) -%>

- -<%= error_messages_for :model_image -%> - -<%= form_tag({:action => :create}, :multipart => true) do %> - <%= hidden_field_tag(:model_id, @image_for_id ) -%> - <%= hidden_field_tag(:return_to, return_path) -%> -
- -
-

- Image file: - <%= file_field 'model_image', 'image_file' %> -

-
- - <%= submit_tag "Upload", data: { disable_with: 'Uploading...' } %> - or <%= cancel_button(return_path) -%> -
-<% end %> diff --git a/config/routes.rb b/config/routes.rb index f607b45e7e..270b111a50 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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