This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
rails_steps.rb
236 lines (207 loc) · 6.62 KB
/
rails_steps.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
Given /^I generate a new rails application$/ do
steps %{
When I run `bundle exec #{new_application_command} #{APP_NAME} --skip-bundle`
And I cd to "#{APP_NAME}"
And I turn off class caching
And I fix the application.rb for 3.0.12
And I write to "Gemfile" with:
"""
source "http://rubygems.org"
gem "rails", "#{framework_version}"
gem "sqlite3", "1.3.8", :platform => [:ruby, :rbx]
gem "activerecord-jdbcsqlite3-adapter", :platform => :jruby
gem "jruby-openssl", :platform => :jruby
gem "capybara"
gem "gherkin"
gem "aws-sdk"
gem "racc", :platform => :rbx
gem "rubysl", :platform => :rbx
"""
And I remove turbolinks
And I empty the application.js file
And I configure the application to use "paperclip" from this project
}
end
Given "I fix the application.rb for 3.0.12" do
cd(".") do
File.open("config/application.rb", "a") do |f|
f << "ActionController::Base.config.relative_url_root = ''"
end
end
end
Given "I allow the attachment to be submitted" do
cd(".") do
transform_file("app/controllers/users_controller.rb") do |content|
content.gsub("params.require(:user).permit(:name)",
"params.require(:user).permit!")
end
end
end
Given "I remove turbolinks" do
cd(".") do
transform_file("app/assets/javascripts/application.js") do |content|
content.gsub("//= require turbolinks", "")
end
transform_file("app/views/layouts/application.html.erb") do |content|
content.gsub(', "data-turbolinks-track" => true', "")
end
end
end
Given /^I attach :attachment$/ do
attach_attachment("attachment")
end
Given /^I attach :attachment with:$/ do |definition|
attach_attachment("attachment", definition)
end
def attach_attachment(name, definition = nil)
snippet = ""
if using_protected_attributes?
snippet += "attr_accessible :name, :#{name}\n"
end
snippet += "has_attached_file :#{name}"
if definition
snippet += ", \n"
snippet += definition
end
snippet += "\ndo_not_validate_attachment_file_type :#{name}\n"
cd(".") do
transform_file("app/models/user.rb") do |content|
content.sub(/end\Z/, "#{snippet}\nend")
end
end
end
Given "I empty the application.js file" do
cd(".") do
transform_file("app/assets/javascripts/application.js") do |content|
""
end
end
end
Given /^I run a rails generator to generate a "([^"]*)" scaffold with "([^"]*)"$/ do |model_name, attributes|
step %[I successfully run `bundle exec #{generator_command} scaffold #{model_name} #{attributes}`]
end
Given /^I run a paperclip generator to add a paperclip "([^"]*)" to the "([^"]*)" model$/ do |attachment_name, model_name|
step %[I successfully run `bundle exec #{generator_command} paperclip #{model_name} #{attachment_name}`]
end
Given /^I run a migration$/ do
step %[I successfully run `bundle exec rake db:migrate --trace`]
end
When /^I rollback a migration$/ do
step %[I successfully run `bundle exec rake db:rollback STEPS=1 --trace`]
end
Given /^I update my new user view to include the file upload field$/ do
steps %{
Given I overwrite "app/views/users/new.html.erb" with:
"""
<%= form_for @user, :html => { :multipart => true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<%= submit_tag "Submit" %>
<% end %>
"""
}
end
Given /^I update my user view to include the attachment$/ do
steps %{
Given I overwrite "app/views/users/show.html.erb" with:
"""
<p>Name: <%= @user.name %></p>
<p>Attachment: <%= image_tag @user.attachment.url %></p>
"""
}
end
Given /^I add this snippet to the User model:$/ do |snippet|
file_name = "app/models/user.rb"
cd(".") do
content = File.read(file_name)
File.open(file_name, 'w') { |f| f << content.sub(/end\Z/, "#{snippet}\nend") }
end
end
Given /^I add this snippet to config\/application.rb:$/ do |snippet|
file_name = "config/application.rb"
cd(".") do
content = File.read(file_name)
File.open(file_name, 'w') {|f| f << content.sub(/class Application < Rails::Application.*$/, "class Application < Rails::Application\n#{snippet}\n")}
end
end
Given /^I start the rails application$/ do
cd(".") do
require "./config/environment"
require "capybara/rails"
end
end
Given /^I reload my application$/ do
Rails::Application.reload!
end
When /^I turn off class caching$/ do
cd(".") do
file = "config/environments/test.rb"
config = IO.read(file)
config.gsub!(%r{^\s*config.cache_classes.*$},
"config.cache_classes = false")
File.open(file, "w"){|f| f.write(config) }
end
end
Then /^the file at "([^"]*)" should be the same as "([^"]*)"$/ do |web_file, path|
expected = IO.read(path)
actual = read_from_web(web_file)
expect(actual).to eq(expected)
end
When /^I configure the application to use "([^\"]+)" from this project$/ do |name|
append_to_gemfile "gem '#{name}', :path => '#{PROJECT_ROOT}'"
steps %{And I run `bundle install --local`}
end
When /^I configure the application to use "([^\"]+)"$/ do |gem_name|
append_to_gemfile "gem '#{gem_name}'"
end
When /^I append gems from Appraisal Gemfile$/ do
File.read(ENV['BUNDLE_GEMFILE']).split(/\n/).each do |line|
if line =~ /^gem "(?!rails|appraisal)/
append_to_gemfile line.strip
end
end
end
When /^I comment out the gem "([^"]*)" from the Gemfile$/ do |gemname|
comment_out_gem_in_gemfile gemname
end
Given /^I am using Rails newer than ([\d\.]+)$/ do |version|
if framework_version < version
pending "Not supported in Rails < #{version}"
end
end
Given(/^I add a "(.*?)" processor in "(.*?)"$/) do |processor, directory|
filename = "#{directory}/#{processor}.rb"
cd(".") do
FileUtils.mkdir_p directory
File.open(filename, "w") do |f|
f.write(<<-CLASS)
module Paperclip
class #{processor.capitalize} < Processor
def make
basename = File.basename(file.path, File.extname(file.path))
dst_format = options[:format] ? ".\#{options[:format]}" : ''
dst = Tempfile.new([basename, dst_format])
dst.binmode
convert(':src :dst',
src: File.expand_path(file.path),
dst: File.expand_path(dst.path)
)
dst
end
end
end
CLASS
end
end
end
def transform_file(filename)
if File.exist?(filename)
content = File.read(filename)
File.open(filename, "w") do |f|
content = yield(content)
f.write(content)
end
end
end