Skip to content

Instantly share code, notes, and snippets.

@oniram
Created September 8, 2012 23:35
Show Gist options
  • Save oniram/3681110 to your computer and use it in GitHub Desktop.
Save oniram/3681110 to your computer and use it in GitHub Desktop.
app/inputs/img_input.rb
#apps/views/seuform.html.erb
f.input :image_id, :as => :img, :collection => ActiveAdmin::ApplicationHelper.all_images, :label => "Imagens"
=begin
O Collection geralmente seria um map com o id e value de cada item, mas no meu exemplo, o value é a url da imagem
e coloquei um terceiro valor para o endereço da imagem original que será usado no href do thumb da imagem.
=end
#/app/helpers/active_admin/application_helper.rb
def self.all_images
Image.all.map {|image| [image.id, image.file.url(:thumb), image.file.url(:original)]}
end
class ImgInput
include Formtastic::Inputs::Base
def to_html
object_name = self.object_name
attribute_name = self.method.to_s
id = "#{object_name}[#{attribute_name}]"
html = "<li class=\"radio input optional\" id=\"#{object_name}_#{attribute_name}_input\"> <label class=\" "+
"label\" for=\"#{id}\">#{self.options[:label]}</label> <div id=\"thumb_images\" > "
self.options[:collection].each do |image|
href = "href=\"#{image[2]}\" target=\"_blank\"" unless image[2].nil?
html << "<div style=\"margin-top:#{self.options[:margin_top]}px; margin-right:5px;heigth:80px; float:left;\">"+
"<input style=\"margin-left:5px;\" type=\"radio\" name=\"#{id}\" id=\"#{id}\" value=\"#{image[0]}\"/>"+
"</div> <div style=\"float:left;\"> <label for=\"#{id}\">"+
"<a #{href} ><img class=\"img_thumb_image\" src=\"#{image[1]}\" /></a></label></div>"
end
html << "</div></li><br style=\"clear: both;\">"
html.html_safe
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment