Commit 6ea7c265 by Keitaroh Kobayashi Committed by Jon Yurek

Pass options through to the underlying adapter.

Column-specific options can be specified by using its name,
for example:

```
t.attachment :avatar, created_at: { index: true }
```
parent 30d0efb4
...@@ -22,9 +22,12 @@ module Paperclip ...@@ -22,9 +22,12 @@ module Paperclip
def add_attachment(table_name, *attachment_names) def add_attachment(table_name, *attachment_names)
raise ArgumentError, "Please specify attachment name in your add_attachment call in your migration." if attachment_names.empty? raise ArgumentError, "Please specify attachment name in your add_attachment call in your migration." if attachment_names.empty?
options = attachment_names.extract_options!
attachment_names.each do |attachment_name| attachment_names.each do |attachment_name|
COLUMNS.each_pair do |column_name, column_type| COLUMNS.each_pair do |column_name, column_type|
add_column(table_name, "#{attachment_name}_#{column_name}", column_type) column_options = options.merge(options[column_name.to_sym] || {})
add_column(table_name, "#{attachment_name}_#{column_name}", column_type, column_options)
end end
end end
end end
...@@ -32,9 +35,12 @@ module Paperclip ...@@ -32,9 +35,12 @@ module Paperclip
def remove_attachment(table_name, *attachment_names) def remove_attachment(table_name, *attachment_names)
raise ArgumentError, "Please specify attachment name in your remove_attachment call in your migration." if attachment_names.empty? raise ArgumentError, "Please specify attachment name in your remove_attachment call in your migration." if attachment_names.empty?
options = attachment_names.extract_options!
attachment_names.each do |attachment_name| attachment_names.each do |attachment_name|
COLUMNS.each_pair do |column_name, column_type| COLUMNS.each_pair do |column_name, column_type|
remove_column(table_name, "#{attachment_name}_#{column_name}") column_options = options.merge(options[column_name.to_sym] || {})
remove_column(table_name, "#{attachment_name}_#{column_name}", column_type, column_options)
end end
end end
end end
...@@ -47,9 +53,11 @@ module Paperclip ...@@ -47,9 +53,11 @@ module Paperclip
module TableDefinition module TableDefinition
def attachment(*attachment_names) def attachment(*attachment_names)
options = attachment_names.extract_options!
attachment_names.each do |attachment_name| attachment_names.each do |attachment_name|
COLUMNS.each_pair do |column_name, column_type| COLUMNS.each_pair do |column_name, column_type|
column("#{attachment_name}_#{column_name}", column_type) column_options = options.merge(options[column_name.to_sym] || {})
column("#{attachment_name}_#{column_name}", column_type, column_options)
end end
end end
end end
......
...@@ -58,6 +58,27 @@ describe Paperclip::Schema do ...@@ -58,6 +58,27 @@ describe Paperclip::Schema do
expect(columns).to include(['avatar_updated_at', :datetime]) expect(columns).to include(['avatar_updated_at', :datetime])
end end
end end
context "using #attachment with options" do
before do
Dummy.connection.create_table :dummies, force: true do |t|
t.attachment :avatar, default: '1', file_name: { default: 'default' }
end
end
it "sets defaults on columns" do
defaults_columns = ["avatar_file_name", "avatar_content_type", "avatar_file_size"]
columns = Dummy.columns.select { |e| defaults_columns.include? e.name }
columns.each do |column|
if column.name == 'avatar_file_name'
expect(column.default).to eq('default')
else
expect(column.default.to_i).to eq(1)
end
end
end
end
end end
context "within schema statement" do context "within schema statement" do
...@@ -81,6 +102,25 @@ describe Paperclip::Schema do ...@@ -81,6 +102,25 @@ describe Paperclip::Schema do
end end
end end
context "with single attachment and options" do
before do
Dummy.connection.add_attachment :dummies, :avatar, default: '1', file_name: { default: 'default' }
end
it "sets defaults on columns" do
defaults_columns = ["avatar_file_name", "avatar_content_type", "avatar_file_size"]
columns = Dummy.columns.select { |e| defaults_columns.include? e.name }
columns.each do |column|
if column.name == 'avatar_file_name'
expect(column.default).to eq('default')
else
expect(column.default.to_i).to eq(1)
end
end
end
end
context "with multiple attachments" do context "with multiple attachments" do
before do before do
Dummy.connection.add_attachment :dummies, :avatar, :photo Dummy.connection.add_attachment :dummies, :avatar, :photo
...@@ -100,6 +140,25 @@ describe Paperclip::Schema do ...@@ -100,6 +140,25 @@ describe Paperclip::Schema do
end end
end end
context "with multiple attachments and options" do
before do
Dummy.connection.add_attachment :dummies, :avatar, :photo, default: '1', file_name: { default: 'default' }
end
it "sets defaults on columns" do
defaults_columns = ["avatar_file_name", "avatar_content_type", "avatar_file_size", "photo_file_name", "photo_content_type", "photo_file_size"]
columns = Dummy.columns.select { |e| defaults_columns.include? e.name }
columns.each do |column|
if column.name == 'avatar_file_name' || column.name == 'photo_file_name'
expect(column.default).to eq('default')
else
expect(column.default.to_i).to eq(1)
end
end
end
end
context "with no attachment" do context "with no attachment" do
it "raises an error" do it "raises an error" do
assert_raises ArgumentError do assert_raises ArgumentError do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment