Commit 3362240c993ec95a9daf6bc83dd36b5b712f07ed
Committed by
Antonio Terceiro
1 parent
860492a3
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. (cherry picked from commit ef41c1547dced46029b4f7292ad8613b1c9b110f)
Showing
9 changed files
with
55 additions
and
27 deletions
Show diff stats
app/controllers/my_profile/cms_controller.rb
... | ... | @@ -100,7 +100,6 @@ class CmsController < MyProfileController |
100 | 100 | refuse_blocks |
101 | 101 | record_coming |
102 | 102 | if request.post? |
103 | - @article.image = nil if params[:remove_image] == 'true' | |
104 | 103 | if @article.image.present? && params[:article][:image_builder] && |
105 | 104 | params[:article][:image_builder][:label] |
106 | 105 | @article.image.label = params[:article][:image_builder][:label] | ... | ... |
app/helpers/application_helper.rb
... | ... | @@ -725,10 +725,10 @@ module ApplicationHelper |
725 | 725 | javascript_include_tag script if script |
726 | 726 | end |
727 | 727 | |
728 | - def file_field_or_thumbnail(label, image, i) | |
728 | + def file_field_or_thumbnail(label, image, i, removable = true) | |
729 | 729 | display_form_field label, ( |
730 | 730 | render :partial => (image && image.valid? ? 'shared/show_thumbnail' : 'shared/change_image'), |
731 | - :locals => { :i => i, :image => image } | |
731 | + :locals => { :i => i, :image => image, :removable => removable } | |
732 | 732 | ) |
733 | 733 | end |
734 | 734 | ... | ... |
app/models/image.rb
... | ... | @@ -23,7 +23,8 @@ class Image < ActiveRecord::Base |
23 | 23 | |
24 | 24 | postgresql_attachment_fu |
25 | 25 | |
26 | - attr_accessible :uploaded_data, :label | |
26 | + attr_accessible :uploaded_data, :label, :remove_image | |
27 | + attr_accessor :remove_image | |
27 | 28 | |
28 | 29 | def current_data |
29 | 30 | File.file?(full_filename) ? File.read(full_filename) : nil | ... | ... |
app/views/cms/_blog.html.erb
... | ... | @@ -55,14 +55,12 @@ |
55 | 55 | |
56 | 56 | <%= labelled_form_field(_('Description:'), text_area(:article, :body, :rows => 10)) %> |
57 | 57 | |
58 | -<%= f.fields_for :image_builder, @article.image do |i| %> | |
59 | - <%= file_field_or_thumbnail(_('Cover image:'), @article.image, i)%> | |
60 | - <%= _("Max size: %s (.jpg, .gif, .png)")% Image.max_size.to_humanreadable %> | |
61 | -<% end %> | |
62 | - | |
63 | -<% unless @article.image.nil? %> | |
64 | - <%= labelled_check_box(_('Remove cover image'),'remove_image',true,false)%> | |
65 | -<% end %> | |
58 | +<div id="blog-image-builder"> | |
59 | + <%= f.fields_for :image_builder, @article.image do |i| %> | |
60 | + <%= file_field_or_thumbnail(_('Cover image:'), @article.image, i)%> | |
61 | + <%= _("Max size: %s (.jpg, .gif, .png)")% Image.max_size.to_humanreadable %> | |
62 | + <% end %> | |
63 | +</div> | |
66 | 64 | |
67 | 65 | <%= labelled_form_field(_('How to display posts:'), f.select(:visualization_format, [ |
68 | 66 | [ _('Full post'), 'full'], | ... | ... |
app/views/shared/_change_image.html.erb
1 | 1 | <%= i.file_field( :uploaded_data, { :onchange => 'updateImg(this.value)' } ) %> |
2 | 2 | <%= labelled_form_field(_("Image Label:"), i.text_field(:label)) %> |
3 | -<%= button_to_function(:cancel,_('Cancel'),"jQuery('#change-image-link').show(); jQuery('#change-image').html('')", :id => 'cancel-change-image-link', :style => 'display: none')%> | |
3 | +<%= 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
1 | - <%= image_tag(image.public_filename(:thumb)) %> | |
1 | +<%= image_tag(image.public_filename(:thumb)) %> | |
2 | 2 | |
3 | - <br/> | |
3 | +<br/> | |
4 | 4 | |
5 | - <%= button_to_function(:photos, _('Change image'), 'display_change_image()', :id => 'change-image-link' ) %> | |
5 | +<%= button_to_function(:photos, _('Change image'), 'display_change_image()', :id => 'change-image-link' ) %> | |
6 | 6 | |
7 | - <script> | |
8 | - function display_change_image() { | |
9 | - var content = "<%= j(render :partial => 'shared/change_image', :locals => { :i => i, :image => image }) %>"; | |
10 | - jQuery('#change-image').html(content); | |
11 | - jQuery('#change-image-link').hide(); | |
12 | - jQuery('#cancel-change-image-link').show(); | |
13 | - } | |
14 | - </script> | |
7 | +<script> | |
8 | + function display_change_image() { | |
9 | + jQuery('#change-image').show(); | |
10 | + jQuery('#change-image-link').hide(); | |
11 | + jQuery('#cancel-change-image-link').show(); | |
12 | + } | |
13 | +</script> | |
15 | 14 | |
16 | - <div id='change-image'> </div> <br/> | |
15 | +<br/> | |
16 | +<div id='change-image' style='display:none'> | |
17 | + <%= render :partial => 'shared/change_image', :locals => { :i => i, :image => image } %> | |
18 | +</div> | |
19 | +<br/> | |
20 | + | |
21 | +<% if image.present? && removable %> | |
22 | + <div id='image-builder-remove-checkbox'> | |
23 | + <%= labelled_form_field(_('Remove image'), i.check_box(:remove_image, {}, true, false))%> | |
24 | + </div> | |
25 | +<% end %> | ... | ... |
lib/acts_as_having_image.rb
public/stylesheets/application.css
... | ... | @@ -2745,9 +2745,16 @@ div#activation_enterprise label, div#activation_enterprise input, div#activation |
2745 | 2745 | .type-img br { |
2746 | 2746 | clear: left; |
2747 | 2747 | } |
2748 | +#blog-image-builder > .formfieldline > .formfield > .formfieldline > .formfield > .formfieldline > label, | |
2749 | +#blog-image-builder > .formfieldline > label, | |
2750 | +#blog-image-builder #change-image > .formfieldline > label, | |
2751 | +#blog-image-builder #image-builder-remove-checkbox > .formfieldline > label, | |
2748 | 2752 | #profile_change_picture label { |
2749 | 2753 | display: none; |
2750 | 2754 | } |
2755 | +#profile_change_picture #image-builder-remove-checkbox > .formfieldline > label { | |
2756 | + display: block; | |
2757 | +} | |
2751 | 2758 | #profile_change_picture img { |
2752 | 2759 | margin-left: 10px; |
2753 | 2760 | } | ... | ... |
test/functional/cms_controller_test.rb
... | ... | @@ -953,15 +953,26 @@ class CmsControllerTest < ActionController::TestCase |
953 | 953 | assert_no_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/cms/edit/#{profile.blog.feed.id}" } |
954 | 954 | end |
955 | 955 | |
956 | - should 'remove the image of an article' do | |
956 | + should 'remove the image of a blog' do | |
957 | 957 | blog = create(Blog, :profile_id => profile.id, :name=>'testblog', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}) |
958 | 958 | blog.save! |
959 | - post :edit, :profile => profile.identifier, :id => blog.id, :remove_image => 'true' | |
959 | + post :edit, :profile => profile.identifier, :id => blog.id, :article => {:image_builder => { :remove_image => 'true'}} | |
960 | 960 | blog.reload |
961 | 961 | |
962 | 962 | assert_nil blog.image |
963 | 963 | end |
964 | 964 | |
965 | + should 'remove the image of an article' do | |
966 | + image = fast_create(Image, :content_type => 'image/png', :filename => 'event-image.png', :label => 'test_label', :size => 1014) | |
967 | + article = fast_create(Article, :profile_id => profile.id, :name => 'test_label_article', :body => 'test_content') | |
968 | + article.image = image | |
969 | + article.save | |
970 | + post :edit, :profile => profile.identifier, :id => article.id, :article => {:image_builder => { :remove_image => 'true'}} | |
971 | + article.reload | |
972 | + | |
973 | + assert_nil article.image | |
974 | + end | |
975 | + | |
965 | 976 | should 'update feed options by edit blog form' do |
966 | 977 | profile.articles << Blog.new(:name => 'Blog for test', :profile => profile) |
967 | 978 | post :edit, :profile => profile.identifier, :id => profile.blog.id, :article => { :feed => { :limit => 7 } } | ... | ... |