Module: Directlytos3::FormHelpers

Defined in:
lib/directlytos3/form_helpers.rb

Instance Method Summary collapse

Instance Method Details

#s3_field_tag(name, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/directlytos3/form_helpers.rb', line 38

def s3_field_tag(name, options = {})
  Directlytos3::configure(options)
  
  url = options[:path] || "https://#{options[:bucket]}.s3.amazonaws.com/"   
  form_tag(url, :remote => options[:remote], :enctype=>"multipart/form-data",:method=>"post", :id => 's3-upload-form', :authenticity_token => false) do
    s3_hidden_fields(options)
    concat file_field_tag 'file'
  end

end

#s3_form_for(record, *args, &block) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/directlytos3/form_helpers.rb', line 52

def s3_form_for(record, *args, &block)
  raise ArgumentError, "s3_form_for: Missing block" unless block_given?

  options = args.extract_options!
  url = options[:path] || "https://#{options[:bucket]}.s3.amazonaws.com/"
  args << {:url => url, :builder => options[:builder], :enctype => "multipart/form-data", :method => "post", :html => {:id => 's3-upload-form'}, :authenticity_token => false, :remote => options[:remote]}
  Directlytos3::configure(options)
  form_for(record, *(args)) do |f|
    s3_hidden_fields(options)
    block.call(f)
    # f.file_field "file", :name => "file"
  end
end

#s3_hidden_fields(options) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/directlytos3/form_helpers.rb', line 66

def s3_hidden_fields(options)
 
  policy = Directlytos3::S3.policy(options)
  signature = Directlytos3::S3.signature(options)
  
  concat hidden_field_tag('key', "#{options[:key]}#{'/' if !options[:key].blank?}#{Directlytos3.random_string if options[:randomize]}${filename}")
  concat hidden_field_tag('AWSAccessKeyId', "#{options[:access_key]}")
  concat hidden_field_tag('acl', "#{options[:acl]}")
  concat hidden_field_tag('success_action_redirect', "#{options[:redirect]}") if options[:redirect]
  concat hidden_field_tag('success_action_status', "#{options[:status].to_s}") if options[:status]
  concat hidden_field_tag('policy', "#{policy}")
  concat hidden_field_tag('signature', "#{signature}")
  concat hidden_field_tag('Content-Type', "#{options[:content_type]}")
  concat hidden_field_tag('Cache-Control', "public,max-age=#{options[:cache_control]}") if options[:cache_control]
  options.except!(:key, :access_key, :acl, :redirect, :content_type, :secret_key, :randomize, :expiration_date, :max_filesize, :path, :status)
end