Skip to content

Commit

Permalink
force file encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
nragaz committed May 24, 2010
1 parent 63634b1 commit 8ddab9c
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/encoded_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,23 @@ def included(base)
end

module ActiveRecordClassMethods
def encode_attachment_in_xml(name, options={})
options[:send_urls] = false unless options[:send_urls]
def encode_attachment_in_xml(name, attachment_options={})
options[:send_urls] = false unless attachment_options[:send_urls]

@_attachment_handling ||= {}
@_attachment_handling[name] = {}
@_attachment_handling[name][:send_urls] = options[:send_urls]
@_attachment_handling[name][:send_urls] = attachment_options[:send_urls]

if attachment_options[:send_urls]
# TODO: handle URLs in ActiveRecord???
define_method "#{name}_url=" do |file_url|
nil
end
end

define_method "to_xml_with_encoded_#{name}" do |*args|
# you can exclude file tags by using :include_files => false
# if send_urls is set, force file encoding using :encode => true
options, block = args

options ||= {}
Expand All @@ -42,7 +50,7 @@ def encode_attachment_in_xml(name, options={})
file_options = { :type => 'file'}

if !(new_record? || frozen?) && send(name).file? \
&& !(send(:class).instance_variable_get("@_attachment_handling")[name][:send_urls])
&& (!(send(:class).instance_variable_get("@_attachment_handling")[name][:send_urls]) || options[:encode])
file_options.merge! :name => send("#{name}_file_name"), :"content-type" => send("#{name}_content_type")
options[:builder].tag!(name, file_options) { options[:builder].cdata! EncodedAttachment.encode(send(name)) }

Expand Down

0 comments on commit 8ddab9c

Please sign in to comment.