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
c555f517
Commit
c555f517
authored
Jan 25, 2013
by
Mike Burns
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move the attachment getter to the HasAttachedFile class
parent
02bd35fe
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
23 deletions
+64
-23
lib/paperclip.rb
+1
-17
lib/paperclip/has_attached_file.rb
+38
-3
test/has_attached_file_test.rb
+25
-3
No files found.
lib/paperclip.rb
View file @
c555f517
...
...
@@ -174,7 +174,7 @@ module Paperclip
# end
# end
def
has_attached_file
(
name
,
options
=
{})
HasAttachedFile
.
new
(
name
,
options
).
define_on
(
self
)
HasAttachedFile
.
define_on
(
self
,
name
,
options
)
options
=
Paperclip
::
AttachmentOptions
.
new
(
options
)
Paperclip
.
classes_with_attachments
<<
self
.
name
...
...
@@ -193,22 +193,6 @@ module Paperclip
attachment
.
send
(
:flush_errors
)
end
define_method
name
do
|*
args
|
ivar
=
"@attachment_
#{
name
}
"
attachment
=
instance_variable_get
(
ivar
)
if
attachment
.
nil?
attachment
=
Attachment
.
new
(
name
,
self
,
options
)
instance_variable_set
(
ivar
,
attachment
)
end
if
args
.
length
>
0
attachment
.
to_s
(
args
.
first
)
else
attachment
end
end
define_method
"
#{
name
}
?"
do
send
(
name
).
file?
end
...
...
lib/paperclip/has_attached_file.rb
View file @
c555f517
module
Paperclip
class
HasAttachedFile
def
initialize
(
name
,
options
)
def
self
.
define_on
(
klass
,
name
,
options
)
new
(
klass
,
name
,
options
).
define
end
def
initialize
(
klass
,
name
,
options
)
@klass
=
klass
@name
=
name
@options
=
options
end
def
define
define_getter
define_setter
end
def
define_on
(
klass
)
private
def
define_getter
name
=
@name
klass
.
send
:define_method
,
"
#{
@name
}
="
do
|
file
|
options
=
@options
@klass
.
send
:define_method
,
@name
do
|*
args
|
ivar
=
"@attachment_
#{
name
}
"
attachment
=
instance_variable_get
(
ivar
)
if
attachment
.
nil?
attachment
=
Attachment
.
new
(
name
,
self
,
options
)
instance_variable_set
(
ivar
,
attachment
)
end
if
args
.
length
>
0
attachment
.
to_s
(
args
.
first
)
else
attachment
end
end
end
def
define_setter
name
=
@name
@klass
.
send
:define_method
,
"
#{
@name
}
="
do
|
file
|
send
(
name
).
assign
(
file
)
end
end
...
...
test/has_attached_file_test.rb
View file @
c555f517
...
...
@@ -4,13 +4,35 @@ require 'paperclip/has_attached_file'
class
HasAttachedFileTest
<
Test
::
Unit
::
TestCase
context
'#define_on'
do
should
'define a setter on the class object'
do
assert_adding_attachment
(
'avatar'
).
defines_method
(
'avatar='
)
end
should
'define a getter on the class object'
do
assert_adding_attachment
(
'avatar'
).
defines_method
(
'avatar'
)
end
end
private
def
assert_adding_attachment
(
attachment_name
)
AttachmentAdder
.
new
(
attachment_name
)
end
class
AttachmentAdder
include
Mocha
::
API
include
Test
::
Unit
::
Assertions
def
initialize
(
attachment_name
)
@attachment_name
=
attachment_name
end
def
defines_method
(
method_name
)
a_class
=
stub
(
'class'
,
define_method:
nil
)
has_attached_file
=
Paperclip
::
HasAttachedFile
.
new
(
:avatar
,
{})
has_attached_file
.
define_on
(
a_class
)
Paperclip
::
HasAttachedFile
.
define_on
(
a_class
,
@attachment_name
,
{}
)
assert_received
(
a_class
,
:define_method
)
do
|
expect
|
expect
.
with
(
'avatar='
)
expect
.
with
(
method_name
)
end
end
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