From ef41c1547dced46029b4f7292ad8613b1c9b110f Mon Sep 17 00:00:00 2001 From: Larissa Reis Date: Thu, 17 Sep 2015 19:25:45 -0300 Subject: [PATCH] Allow user to remove article or profile image --- app/controllers/my_profile/cms_controller.rb | 1 - app/helpers/application_helper.rb | 4 ++-- app/models/image.rb | 3 ++- app/views/cms/_blog.html.erb | 14 ++++++-------- app/views/shared/_change_image.html.erb | 2 +- app/views/shared/_show_thumbnail.html.erb | 33 +++++++++++++++++++++------------ lib/acts_as_having_image.rb | 3 +++ public/stylesheets/application.scss | 7 +++++++ test/functional/cms_controller_test.rb | 15 +++++++++++++-- 9 files changed, 55 insertions(+), 27 deletions(-) diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 27eefb4..8ad6665 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -93,7 +93,6 @@ class CmsController < MyProfileController refuse_blocks record_coming if request.post? - @article.image = nil if params[:remove_image] == 'true' if @article.image.present? && params[:article][:image_builder] && params[:article][:image_builder][:label] @article.image.label = params[:article][:image_builder][:label] diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 6e9954b..9ebde16 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -725,10 +725,10 @@ module ApplicationHelper javascript_include_tag script if script end - def file_field_or_thumbnail(label, image, i) + def file_field_or_thumbnail(label, image, i, removable = true) display_form_field label, ( render :partial => (image && image.valid? ? 'shared/show_thumbnail' : 'shared/change_image'), - :locals => { :i => i, :image => image } + :locals => { :i => i, :image => image, :removable => removable } ) end diff --git a/app/models/image.rb b/app/models/image.rb index 5a78449..fd7651e 100644 --- a/app/models/image.rb +++ b/app/models/image.rb @@ -23,7 +23,8 @@ class Image < ActiveRecord::Base postgresql_attachment_fu - attr_accessible :uploaded_data, :label + attr_accessible :uploaded_data, :label, :remove_image + attr_accessor :remove_image def current_data File.file?(full_filename) ? File.read(full_filename) : nil diff --git a/app/views/cms/_blog.html.erb b/app/views/cms/_blog.html.erb index b2efe2c..5124ff8 100644 --- a/app/views/cms/_blog.html.erb +++ b/app/views/cms/_blog.html.erb @@ -55,14 +55,12 @@ <%= labelled_form_field(_('Description:'), text_area(:article, :body, :rows => 10, :class => 'mceEditor')) %> -<%= f.fields_for :image_builder, @article.image do |i| %> - <%= file_field_or_thumbnail(_('Cover image:'), @article.image, i)%> - <%= _("Max size: %s (.jpg, .gif, .png)")% Image.max_size.to_humanreadable %> -<% end %> - -<% unless @article.image.nil? %> - <%= labelled_check_box(_('Remove cover image'),'remove_image',true,false)%> -<% end %> +
+ <%= f.fields_for :image_builder, @article.image do |i| %> + <%= file_field_or_thumbnail(_('Cover image:'), @article.image, i)%> + <%= _("Max size: %s (.jpg, .gif, .png)")% Image.max_size.to_humanreadable %> + <% end %> +
<%= labelled_form_field(_('How to display posts:'), f.select(:visualization_format, [ [ _('Full post'), 'full'], diff --git a/app/views/shared/_change_image.html.erb b/app/views/shared/_change_image.html.erb index 07dd742..97de2ed 100644 --- a/app/views/shared/_change_image.html.erb +++ b/app/views/shared/_change_image.html.erb @@ -1,3 +1,3 @@ <%= i.file_field( :uploaded_data, { :onchange => 'updateImg(this.value)' } ) %> <%= labelled_form_field(_("Image Label:"), i.text_field(:label)) %> -<%= button_to_function(:cancel,_('Cancel'),"jQuery('#change-image-link').show(); jQuery('#change-image').html('')", :id => 'cancel-change-image-link', :style => 'display: none')%> +<%= button_to_function(:cancel,_('Cancel'),"jQuery('#change-image-link').show(); jQuery('#change-image').hide()", :id => 'cancel-change-image-link', :style => 'display: none')%> diff --git a/app/views/shared/_show_thumbnail.html.erb b/app/views/shared/_show_thumbnail.html.erb index b69ca84..a583c74 100644 --- a/app/views/shared/_show_thumbnail.html.erb +++ b/app/views/shared/_show_thumbnail.html.erb @@ -1,16 +1,25 @@ - <%= image_tag(image.public_filename(:thumb)) %> +<%= image_tag(image.public_filename(:thumb)) %> -
+
- <%= button_to_function(:photos, _('Change image'), 'display_change_image()', :id => 'change-image-link' ) %> +<%= button_to_function(:photos, _('Change image'), 'display_change_image()', :id => 'change-image-link' ) %> - + -

+
+ +
+ +<% if image.present? && removable %> +
+ <%= labelled_form_field(_('Remove image'), i.check_box(:remove_image, {}, true, false))%> +
+<% end %> diff --git a/lib/acts_as_having_image.rb b/lib/acts_as_having_image.rb index 7faa4d7..5830e02 100644 --- a/lib/acts_as_having_image.rb +++ b/lib/acts_as_having_image.rb @@ -16,6 +16,9 @@ module ActsAsHavingImage else build_image(img) end unless img[:uploaded_data].blank? + if img[:remove_image] == 'true' + self.image_id = nil + end end end diff --git a/public/stylesheets/application.scss b/public/stylesheets/application.scss index 629d421..23e11d9 100644 --- a/public/stylesheets/application.scss +++ b/public/stylesheets/application.scss @@ -2744,9 +2744,16 @@ div#activation_enterprise label, div#activation_enterprise input, div#activation .type-img br { clear: left; } +#blog-image-builder > .formfieldline > .formfield > .formfieldline > .formfield > .formfieldline > label, +#blog-image-builder > .formfieldline > label, +#blog-image-builder #change-image > .formfieldline > label, +#blog-image-builder #image-builder-remove-checkbox > .formfieldline > label, #profile_change_picture label { display: none; } +#profile_change_picture #image-builder-remove-checkbox > .formfieldline > label { + display: block; +} #profile_change_picture img { margin-left: 10px; } diff --git a/test/functional/cms_controller_test.rb b/test/functional/cms_controller_test.rb index 64bec38..501af9a 100644 --- a/test/functional/cms_controller_test.rb +++ b/test/functional/cms_controller_test.rb @@ -962,15 +962,26 @@ class CmsControllerTest < ActionController::TestCase assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/edit/#{profile.blog.feed.id}" } end - should 'remove the image of an article' do + should 'remove the image of a blog' do blog = create(Blog, :profile_id => profile.id, :name=>'testblog', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) blog.save! - post :edit, :profile => profile.identifier, :id => blog.id, :remove_image => 'true' + post :edit, :profile => profile.identifier, :id => blog.id, :article => {:image_builder => { :remove_image => 'true'}} blog.reload assert_nil blog.image end + should 'remove the image of an article' do + image = fast_create(Image, :content_type => 'image/png', :filename => 'event-image.png', :label => 'test_label', :size => 1014) + article = fast_create(Article, :profile_id => profile.id, :name => 'test_label_article', :body => 'test_content') + article.image = image + article.save + post :edit, :profile => profile.identifier, :id => article.id, :article => {:image_builder => { :remove_image => 'true'}} + article.reload + + assert_nil article.image + end + should 'update feed options by edit blog form' do profile.articles << Blog.new(:name => 'Blog for test', :profile => profile) post :edit, :profile => profile.identifier, :id => profile.blog.id, :article => { :feed => { :limit => 7 } } -- libgit2 0.21.2