Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
paperclip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
paperclip
Commits
2226c020
Commit
2226c020
authored
Dec 14, 2010
by
Matthew Mongeau
Committed by
Jon Yurek
Dec 14, 2010
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor the looping of attachments in the rake task
parent
6cbd3d50
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
69 additions
and
45 deletions
+69
-45
gemfiles/rails2.gemfile
+3
-2
gemfiles/rails2.gemfile.lock
+4
-0
gemfiles/rails3.gemfile
+4
-3
gemfiles/rails3.gemfile.lock
+5
-1
lib/paperclip.rb
+6
-0
lib/tasks/paperclip.rake
+30
-39
test/paperclip_test.rb
+17
-0
No files found.
gemfiles/rails2.gemfile
View file @
2226c020
...
...
@@ -7,4 +7,5 @@ gem "rake"
gem "sqlite3-ruby", "~>1.3.0"
gem "shoulda"
gem "mocha"
gem "aws-s3", {:require=>"aws/s3"}
\ No newline at end of file
gem "aws-s3", {:require=>"aws/s3"}
gem "appraisal"
\ No newline at end of file
gemfiles/rails2.gemfile.lock
View file @
2226c020
...
...
@@ -11,6 +11,9 @@ GEM
activeresource (2.3.10)
activesupport (= 2.3.10)
activesupport (2.3.10)
appraisal (0.1)
bundler
rake
aws-s3 (0.6.2)
builder
mime-types
...
...
@@ -43,6 +46,7 @@ PLATFORMS
ruby
DEPENDENCIES
appraisal
aws-s3
mocha
rails (~> 2.3.0)
...
...
gemfiles/rails3.gemfile
View file @
2226c020
...
...
@@ -2,9 +2,10 @@
source "http://rubygems.org"
gem "ruby-debug"
gem "rails", "
~>3.0.0
"
gem "rails", "
>=3.0.3
"
gem "rake"
gem "sqlite3-ruby", "~>1.3.0"
gem "shoulda"
gem "mocha"
gem "aws-s3", {:require=>"aws/s3"}
\ No newline at end of file
gem "aws-s3", {:require=>"aws/s3"}
gem "appraisal"
\ No newline at end of file
gemfiles/rails3.gemfile.lock
View file @
2226c020
...
...
@@ -28,6 +28,9 @@ GEM
activemodel (= 3.0.3)
activesupport (= 3.0.3)
activesupport (3.0.3)
appraisal (0.1)
bundler
rake
arel (2.0.4)
aws-s3 (0.6.2)
builder
...
...
@@ -84,9 +87,10 @@ PLATFORMS
ruby
DEPENDENCIES
appraisal
aws-s3
mocha
rails (
~> 3.0.0
)
rails (
>= 3.0.3
)
rake
ruby-debug
shoulda
...
...
lib/paperclip.rb
View file @
2226c020
...
...
@@ -113,6 +113,12 @@ module Paperclip
processor
end
def
each_instance_with_attachment
(
klass
,
name
)
Object
.
const_get
(
klass
).
all
.
each
do
|
instance
|
yield
(
instance
)
if
instance
.
send
(
:"
#{
name
}
?"
)
end
end
# Log a paperclip-specific line. Uses ActiveRecord::Base.logger
# by default. Set Paperclip.options[:log] to false to turn off.
def
log
message
...
...
lib/tasks/paperclip.rake
View file @
2226c020
def
obtain_class
class_name
=
ENV
[
'CLASS'
]
||
ENV
[
'class'
]
raise
"Must specify CLASS"
unless
class_name
@klass
=
Object
.
const_get
(
class_name
)
end
def
obtain_attachments
...
...
@@ -14,25 +13,6 @@ def obtain_attachments
end
end
def
for_all_attachments
klass
=
obtain_class
names
=
obtain_attachments
ids
=
klass
.
connection
.
select_values
(
klass
.
send
(
:construct_finder_sql
,
:select
=>
'id'
))
ids
.
each
do
|
id
|
instance
=
klass
.
find
(
id
)
names
.
each
do
|
name
|
result
=
if
instance
.
send
(
"
#{
name
}
?"
)
yield
(
instance
,
name
)
else
true
end
print
result
?
"."
:
"x"
;
$stdout
.
flush
end
end
puts
" Done."
end
namespace
:paperclip
do
desc
"Refreshes both metadata and thumbnails."
task
:refresh
=>
[
"paperclip:refresh:metadata"
,
"paperclip:refresh:thumbnails"
]
...
...
@@ -41,24 +21,31 @@ namespace :paperclip do
desc
"Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)."
task
:thumbnails
=>
:environment
do
errors
=
[]
for_all_attachments
do
|
instance
,
name
|
result
=
instance
.
send
(
name
).
reprocess!
errors
<<
[
instance
.
id
,
instance
.
errors
]
unless
instance
.
errors
.
blank?
result
names
=
obtain_attachments
klass
=
obtain_class
names
.
each
do
|
name
|
Paperclip
.
each_instance_with_attachment
(
klass
,
name
)
do
|
instance
|
result
=
instance
.
send
(
name
).
reprocess!
errors
<<
[
instance
.
id
,
instance
.
errors
]
unless
instance
.
errors
.
blank?
end
end
errors
.
each
{
|
e
|
puts
"
#{
e
.
first
}
:
#{
e
.
last
.
full_messages
.
inspect
}
"
}
end
desc
"Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
task
:metadata
=>
:environment
do
for_all_attachments
do
|
instance
,
name
|
if
file
=
instance
.
send
(
name
).
to_file
instance
.
send
(
"
#{
name
}
_file_name="
,
instance
.
send
(
"
#{
name
}
_file_name"
).
strip
)
instance
.
send
(
"
#{
name
}
_content_type="
,
file
.
content_type
.
strip
)
instance
.
send
(
"
#{
name
}
_file_size="
,
file
.
size
)
if
instance
.
respond_to?
(
"
#{
name
}
_file_size"
)
instance
.
save
(
false
)
else
true
names
=
obtain_attachments
klass
=
obtain_class
names
.
each
do
|
name
|
Paperclip
.
each_instance_with_attachment
(
klass
,
name
)
do
|
instance
|
if
file
=
instance
.
send
(
name
).
to_file
instance
.
send
(
"
#{
name
}
_file_name="
,
instance
.
send
(
"
#{
name
}
_file_name"
).
strip
)
instance
.
send
(
"
#{
name
}
_content_type="
,
file
.
content_type
.
strip
)
instance
.
send
(
"
#{
name
}
_file_size="
,
file
.
size
)
if
instance
.
respond_to?
(
"
#{
name
}
_file_size"
)
instance
.
save
(
false
)
else
true
end
end
end
end
...
...
@@ -66,13 +53,17 @@ namespace :paperclip do
desc
"Cleans out invalid attachments. Useful after you've added new validations."
task
:clean
=>
:environment
do
for_all_attachments
do
|
instance
,
name
|
instance
.
send
(
name
).
send
(
:validate
)
if
instance
.
send
(
name
).
valid?
true
else
instance
.
send
(
"
#{
name
}
="
,
nil
)
instance
.
save
names
=
obtain_attachments
klass
=
obtain_class
names
.
each
do
|
name
|
Paperclip
.
each_instance_with_attachment
(
klass
,
name
)
do
|
instance
|
instance
.
send
(
name
).
send
(
:validate
)
if
instance
.
send
(
name
).
valid?
true
else
instance
.
send
(
"
#{
name
}
="
,
nil
)
instance
.
save
end
end
end
end
...
...
test/paperclip_test.rb
View file @
2226c020
...
...
@@ -43,6 +43,23 @@ class PaperclipTest < Test::Unit::TestCase
end
end
context
"Paperclip.each_instance_with_attachment"
do
setup
do
@file
=
File
.
new
(
File
.
join
(
FIXTURES_DIR
,
"5k.png"
),
'rb'
)
d1
=
Dummy
.
create
(
:avatar
=>
@file
)
d2
=
Dummy
.
create
d3
=
Dummy
.
create
(
:avatar
=>
@file
)
@expected
=
[
d1
,
d3
]
end
should
"yield every instance of a model that has an attachment"
do
actual
=
[]
Paperclip
.
each_instance_with_attachment
(
"Dummy"
,
"avatar"
)
do
|
instance
|
actual
<<
instance
end
assert_same_elements
@expected
,
actual
end
end
should
"raise when sent #processor and the name of a class that exists but isn't a subclass of Processor"
do
assert_raises
(
Paperclip
::
PaperclipError
){
Paperclip
.
processor
(
:attachment
)
}
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment