Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 93f8065c5a5f653e5e74a663eb32261e9e485a85 @dim dim committed Jun 17, 2011
Showing with 263 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +12 −0 Gemfile
  3. +50 −0 Gemfile.lock
  4. +21 −0 LICENSE
  5. +27 −0 README.rdoc
  6. +9 −0 Rakefile
  7. +1 −0 lib/paperclip-remote.rb
  8. +44 −0 lib/paperclip/remote.rb
  9. +1 −0 lib/paperclip_remote.rb
  10. +20 −0 paperclip-remote.gemspec
  11. +1 −0 rails/init.rb
  12. +34 −0 spec/paperclip/remote_spec.rb
  13. +41 −0 spec/spec_helper.rb
2 .gitignore
@@ -0,0 +1,2 @@
+.bundle
+*.gem
12 Gemfile
@@ -0,0 +1,12 @@
+source 'http://rubygems.org'
+
+gemspec
+
+group :development, :test do
+ gem 'rake'
+end
+
+group :test do
+ gem 'rspec'
+ gem 'webmock'
+end
50 Gemfile.lock
@@ -0,0 +1,50 @@
+PATH
+ remote: .
+ specs:
+ paperclip-remote (0.1.0)
+ paperclip (~> 2.3.0)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activemodel (3.0.9)
+ activesupport (= 3.0.9)
+ builder (~> 2.1.2)
+ i18n (~> 0.5.0)
+ activerecord (3.0.9)
+ activemodel (= 3.0.9)
+ activesupport (= 3.0.9)
+ arel (~> 2.0.10)
+ tzinfo (~> 0.3.23)
+ activesupport (3.0.9)
+ addressable (2.2.6)
+ arel (2.0.10)
+ builder (2.1.2)
+ crack (0.1.8)
+ diff-lcs (1.1.2)
+ i18n (0.5.0)
+ paperclip (2.3.11)
+ activerecord (>= 2.3.0)
+ activesupport (>= 2.3.2)
+ rake (0.9.2)
+ rspec (2.6.0)
+ rspec-core (~> 2.6.0)
+ rspec-expectations (~> 2.6.0)
+ rspec-mocks (~> 2.6.0)
+ rspec-core (2.6.4)
+ rspec-expectations (2.6.0)
+ diff-lcs (~> 1.1.2)
+ rspec-mocks (2.6.0)
+ tzinfo (0.3.28)
+ webmock (1.6.4)
+ addressable (> 2.2.5, ~> 2.2)
+ crack (>= 0.1.7)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ paperclip-remote!
+ rake
+ rspec
+ webmock
21 LICENSE
@@ -0,0 +1,21 @@
+
+ Copyright (C) 2011 Dimitrij Denissenko
+
+ Permission is hereby granted, free of charge, to any person obtaining
+ a copy of this software and associated documentation files (the
+ "Software"), to deal in the Software without restriction, including
+ without limitation the rights to use, copy, modify, merge, publish,
+ distribute, sublicense, and/or sell copies of the Software, and to
+ permit persons to whom the Software is furnished to do so, subject to
+ the following conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 README.rdoc
@@ -0,0 +1,27 @@
+= Paperclip remote URL plugin
+
+Allows fetching files/attachments from remote locations.
+
+== Installation
+
+Just add <tt>gem paperclip-remote</tt> to your Gemfile.
+
+Alternatively, run <tt>sudo gem install paperclip-remote</tt> and add
+<tt>require 'paperclip_remote'</tt> to your app.
+
+== Example
+
+In the model, you can specify:
+
+ class User < ActiveRecord::Base
+ has_attached_file :photo, :remote => true
+ attr_accessible :photo, :photo_remote_url
+ end
+
+In the form you can then have:
+
+ <%= f.label :photo, "Please update a photo" %>
+ <%= f.file_field :photo %>
+ <br/>
+ <%= f.label :photo_remote_url, "or specify a URL" %>
+ <%= f.text_field :photo_remote_url %>
9 Rakefile
@@ -0,0 +1,9 @@
+require 'rake'
+require 'rspec/mocks/version'
+require 'rspec/core/rake_task'
+
+include Rake::DSL
+RSpec::Core::RakeTask.new(:spec)
+
+desc 'Default: run specs.'
+task :default => :spec
1 lib/paperclip-remote.rb
@@ -0,0 +1 @@
+require 'paperclip_remote'
44 lib/paperclip/remote.rb
@@ -0,0 +1,44 @@
+require 'paperclip'
+require 'open-uri'
+
+# Allows fetching files from remote locations. Example:
+#
+# class User < ActiveRecord::Base
+# has_attached_file :photo, :remote => true
+# attr_accessible :photo, :photo_remote_url
+# end
+#
+# In the form you can then have:
+#
+# <%= f.label :photo, "Please update a photo" %>
+# <%= f.file_field :photo %>
+# <br/>
+# <%= f.label :photo_remote_url, "or specify a URL" %>
+# <%= f.text_field :photo_remote_url %>
+#
+module Paperclip::Remote
+
+ def has_attached_file_with_remote(name, options = {})
+ has_attached_file_without_remote(name, options)
+
+ if attachment_definitions[name][:remote]
+ attr_accessor :"#{name}_remote_url"
+
+ before_validation do |record|
+ url = record.send(:"#{name}_remote_url")
+ if url.present? && (upload = open(URI.parse(url)) rescue nil)
+ upload.original_filename = File.basename(upload.base_uri.path)
+ send :"#{name}=", upload
+ end
+ end
+ end
+ end
+
+end
+
+module Paperclip::ClassMethods # :nodoc:
+ include Paperclip::Remote
+
+ alias_method :has_attached_file_without_remote, :has_attached_file
+ alias_method :has_attached_file, :has_attached_file_with_remote
+end
1 lib/paperclip_remote.rb
@@ -0,0 +1 @@
+require 'paperclip/remote'
20 paperclip-remote.gemspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+Gem::Specification.new do |s|
+ s.platform = Gem::Platform::RUBY
+ s.required_ruby_version = '>= 1.8.7'
+ s.required_rubygems_version = ">= 1.3.6"
+
+ s.name = "paperclip-remote"
+ s.summary = "Plugin for Paperclip"
+ s.description = "Allows to fetch attachments from remote locations"
+ s.version = '0.1.0'
+
+ s.authors = ["Dimitrij Denissenko"]
+ s.email = "dimitrij@blacksquaremedia.com"
+ s.homepage = "https://github.com/bsm/paperclip-remote"
+
+ s.require_path = 'lib'
+ s.files = Dir['LICENSE', 'README.rdoc', 'lib/**/*', 'rails/**/*']
+
+ s.add_dependency "paperclip", "~> 2.3.0"
+end
1 rails/init.rb
@@ -0,0 +1 @@
+require 'paperclip_remote'
34 spec/paperclip/remote_spec.rb
@@ -0,0 +1,34 @@
+require 'spec_helper'
+
+describe Paperclip::Remote do
+
+ IMAGE_URL = 'http://www.gravatar.com/avatar/file.gif'
+
+ before do
+ stub_request(:get, IMAGE_URL).
+ to_return(:status => 200, :body => "GIF89", :headers => {'Content-Type' => 'image/gif'})
+ end
+
+ it 'should have remote URL accessor' do
+ record = User.new
+ record.photo_remote_url.should be_nil
+ record.photo_remote_url = IMAGE_URL
+ record.photo_remote_url.should == IMAGE_URL
+ end
+
+ it 'should assign/overwrite images if remote URL is given' do
+ record = User.new
+ record.photo_remote_url = IMAGE_URL
+ record.tap(&:valid?).photo.should be_file
+ record.photo.original_filename.should == 'file.gif'
+ record.photo.size.should == 5
+ record.photo.content_type.should == 'image/gif'
+ end
+
+ it 'should ignore invalid inputs' do
+ record = User.new
+ record.photo_remote_url = 'SOMETHING-INVALID'
+ record.tap(&:valid?).photo.should_not be_file
+ end
+
+end
41 spec/spec_helper.rb
@@ -0,0 +1,41 @@
+ENV["RAILS_ENV"] = "test"
+$:.unshift File.dirname(__FILE__)
+$:.unshift File.expand_path('../../lib', __FILE__)
+
+require 'rubygems'
+require 'bundler'
+Bundler.setup
+
+module Rails
+
+ def self.root
+ @root ||= Pathname.new(File.dirname(__FILE__))
+ end
+
+ def self.env
+ @env ||= ActiveSupport::StringInquirer.new(ENV["RAILS_ENV"])
+ end
+
+end
+
+Bundler.require :default, :test
+
+require 'webmock/rspec'
+WebMock.disable_net_connect!
+
+require 'active_record'
+Paperclip::Railtie.insert
+
+require 'paperclip_remote'
+
+class User < ActiveRecord::Base
+ def self.columns
+ @columns ||= [
+ ActiveRecord::ConnectionAdapters::Column.new('photo_file_name', nil, 'string'),
+ ActiveRecord::ConnectionAdapters::Column.new('photo_file_size', nil, 'int'),
+ ActiveRecord::ConnectionAdapters::Column.new('photo_content_type', nil, 'string'),
+ ]
+ end
+
+ has_attached_file :photo, :remote => true
+end

0 comments on commit 93f8065

Please sign in to comment.