Skip to content

Instantly share code, notes, and snippets.

@goosys
Last active August 5, 2016 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goosys/0071a19f122d89ae7028bd2f7d2a2228 to your computer and use it in GitHub Desktop.
Save goosys/0071a19f122d89ae7028bd2f7d2a2228 to your computer and use it in GitHub Desktop.
ActiveAdmin+Carrierwave
/**
* FileInput < Formtastic::Inputs::FileInput
*/
.attachment_wrap {
@extend p.inline-hints;
font-style: default;
display: block;
&.attachment_img img {
height: 80px;
}
&.attachment_plain {
}
[name*="[remove_"] {
display: inline-block;
margin-left: 5px;
}
}
#
# https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/view_helpers/display_helper.rb
#
module ActiveAdmin
module ViewHelpers
module DisplayHelper
def pretty_format_with_pretty_other_format(object)
pretty_uploader_format(object) ||
pretty_jsonb_format(object) ||
pretty_format_without_pretty_other_format(object)
end
alias_method_chain :pretty_format, :pretty_other_format
def pretty_uploader_format(object)
if object.class.ancestors.include?(CarrierWave::Uploader::Base)
if object.present?
if object.version_exists?(:thumb) && object.thumb.file.exists?
content_tag(:p,
image_tag(object.url(:thumb)),
{class: 'attachment_wrap attachment_img'}
)
else
link_to object.file.identifier, object.url
end
end
end
end
def pretty_jsonb_format(object)
if object.class.ancestors.include?(Hash)
content_tag(:pre,
JSON.pretty_generate(object).gsub(":", " =>")
)
end
end
end
end
end
class FileInput < Formtastic::Inputs::FileInput
def image_html_options
{:class => 'attachment_wrap attachment_img'}.merge(options[:image_html] || {})
end
def image_plain_html_options
{:class => 'attachment_wrap attachment_plain'}.merge(options[:image_html] || {})
end
def to_html
input_wrapping do
label_html <<
builder.file_field(method, input_html_options) <<
image_html
end
end
protected
def image_html
return "".html_safe if builder.object.new_record?
case options[:image]
when Symbol
builder.object.send(options[:image])
when Proc
options[:image].call(builder.object)
when String
options[:image].to_s
else
att = builder.object.public_send(method)
if att.present?
if att.version_exists?(:thumb) && att.thumb.file.exists?
builder.template.content_tag :div, nil, image_html_options, true do
builder.template.image_tag(att.url(:thumb) ) +
builder.check_box("remove_#{method.to_s}".to_sym, {}, true, false)
end
else
builder.template.content_tag :div, nil, image_plain_html_options, true do
builder.template.content_tag(:span, att.url() ) +
builder.check_box("remove_#{method.to_s}".to_sym, {}, true, false)
end
end
else
"".html_safe
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment