Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

gemified

  • Loading branch information...
commit f51a55462e9be513c14aa784f99628478f49b6df 1 parent fa407e6
@nragaz authored
View
2  Gemfile
@@ -1,5 +1,7 @@
source :gemcutter
+gem "encoded_attachment", :path => File.expand_path("..", __FILE__)
+
gem "mime-types", :require => 'mime/types'
gem "sqlite3-ruby", :require => 'sqlite3'
View
90 Gemfile.lock
@@ -1,90 +0,0 @@
----
-dependencies:
- activeresource:
- group:
- - :default
- version: ">= 0"
- require:
- - active_resource
- activerecord:
- group:
- - :default
- version: ">= 0"
- require:
- - active_record
- sqlite3-ruby:
- group:
- - :default
- version: ">= 0"
- require:
- - sqlite3
- mime-types:
- group:
- - :default
- version: ">= 0"
- require:
- - mime/types
- paperclip:
- group:
- - :default
- version: ">= 0"
- activesupport:
- group:
- - :default
- version: ">= 0"
- require:
- - active_support
-specs:
-- builder:
- version: 2.1.2
-- i18n:
- version: 0.4.0.beta1
-- memcache-client:
- version: 1.8.3
-- tzinfo:
- version: 0.3.20
-- activesupport:
- version: 3.0.0.beta3
- source: 1
-- activemodel:
- version: 3.0.0.beta3
- source: 1
-- arel:
- version: 0.3.3
-- activerecord:
- version: 3.0.0.beta3
- source: 3
-- activeresource:
- version: 3.0.0.beta3
- source: 2
-- mime-types:
- version: "1.16"
-- paperclip:
- version: 2.3.2.beta1
- source: 0
-- sqlite3-ruby:
- version: 1.2.5
-hash: 12360ada2663b2e66de9628f1528ae42db541e64
-sources:
-- Git:
- uri: git://github.com/thoughtbot/paperclip
- branch: rails3
- git: git://github.com/thoughtbot/paperclip
- ref: f0b5ac3cfcd03aac31f4c1889569f33356aa5d04
-- Git:
- uri: git://github.com/rails/rails
- require: active_support
- git: git://github.com/rails/rails
- ref: e3549a80549326049222c0916926a906d2638490
-- Git:
- uri: git://github.com/rails/rails
- require: active_resource
- git: git://github.com/rails/rails
- ref: e3549a80549326049222c0916926a906d2638490
-- Git:
- uri: git://github.com/rails/rails
- require: active_record
- git: git://github.com/rails/rails
- ref: e3549a80549326049222c0916926a906d2638490
-- Rubygems:
- uri: http://gemcutter.org
View
0  MIT-LICENSE → LICENSE
File renamed without changes
View
32 README.textile → README.md
@@ -1,8 +1,13 @@
-h1. EncodedAttachment
+EncodedAttachment
+=================
-This is the bestest and certainly the easiest way to handle file uploads/downloads to Paperclip-using Active Record-backed resources using Active Resource. Rather than trying to create a multipart form submission, it just embeds the file's binary data in the Active Record model's <tt>to_xml</tt>. You can also embed binary data into XML to POST or PUT using Active Resource. These tags will automatically be parsed by Active Record and Active Resource to create files.
+This is the bestest and certainly the easiest way to handle file uploads/downloads to Paperclip-using Active Record-backed resources using Active Resource.
-h2. Active Record
+Rather than trying to create a multipart form submission, it just embeds the file's binary data in the Active Record model's <tt>to_xml</tt>. You can also embed binary data into XML to POST or PUT using Active Resource. These tags will automatically be parsed by Active Record and Active Resource to create files.
+
+
+Active Record
+-------------
Adds a class method called <tt>encode_attachment_in_xml</tt> to Active Record that can be used alongside Paperclip's <tt>has_attached_file</tt> to automatically generate useful and usable binary XML tags for the attachment's original file by wrapping <tt>to_xml</tt>. These tags will not be generated on new or destroyed records (because the Paperclip file needs to be saved to disk before it is encoded).
@@ -13,20 +18,20 @@ Note that by default, the XML will *not* include Paperclip attributes such as <t
The Active Record methods will only be included if both Active Record and Paperclip are loaded.
-h2. Active Resource
+Active Resource
+---------------
Adds a class method called <tt>has_encoded_attachment</tt> to Active Resource that generates a schema for the file's attributes and then embeds the file's binary content in <tt>to_xml</tt> if the file has been changed or the record is new.
You can force embedding using <tt>to_xml(:include_attachments => true)</tt>. File setters include <tt>file=</tt> and <tt>file_path=</tt>. MIME types are detected based on the file name.
-h2. Downloading Files using URLs
+Downloading Files using URLs
+----------------------------
To avoid transmitting huge XML files (particularly in index actions), you can choose to have Active Record send the URL of the file instead of the encoded path using:
-<pre>
encode_attachment_in_xml :attachment_name, :send_urls => true, :root_url => "http://yourdomain"
-</pre>
<tt>:root_url</tt> is optional.
@@ -40,7 +45,6 @@ There is no support for Active Resource submitting a file URL back to Active Rec
Potential use cases include:
-<pre>
class MyModel < ActiveRecord::Base
encode_attachment_in_xml :my_file, :send_urls => true, :root_url => "http://yourdomain"
end
@@ -54,14 +58,14 @@ Potential use cases include:
MyModel.find(params[:id]).to_xml(:encode_attachments => true) # sends encoded files
end
end
-</pre>
-h2. Example
+Example
+=======
In Active Record:
-<pre>
+
class MyModel < ActiveRecord::Base
has_attached_file :pdf
encode_attachment_in_xml :pdf
@@ -69,11 +73,11 @@ In Active Record:
my_model = MyModel.create(:pdf => File.open('example.pdf'))
my_model.to_xml => '<my-model>\n<pdf type="file" name="example.pdf" content-type="application/pdf">[binary data]</pdf>\n</my-model>'
-</pre>
+
In Active Resource:
-<pre>
+
class MyModelResource < ActiveResource::Base
self.element_name = "my_model"
@@ -87,4 +91,4 @@ In Active Resource:
my_model_resource.pdf = File.open('example-downloaded.pdf')
my_model_resource.pdf_file_name # => "example_downloaded.pdf"
my_model_resource.pdf_content_type # => "application/pdf"
-</pre>
+
View
13 Rakefile
@@ -5,7 +5,7 @@ require 'rake/rdoctask'
desc 'Default: run unit tests.'
task :default => :test
-desc 'Test the encoded_attachment plugin.'
+desc 'Run unit tests'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.libs << 'test'
@@ -13,11 +13,18 @@ Rake::TestTask.new(:test) do |t|
t.verbose = true
end
-desc 'Generate documentation for the encoded_attachment plugin.'
+desc 'Generate documentation'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'EncodedAttachment'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
-end
+end
+
+desc 'Build gem'
+task :build do
+ require 'lib/encoded_attachment/version'
+ system "gem build encoded_attachment.gemspec"
+ system "gem install encoded_attachment-#{EncodedAttachment::VERSION}.gem"
+end
View
23 encoded_attachment.gemspec
@@ -0,0 +1,23 @@
+require File.expand_path("../lib/encoded_attachment/version", __FILE__)
+
+Gem::Specification.new do |s|
+ s.name = "encoded_attachment"
+ s.version = EncodedAttachment::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Nick Ragaz"]
+ s.email = ["nick.ragaz@gmail.com"]
+ s.homepage = "http://github.com/nragaz/encoded_attachment"
+ s.summary = "Handles downloading and uploading Paperclip attachments using Active Resource"
+ s.description = "Adds methods to ActiveRecord::Base and ActiveResource::Base to transmit file attachments via REST, either as encoded binary or via a separate URL"
+
+ s.required_rubygems_version = ">= 1.3.6"
+ s.rubyforge_project = "encoded_attachment"
+
+ s.add_dependency "mime-types"
+ # s.add_dependency "paperclip", "~> 2.3"
+ # s.add_dependency "activerecord", "~> 2.3"
+ # s.add_dependency "activeresource", "~> 2.3"
+
+ s.files = Dir["{lib}/**/*.rb", "LICENSE", "*.md"]
+ s.require_path = 'lib'
+end
View
9 init.rb
@@ -1,9 +0,0 @@
-require File.dirname(__FILE__) + '/lib/encoded_attachment' unless Object.const_defined?("EncodedAttachment")
-
-if Object.const_defined?("ActiveRecord") && Object.const_defined?("Paperclip")
- ActiveRecord::Base.send(:include, EncodedAttachment)
-end
-
-if Object.const_defined?("ActiveResource")
- ActiveResource::Base.send(:include, EncodedAttachment)
-end
View
23 lib/encoded_attachment.rb
@@ -4,8 +4,6 @@
require File.dirname(__FILE__) + '/activeresource/connection'
module EncodedAttachment
- VERSION = "0.1"
-
class << self
def encode(attachment, style = :original)
encode_io( File.open(attachment.path(style)) )
@@ -23,5 +21,26 @@ def included(base)
ActiveResource::Connection.send :include, ActiveResourceConnectionMethods
end
end
+
+ def setup_activerecord
+ if Object.const_defined?('Paperclip')
+ ActiveRecord::Base.send(:include, EncodedAttachment)
+ else
+ raise "Could not load EncodedAttachment::ActiveRecord because Paperclip is not available"
+ end
+ end
+
+ def setup_activeresource
+ ActiveResource::Base.send(:include, EncodedAttachment)
+ end
end
end
+
+
+if Object.const_defined?('ActiveRecord')
+ EncodedAttachment.setup_activerecord
+end
+
+if Object.const_defined?('ActiveResource')
+ EncodedAttachment.setup_activeresource
+end
View
3  lib/encoded_attachment/version.rb
@@ -0,0 +1,3 @@
+module EncodedAttachment
+ VERSION = "0.1"
+end
View
9 lib/rails/init.rb
@@ -0,0 +1,9 @@
+ActiveSupport.on_load(:active_record) do
+ ActiveSupport.on_load(:before_initialize) do
+ EncodedAttachment.setup_activerecord
+ end
+end
+
+ActiveSupport.on_load(:active_resource) do
+ EncodedAttachment.setup_activeresource
+end
View
2  test/test_helper.rb
@@ -9,8 +9,6 @@
Paperclip.configure
Paperclip::Railtie.insert
-require (File.dirname(__FILE__) + '/../init.rb')
-
class User < ActiveRecord::Base
has_attached_file :avatar,
:path => File.dirname(__FILE__) + "/avatars/:id_partition/:attachment/:style.:extension",
Please sign in to comment.
Something went wrong with that request. Please try again.