Created
March 24, 2011 10:14
-
-
Save gunn/884835 to your computer and use it in GitHub Desktop.
Carrierwave support for rails_admin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
@ogennadi Okay, updated with exactly what I'm using now.
Thanks a lot.
have you submitted a pull request for this ?
I guess this would interest many people
@bartocc it's being integrated at railsadminteam/rails_admin#138
@gunn. Great. Let's hope it'll be fixed soon!
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
Hey there, does this gist still work for you? It worked perfectly for me with rails_admin ref 14882c71a2 but stopped working when I upgraded to ref 32aba784e71a467094e4