From 3e03528db19ebb24039f24ce9bb0b87df253a739 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Wed, 21 Jan 2015 17:10:03 -0500 Subject: [PATCH 1/3] Rescue failed gallery upload Reset the gallery after pressing the refresh button No need for class_eval when extending ActiveSupport::Concern --- app/assets/javascripts/bootsy/bootsy.js | 16 +++--- lib/bootsy/container.rb | 68 ++++++++++++------------- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/app/assets/javascripts/bootsy/bootsy.js b/app/assets/javascripts/bootsy/bootsy.js index 790516fb..ffe26a3d 100644 --- a/app/assets/javascripts/bootsy/bootsy.js +++ b/app/assets/javascripts/bootsy/bootsy.js @@ -100,6 +100,11 @@ Bootsy.Area.prototype.setUploadForm = function(html) { }.bind(this)); }; +// The image upload failed +Bootsy.Area.prototype.imageUploadFailed = function() { + alert(Bootsy.translations[this.locale].error); + this.showRefreshButton(); +}; // Set image gallery Bootsy.Area.prototype.setImageGallery = function() { @@ -114,7 +119,9 @@ Bootsy.Area.prototype.setImageGallery = function() { }, dataType: 'json', success: function(data) { + this.hideRefreshButton(); this.hideGalleryLoadingAnimation(); + this.find('.bootsy-gallery .bootsy-image').remove(); $.each(data.images, function(index, value) { this.addImage(value); @@ -128,11 +135,7 @@ Bootsy.Area.prototype.setImageGallery = function() { this.modal.data('gallery-loaded', true); }.bind(this), - error: function() { - alert(Bootsy.translations[this.locale].error); - - this.showRefreshButton(); - }.bind(this) + error: this.imageUploadFailed.bind(this) }); }; @@ -152,7 +155,6 @@ Bootsy.Area.prototype.deleteImage = function (id) { }.bind(this)); }; - // Add image to gallery Bootsy.Area.prototype.addImage = function(html) { this.hideEmptyAlert(); @@ -262,6 +264,8 @@ Bootsy.Area.prototype.init = function() { this.deleteImage(data.id); }.bind(this)); + this.modal.on('ajax:error', '.bootsy-upload-form', this.imageUploadFailed.bind(this)); + this.modal.on('ajax:success', '.bootsy-upload-form', function(evt, data) { this.setImageGalleryId(data.gallery_id); this.addImage(data.image); diff --git a/lib/bootsy/container.rb b/lib/bootsy/container.rb index 773d8dc7..eef9d5d6 100644 --- a/lib/bootsy/container.rb +++ b/lib/bootsy/container.rb @@ -11,43 +11,41 @@ module Container extend ActiveSupport::Concern included do - class_eval do - has_one :bootsy_image_gallery, - class_name: 'Bootsy::ImageGallery', - as: :bootsy_resource, - dependent: :destroy + has_one :bootsy_image_gallery, + class_name: 'Bootsy::ImageGallery', + as: :bootsy_resource, + dependent: :destroy + end - # Public: Get the `id` attribute of the image gallery. - # - # Returns an Integer id or nil when there is - # not an image gallery. - def bootsy_image_gallery_id - bootsy_image_gallery.try(:id) - end + # Public: Get the `id` attribute of the image gallery. + # + # Returns an Integer id or nil when there is + # not an image gallery. + def bootsy_image_gallery_id + bootsy_image_gallery.try(:id) + end - # Public: Set the image gallery `id` and save - # the association between models. - # - # Examples - # - # container.id - # # => 34 - # gallery.id - # # => 12 - # container.bootsy_image_gallery_id = gallery.id - # container.image_gallery_id - # # => 12 - # gallery.bootsy_resource.id - # # => 34 - def bootsy_image_gallery_id=(value) - if bootsy_image_gallery.nil? && value.present? - self.bootsy_image_gallery = Bootsy::ImageGallery.find(value) - bootsy_image_gallery.bootsy_resource = self - bootsy_image_gallery.save - else - value - end - end + # Public: Set the image gallery `id` and save + # the association between models. + # + # Examples + # + # container.id + # # => 34 + # gallery.id + # # => 12 + # container.bootsy_image_gallery_id = gallery.id + # container.image_gallery_id + # # => 12 + # gallery.bootsy_resource.id + # # => 34 + def bootsy_image_gallery_id=(value) + if bootsy_image_gallery.nil? && value.present? + self.bootsy_image_gallery = Bootsy::ImageGallery.find(value) + bootsy_image_gallery.bootsy_resource = self + bootsy_image_gallery.save + else + value end end end From 7f291027762c066737b09011b435473dee329948 Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Thu, 22 Jan 2015 01:07:17 -0500 Subject: [PATCH 2/3] Display image validation messages --- app/assets/javascripts/bootsy/bootsy.js | 13 +++++++++++-- features/step_definitions/global_steps.rb | 4 ++++ features/upload_image.feature | 7 +++++++ spec/dummy/public/test.fake | 0 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 spec/dummy/public/test.fake diff --git a/app/assets/javascripts/bootsy/bootsy.js b/app/assets/javascripts/bootsy/bootsy.js index ffe26a3d..d9189c22 100644 --- a/app/assets/javascripts/bootsy/bootsy.js +++ b/app/assets/javascripts/bootsy/bootsy.js @@ -101,8 +101,17 @@ Bootsy.Area.prototype.setUploadForm = function(html) { }; // The image upload failed -Bootsy.Area.prototype.imageUploadFailed = function() { - alert(Bootsy.translations[this.locale].error); +Bootsy.Area.prototype.imageUploadFailed = function(e, xhr, status, error) { + this.invalidErrors = xhr.responseJSON; + if (Number(xhr.status) === 422 && this.invalidErrors.image_file) { + this.hideUploadLoadingAnimation(); + if (this.validation) this.validation.remove(); + this.validation = $("

"); + this.validation.text(this.invalidErrors.image_file[0]); + this.find('.bootsy-upload-form').append(this.validation); + } else { + alert(Bootsy.translations[this.locale].error); + } this.showRefreshButton(); }; diff --git a/features/step_definitions/global_steps.rb b/features/step_definitions/global_steps.rb index 4d2ba000..886f53ec 100644 --- a/features/step_definitions/global_steps.rb +++ b/features/step_definitions/global_steps.rb @@ -44,3 +44,7 @@ Then(/^I see "(.*?)" on the page$/) do |content| expect(page).to have_content(content) end + +Then(/^I don't see "(.*?)" on the page$/) do |content| + expect(page).not_to have_content(content) +end diff --git a/features/upload_image.feature b/features/upload_image.feature index 9a48d3e2..4d925919 100644 --- a/features/upload_image.feature +++ b/features/upload_image.feature @@ -16,6 +16,13 @@ Feature: Upload an image And I press "Go" Then I see the thumbnail "test.jpg" on the image gallery + Scenario: Upload an invaild image + When I attach the file "test.fake" on "image_file" + Then I don't see the thumbnail "test.jpg" on the image gallery + Then I see "You are not allowed to upload" on the page + When I press "Refresh" + Then I don't see "You are not allowed to upload" on the page + Scenario: Associate the uploaded image with the container When I upload the image "test.jpg" And I insert the image "test.jpg" on the text diff --git a/spec/dummy/public/test.fake b/spec/dummy/public/test.fake new file mode 100644 index 00000000..e69de29b From de4a9c8fb8843cb9bc0a1a7dc9989035963c738d Mon Sep 17 00:00:00 2001 From: Ray Zane Date: Sat, 24 Jan 2015 12:36:13 -0500 Subject: [PATCH 3/3] Allow configuration of base controller for auth purposes Remove the update route Add upload spec for invalid w/ url --- app/controllers/bootsy/application_controller.rb | 2 +- config/routes.rb | 4 ++-- features/upload_image.feature | 7 ++++++- lib/bootsy.rb | 4 ++++ lib/generators/bootsy/templates/bootsy.rb | 5 +++++ spec/dummy/public/test.fake | 1 + 6 files changed, 19 insertions(+), 4 deletions(-) diff --git a/app/controllers/bootsy/application_controller.rb b/app/controllers/bootsy/application_controller.rb index d8ddf91d..a045281e 100644 --- a/app/controllers/bootsy/application_controller.rb +++ b/app/controllers/bootsy/application_controller.rb @@ -1,5 +1,5 @@ module Bootsy - class ApplicationController < ActionController::Base + class ApplicationController < Bootsy.base_controller # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception diff --git a/config/routes.rb b/config/routes.rb index fa305d6a..de259ab5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,9 @@ Bootsy::Engine.routes.draw do resources :image_galleries, only: [] do - resources :images, only: [:index, :create, :update, :destroy] + resources :images, only: [:index, :create, :destroy] end - file_routes = [:index, :create, :update] + file_routes = [:index, :create] file_routes << :destroy if Bootsy.allow_destroy diff --git a/features/upload_image.feature b/features/upload_image.feature index 4d925919..83bef944 100644 --- a/features/upload_image.feature +++ b/features/upload_image.feature @@ -16,13 +16,18 @@ Feature: Upload an image And I press "Go" Then I see the thumbnail "test.jpg" on the image gallery - Scenario: Upload an invaild image + Scenario: Upload an invalid image When I attach the file "test.fake" on "image_file" Then I don't see the thumbnail "test.jpg" on the image gallery Then I see "You are not allowed to upload" on the page When I press "Refresh" Then I don't see "You are not allowed to upload" on the page + Scenario: Upload an invalid image from a URL + When I fill in "image[remote_image_file_url]" with "http://stubhost.com/test.fake" + And I press "Go" + Then I see "You are not allowed to upload" on the page + Scenario: Associate the uploaded image with the container When I upload the image "test.jpg" And I insert the image "test.jpg" on the text diff --git a/lib/bootsy.rb b/lib/bootsy.rb index f82c8dde..62694234 100644 --- a/lib/bootsy.rb +++ b/lib/bootsy.rb @@ -56,6 +56,10 @@ module Bootsy mattr_accessor :store_dir @@store_dir = 'uploads' + # Specify which controller to inherit from + mattr_accessor :base_controller + @@base_controller = ActionController::Base + # Default way to setup Bootsy. Run rails generate bootsy:install # to create a fresh initializer with all configuration values. def self.setup diff --git a/lib/generators/bootsy/templates/bootsy.rb b/lib/generators/bootsy/templates/bootsy.rb index 1c09b213..4d59d5d8 100644 --- a/lib/generators/bootsy/templates/bootsy.rb +++ b/lib/generators/bootsy/templates/bootsy.rb @@ -61,4 +61,9 @@ # Store directory (inside 'public') for storage = :file # BE CAREFUL! Changing this may break previously uploaded file paths! # config.store_dir = 'uploads' + # + # + # Specify the controller to inherit from. Using ApplicationController + # allows you to perform authentication from within your app. + # config.base_controller = ActionController::Base end diff --git a/spec/dummy/public/test.fake b/spec/dummy/public/test.fake index e69de29b..6f965ff6 100644 --- a/spec/dummy/public/test.fake +++ b/spec/dummy/public/test.fake @@ -0,0 +1 @@ +This file is used for testing invalid image uploads.