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
fba5b8ae
Commit
fba5b8ae
authored
Dec 12, 2011
by
Mike Burns
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'schema-helper' of
https://github.com/dasch/paperclip
parents
81129ad1
693b5280
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
146 additions
and
8 deletions
+146
-8
README.md
+4
-8
lib/paperclip/railtie.rb
+5
-0
lib/paperclip/schema.rb
+39
-0
test/schema_test.rb
+98
-0
No files found.
README.md
View file @
fba5b8ae
...
...
@@ -80,17 +80,13 @@ In your migrations:
class AddAvatarColumnsToUser < ActiveRecord::Migration
def self.up
add_column :users, :avatar_file_name, :string
add_column :users, :avatar_content_type, :string
add_column :users, :avatar_file_size, :integer
add_column :users, :avatar_updated_at, :datetime
change_table :users do |t|
t.has_attached_file :avatar
end
end
def self.down
remove_column :users, :avatar_file_name
remove_column :users, :avatar_content_type
remove_column :users, :avatar_file_size
remove_column :users, :avatar_updated_at
drop_attached_file :users, :avatar
end
end
...
...
lib/paperclip/railtie.rb
View file @
fba5b8ae
require
'paperclip'
require
'paperclip/schema'
module
Paperclip
if
defined?
Rails
::
Railtie
...
...
@@ -21,6 +22,10 @@ module Paperclip
File
.
send
(
:include
,
Paperclip
::
Upfile
)
Paperclip
.
options
[
:logger
]
=
defined?
(
ActiveRecord
)
?
ActiveRecord
::
Base
.
logger
:
Rails
.
logger
ActiveRecord
::
ConnectionAdapters
::
AbstractAdapter
.
send
(
:include
,
Paperclip
::
Schema
)
ActiveRecord
::
ConnectionAdapters
::
Table
.
send
(
:include
,
Paperclip
::
Schema
)
ActiveRecord
::
ConnectionAdapters
::
TableDefinition
.
send
(
:include
,
Paperclip
::
Schema
)
end
end
end
lib/paperclip/schema.rb
0 → 100644
View file @
fba5b8ae
module
Paperclip
# Provides two helpers that can be used in migrations.
#
# In order to use this module, the target class should implement a
# +column+ method that takes the column name and type, both as symbols,
# as well as a +remove_column+ method that takes a table and column name,
# also both symbols.
module
Schema
@@columns
=
{
:file_name
=>
:string
,
:content_type
=>
:string
,
:file_size
=>
:integer
,
:updated_at
=>
:datetime
}
def
has_attached_file
(
attachment_name
)
with_columns_for
(
attachment_name
)
do
|
column_name
,
column_type
|
column
(
column_name
,
column_type
)
end
end
def
drop_attached_file
(
table_name
,
attachment_name
)
with_columns_for
(
attachment_name
)
do
|
column_name
,
column_type
|
remove_column
(
table_name
,
column_name
)
end
end
protected
def
with_columns_for
(
attachment_name
)
@@columns
.
each
do
|
suffix
,
column_type
|
column_name
=
full_column_name
(
attachment_name
,
suffix
)
yield
column_name
,
column_type
end
end
def
full_column_name
(
attachment_name
,
column_name
)
"
#{
attachment_name
}
_
#{
column_name
}
"
.
to_sym
end
end
end
test/schema_test.rb
0 → 100644
View file @
fba5b8ae
require
'test/helper'
class
MockSchema
include
Paperclip
::
Schema
def
initialize
(
table_name
=
nil
)
@table_name
=
table_name
@columns
=
{}
@deleted_columns
=
[]
end
def
column
(
name
,
type
)
@columns
[
name
]
=
type
end
def
remove_column
(
table_name
,
column_name
)
return
if
@table_name
&&
@table_name
!=
table_name
@columns
.
delete
(
column_name
)
@deleted_columns
.
push
(
column_name
)
end
def
has_column?
(
column_name
)
@columns
.
key?
(
column_name
)
end
def
deleted_column?
(
column_name
)
@deleted_columns
.
include?
(
column_name
)
end
def
type_of
(
column_name
)
@columns
[
column_name
]
end
end
class
SchemaTest
<
Test
::
Unit
::
TestCase
context
"Migrating up"
do
setup
do
@schema
=
MockSchema
.
new
@schema
.
has_attached_file
:avatar
end
should
"create the file_name column"
do
assert
@schema
.
has_column?
(
:avatar_file_name
)
end
should
"create the content_type column"
do
assert
@schema
.
has_column?
(
:avatar_content_type
)
end
should
"create the file_size column"
do
assert
@schema
.
has_column?
(
:avatar_file_size
)
end
should
"create the updated_at column"
do
assert
@schema
.
has_column?
(
:avatar_updated_at
)
end
should
"make the file_name column a string"
do
assert_equal
:string
,
@schema
.
type_of
(
:avatar_file_name
)
end
should
"make the content_type column a string"
do
assert_equal
:string
,
@schema
.
type_of
(
:avatar_content_type
)
end
should
"make the file_size column an integer"
do
assert_equal
:integer
,
@schema
.
type_of
(
:avatar_file_size
)
end
should
"make the updated_at column a datetime"
do
assert_equal
:datetime
,
@schema
.
type_of
(
:avatar_updated_at
)
end
end
context
"Migrating down"
do
setup
do
@schema
=
MockSchema
.
new
(
:users
)
@schema
.
drop_attached_file
:users
,
:avatar
end
should
"remove the file_name column"
do
assert
@schema
.
deleted_column?
(
:avatar_file_name
)
end
should
"remove the content_type column"
do
assert
@schema
.
deleted_column?
(
:avatar_content_type
)
end
should
"remove the file_size column"
do
assert
@schema
.
deleted_column?
(
:avatar_file_size
)
end
should
"remove the updated_at column"
do
assert
@schema
.
deleted_column?
(
:avatar_updated_at
)
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