Skip to content

Commit

Permalink
Merge pull request andreapavoni#3 from ph/master
Browse files Browse the repository at this point in the history
add fallback to default_url or carrierwave's default_url if missing image in the field.
  • Loading branch information
Andrea Pavoni committed Mar 15, 2012
2 parents 30973fb + 2807048 commit 6078e45
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Simply add `gem 'simple_form_fancy_uploads'` to your `Gemfile` and run `bundle i
### Usage

Here's a basic example, as you can see, it's just a matter of specify the input as `:image_preview` or `:attachment_preview`. If using `:image_preview`, you can also specify a `:preview_version => :some_version_name` inside the `:input_html` Hash. This will let you to show a custom version generated with Carrierwave. Nice, isn't it?
The default configuration of this gem is to not use `the default_url` method of carrierwave, if you want to show the default image defined in your upload class, you can use `:use_default_url => true` in the options hash.

```
<%= simple_form_for @some_model do |f| %>
Expand Down
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
2 changes: 1 addition & 1 deletion lib/simple_form_fancy_uploads/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SimpleFormFancyUploads
VERSION = "0.0.1"
VERSION = "0.0.2"
end
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 6078e45

Please sign in to comment.