Skip to content

Commit

Permalink
Add :use_default_url options to the ImagePreviewInput class,
Browse files Browse the repository at this point in the history
if set to true and the image field is nil it will fall back to the
  default_url method of carrierwave.
  • Loading branch information
ph committed Mar 14, 2012
1 parent 30973fb commit 4a8c452
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/simple_form_fancy_uploads/image_preview_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ module SimpleFormFancyUploads
class ImagePreviewInput < SimpleForm::Inputs::FileInput
def input
version = input_html_options.delete(:preview_version)
use_default_url = options.delete(:use_default_url) || false

out = ''
if object.send("#{attribute_name}?")
if object.send("#{attribute_name}?") || use_default_url
out << template.image_tag(object.send(attribute_name).tap {|o| break o.send(version) if version}.send('url'))
end
(out << super).html_safe
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/app/models/page.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Page < ActiveRecord::Base
mount_uploader :avatar, ImageUploader
mount_uploader :image, ImageUploader
mount_uploader :attachment, AttachmentUploader
end
3 changes: 3 additions & 0 deletions spec/dummy/app/uploaders/image_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ def extension_white_list
%w(jpg jpeg png)
end

def default_url
"fancy-upload-default-url.png"
end
end
1 change: 1 addition & 0 deletions spec/dummy/app/views/pages/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<%= f.input :remove_image, as: :boolean if f.object.image? %>
<%= f.input :attachment, as: :attachment_preview %>
<%= f.input :remove_attachment, as: :boolean if f.object.attachment? %>
<%= f.input :avatar, as: :image_preview, use_default_url: true %>

<div class="actions">
<%= f.button :submit %>
Expand Down
4 changes: 4 additions & 0 deletions spec/views/pages_views_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ def stub_file(filename)
it "contains image preview" do
rendered.should have_selector 'img[alt=Rails]'
end

it "should fallback to default_url if specified" do
rendered.should have_selector 'img[alt=Fancy-upload-default-url]'
end
end # uploaded image

context "uploaded attachment" do
Expand Down

0 comments on commit 4a8c452

Please sign in to comment.