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
7436b512
Commit
7436b512
authored
Jun 23, 2012
by
Jérémy FRERE
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added support for created_at field
parent
b4369301
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
0 deletions
+56
-0
lib/paperclip/attachment.rb
+21
-0
test/attachment_test.rb
+35
-0
No files found.
lib/paperclip/attachment.rb
View file @
7436b512
...
@@ -99,6 +99,7 @@ module Paperclip
...
@@ -99,6 +99,7 @@ module Paperclip
instance_write
(
:content_type
,
file
.
content_type
.
to_s
.
strip
)
instance_write
(
:content_type
,
file
.
content_type
.
to_s
.
strip
)
instance_write
(
:file_size
,
file
.
size
)
instance_write
(
:file_size
,
file
.
size
)
instance_write
(
:fingerprint
,
file
.
fingerprint
)
if
instance_respond_to?
(
:fingerprint
)
instance_write
(
:fingerprint
,
file
.
fingerprint
)
if
instance_respond_to?
(
:fingerprint
)
instance_write
(
:created_at
,
Time
.
now
)
if
has_enabled_but_unset_created_at?
instance_write
(
:updated_at
,
Time
.
now
)
instance_write
(
:updated_at
,
Time
.
now
)
@dirty
=
true
@dirty
=
true
...
@@ -254,6 +255,15 @@ module Paperclip
...
@@ -254,6 +255,15 @@ module Paperclip
instance_read
(
:content_type
)
instance_read
(
:content_type
)
end
end
# Returns the creation time of the file as originally assigned, and
# lives in the <attachment>_created_at attribute of the model.
def
created_at
if
able_to_store_created_at?
time
=
instance_read
(
:created_at
)
time
&&
time
.
to_f
.
to_i
end
end
# Returns the last modified time of the file as originally assigned, and
# Returns the last modified time of the file as originally assigned, and
# lives in the <attachment>_updated_at attribute of the model.
# lives in the <attachment>_updated_at attribute of the model.
def
updated_at
def
updated_at
...
@@ -429,6 +439,7 @@ module Paperclip
...
@@ -429,6 +439,7 @@ module Paperclip
instance_write
(
:content_type
,
nil
)
instance_write
(
:content_type
,
nil
)
instance_write
(
:file_size
,
nil
)
instance_write
(
:file_size
,
nil
)
instance_write
(
:fingerprint
,
nil
)
instance_write
(
:fingerprint
,
nil
)
instance_write
(
:created_at
,
nil
)
if
has_enabled_but_unset_created_at?
instance_write
(
:updated_at
,
nil
)
instance_write
(
:updated_at
,
nil
)
end
end
...
@@ -453,5 +464,15 @@ module Paperclip
...
@@ -453,5 +464,15 @@ module Paperclip
filename
filename
end
end
end
end
# Check if attachment database table has a created_at field
def
able_to_store_created_at?
@instance
.
respond_to?
(
"
#{
name
}
_created_at"
.
to_sym
)
end
# Check if attachment database table has a created_at field which is not yet set
def
has_enabled_but_unset_created_at?
able_to_store_created_at?
&&
!
instance_read
(
:created_at
)
end
end
end
end
end
test/attachment_test.rb
View file @
7436b512
...
@@ -1101,6 +1101,41 @@ class AttachmentTest < Test::Unit::TestCase
...
@@ -1101,6 +1101,41 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal
File
.
size
(
@file
),
@dummy
.
avatar
.
size
assert_equal
File
.
size
(
@file
),
@dummy
.
avatar
.
size
end
end
should
"return the right value when sent #avatar_file_size"
do
@dummy
.
avatar
=
@file
assert_equal
File
.
size
(
@file
),
@dummy
.
avatar
.
size
end
context
"and avatar_created_at column"
do
setup
do
ActiveRecord
::
Base
.
connection
.
add_column
:dummies
,
:avatar_created_at
,
:timestamp
rebuild_class
@dummy
=
Dummy
.
new
end
should
"not error when assigned an attachment"
do
assert_nothing_raised
{
@dummy
.
avatar
=
@file
}
end
should
"return the creation time when sent #avatar_created_at"
do
now
=
Time
.
now
Time
.
stubs
(
:now
).
returns
(
now
)
@dummy
.
avatar
=
@file
assert_equal
now
.
to_i
,
@dummy
.
avatar
.
created_at
end
should
"return the creation time when sent #avatar_created_at and the entry has been updated"
do
creation
=
2
.
hours
.
ago
now
=
Time
.
now
Time
.
stubs
(
:now
).
returns
(
creation
)
@dummy
.
avatar
=
@file
Time
.
stubs
(
:now
).
returns
(
now
)
@dummy
.
avatar
=
@file
assert_equal
creation
.
to_i
,
@dummy
.
avatar
.
created_at
assert_not_equal
now
.
to_i
,
@dummy
.
avatar
.
created_at
end
end
context
"and avatar_updated_at column"
do
context
"and avatar_updated_at column"
do
setup
do
setup
do
ActiveRecord
::
Base
.
connection
.
add_column
:dummies
,
:avatar_updated_at
,
:timestamp
ActiveRecord
::
Base
.
connection
.
add_column
:dummies
,
:avatar_updated_at
,
:timestamp
...
...
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