Skip to content
This repository has been archived by the owner on Apr 18, 2018. It is now read-only.

Commit

Permalink
Merge pull request #111 from rzane/catch-gallery-upload-errors
Browse files Browse the repository at this point in the history
Rescue failed gallery upload
  • Loading branch information
volmer committed Jan 25, 2015
2 parents dea651d + de4a9c8 commit 9ab3789
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 44 deletions.
25 changes: 19 additions & 6 deletions app/assets/javascripts/bootsy/bootsy.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ Bootsy.Area.prototype.setUploadForm = function(html) {
}.bind(this));
};

// The image upload failed
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 = $("<p class='text-danger'>");
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();
};

// Set image gallery
Bootsy.Area.prototype.setImageGallery = function() {
Expand All @@ -114,7 +128,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);
Expand All @@ -128,11 +144,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)
});
};

Expand All @@ -152,7 +164,6 @@ Bootsy.Area.prototype.deleteImage = function (id) {
}.bind(this));
};


// Add image to gallery
Bootsy.Area.prototype.addImage = function(html) {
this.hideEmptyAlert();
Expand Down Expand Up @@ -262,6 +273,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);
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/bootsy/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 4 additions & 0 deletions features/step_definitions/global_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions features/upload_image.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ Feature: Upload an image
And I press "Go"
Then I see the thumbnail "test.jpg" on the image gallery

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
Expand Down
4 changes: 4 additions & 0 deletions lib/bootsy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 33 additions & 35 deletions lib/bootsy/container.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/bootsy/templates/bootsy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions spec/dummy/public/test.fake
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This file is used for testing invalid image uploads.

0 comments on commit 9ab3789

Please sign in to comment.