Skip to content

Commit 0d04ae1

Browse files
author
Jon Yurek
committed
Responses to PR comments
1 parent f29f96b commit 0d04ae1

File tree

8 files changed

+60
-31
lines changed

8 files changed

+60
-31
lines changed

features/basic_integration.feature

+8-15
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ Feature: Rails integration
1414
"""
1515
config.paperclip_defaults = {:url => "/paperclip/custom/:attachment/:style/:filename"}
1616
"""
17-
Given I add this snippet to the User model:
18-
"""
19-
attr_accessible :name, :attachment if Rails::VERSION::MAJOR < 4
20-
has_attached_file :attachment
21-
"""
17+
And I attach :attachment
2218
And I start the rails application
2319
When I go to the new user page
2420
And I fill in "Name" with "something"
@@ -29,10 +25,9 @@ Feature: Rails integration
2925
And the file at "/paperclip/custom/attachments/original/5k.png" should be the same as "test/fixtures/5k.png"
3026

3127
Scenario: Filesystem integration test
32-
Given I add this snippet to the User model:
28+
Given I attach :attachment with:
3329
"""
34-
attr_accessible :name, :attachment if Rails::VERSION::MAJOR < 4
35-
has_attached_file :attachment, :url => "/system/:attachment/:style/:filename"
30+
:url => "/system/:attachment/:style/:filename"
3631
"""
3732
And I start the rails application
3833
When I go to the new user page
@@ -44,14 +39,12 @@ Feature: Rails integration
4439
And the file at "/system/attachments/original/5k.png" should be the same as "test/fixtures/5k.png"
4540

4641
Scenario: S3 Integration test
47-
Given I add this snippet to the User model:
42+
Given I attach :attachment with:
4843
"""
49-
attr_accessible :name, :attachment if Rails::VERSION::MAJOR < 4
50-
has_attached_file :attachment,
51-
:storage => :s3,
52-
:path => "/:attachment/:style/:filename",
53-
:s3_credentials => Rails.root.join("config/s3.yml"),
54-
:styles => { :square => "100x100#" }
44+
:storage => :s3,
45+
:path => "/:attachment/:style/:filename",
46+
:s3_credentials => Rails.root.join("config/s3.yml"),
47+
:styles => { :square => "100x100#" }
5548
"""
5649
And I write to "config/s3.yml" with:
5750
"""

features/rake_tasks.feature

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ Feature: Rake tasks
55
And I run a rails generator to generate a "User" scaffold with "name:string"
66
And I run a paperclip generator to add a paperclip "attachment" to the "User" model
77
And I run a migration
8-
And I add this snippet to the User model:
8+
And I attach :attachment with:
99
"""
10-
attr_accessible :name, :attachment if Rails::VERSION::MAJOR < 4
11-
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
10+
:path => ":rails_root/public/system/:attachment/:style/:filename"
1211
"""
1312

1413
Scenario: Paperclip refresh thumbnails task

features/step_definitions/rails_steps.rb

+30-11
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
gem "gherkin"
1616
gem "aws-sdk"
1717
"""
18-
And I remove turbolinks if it exists
19-
And I empty the application.js file if it exists
18+
And I remove turbolinks
19+
And I empty the application.js file
2020
And I configure the application to use "paperclip" from this project
2121
And I reset Bundler environment variable
2222
And I successfully run `bundle install --local`
@@ -40,7 +40,7 @@
4040
end
4141
end
4242

43-
Given "I remove turbolinks if it exists" do
43+
Given "I remove turbolinks" do
4444
in_current_dir do
4545
transform_file("app/assets/javascripts/application.js") do |content|
4646
content.gsub("//= require turbolinks", "")
@@ -51,7 +51,32 @@
5151
end
5252
end
5353

54-
Given "I empty the application.js file if it exists" do
54+
Given /^I attach :attachment$/ do
55+
attach_attachment("attachment")
56+
end
57+
58+
Given /^I attach :attachment with:$/ do |definition|
59+
attach_attachment("attachment", definition)
60+
end
61+
62+
def attach_attachment(name, definition = nil)
63+
snippet = ""
64+
if using_protected_attributes?
65+
snippet += "attr_accessible :name, :#{name}\n"
66+
end
67+
snippet += "has_attached_file :#{name}"
68+
if definition
69+
snippet += ", \n"
70+
snippet += definition
71+
end
72+
in_current_dir do
73+
transform_file("app/models/user.rb") do |content|
74+
content.sub(/end\Z/, "#{snippet}\nend")
75+
end
76+
end
77+
end
78+
79+
Given "I empty the application.js file" do
5580
in_current_dir do
5681
transform_file("app/assets/javascripts/application.js") do |content|
5782
""
@@ -139,13 +164,7 @@
139164

140165
Then /^the file at "([^"]*)" should be the same as "([^"]*)"$/ do |web_file, path|
141166
expected = IO.read(path)
142-
actual = if web_file.match %r{^https?://}
143-
Net::HTTP.get(URI.parse(web_file))
144-
else
145-
visit(web_file)
146-
page.source
147-
end
148-
actual.force_encoding("UTF-8") if actual.respond_to?(:force_encoding)
167+
actual = read_from_web(web_file)
149168
actual.should == expected
150169
end
151170

features/support/file_helpers.rb

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ def comment_out_gem_in_gemfile(gemname)
1919
File.open("Gemfile", 'w'){ |file| file.write(gemfile) }
2020
end
2121
end
22+
23+
def read_from_web(url)
24+
file = if url.match %r{^https?://}
25+
Net::HTTP.get(URI.parse(url))
26+
else
27+
visit(url)
28+
page.source
29+
end
30+
file.force_encoding("UTF-8") if file.respond_to?(:force_encoding)
31+
end
2232
end
2333

2434
World(FileHelpers)

features/support/rails.rb

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def framework_major_version
3535
framework_version.split(".").first.to_i
3636
end
3737

38+
def using_protected_attributes?
39+
framework_major_version < 4
40+
end
41+
3842
def new_application_command
3943
"rails new"
4044
end

gemfiles/4.0.gemfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ gem "sqlite3", :platform=>:ruby
88
gem "rails", "~> 4.0.0"
99
gem "paperclip", :path=>"../"
1010

11-
gemspec :path=>"../"
11+
gemspec :path=>"../"

test/helper.rb

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ def setup
5151
ActiveRecord::Base.establish_connection(config['test'])
5252
Paperclip.options[:logger] = ActiveRecord::Base.logger
5353

54+
def using_protected_attributes?
55+
ActiveRecord::VERSION::MAJOR < 4
56+
end
57+
5458
def require_everything_in_directory(directory_name)
5559
Dir[File.join(File.dirname(__FILE__), directory_name, '*')].each do |f|
5660
require f

test/paperclip_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class ::Four; end
123123
end
124124
end
125125

126-
if ActiveSupport::VERSION::MAJOR < 4
126+
if using_protected_attributes?
127127
context "that is attr_protected" do
128128
setup do
129129
Dummy.class_eval do

0 commit comments

Comments
 (0)