module Weeler
module ActionView
module Helpers
module ImageFormHelper
# Weeler action view helper method.
# It creates file upload field with info and preview for image.
#
# e.g.
#
# <%= f.image_upload_field :image, size_info: "270x294" %>
#
# It creates:
#
#
# If you use another image handler than Paperclip, you can also pass image_url_method for image preview.
#
# Also with remove_image action in controller and route for that,
# it removes only image from object.
def image_upload_field(name, options = {})
options = options.reverse_merge({size_info: "200x80", image_url_method: "#{name.to_s}.url(\"original\")"})
self.multipart = true
buffer = @template.label(@object_name, name, :class => "col-lg-2 col-md-2 control-label")
buffer += image_upload_field_file_block name, options[:size_info]
buffer += image_upload_field_preview_block name, options[:image_url_method] if File.exist?(@object.instance_eval(options[:image_url_method]))
buffer
end
private
def image_upload_field_file_block name, size_info
@template.content_tag :div, :class => "col-lg-5 col-md-5" do
sub_buffer = @template.content_tag :div, :class => "row" do
@template.content_tag :div, :class => "col-lg-12 col-md-12" do
@template.file_field @object_name, name, :class => 'form-control'
end
end
sub_buffer += @template.content_tag :div, :class => "row" do
@template.content_tag :div, :class => "col-lg-12 col-md-12" do
"Size should be #{size_info}"
end
end
sub_buffer
end
end
def image_upload_field_preview_block name, image_url_method
@template.content_tag :div, :class => "col-lg-5 col-md-5" do
if @object.instance_eval(name.to_s).present?
sub_buffer = @template.content_tag :div, :class => "row" do
@template.content_tag :div, :class => "col-lg-12 col-md-12" do
@template.image_tag @object.instance_eval(image_url_method), :style => "height: 80px;"
end
end
build_route = true
begin
@template.link_to("Remove", action: "remove_image")
rescue Exception
build_route = false
end
if build_route
sub_buffer += @template.content_tag :div, :class => "row" do
@template.content_tag :div, :class => "col-lg-12 col-md-12" do
@template.link_to("Remove", :action => "remove_image")
end
end
end
sub_buffer
end
end
end
end
end
end
end