module Inputs class AssetBox delegate :content_tag, :render, :to => :@template def initialize(object, object_name, template, method, opts) @object = object @object_name = object_name @template = template @options = initialize_options(method, opts) end def to_html output = '' output += header_html if @options[:uploader] == :top output += uploader_html output += attachments_html output += dialog_html if @options[:dialog] else output += attachments_html output += dialog_html if @options[:dialog] output += uploader_html if @options[:uploader] end output += footer_html output.html_safe end private def header_html "
".html_safe end def attachments @object.attachments.select { |attachment| attachment.box == @options[:box] } end def initialize_options(method, opts) { :uploader => true, # :top, :bottom, true or false :uploader_drop_files => false, :drop_files_help_text => 'Drop files here', :progress_bar_partial => 'asset_box_input/progress_bar_template', :attachment_style => :thumbnail, # :thumbnail, :table, or :list :attachment_links => true, :attachment_add_to => :bottom, # :bottom or :top (of attachments div) :attachment_actions => [:remove], # or :insert, :delete, :remove :table_filter_bar => false, :dialog => false, :dialog_url => @template.effective_assets.effective_assets_path, :disabled => false, :file_types => [:any], :btn_label => "Upload..." }.merge(opts).tap do |options| options[:method] = method.to_s options[:box] = method.to_s.pluralize options[:attachable_id] ||= (@object.try(:id) rescue nil) options[:attachable_type] ||= @object.class.name.titleize.gsub(" ", "_").gsub('/', '_').downcase # The logic for the AWS ACL is such that: # 1.) If the :private or :aws_acl keys are set on the asset_box input, use those values # 2.) If the :private or :public keys are set on the acts_as_asset_box declaration, use those values # 3.) Fall back to default EffectiveAssets.aws_acl as per config file uploader_private = (opts[:private] == true || opts[:public] == false || opts[:aws_acl] == EffectiveAssets::AWS_PRIVATE) uploader_public = (opts[:private] == false || opts[:public] == true || opts[:aws_acl] == EffectiveAssets::AWS_PUBLIC) object_private = ((@object.asset_boxes[method] == :private || @object.asset_boxes[method].first[:private] == true || @object.asset_boxes[method].first[:public] == false) rescue false) object_public = ((@object.asset_boxes[method] == :public || @object.asset_boxes[method].first[:public] == true || @object.asset_boxes[method].first[:private] == false) rescue false) if uploader_private options[:aws_acl] = EffectiveAssets::AWS_PRIVATE elsif uploader_public options[:aws_acl] = EffectiveAssets::AWS_PUBLIC elsif object_private options[:aws_acl] = EffectiveAssets::AWS_PRIVATE elsif object_public options[:aws_acl] = EffectiveAssets::AWS_PUBLIC else options[:aws_acl] = EffectiveAssets.aws_acl end options[:uid] = "#{options[:attachable_type]}-#{options[:attachable_id]}-#{options[:method]}-#{Time.zone.now.to_f}".parameterize options[:limit] = (options[:method] == options[:box] ? (options[:limit] || 10000) : 1) end end end end