learn_rails 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/
19
+ .rvmrc
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 1.9.3
5
+
6
+ notifications:
7
+ email:
8
+ on_failure: always
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in learn_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Peter-Jan Celis
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # Learn Rails
2
+ [![Build Status](https://secure.travis-ci.org/pjc/learn_rails.png?branch=master)][travis]
3
+ [![Dependency Status](https://gemnasium.com/pjc/learn_rails.png)][gemnasium]
4
+ [![Code Climate](https://codeclimate.com/github/pjc/learn_rails.png)][codeclimate]
5
+ [![Coverage Status](https://coveralls.io/repos/pjc/learn_rails/badge.png?branch=master)][coveralls]
6
+
7
+ [travis]: https://travis-ci.org/pjc/learn_rails
8
+ [codeclimate]: https://codeclimate.com/github/pjc/learn_rails
9
+ [gemnasium]: https://gemnasium.com/pjc/learn_rails
10
+ [coveralls]: https://coveralls.io/r/pjc/learn_rails
11
+
12
+ Learn Ruby on Rails by seeing all the ruby code behind the rails (and ruby) magic you use.
13
+
14
+ Currently works for:
15
+ - accessors (`attr_reader`, `attr_writer`, `attr_accessor`)
16
+ - associations (`belongs_to` and `has_one` only so far)
17
+
18
+ Also note that for associations, the only options that work so far are `class_name`, `readonly`, `primary_key` and `foreign_key`.
19
+
20
+ Coming soon:
21
+ - All rails magic / helpers
22
+ - Annotate your rails app with a single command (e.g. associations code shown in each model)
23
+
24
+ ## Installation
25
+
26
+ Add this line to your application's Gemfile:
27
+
28
+ gem 'learn_rails'
29
+
30
+ And then execute:
31
+
32
+ $ bundle
33
+
34
+ Or install it yourself as:
35
+
36
+ $ gem install learn_rails
37
+
38
+ ## Usage
39
+
40
+ This gem installs an executable called `learn` and a general purpose command called `rails`, so all you need to do is go to the terminal and type:
41
+
42
+ $ learn rails the_rails_magic_I_want_to_learn_about
43
+
44
+ Say you want to know what the `attr_accessor :color, :size` line in one of the models in your code really does. Type the following terminal command:
45
+
46
+ $ learn rails attr_accessor :color, :size
47
+
48
+ The gem will now print the following ruby code in your terminal output:
49
+
50
+ # def color
51
+ # @color
52
+ # end
53
+ #
54
+ # def color=(value)
55
+ # @color = value
56
+ # end
57
+ #
58
+ # def size
59
+ # @size
60
+ # end
61
+ #
62
+ # def size=(value)
63
+ # @size = value
64
+ # end
65
+
66
+ Sweet!
67
+
68
+ Maybe you also want to know a bit more about the association `has_one :task, class_name: "ToDo"` in your `user.rb` model. You would now type:
69
+
70
+ $ learn rails user has_one :task, class_name: "ToDo"
71
+
72
+ (Note that we included the model name in the command, because our gem needs it to generate the ruby code!)
73
+
74
+ This is the terminal output you'll see:
75
+
76
+ # def task(force_reload = false)
77
+ # @task = nil if force_reload
78
+ # @task ||= ToDo.find_by_user_id(self.id)
79
+ # end
80
+ #
81
+ # def task=(task)
82
+ # task.user_id = self.id
83
+ # task.save
84
+ # end
85
+ #
86
+ # def build_task(attributes = {})
87
+ # attributes[:user_id] = self.id
88
+ # ToDo.new(attributes)
89
+ # end
90
+ #
91
+ # def create_task(attributes = {})
92
+ # attributes[:user_id] = self.id
93
+ # ToDo.create(attributes)
94
+ # end
95
+ #
96
+ # def create_task!(attributes = {})
97
+ # attributes[:user_id] = self.id
98
+ # ToDo.create!(attributes)
99
+ # end
100
+
101
+ Happy learning!
102
+
103
+ ## 2 rules to remember
104
+
105
+ 1. Always use the new ruby hash syntax for the options you specify. So type `class_name: "ToDo"` and not `class_name => "ToDo"`.
106
+
107
+ 2. Don't forget to prepend your associations with the ruby model they are defined in (see example above).
108
+
109
+ Don't worry too much about rule 2 though: If you forget to give the model name when we need it, we'll ask for it. And when you give it when we don't need it, we'll ignore it.
110
+
111
+ ## List of available commands
112
+
113
+ Accessors:
114
+
115
+ $ learn rails attr_reader :as, :many, :attributes, :as, :you, :want
116
+
117
+ $ learn rails attr_writer :as, :many, :attributes, :as, :you, :want
118
+
119
+ $ learn rails attr_accessor :as, :many, :attributes, :as, :you, :want
120
+
121
+ Associations:
122
+
123
+ $ learn rails mymodelname belongs_to :associate
124
+
125
+ $ learn rails mymodelname has_one :associate
126
+
127
+ Options for each association: `class_name: "Whatever"`, `foreign_key: "foreign_id"`, `primary_id: "primary_id"`, `readonly: true`
128
+
129
+ ## Changelog
130
+
131
+ This gem has yet to be published at Rubygems. Coming soon!
132
+
133
+ ## Feedback
134
+
135
+ Comments and feedback are very welcome on [our github issues page](https://github.com/pjc/learn_rails/issues?state=open).
136
+
137
+ ## Contributing
138
+
139
+ 1. Fork it
140
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
141
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
142
+ 4. Push to the branch (`git push origin my-new-feature`)
143
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require "bundler/gem_tasks"
2
+ require 'cucumber/rake/task'
3
+ require 'rspec/core/rake_task'
4
+ require 'coveralls/rake/task'
5
+
6
+ Cucumber::Rake::Task.new(:features) do |t|
7
+ t.cucumber_opts = "features --format pretty"
8
+ end
9
+
10
+ RSpec::Core::RakeTask.new(:spec) do |t|
11
+ t.rspec_opts = "--color --format documentation"
12
+ end
13
+
14
+ Coveralls::RakeTask.new
15
+
16
+ task :default => [:spec, :features, 'coveralls:push']
data/bin/learn ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'learn_rails/cli'
4
+ LearnRails::CLI.start
@@ -0,0 +1,122 @@
1
+ Feature: Accessors
2
+ In order to understand rails accessors
3
+ As a learn_rails gem user
4
+ I want to see the right ruby code in the terminal
5
+
6
+ Scenario: attr_reader
7
+ When I run `learn rails attr_reader :whatever`
8
+ Then the output should contain:
9
+ """
10
+ # def whatever
11
+ # @whatever
12
+ # end
13
+ """
14
+
15
+ Scenario: attr_writer
16
+ When I run `learn rails attr_writer :whatever`
17
+ Then the output should contain:
18
+ """
19
+ # def whatever=(value)
20
+ # @whatever = value
21
+ # end
22
+ """
23
+
24
+ Scenario: attr_accessor
25
+ When I run `learn rails attr_accessor :whatever`
26
+ Then the output should contain:
27
+ """
28
+ # def whatever
29
+ # @whatever
30
+ # end
31
+ #
32
+ # def whatever=(value)
33
+ # @whatever = value
34
+ # end
35
+ """
36
+
37
+ Scenario: attr_reader with anymodelname
38
+ When I run `learn rails anymodelname attr_reader :whatever`
39
+ Then the output should contain:
40
+ """
41
+ # def whatever
42
+ # @whatever
43
+ # end
44
+ """
45
+
46
+ Scenario: attr_writer with anymodelname
47
+ When I run `learn rails anymodelname attr_writer :whatever`
48
+ Then the output should contain:
49
+ """
50
+ # def whatever=(value)
51
+ # @whatever = value
52
+ # end
53
+ """
54
+
55
+ Scenario: attr_accessor with anymodelname
56
+ When I run `learn rails anymodelname attr_accessor :whatever`
57
+ Then the output should contain:
58
+ """
59
+ # def whatever
60
+ # @whatever
61
+ # end
62
+ #
63
+ # def whatever=(value)
64
+ # @whatever = value
65
+ # end
66
+ """
67
+
68
+ Scenario: attr_reader with multiple attributes
69
+ When I run `learn rails attr_reader :whatever, :another`
70
+ Then the output should contain:
71
+ """
72
+ # def whatever
73
+ # @whatever
74
+ # end
75
+ #
76
+ # def another
77
+ # @another
78
+ # end
79
+ """
80
+
81
+ Scenario: attr_writer with multiple attrributes
82
+ When I run `learn rails attr_writer :whatever, :another`
83
+ Then the output should contain:
84
+ """
85
+ # def whatever=(value)
86
+ # @whatever = value
87
+ # end
88
+ #
89
+ # def another=(value)
90
+ # @another = value
91
+ # end
92
+ """
93
+
94
+ Scenario: attr_accessor with multiple attributes
95
+ When I run `learn rails attr_accessor :whatever, :another`
96
+ Then the output should contain:
97
+ """
98
+ # def whatever
99
+ # @whatever
100
+ # end
101
+ #
102
+ # def whatever=(value)
103
+ # @whatever = value
104
+ # end
105
+ #
106
+ # def another
107
+ # @another
108
+ # end
109
+ #
110
+ # def another=(value)
111
+ # @another = value
112
+ # end
113
+ """
114
+
115
+ Scenario: valid statement but with comma right after
116
+ When I run `learn rails attr_reader :whatever,`
117
+ Then the output should not contain ","
118
+
119
+ Scenario: valid statement but with standalone comma after it
120
+ When I run `learn rails attr_reader :whatever ,`
121
+ Then the output should not contain ","
122
+ And the output should not contain "@ "
@@ -0,0 +1,506 @@
1
+ Feature: Associations
2
+ In order to understand rails associations
3
+ As a learn_rails gem user
4
+ I want to see the right ruby code in the terminal
5
+
6
+ Scenario: belongs_to association
7
+ When I run `learn rails task belongs_to :user`
8
+ Then the output should contain:
9
+ """
10
+ # def user(force_reload = false)
11
+ # @user = nil if force_reload
12
+ # @user ||= User.find_by_id(self.user_id)
13
+ # end
14
+ #
15
+ # def user=(user)
16
+ # self.user_id = user.id
17
+ # end
18
+ #
19
+ # def build_user(attributes = {})
20
+ # self.user = User.new(attributes)
21
+ # end
22
+ #
23
+ # def create_user(attributes = {})
24
+ # self.user = User.create(attributes)
25
+ # end
26
+ #
27
+ # def create_user!(attributes = {})
28
+ # self.user = User.create!(attributes)
29
+ # end
30
+ """
31
+
32
+ Scenario: belongs_to association without the model name
33
+ When I run `learn rails belongs_to :user`
34
+ Then the output should contain:
35
+ """
36
+ # def user(force_reload = false)
37
+ # @user = nil if force_reload
38
+ # @user ||= User.find_by_id(self.user_id)
39
+ # end
40
+ #
41
+ # def user=(user)
42
+ # self.user_id = user.id
43
+ # end
44
+ #
45
+ # def build_user(attributes = {})
46
+ # self.user = User.new(attributes)
47
+ # end
48
+ #
49
+ # def create_user(attributes = {})
50
+ # self.user = User.create(attributes)
51
+ # end
52
+ #
53
+ # def create_user!(attributes = {})
54
+ # self.user = User.create!(attributes)
55
+ # end
56
+ """
57
+
58
+ Scenario: belongs_to association with multi-word model
59
+ When I run `learn rails manual belongs_to :car_part`
60
+ Then the output should contain:
61
+ """
62
+ # def car_part(force_reload = false)
63
+ # @car_part = nil if force_reload
64
+ # @car_part ||= CarPart.find_by_id(self.car_part_id)
65
+ # end
66
+ #
67
+ # def car_part=(car_part)
68
+ # self.car_part_id = car_part.id
69
+ # end
70
+ #
71
+ # def build_car_part(attributes = {})
72
+ # self.car_part = CarPart.new(attributes)
73
+ # end
74
+ #
75
+ # def create_car_part(attributes = {})
76
+ # self.car_part = CarPart.create(attributes)
77
+ # end
78
+ #
79
+ # def create_car_part!(attributes = {})
80
+ # self.car_part = CarPart.create!(attributes)
81
+ # end
82
+ """
83
+ And the output should not contain "carpart"
84
+ And the output should not contain "Car_Part"
85
+
86
+ Scenario: belongs_to association with :class_name option
87
+ When I run `learn rails task belongs_to :user, class_name: "Person"`
88
+ Then the output should contain:
89
+ """
90
+ # def user(force_reload = false)
91
+ # @user = nil if force_reload
92
+ # @user ||= Person.find_by_id(self.user_id)
93
+ # end
94
+ #
95
+ # def user=(user)
96
+ # self.user_id = user.id
97
+ # end
98
+ #
99
+ # def build_user(attributes = {})
100
+ # self.user = Person.new(attributes)
101
+ # end
102
+ #
103
+ # def create_user(attributes = {})
104
+ # self.user = Person.create(attributes)
105
+ # end
106
+ #
107
+ # def create_user!(attributes = {})
108
+ # self.user = Person.create!(attributes)
109
+ # end
110
+ """
111
+ And the output should not contain "# @user ||= User.find_by_id(self.user_id)"
112
+ And the output should not contain "# self.user = User.new(attributes)"
113
+ And the output should not contain "# self.user = User.create(attributes)"
114
+ And the output should not contain "# self.user = User.create!(attributes)"
115
+
116
+ Scenario: belongs_to association with :foreign_key option
117
+ When I run `learn rails task belongs_to :user, foreign_key: "person_id"`
118
+ Then the output should contain:
119
+ """
120
+ # def user(force_reload = false)
121
+ # @user = nil if force_reload
122
+ # @user ||= User.find_by_id(self.person_id)
123
+ # end
124
+ #
125
+ # def user=(user)
126
+ # self.person_id = user.id
127
+ # end
128
+ #
129
+ # def build_user(attributes = {})
130
+ # self.user = User.new(attributes)
131
+ # end
132
+ #
133
+ # def create_user(attributes = {})
134
+ # self.user = User.create(attributes)
135
+ # end
136
+ #
137
+ # def create_user!(attributes = {})
138
+ # self.user = User.create!(attributes)
139
+ # end
140
+ """
141
+ And the output should not contain "# @user ||= User.find_by_id(self.user_id)"
142
+ And the output should not contain "# self.user_id = user.id"
143
+
144
+ Scenario: belongs_to association with :primary_key option
145
+ When I run `learn rails task belongs_to :user, primary_key: "todo_id"`
146
+ Then the output should contain:
147
+ """
148
+ # def user(force_reload = false)
149
+ # @user = nil if force_reload
150
+ # @user ||= User.find_by_id(self.user_id)
151
+ # end
152
+ #
153
+ # def user=(user)
154
+ # self.user_id = user.todo_id
155
+ # end
156
+ #
157
+ # def build_user(attributes = {})
158
+ # self.user = User.new(attributes)
159
+ # end
160
+ #
161
+ # def create_user(attributes = {})
162
+ # self.user = User.create(attributes)
163
+ # end
164
+ #
165
+ # def create_user!(attributes = {})
166
+ # self.user = User.create!(attributes)
167
+ # end
168
+ """
169
+ And the output should not contain "# self.user_id = user.id"
170
+
171
+ Scenario: belongs_to association with :readonly option
172
+ When I run `learn rails task belongs_to :user, readonly: true`
173
+ Then the output should contain:
174
+ """
175
+ # def user(force_reload = false)
176
+ # @user = nil if force_reload
177
+ # @user ||= User.find_by_id(self.user_id)
178
+ # end
179
+ #
180
+ # def build_user(attributes = {})
181
+ # self.user = User.new(attributes)
182
+ # end
183
+ #
184
+ # def create_user(attributes = {})
185
+ # self.user = User.create(attributes)
186
+ # end
187
+ #
188
+ # def create_user!(attributes = {})
189
+ # self.user = User.create!(attributes)
190
+ # end
191
+ """
192
+ And the output should not contain "# def user=(user)"
193
+ And the output should not contain "# self.user_id = user.id"
194
+
195
+ Scenario: belongs_to association with :conditions option
196
+ When I run `learn rails task belongs_to :user, conditions: { status: "active"}`
197
+ And the output should contain "# def user(force_reload = false)"
198
+ And the output should contain:
199
+ """
200
+ # @user ||= User.first(:conditions => {:id => self.user_id, :status => "active"})
201
+ """
202
+
203
+ Scenario: has_one association
204
+ When I run `learn rails user has_one :task`
205
+ Then the output should contain:
206
+ """
207
+ # def task(force_reload = false)
208
+ # @task = nil if force_reload
209
+ # @task ||= Task.find_by_user_id(self.id)
210
+ # end
211
+ #
212
+ # def task=(task)
213
+ # task.user_id = self.id
214
+ # task.save
215
+ # end
216
+ #
217
+ # def build_task(attributes = {})
218
+ # attributes[:user_id] = self.id
219
+ # Task.new(attributes)
220
+ # end
221
+ #
222
+ # def create_task(attributes = {})
223
+ # attributes[:user_id] = self.id
224
+ # Task.create(attributes)
225
+ # end
226
+ #
227
+ # def create_task!(attributes = {})
228
+ # attributes[:user_id] = self.id
229
+ # Task.create!(attributes)
230
+ # end
231
+ """
232
+
233
+ Scenario: has_one association without the model name
234
+ Given PENDING: Aruba process keeps hanging, need to test for terminal question
235
+ When I run `learn rails has_one :task`
236
+ Then the output should contain "What is the model name for this association?"
237
+
238
+ Scenario: has_one association with model name given when prompted
239
+ Given PENDING: Aruba process keeps hanging, need to test for terminal question
240
+ When I run `learn rails has_one :task`
241
+ And I type `user`
242
+ Then the output should contain:
243
+ """
244
+ # def task(force_reload = false)
245
+ # @task = nil if force_reload
246
+ # @task ||= Task.find_by_user_id(self.id)
247
+ # end
248
+ #
249
+ # def task=(task)
250
+ # task.user_id = self.id
251
+ # task.save
252
+ # end
253
+ #
254
+ # def build_task(attributes = {})
255
+ # attributes[:user_id] = self.id
256
+ # Task.new(attributes)
257
+ # end
258
+ #
259
+ # def create_task(attributes = {})
260
+ # attributes[:user_id] = self.id
261
+ # Task.create(attributes)
262
+ # end
263
+ #
264
+ # def create_task!(attributes = {})
265
+ # attributes[:user_id] = self.id
266
+ # Task.create!(attributes)
267
+ # end
268
+ """
269
+
270
+ Scenario: has_one association with multi-word model
271
+ When I run `learn rails user has_one :voodoo_pop`
272
+ Then the output should contain:
273
+ """
274
+ # def voodoo_pop(force_reload = false)
275
+ # @voodoo_pop = nil if force_reload
276
+ # @voodoo_pop ||= VoodooPop.find_by_user_id(self.id)
277
+ # end
278
+ #
279
+ # def voodoo_pop=(voodoo_pop)
280
+ # voodoo_pop.user_id = self.id
281
+ # voodoo_pop.save
282
+ # end
283
+ #
284
+ # def build_voodoo_pop(attributes = {})
285
+ # attributes[:user_id] = self.id
286
+ # VoodooPop.new(attributes)
287
+ # end
288
+ #
289
+ # def create_voodoo_pop(attributes = {})
290
+ # attributes[:user_id] = self.id
291
+ # VoodooPop.create(attributes)
292
+ # end
293
+ #
294
+ # def create_voodoo_pop!(attributes = {})
295
+ # attributes[:user_id] = self.id
296
+ # VoodooPop.create!(attributes)
297
+ # end
298
+ """
299
+ And the output should not contain "voodoopop"
300
+ And the output should not contain "Voodoo_Pop"
301
+
302
+ Scenario: has_one association with :class_name option using the old hash syntax
303
+ When I run `learn rails user has_one :task, :class_name => "ToDo"`
304
+ Then the output should contain:
305
+ """
306
+ # def task(force_reload = false)
307
+ # @task = nil if force_reload
308
+ # @task ||= ToDo.find_by_user_id(self.id)
309
+ # end
310
+ #
311
+ # def task=(task)
312
+ # task.user_id = self.id
313
+ # task.save
314
+ # end
315
+ #
316
+ # def build_task(attributes = {})
317
+ # attributes[:user_id] = self.id
318
+ # ToDo.new(attributes)
319
+ # end
320
+ #
321
+ # def create_task(attributes = {})
322
+ # attributes[:user_id] = self.id
323
+ # ToDo.create(attributes)
324
+ # end
325
+ #
326
+ # def create_task!(attributes = {})
327
+ # attributes[:user_id] = self.id
328
+ # ToDo.create!(attributes)
329
+ # end
330
+ """
331
+ And the output should not contain "# @task ||= Task.find_by_user_id(self.id)"
332
+ And the output should not contain "# Task.new(attributes)"
333
+ And the output should not contain "# Task.create(attributes)"
334
+ And the output should not contain "# Task.create!(attributes)"
335
+
336
+ Scenario: has_one association with :class_name option using the new hash syntax
337
+ When I run `learn rails user has_one :task, class_name: "ToDo"`
338
+ Then the output should contain:
339
+ """
340
+ # def task(force_reload = false)
341
+ # @task = nil if force_reload
342
+ # @task ||= ToDo.find_by_user_id(self.id)
343
+ # end
344
+ #
345
+ # def task=(task)
346
+ # task.user_id = self.id
347
+ # task.save
348
+ # end
349
+ #
350
+ # def build_task(attributes = {})
351
+ # attributes[:user_id] = self.id
352
+ # ToDo.new(attributes)
353
+ # end
354
+ #
355
+ # def create_task(attributes = {})
356
+ # attributes[:user_id] = self.id
357
+ # ToDo.create(attributes)
358
+ # end
359
+ #
360
+ # def create_task!(attributes = {})
361
+ # attributes[:user_id] = self.id
362
+ # ToDo.create!(attributes)
363
+ # end
364
+ """
365
+ And the output should not contain "# @task ||= Task.find_by_user_id(self.id)"
366
+ And the output should not contain "# Task.new(attributes)"
367
+ And the output should not contain "# Task.create(attributes)"
368
+ And the output should not contain "# Task.create!(attributes)"
369
+
370
+ Scenario: has_one association with :foreign_key option
371
+ When I run `learn rails user has_one :task, foreign_key: "employee_id"`
372
+ Then the output should contain:
373
+ """
374
+ # def task(force_reload = false)
375
+ # @task = nil if force_reload
376
+ # @task ||= Task.find_by_employee_id(self.id)
377
+ # end
378
+ #
379
+ # def task=(task)
380
+ # task.employee_id = self.id
381
+ # task.save
382
+ # end
383
+ #
384
+ # def build_task(attributes = {})
385
+ # attributes[:employee_id] = self.id
386
+ # Task.new(attributes)
387
+ # end
388
+ #
389
+ # def create_task(attributes = {})
390
+ # attributes[:employee_id] = self.id
391
+ # Task.create(attributes)
392
+ # end
393
+ #
394
+ # def create_task!(attributes = {})
395
+ # attributes[:employee_id] = self.id
396
+ # Task.create!(attributes)
397
+ # end
398
+ """
399
+ And the output should not contain "# @task ||= Task.find_by_user_id(self.id)"
400
+ And the output should not contain "# task.user_id = self.id"
401
+ And the output should not contain "# attributes[:user_id] = self.id"
402
+ And the output should not contain "# attributes[:user_id] = self.id"
403
+ And the output should not contain "# attributes[:user_id] = self.id"
404
+
405
+ Scenario: has_one association with :primary_key option
406
+ When I run `learn rails user has_one :task, primary_key: "primary_id"`
407
+ Then the output should contain:
408
+ """
409
+ # def task(force_reload = false)
410
+ # @task = nil if force_reload
411
+ # @task ||= Task.find_by_user_id(self.primary_id)
412
+ # end
413
+ #
414
+ # def task=(task)
415
+ # task.user_id = self.primary_id
416
+ # task.save
417
+ # end
418
+ #
419
+ # def build_task(attributes = {})
420
+ # attributes[:user_id] = self.primary_id
421
+ # Task.new(attributes)
422
+ # end
423
+ #
424
+ # def create_task(attributes = {})
425
+ # attributes[:user_id] = self.primary_id
426
+ # Task.create(attributes)
427
+ # end
428
+ #
429
+ # def create_task!(attributes = {})
430
+ # attributes[:user_id] = self.primary_id
431
+ # Task.create!(attributes)
432
+ # end
433
+ """
434
+ And the output should not contain "# @task ||= Task.find_by_user_id(self.id)"
435
+ And the output should not contain "# task.user_id = self.id"
436
+ And the output should not contain "# attributes[:user_id] = self.id"
437
+ And the output should not contain "# attributes[:user_id] = self.id"
438
+ And the output should not contain "# attributes[:user_id] = self.id"
439
+
440
+ Scenario: has_one association with :readonly option
441
+ When I run `learn rails user has_one :task, readonly: true`
442
+ Then the output should contain:
443
+ """
444
+ # def task(force_reload = false)
445
+ # @task = nil if force_reload
446
+ # @task ||= Task.find_by_user_id(self.id)
447
+ # end
448
+ #
449
+ # def build_task(attributes = {})
450
+ # attributes[:user_id] = self.id
451
+ # Task.new(attributes)
452
+ # end
453
+ #
454
+ # def create_task(attributes = {})
455
+ # attributes[:user_id] = self.id
456
+ # Task.create(attributes)
457
+ # end
458
+ #
459
+ # def create_task!(attributes = {})
460
+ # attributes[:user_id] = self.id
461
+ # Task.create!(attributes)
462
+ # end
463
+ """
464
+ And the output should not contain "# def task="
465
+ And the output should not contain "# task.user_id = self.id"
466
+
467
+ Scenario: has_many association
468
+ When I run `learn rails user has_many :tasks`
469
+ Then the output should contain:
470
+ """
471
+ # def tasks
472
+ # Task.where(user_id: self.id)
473
+ # end
474
+ """
475
+
476
+ Scenario: has_many association without the model name
477
+ Given PENDING: Aruba process keeps hanging, need to test for terminal question
478
+ When I run `learn rails has_many :tasks`
479
+ Then the output should contain "What is the model name for this association?"
480
+
481
+ Scenario: has_many association with model name given when prompted
482
+ Given PENDING: Aruba process keeps hanging, need to test for terminal question
483
+ When I run `learn rails has_many :tasks`
484
+ And I type `user`
485
+ Then the output should contain:
486
+ """
487
+ # def tasks
488
+ # Task.where(user_id: self.id)
489
+ # end
490
+ """
491
+
492
+ Scenario: has_many association with multi-word model
493
+ When I run `learn rails user has_many :car_parts`
494
+ Then the output should contain:
495
+ """
496
+ # def car_parts
497
+ # CarPart.where(user_id: self.id)
498
+ # end
499
+ """
500
+ And the output should not contain "carpart"
501
+ And the output should not contain "Car_Part"
502
+
503
+ Scenario: non valid rails magic
504
+ When I run `learn rails random gibberish`
505
+ Then the output should contain "No ruby code available."
506
+ And the output should contain "See http://www.github.com/pjc/learn_rails for list of valid instructions."