Skip to content

Instantly share code, notes, and snippets.

@gunn
Created March 24, 2011 10:14
Show Gist options
  • Save gunn/884835 to your computer and use it in GitHub Desktop.
Save gunn/884835 to your computer and use it in GitHub Desktop.
Carrierwave support for rails_admin
= label_tag "#{field.abstract_model.to_param}_#{field.name}", field.label
.input
- if field.bindings[:object].send("#{field.name}_url")
.row
= link_to field.bindings[:object].send("#{field.name}_url")
%br
= form.check_box "remove_#{field.name}"
= form.label "remove_#{field.name}", "Remove existing #{field.label.downcase}", class: "inline"
.row
= form.file_field field.name, class: "fileUploadField #{field.has_errors? ? "errorField" : nil}"
- if field.has_errors?
%span.errorMessage(style="margin-left:3px") #{field.label } #{field.errors.first}
= form.hidden_field "#{field.name}_cache"
= label_tag "#{field.abstract_model.to_param}_#{field.name}", field.label
.input{ style: "padding-left:100px; padding-top:15px; padding-bottom:15px" }
- image = field.bindings[:object].send(field.name)
- if image.path # the most direct check of an assets existence I could see
.row
-# load a default 'version' if it exists. should really be set through rails_admin's DSL:
- default_version = image.versions[:main]
= image_tag default_version && default_version.url || image.url
%br
= form.check_box "remove_#{field.name}"
= form.label "remove_#{field.name}", "Remove existing #{field.label.downcase}", class: "inline"
.row
= form.file_field field.name, class: "fileUploadField #{field.has_errors? ? "errorField" : nil}"
- if field.has_errors?
%span.errorMessage(style="margin-left:3px") #{field.label } #{field.errors.first}
= form.hidden_field "#{field.name}_cache"
# Register a custom field factory and field type for CarrierWave if its defined
if defined?(::CarrierWave)
module RailsAdmin::Config::Fields::Types
# Field type that supports CarrierWave file uploads
class CarrierWaveFile < RailsAdmin::Config::Fields::Types::FileUpload
register_instance_option(:partial) do
:form_carrier_wave_file
end
register_instance_option(:formatted_value) do
# get the value stored in the db e.g. "smiley.png" instead of the files entire path
# note we need to call `name.to_s` because attributes takes a string and name returns a symbol.
bindings[:object].attributes[name.to_s]
end
end
# Field type that supports CarrierWave file uploads with image preview
class CarrierWaveImage < CarrierWaveFile
register_instance_option(:partial) do
:form_carrier_wave_image
end
end
# Register field type to the types registry
register(:carrier_wave_file, CarrierWaveFile)
register(:carrier_wave_image, CarrierWaveImage)
end
RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
model = parent.abstract_model.model
if model.kind_of?(CarrierWave::Mount) && model.uploaders.include?(properties[:name])
type = properties[:name] =~ /image|picture|thumb/ ? :carrier_wave_image : :carrier_wave_file
fields << RailsAdmin::Config::Fields::Types.load(type).new(parent, properties[:name], properties)
true
else
false
end
end
end
@bartocc
Copy link

bartocc commented Aug 22, 2011

have you submitted a pull request for this ?
I guess this would interest many people

@gunn
Copy link
Author

gunn commented Aug 22, 2011

@bartocc it's being integrated at railsadminteam/rails_admin#138

@bartocc
Copy link

bartocc commented Aug 23, 2011

@gunn. Great. Let's hope it'll be fixed soon!

@rafaelcgo
Copy link

Any temporary solutions to the newest commit of rails admin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment