polypaperclip 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +16 -0
- data/Gemfile.lock +103 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +55 -0
- data/Rakefile +43 -0
- data/VERSION +1 -0
- data/lib/generators/migration_generator.rb +24 -0
- data/lib/generators/templates/migration.erb +21 -0
- data/lib/polypaperclip/attachment.rb +29 -0
- data/lib/polypaperclip/persisted_attachment.rb +8 -0
- data/lib/polypaperclip/railtie.rb +18 -0
- data/lib/polypaperclip.rb +86 -0
- data/polypaperclip.gemspec +90 -0
- data/rails/init.rb +3 -0
- data/spec/database.yml +5 -0
- data/spec/fixtures/rails.png +0 -0
- data/spec/page_spec.rb +50 -0
- data/spec/spec_helper.rb +46 -0
- metadata +242 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
gem "rake", "0.8.7"
|
4
|
+
gem "paperclip", "~> 2.3"
|
5
|
+
|
6
|
+
gem "rails", "~>3.0.0"
|
7
|
+
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.3.0"
|
10
|
+
gem "yard", "~> 0.6.0"
|
11
|
+
gem "bundler", "~> 1.0.0"
|
12
|
+
gem "jeweler", "~> 1.5.2"
|
13
|
+
gem "sqlite3-ruby", "~>1.3.0"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
gem "mocha"
|
16
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
abstract (1.0.0)
|
5
|
+
actionmailer (3.0.7)
|
6
|
+
actionpack (= 3.0.7)
|
7
|
+
mail (~> 2.2.15)
|
8
|
+
actionpack (3.0.7)
|
9
|
+
activemodel (= 3.0.7)
|
10
|
+
activesupport (= 3.0.7)
|
11
|
+
builder (~> 2.1.2)
|
12
|
+
erubis (~> 2.6.6)
|
13
|
+
i18n (~> 0.5.0)
|
14
|
+
rack (~> 1.2.1)
|
15
|
+
rack-mount (~> 0.6.14)
|
16
|
+
rack-test (~> 0.5.7)
|
17
|
+
tzinfo (~> 0.3.23)
|
18
|
+
activemodel (3.0.7)
|
19
|
+
activesupport (= 3.0.7)
|
20
|
+
builder (~> 2.1.2)
|
21
|
+
i18n (~> 0.5.0)
|
22
|
+
activerecord (3.0.7)
|
23
|
+
activemodel (= 3.0.7)
|
24
|
+
activesupport (= 3.0.7)
|
25
|
+
arel (~> 2.0.2)
|
26
|
+
tzinfo (~> 0.3.23)
|
27
|
+
activeresource (3.0.7)
|
28
|
+
activemodel (= 3.0.7)
|
29
|
+
activesupport (= 3.0.7)
|
30
|
+
activesupport (3.0.7)
|
31
|
+
arel (2.0.10)
|
32
|
+
builder (2.1.2)
|
33
|
+
diff-lcs (1.1.2)
|
34
|
+
erubis (2.6.6)
|
35
|
+
abstract (>= 1.0.0)
|
36
|
+
git (1.2.5)
|
37
|
+
i18n (0.5.0)
|
38
|
+
jeweler (1.5.2)
|
39
|
+
bundler (~> 1.0.0)
|
40
|
+
git (>= 1.2.5)
|
41
|
+
rake
|
42
|
+
mail (2.2.19)
|
43
|
+
activesupport (>= 2.3.6)
|
44
|
+
i18n (>= 0.4.0)
|
45
|
+
mime-types (~> 1.16)
|
46
|
+
treetop (~> 1.4.8)
|
47
|
+
mime-types (1.16)
|
48
|
+
mocha (0.9.12)
|
49
|
+
paperclip (2.3.11)
|
50
|
+
activerecord (>= 2.3.0)
|
51
|
+
activesupport (>= 2.3.2)
|
52
|
+
polyglot (0.3.1)
|
53
|
+
rack (1.2.3)
|
54
|
+
rack-mount (0.6.14)
|
55
|
+
rack (>= 1.0.0)
|
56
|
+
rack-test (0.5.7)
|
57
|
+
rack (>= 1.0)
|
58
|
+
rails (3.0.7)
|
59
|
+
actionmailer (= 3.0.7)
|
60
|
+
actionpack (= 3.0.7)
|
61
|
+
activerecord (= 3.0.7)
|
62
|
+
activeresource (= 3.0.7)
|
63
|
+
activesupport (= 3.0.7)
|
64
|
+
bundler (~> 1.0)
|
65
|
+
railties (= 3.0.7)
|
66
|
+
railties (3.0.7)
|
67
|
+
actionpack (= 3.0.7)
|
68
|
+
activesupport (= 3.0.7)
|
69
|
+
rake (>= 0.8.7)
|
70
|
+
thor (~> 0.14.4)
|
71
|
+
rake (0.8.7)
|
72
|
+
rcov (0.9.9)
|
73
|
+
rspec (2.3.0)
|
74
|
+
rspec-core (~> 2.3.0)
|
75
|
+
rspec-expectations (~> 2.3.0)
|
76
|
+
rspec-mocks (~> 2.3.0)
|
77
|
+
rspec-core (2.3.1)
|
78
|
+
rspec-expectations (2.3.0)
|
79
|
+
diff-lcs (~> 1.1.2)
|
80
|
+
rspec-mocks (2.3.0)
|
81
|
+
sqlite3 (1.3.3)
|
82
|
+
sqlite3-ruby (1.3.3)
|
83
|
+
sqlite3 (>= 1.3.3)
|
84
|
+
thor (0.14.6)
|
85
|
+
treetop (1.4.9)
|
86
|
+
polyglot (>= 0.3.1)
|
87
|
+
tzinfo (0.3.27)
|
88
|
+
yard (0.6.8)
|
89
|
+
|
90
|
+
PLATFORMS
|
91
|
+
ruby
|
92
|
+
|
93
|
+
DEPENDENCIES
|
94
|
+
bundler (~> 1.0.0)
|
95
|
+
jeweler (~> 1.5.2)
|
96
|
+
mocha
|
97
|
+
paperclip (~> 2.3)
|
98
|
+
rails (~> 3.0.0)
|
99
|
+
rake (= 0.8.7)
|
100
|
+
rcov
|
101
|
+
rspec (~> 2.3.0)
|
102
|
+
sqlite3-ruby (~> 1.3.0)
|
103
|
+
yard (~> 0.6.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Dan Pickett
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
= polypaperclip
|
2
|
+
|
3
|
+
An adaptation of paperclip that uses a central attachments table so that you can attach many files to a given model without adding large amounts of database columns
|
4
|
+
|
5
|
+
class Page < ActiveRecord::Base
|
6
|
+
has_attachment :primary_image,
|
7
|
+
:styles => {:large => "300x300", :thumb => "100x100"}
|
8
|
+
has_attachment :secondary_image,
|
9
|
+
:styles => {:large => "200x200", :thumb => "50x50"}
|
10
|
+
end
|
11
|
+
|
12
|
+
== Installing
|
13
|
+
|
14
|
+
Polypaperclip works with Rails 3 only.
|
15
|
+
|
16
|
+
Add polypaperclip to your Gemfile.
|
17
|
+
|
18
|
+
gem 'polypaperclip'
|
19
|
+
|
20
|
+
Generate the migration that creates the attachments table for you. The migration adds an index for optimized querying.
|
21
|
+
|
22
|
+
rails g polypaperclip:migration
|
23
|
+
|
24
|
+
== Performance Note
|
25
|
+
|
26
|
+
If you're going to be referencing these attachments in massive queries, don't forget:
|
27
|
+
|
28
|
+
Page.include(:primary_image_attachment, :secondary_image_attachment)
|
29
|
+
|
30
|
+
This will eliminate n+1 query situations which can adversely impact performance.
|
31
|
+
|
32
|
+
== TODO
|
33
|
+
|
34
|
+
* has_many_attachments
|
35
|
+
* validations?
|
36
|
+
|
37
|
+
== Special Thanks
|
38
|
+
|
39
|
+
Thanks to Coyne Design (http://www.coyne-design.com/) for granting MIT license to this gem.
|
40
|
+
|
41
|
+
== Contributing to polypaperclip
|
42
|
+
|
43
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
44
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
45
|
+
* Fork the project
|
46
|
+
* Start a feature/bugfix branch
|
47
|
+
* Commit and push until you are happy with your contribution
|
48
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
49
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
50
|
+
|
51
|
+
== Copyright
|
52
|
+
|
53
|
+
Copyright (c) 2011 Dan Pickett. See LICENSE.txt for
|
54
|
+
further details.
|
55
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "polypaperclip"
|
16
|
+
gem.homepage = "http://github.com/dpickett/polypaperclip"
|
17
|
+
gem.license = "MIT"
|
18
|
+
gem.summary = %Q{Polymorphic attachments with paperclip}
|
19
|
+
gem.description = %Q{hacks paperclip to have a single repo of attachments}
|
20
|
+
gem.email = "dpickett@enlightsolutions.com"
|
21
|
+
gem.authors = ["Dan Pickett"]
|
22
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
23
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
24
|
+
# gem.add_runtime_dependency 'jabber4r', '> 0.1'
|
25
|
+
# gem.add_development_dependency 'rspec', '> 1.2.3'
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
36
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
37
|
+
spec.rcov = true
|
38
|
+
end
|
39
|
+
|
40
|
+
task :default => :spec
|
41
|
+
|
42
|
+
require 'yard'
|
43
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Polypaperclip
|
2
|
+
module Generators
|
3
|
+
#shamelessly addapted from Sutto's excellent slugged library: https://github.com/Sutto/slugged
|
4
|
+
class MigrationGenerator < Rails::Generators::Base
|
5
|
+
include Rails::Generators::Migration
|
6
|
+
|
7
|
+
def self.source_root
|
8
|
+
@_ps_source_root ||= File.expand_path("templates", File.dirname(__FILE__))
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.next_migration_number(dirname) #:nodoc:
|
12
|
+
if ActiveRecord::Base.timestamped_migrations
|
13
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
14
|
+
else
|
15
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_migration_file
|
20
|
+
migration_template "migration.erb", "db/migrate/create_polypaperclip_attachments_table.rb"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration
|
2
|
+
|
3
|
+
def self.up
|
4
|
+
create_table :attachments do |table|
|
5
|
+
table.integer :attachable_id, :null => false
|
6
|
+
table.string :attachable_type, :null => false
|
7
|
+
table.string :attachment_type, :null => false
|
8
|
+
table.string :attachment_file_name, :null => false
|
9
|
+
table.string :attachment_content_type, :null => false
|
10
|
+
table.integer :attachment_file_size, :null => false
|
11
|
+
table.datetime :attachment_updated_at, :null => false
|
12
|
+
end
|
13
|
+
|
14
|
+
add_index :attachments, [:attachable_id, :attachable_type, :attachment_type]
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
remove_table :attachments
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Polypaperclip
|
2
|
+
class Attachment < Paperclip::Attachment
|
3
|
+
def initialize(name, instance, options = {})
|
4
|
+
super
|
5
|
+
|
6
|
+
#retain the actual object in the attachment instance
|
7
|
+
@poly_instance = instance
|
8
|
+
@poly_name = name
|
9
|
+
|
10
|
+
#cheat instance to reflect the polymorphic attachment
|
11
|
+
#this sort of tricks paperclip in order for it to the right things
|
12
|
+
@instance = instance.send("#{name}_attachment")
|
13
|
+
@name = "attachment"
|
14
|
+
end
|
15
|
+
|
16
|
+
def assign(uploaded_file)
|
17
|
+
build_instance
|
18
|
+
super
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
def build_instance
|
23
|
+
@instance ||= @poly_instance.send("build_#{@poly_name}_attachment")
|
24
|
+
@instance.attachment_type = name
|
25
|
+
@instance
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "paperclip/railtie"
|
2
|
+
|
3
|
+
module Polypaperclip
|
4
|
+
require 'rails'
|
5
|
+
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
initializer 'polypaperclip.insert_into_active_record' do
|
8
|
+
ActiveSupport.on_load :active_record do
|
9
|
+
ActiveRecord::Base.extend(Polypaperclip::ClassMethods)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
generators do
|
14
|
+
require "generators/migration_generator"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require "active_support"
|
2
|
+
require "active_record"
|
3
|
+
require "paperclip"
|
4
|
+
|
5
|
+
require "polypaperclip/attachment"
|
6
|
+
require "polypaperclip/persisted_attachment"
|
7
|
+
require "polypaperclip/railtie"
|
8
|
+
|
9
|
+
module Polypaperclip
|
10
|
+
module ClassMethods
|
11
|
+
def polypaperclip_definitions
|
12
|
+
read_inheritable_attribute(:polypaperclip_definitions)
|
13
|
+
end
|
14
|
+
|
15
|
+
def has_attachment(name, options = {})
|
16
|
+
initialize_polypaperclip
|
17
|
+
|
18
|
+
polypaperclip_definitions[name] = {:validations => []}.merge(options)
|
19
|
+
|
20
|
+
has_one "#{name}_attachment",
|
21
|
+
:as => :attachable,
|
22
|
+
:dependent => :destroy,
|
23
|
+
:class_name => "Polypaperclip::PersistedAttachment",
|
24
|
+
:conditions => "attachment_type = '#{name}'"
|
25
|
+
|
26
|
+
define_method name do |*args|
|
27
|
+
a = paperclip_attachment_for(name)
|
28
|
+
(args.length > 0) ? a.to_s(args.first) : a
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method "#{name}=" do |file|
|
32
|
+
paperclip_attachment_for(name).assign(file)
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method "#{name}?" do
|
36
|
+
paperclip_attachment_for(name).file?
|
37
|
+
end
|
38
|
+
|
39
|
+
validates_each(name) do |record, attr, value|
|
40
|
+
attachment = record.paperclip_attachment_for(name)
|
41
|
+
attachment.send(:flush_errors)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
#initialize a polypaperclip model if a configuration hasn't already been loaded
|
47
|
+
def initialize_polypaperclip
|
48
|
+
if polypaperclip_definitions.nil?
|
49
|
+
has_many_attachments_association
|
50
|
+
|
51
|
+
after_save :save_attached_files
|
52
|
+
before_destroy :destroy_attached_files
|
53
|
+
|
54
|
+
write_inheritable_attribute(:polypaperclip_definitions, {})
|
55
|
+
|
56
|
+
#sequence is important here - we have to override some paperclip stuff
|
57
|
+
include Paperclip::InstanceMethods
|
58
|
+
include InstanceMethods
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def has_many_attachments_association
|
63
|
+
unless self.respond_to?(:attachments)
|
64
|
+
has_many :attachments,
|
65
|
+
:as => :attachable
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
module InstanceMethods
|
71
|
+
#we override Paperclip's each_attachment so that we proxy through the attachments model
|
72
|
+
def each_attachment
|
73
|
+
self.class.polypaperclip_definitions.each do |name, definition|
|
74
|
+
yield(name, paperclip_attachment_for(name))
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def paperclip_attachment_for(name)
|
79
|
+
@_paperclip_attachments ||= {}
|
80
|
+
@_paperclip_attachments[name] ||= Polypaperclip::Attachment.new(name,
|
81
|
+
self,
|
82
|
+
self.class.polypaperclip_definitions[name])
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{polypaperclip}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Dan Pickett"]
|
12
|
+
s.date = %q{2011-05-29}
|
13
|
+
s.description = %q{hacks paperclip to have a single repo of attachments}
|
14
|
+
s.email = %q{dpickett@enlightsolutions.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"lib/generators/migration_generator.rb",
|
29
|
+
"lib/generators/templates/migration.erb",
|
30
|
+
"lib/polypaperclip.rb",
|
31
|
+
"lib/polypaperclip/attachment.rb",
|
32
|
+
"lib/polypaperclip/persisted_attachment.rb",
|
33
|
+
"lib/polypaperclip/railtie.rb",
|
34
|
+
"polypaperclip.gemspec",
|
35
|
+
"rails/init.rb",
|
36
|
+
"spec/database.yml",
|
37
|
+
"spec/fixtures/rails.png",
|
38
|
+
"spec/page_spec.rb",
|
39
|
+
"spec/spec_helper.rb"
|
40
|
+
]
|
41
|
+
s.homepage = %q{http://github.com/dpickett/polypaperclip}
|
42
|
+
s.licenses = ["MIT"]
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.rubygems_version = %q{1.6.2}
|
45
|
+
s.summary = %q{Polymorphic attachments with paperclip}
|
46
|
+
s.test_files = [
|
47
|
+
"spec/page_spec.rb",
|
48
|
+
"spec/spec_helper.rb"
|
49
|
+
]
|
50
|
+
|
51
|
+
if s.respond_to? :specification_version then
|
52
|
+
s.specification_version = 3
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_runtime_dependency(%q<rake>, ["= 0.8.7"])
|
56
|
+
s.add_runtime_dependency(%q<paperclip>, ["~> 2.3"])
|
57
|
+
s.add_runtime_dependency(%q<rails>, ["~> 3.0.0"])
|
58
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
59
|
+
s.add_development_dependency(%q<yard>, ["~> 0.6.0"])
|
60
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
61
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
|
62
|
+
s.add_development_dependency(%q<sqlite3-ruby>, ["~> 1.3.0"])
|
63
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
64
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
65
|
+
else
|
66
|
+
s.add_dependency(%q<rake>, ["= 0.8.7"])
|
67
|
+
s.add_dependency(%q<paperclip>, ["~> 2.3"])
|
68
|
+
s.add_dependency(%q<rails>, ["~> 3.0.0"])
|
69
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
70
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
71
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
72
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
73
|
+
s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.0"])
|
74
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
75
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
76
|
+
end
|
77
|
+
else
|
78
|
+
s.add_dependency(%q<rake>, ["= 0.8.7"])
|
79
|
+
s.add_dependency(%q<paperclip>, ["~> 2.3"])
|
80
|
+
s.add_dependency(%q<rails>, ["~> 3.0.0"])
|
81
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
82
|
+
s.add_dependency(%q<yard>, ["~> 0.6.0"])
|
83
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
84
|
+
s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
|
85
|
+
s.add_dependency(%q<sqlite3-ruby>, ["~> 1.3.0"])
|
86
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
87
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
data/rails/init.rb
ADDED
data/spec/database.yml
ADDED
Binary file
|
data/spec/page_spec.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Page" do
|
4
|
+
subject { Page.new(:name => "some page") }
|
5
|
+
|
6
|
+
its(:primary_image) { should_not be_nil }
|
7
|
+
its(:primary_image_attachment) { should be_nil }
|
8
|
+
|
9
|
+
its(:secondary_image) { should_not be_nil }
|
10
|
+
its(:secondary_image_attachment) { should be_nil }
|
11
|
+
|
12
|
+
it "does not save an attachment if I did not assign one" do
|
13
|
+
subject.save!
|
14
|
+
subject.primary_image_attachment.should be_nil
|
15
|
+
end
|
16
|
+
|
17
|
+
it "saves an attachment when I do save one" do
|
18
|
+
attach_primary_image
|
19
|
+
subject.save!
|
20
|
+
subject.primary_image_attachment.should_not be_nil
|
21
|
+
end
|
22
|
+
|
23
|
+
it "does not save the attachment if validation fails" do
|
24
|
+
subject.name = nil
|
25
|
+
attach_primary_image
|
26
|
+
subject.save.should be_false
|
27
|
+
|
28
|
+
subject.primary_image_attachment.should_not be_persisted
|
29
|
+
end
|
30
|
+
|
31
|
+
it "sets the content type on the associated attachment" do
|
32
|
+
attach_primary_image
|
33
|
+
subject.primary_image_attachment.attachment_content_type.should_not be_nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "sets the file size on the associated attachment" do
|
37
|
+
attach_primary_image
|
38
|
+
subject.primary_image_attachment.attachment_file_size.should_not be_nil
|
39
|
+
end
|
40
|
+
|
41
|
+
it "sets the attachment updated at on the associated attachment" do
|
42
|
+
attach_primary_image
|
43
|
+
subject.primary_image_attachment.attachment_updated_at.should_not be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
PRIMARY_IMAGE_PATH = File.dirname(__FILE__) + "/fixtures/rails.png"
|
47
|
+
def attach_primary_image
|
48
|
+
subject.primary_image = File.open(PRIMARY_IMAGE_PATH)
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'polypaperclip'
|
5
|
+
require 'polypaperclip/railtie'
|
6
|
+
|
7
|
+
# Requires supporting files with custom matchers and macros, etc,
|
8
|
+
# in ./support/ and its subdirectories.
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
10
|
+
|
11
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
12
|
+
ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "/debug.log")
|
13
|
+
ActiveRecord::Base.establish_connection(config['test'])
|
14
|
+
|
15
|
+
RSpec.configure do |config|
|
16
|
+
config.mock_with :mocha
|
17
|
+
config.before(:all) do
|
18
|
+
ActiveRecord::Base.connection.create_table :pages, :force => true do |table|
|
19
|
+
table.string :name, :null => false
|
20
|
+
end
|
21
|
+
puts "created table"
|
22
|
+
|
23
|
+
ActiveRecord::Base.connection.create_table :attachments, :force => true do |table|
|
24
|
+
table.integer :attachable_id, :null => false
|
25
|
+
table.string :attachable_type, :null => false
|
26
|
+
table.string :attachment_type, :null => false
|
27
|
+
table.string :attachment_file_name, :null => false
|
28
|
+
table.string :attachment_content_type, :null => false
|
29
|
+
table.integer :attachment_file_size, :null => false
|
30
|
+
table.datetime :attachment_updated_at, :null => false
|
31
|
+
end
|
32
|
+
|
33
|
+
ActiveRecord::Base.extend(Polypaperclip::ClassMethods)
|
34
|
+
|
35
|
+
class Page < ActiveRecord::Base
|
36
|
+
validates_presence_of :name
|
37
|
+
|
38
|
+
has_attachment :primary_image,
|
39
|
+
:styles => {:large => "300x300", :thumb => "100x100"}
|
40
|
+
has_attachment :secondary_image,
|
41
|
+
:styles => {:large => "200x200", :thumb => "50x50"}
|
42
|
+
end
|
43
|
+
|
44
|
+
Object.const_set(:Rails, stub('Rails', :root => File.dirname(__FILE__), :env => 'test'))
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,242 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polypaperclip
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dan Pickett
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-29 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
type: :runtime
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - "="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 49
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
- 8
|
32
|
+
- 7
|
33
|
+
version: 0.8.7
|
34
|
+
name: rake
|
35
|
+
version_requirements: *id001
|
36
|
+
prerelease: false
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
type: :runtime
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 5
|
45
|
+
segments:
|
46
|
+
- 2
|
47
|
+
- 3
|
48
|
+
version: "2.3"
|
49
|
+
name: paperclip
|
50
|
+
version_requirements: *id002
|
51
|
+
prerelease: false
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
type: :runtime
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 7
|
60
|
+
segments:
|
61
|
+
- 3
|
62
|
+
- 0
|
63
|
+
- 0
|
64
|
+
version: 3.0.0
|
65
|
+
name: rails
|
66
|
+
version_requirements: *id003
|
67
|
+
prerelease: false
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
type: :development
|
70
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
hash: 3
|
76
|
+
segments:
|
77
|
+
- 2
|
78
|
+
- 3
|
79
|
+
- 0
|
80
|
+
version: 2.3.0
|
81
|
+
name: rspec
|
82
|
+
version_requirements: *id004
|
83
|
+
prerelease: false
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
type: :development
|
86
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ~>
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
hash: 7
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
- 6
|
95
|
+
- 0
|
96
|
+
version: 0.6.0
|
97
|
+
name: yard
|
98
|
+
version_requirements: *id005
|
99
|
+
prerelease: false
|
100
|
+
- !ruby/object:Gem::Dependency
|
101
|
+
type: :development
|
102
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ~>
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
hash: 23
|
108
|
+
segments:
|
109
|
+
- 1
|
110
|
+
- 0
|
111
|
+
- 0
|
112
|
+
version: 1.0.0
|
113
|
+
name: bundler
|
114
|
+
version_requirements: *id006
|
115
|
+
prerelease: false
|
116
|
+
- !ruby/object:Gem::Dependency
|
117
|
+
type: :development
|
118
|
+
requirement: &id007 !ruby/object:Gem::Requirement
|
119
|
+
none: false
|
120
|
+
requirements:
|
121
|
+
- - ~>
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
hash: 7
|
124
|
+
segments:
|
125
|
+
- 1
|
126
|
+
- 5
|
127
|
+
- 2
|
128
|
+
version: 1.5.2
|
129
|
+
name: jeweler
|
130
|
+
version_requirements: *id007
|
131
|
+
prerelease: false
|
132
|
+
- !ruby/object:Gem::Dependency
|
133
|
+
type: :development
|
134
|
+
requirement: &id008 !ruby/object:Gem::Requirement
|
135
|
+
none: false
|
136
|
+
requirements:
|
137
|
+
- - ~>
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
hash: 27
|
140
|
+
segments:
|
141
|
+
- 1
|
142
|
+
- 3
|
143
|
+
- 0
|
144
|
+
version: 1.3.0
|
145
|
+
name: sqlite3-ruby
|
146
|
+
version_requirements: *id008
|
147
|
+
prerelease: false
|
148
|
+
- !ruby/object:Gem::Dependency
|
149
|
+
type: :development
|
150
|
+
requirement: &id009 !ruby/object:Gem::Requirement
|
151
|
+
none: false
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
hash: 3
|
156
|
+
segments:
|
157
|
+
- 0
|
158
|
+
version: "0"
|
159
|
+
name: rcov
|
160
|
+
version_requirements: *id009
|
161
|
+
prerelease: false
|
162
|
+
- !ruby/object:Gem::Dependency
|
163
|
+
type: :development
|
164
|
+
requirement: &id010 !ruby/object:Gem::Requirement
|
165
|
+
none: false
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
hash: 3
|
170
|
+
segments:
|
171
|
+
- 0
|
172
|
+
version: "0"
|
173
|
+
name: mocha
|
174
|
+
version_requirements: *id010
|
175
|
+
prerelease: false
|
176
|
+
description: hacks paperclip to have a single repo of attachments
|
177
|
+
email: dpickett@enlightsolutions.com
|
178
|
+
executables: []
|
179
|
+
|
180
|
+
extensions: []
|
181
|
+
|
182
|
+
extra_rdoc_files:
|
183
|
+
- LICENSE.txt
|
184
|
+
- README.rdoc
|
185
|
+
files:
|
186
|
+
- .document
|
187
|
+
- .rspec
|
188
|
+
- Gemfile
|
189
|
+
- Gemfile.lock
|
190
|
+
- LICENSE.txt
|
191
|
+
- README.rdoc
|
192
|
+
- Rakefile
|
193
|
+
- VERSION
|
194
|
+
- lib/generators/migration_generator.rb
|
195
|
+
- lib/generators/templates/migration.erb
|
196
|
+
- lib/polypaperclip.rb
|
197
|
+
- lib/polypaperclip/attachment.rb
|
198
|
+
- lib/polypaperclip/persisted_attachment.rb
|
199
|
+
- lib/polypaperclip/railtie.rb
|
200
|
+
- polypaperclip.gemspec
|
201
|
+
- rails/init.rb
|
202
|
+
- spec/database.yml
|
203
|
+
- spec/fixtures/rails.png
|
204
|
+
- spec/page_spec.rb
|
205
|
+
- spec/spec_helper.rb
|
206
|
+
has_rdoc: true
|
207
|
+
homepage: http://github.com/dpickett/polypaperclip
|
208
|
+
licenses:
|
209
|
+
- MIT
|
210
|
+
post_install_message:
|
211
|
+
rdoc_options: []
|
212
|
+
|
213
|
+
require_paths:
|
214
|
+
- lib
|
215
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
216
|
+
none: false
|
217
|
+
requirements:
|
218
|
+
- - ">="
|
219
|
+
- !ruby/object:Gem::Version
|
220
|
+
hash: 3
|
221
|
+
segments:
|
222
|
+
- 0
|
223
|
+
version: "0"
|
224
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
225
|
+
none: false
|
226
|
+
requirements:
|
227
|
+
- - ">="
|
228
|
+
- !ruby/object:Gem::Version
|
229
|
+
hash: 3
|
230
|
+
segments:
|
231
|
+
- 0
|
232
|
+
version: "0"
|
233
|
+
requirements: []
|
234
|
+
|
235
|
+
rubyforge_project:
|
236
|
+
rubygems_version: 1.6.2
|
237
|
+
signing_key:
|
238
|
+
specification_version: 3
|
239
|
+
summary: Polymorphic attachments with paperclip
|
240
|
+
test_files:
|
241
|
+
- spec/page_spec.rb
|
242
|
+
- spec/spec_helper.rb
|