[Git][noosfero/noosfero][master] 2 commits: Allow user to remove article or profile image

Daniela Feitosa gitlab at gitlab.com
Fri Sep 25 09:02:37 BRT 2015


Daniela Feitosa pushed to branch master at Noosfero / noosfero


Commits:
ef41c154 by Larissa Reis at 2015-09-24T12:38:04Z
Allow user to remove article or profile image

Additional improvements include:

- Makes 'Remove Image' checkbox optional
- Very ugly css hack to hide duplicated labels in some forms. I couldn't
find another way to avoid the labels generated by labelled_form_for nor
could I skip adding labels inside the helper since it's supposed to be
generic.

- - - - -
ec8b416d by Daniela Feitosa at 2015-09-25T12:01:57Z
Merge branch 'remove-article-images' into 'master'

Allow user to remove main image for article or profile

Right now if you add an image for a profile or an article, you can change the image but you can't remove it. With this patch the user can remove a profile's or article's image just like the user removes a blog's cover image. To achieve that, this patch makes the removal mechanism part of the `#file_field_or_thumbnail` helper.

See merge request !665
- - - - -


9 changed files:

- app/controllers/my_profile/cms_controller.rb
- app/helpers/application_helper.rb
- app/models/image.rb
- app/views/cms/_blog.html.erb
- app/views/shared/_change_image.html.erb
- app/views/shared/_show_thumbnail.html.erb
- lib/acts_as_having_image.rb
- public/stylesheets/application.scss
- test/functional/cms_controller_test.rb


Changes:

=====================================
app/controllers/my_profile/cms_controller.rb
=====================================
--- 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]


=====================================
app/helpers/application_helper.rb
=====================================
--- 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
 


=====================================
app/models/image.rb
=====================================
--- 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


=====================================
app/views/cms/_blog.html.erb
=====================================
--- 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 %>
+<div id="blog-image-builder">
+  <%= 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 %>
+</div>
 
 <%= labelled_form_field(_('How to display posts:'), f.select(:visualization_format, [
   [ _('Full post'), 'full'],


=====================================
app/views/shared/_change_image.html.erb
=====================================
--- 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')%>


=====================================
app/views/shared/_show_thumbnail.html.erb
=====================================
--- 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)) %>
 
-  <br/>
+<br/>
 
-  <%= 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' ) %>
 
-  <script>
-    function display_change_image() {
-      var content = "<%= j(render :partial => 'shared/change_image', :locals => { :i => i, :image => image }) %>";
-      jQuery('#change-image').html(content);
-      jQuery('#change-image-link').hide();
-      jQuery('#cancel-change-image-link').show();
-    }
-  </script>
+<script>
+  function display_change_image() {
+    jQuery('#change-image').show();
+    jQuery('#change-image-link').hide();
+    jQuery('#cancel-change-image-link').show();
+  }
+</script>
 
-  <div id='change-image'> </div> <br/>
+<br/>
+<div id='change-image' style='display:none'>
+  <%= render :partial => 'shared/change_image', :locals => { :i => i, :image => image } %>
+</div>
+<br/>
+
+<% if image.present? && removable %>
+  <div id='image-builder-remove-checkbox'>
+    <%= labelled_form_field(_('Remove image'), i.check_box(:remove_image, {}, true, false))%>
+  </div>
+<% end %>


=====================================
lib/acts_as_having_image.rb
=====================================
--- 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


=====================================
public/stylesheets/application.scss
=====================================
--- 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;
 }


=====================================
test/functional/cms_controller_test.rb
=====================================
--- 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 } }



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/3d2cad406f7edc9718cbe703efc2343f18ff88a8...ec8b416d372f2b75e8916e28e76f187221621123
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150925/203ac40f/attachment-0001.html>


More information about the Noosfero-dev mailing list