spbtv_pickle 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +10 -0
  5. data/Gemfile +3 -0
  6. data/Gemfile.lock.development +158 -0
  7. data/History.txt +499 -0
  8. data/License.txt +20 -0
  9. data/README.md +566 -0
  10. data/Rakefile +20 -0
  11. data/Rakefile.d/cucumber.rake +27 -0
  12. data/Rakefile.d/release.rake +44 -0
  13. data/Rakefile.d/rspec.rake +3 -0
  14. data/Rakefile.d/yard.rake +5 -0
  15. data/Todo.txt +3 -0
  16. data/autotest/discover.rb +9 -0
  17. data/features/app/app.rb +128 -0
  18. data/features/app/blueprints.rb +6 -0
  19. data/features/app/fabricators.rb +6 -0
  20. data/features/app/factories.rb +25 -0
  21. data/features/app/views/notifier/email.erb +1 -0
  22. data/features/app/views/notifier/user_email.erb +6 -0
  23. data/features/email/email.feature +64 -0
  24. data/features/generator/generators.feature +59 -0
  25. data/features/path/models_page.feature +44 -0
  26. data/features/path/named_route_page.feature +10 -0
  27. data/features/pickle/create_from_active_record.feature +83 -0
  28. data/features/pickle/create_from_fabrication.feature +46 -0
  29. data/features/pickle/create_from_factory_girl.feature +66 -0
  30. data/features/pickle/create_from_machinist.feature +46 -0
  31. data/features/step_definitions/email_steps.rb +1 -0
  32. data/features/step_definitions/extra_email_steps.rb +12 -0
  33. data/features/step_definitions/fork_steps.rb +4 -0
  34. data/features/step_definitions/generator_steps.rb +52 -0
  35. data/features/step_definitions/path_steps.rb +14 -0
  36. data/features/step_definitions/pickle_steps.rb +1 -0
  37. data/features/step_definitions/raise_error_steps.rb +7 -0
  38. data/features/support/email.rb +1 -0
  39. data/features/support/env.rb +14 -0
  40. data/features/support/paths.rb +47 -0
  41. data/features/support/pickle.rb +27 -0
  42. data/features/support/pickle_app.rb +4 -0
  43. data/init.rb +0 -0
  44. data/lib/generators/pickle_generator.rb +44 -0
  45. data/lib/pickle.rb +26 -0
  46. data/lib/pickle/adapter.rb +183 -0
  47. data/lib/pickle/adapters/active_record.rb +67 -0
  48. data/lib/pickle/adapters/data_mapper.rb +42 -0
  49. data/lib/pickle/adapters/mongoid.rb +54 -0
  50. data/lib/pickle/config.rb +49 -0
  51. data/lib/pickle/email.rb +87 -0
  52. data/lib/pickle/email/parser.rb +18 -0
  53. data/lib/pickle/email/world.rb +13 -0
  54. data/lib/pickle/parser.rb +65 -0
  55. data/lib/pickle/parser/matchers.rb +87 -0
  56. data/lib/pickle/path.rb +45 -0
  57. data/lib/pickle/path/world.rb +5 -0
  58. data/lib/pickle/session.rb +244 -0
  59. data/lib/pickle/session/parser.rb +34 -0
  60. data/lib/pickle/version.rb +3 -0
  61. data/lib/pickle/world.rb +14 -0
  62. data/lib/spbtv_pickle.rb +1 -0
  63. data/rails_generators/pickle/pickle_generator.rb +31 -0
  64. data/rails_generators/pickle/templates/email.rb +21 -0
  65. data/rails_generators/pickle/templates/email_steps.rb +65 -0
  66. data/rails_generators/pickle/templates/paths.rb +47 -0
  67. data/rails_generators/pickle/templates/pickle.rb +29 -0
  68. data/rails_generators/pickle/templates/pickle_steps.rb +105 -0
  69. data/spbtv_pickle.gemspec +38 -0
  70. data/spec/pickle/adapter_spec.rb +203 -0
  71. data/spec/pickle/config_spec.rb +112 -0
  72. data/spec/pickle/email/parser_spec.rb +51 -0
  73. data/spec/pickle/email_spec.rb +187 -0
  74. data/spec/pickle/parser/matchers_spec.rb +70 -0
  75. data/spec/pickle/parser_spec.rb +165 -0
  76. data/spec/pickle/path_spec.rb +120 -0
  77. data/spec/pickle/session_spec.rb +448 -0
  78. data/spec/pickle_spec.rb +24 -0
  79. data/spec/spec_helper.rb +78 -0
  80. metadata +370 -0
@@ -0,0 +1,120 @@
1
+ require 'spec_helper'
2
+
3
+ require 'pickle/path'
4
+
5
+ describe Pickle::Path do
6
+ include Pickle::Path
7
+
8
+ describe "#path_to_pickle, when the model doesn't exist" do
9
+ before do
10
+ allow(self).to receive(:model).and_return(nil)
11
+ end
12
+ it "('that user', :extra => 'new comment') should raise the error raised by model!" do
13
+ expect { path_to_pickle "that user", "new comment" }.to raise_error(RuntimeError, 'Could not figure out a path for ["that user", "new comment"] {}')
14
+ end
15
+
16
+ end
17
+
18
+ describe "#path_to_pickle" do
19
+ describe "when model returns a user" do
20
+ let :user_class do
21
+ double 'User', :name => 'User'
22
+ end
23
+
24
+ let :user do
25
+ double 'user', :class => user_class
26
+ end
27
+
28
+ before do
29
+ allow(self).to receive(:model).and_return(user)
30
+ end
31
+
32
+ it "('a user', 'the user: \"fred\"') should retrieve 'a user', and 'the user: \"fred\"' models" do
33
+ expect(self).to receive(:model).with('a user')
34
+ expect(self).to receive(:model).with('the user: "fred"')
35
+ allow(self).to receive(:user_user_path).and_return('the path')
36
+ expect(path_to_pickle('a user', 'the user: "fred"')).to eq('the path')
37
+ end
38
+
39
+ it "('a user', :action => 'foo') should return foo_user_path(<user>)" do
40
+ expect(self).to receive(:foo_user_path).with(user).and_return('the path')
41
+ expect(path_to_pickle('a user', :action => 'foo')).to eq('the path')
42
+ end
43
+
44
+ it "('a user', :action => 'foo') should raise informative error if foo_user_path not defined" do
45
+ expect(self).to receive(:foo_user_path).with(user).and_raise(NoMethodError)
46
+ expect { path_to_pickle('a user', :action => 'foo') }.to raise_error(Exception, /Could not figure out a path for/)
47
+ end
48
+
49
+ it "('a user', :segment => 'foo') should return user_foo_path(<user>)" do
50
+ expect(self).to receive(:user_foo_path).with(user).and_return('the path')
51
+ expect(path_to_pickle('a user', :segment => 'foo')).to eq('the path')
52
+ end
53
+
54
+ it "('a user', :segment => 'foo') should raise informative error if foo_user_path not defined" do
55
+ expect(self).to receive(:user_foo_path).with(user).and_raise(NoMethodError)
56
+ expect { path_to_pickle('a user', :segment => 'foo') }.to raise_error(Exception, /Could not figure out a path for/)
57
+ end
58
+
59
+ it "('a user', :action => 'new', :segment => 'comment') should return new_user_comment_path(<user>)" do
60
+ expect(self).to receive(:new_user_comment_path).with(user).and_return('the path')
61
+ expect(path_to_pickle('a user', :segment => 'comment', :action => 'new')).to eq('the path')
62
+ end
63
+
64
+ it "('a user', :action => 'new', :segment => 'comment') should raise informative error if new_user_comment_path not defined" do
65
+ expect(self).to receive(:new_user_comment_path).with(user).and_raise(NoMethodError)
66
+ expect { path_to_pickle('a user', :action => 'new', :segment => 'comment') }.to raise_error(Exception, /Could not figure out a path for/)
67
+ end
68
+
69
+ it "('a user', :extra => 'new comment') should return new_user_comment_path(<user>)" do
70
+ expect(self).to receive(:new_user_comment_path).with(user).and_return('the path')
71
+ expect(path_to_pickle('a user', :extra => 'new comment')).to eq('the path')
72
+ end
73
+
74
+ it "('a user', :extra => 'new comment') should raise informative error if new_user_comment_path not defined" do
75
+ expect(self).to receive(:new_user_comment_path).with(user).and_raise(NoMethodError)
76
+ expect { path_to_pickle('a user', :extra => 'new comment') }.to raise_error(Exception, /Could not figure out a path for/)
77
+ end
78
+
79
+ describe "when args is a list of pickle and non pickle models" do
80
+ before do
81
+ allow(self).to receive(:model).with("account").and_return(nil)
82
+ end
83
+
84
+ it "('account', 'the user') should return account_user_path(<user>)" do
85
+ expect(self).to receive(:account_user_path).with(user).and_return("the path")
86
+ expect(path_to_pickle('account', 'the user')).to eq('the path')
87
+ end
88
+ end
89
+
90
+ describe "(private API)" do
91
+ it "('a user', :extra => 'new ish comment') should try combinations of 'new', 'ish', 'comment'" do
92
+ expect(self).to receive(:pickle_path_for_resources_action_segment).with([user], '', 'new_ish_comment').once
93
+ expect(self).to receive(:pickle_path_for_resources_action_segment).with([user], 'new', 'ish_comment').once
94
+ expect(self).to receive(:pickle_path_for_resources_action_segment).with([user], 'new_ish', 'comment').once
95
+ expect(self).to receive(:pickle_path_for_resources_action_segment).with([user], 'new_ish_comment', '').once
96
+ expect { path_to_pickle('a user', :extra => 'new ish comment') }.to raise_error(Exception, /Could not figure out a path for/)
97
+ end
98
+ end
99
+ end
100
+
101
+ describe "when model returns namespaced user" do
102
+ let :user_class do
103
+ double 'User', :name => 'Admin::User'
104
+ end
105
+
106
+ let :user do
107
+ double 'user', :class => user_class
108
+ end
109
+
110
+ before do
111
+ allow(self).to receive(:model).and_return(user)
112
+ end
113
+
114
+ it "('a user', :action => 'foo') should return foo_admin_user_path(<user>)" do
115
+ expect(self).to receive(:foo_admin_user_path).with(user).and_return('the path')
116
+ expect(path_to_pickle('a user', :action => 'foo')).to eq('the path')
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,448 @@
1
+ require 'spec_helper'
2
+
3
+ # TODO: remove this and push AR stuff into ORM adapter
4
+ module ActiveRecord
5
+ class Base
6
+ end
7
+ end
8
+
9
+ module DataMapper
10
+ class Model
11
+ end
12
+ end
13
+
14
+ describe Pickle::Session do
15
+ include Pickle::Session
16
+
17
+ let :user_class do
18
+ double("User class", :name => 'User')
19
+ end
20
+
21
+ let :user do
22
+ double("user", :class => user_class, :id => 100)
23
+ end
24
+
25
+ let :user_factory do
26
+ Pickle::Adapter::Orm.new(user_class)
27
+ end
28
+
29
+ before do
30
+ allow(config).to receive(:factories).and_return('user' => user_factory)
31
+ end
32
+
33
+ shared_examples_for "Pickle::Session proxy missing methods to parser" do
34
+ it "should forward to pickle_parser it responds_to them" do
35
+ expect(subject.pickle_parser).to receive(:parse_model)
36
+ subject.parse_model
37
+ end
38
+
39
+ it "should raise error if pickle_parser don't know about em" do
40
+ expect { subject.parse_infinity }.to raise_error
41
+ end
42
+ end
43
+
44
+ describe "including Pickle::Session" do
45
+ subject do
46
+ self
47
+ end
48
+
49
+ it_should_behave_like "Pickle::Session proxy missing methods to parser"
50
+ end
51
+
52
+ describe "extending Pickle::Session" do
53
+ subject do
54
+ Object.new.tap {|object| object.extend Pickle::Session }
55
+ end
56
+
57
+ it_should_behave_like "Pickle::Session proxy missing methods to parser"
58
+ end
59
+
60
+ shared_examples_for "after storing a single user" do
61
+ it "created_models('user') should be array containing the original user" do
62
+ expect(created_models('user')).to eq([user])
63
+ end
64
+
65
+ describe "the original user should be retrievable with" do
66
+ it "created_model('the user')" do
67
+ expect(created_model('the user')).to eq(user)
68
+ end
69
+
70
+ it "created_model('1st user')" do
71
+ expect(created_model('1st user')).to eq(user)
72
+ end
73
+
74
+ it "created_model('last user')" do
75
+ expect(created_model('last user')).to eq(user)
76
+ end
77
+ end
78
+
79
+ describe "(found from db)" do
80
+ let :user_from_db do
81
+ user.dup.tap {|from_db| allow(from_db).to receive(:id).and_return(100) }
82
+ end
83
+
84
+ before do
85
+ allow(Pickle::Adapter).to receive(:get_model).with(user_class, 100).and_return(user_from_db)
86
+ end
87
+
88
+ it "models('user') should be array containing user" do
89
+ expect(models('user')).to eq([user_from_db])
90
+ end
91
+
92
+ describe "user should be retrievable with" do
93
+ it "model('the user')" do
94
+ expect(model('the user')).to eq(user_from_db)
95
+ end
96
+
97
+ it "model('1st user')" do
98
+ expect(model('1st user')).to eq(user_from_db)
99
+ end
100
+
101
+ it "model('last user')" do
102
+ expect(model('last user')).to eq(user_from_db)
103
+ end
104
+
105
+ it "model!('last user')" do
106
+ expect(model('last user')).to eq(user_from_db)
107
+ end
108
+ end
109
+ end
110
+ end
111
+
112
+ describe "#create_model" do
113
+ before do
114
+ allow(user_factory).to receive(:create).and_return(user)
115
+ end
116
+
117
+ describe "('a user')" do
118
+ it "should call user_factory.create({})" do
119
+ expect(user_factory).to receive(:create).with({})
120
+ create_model('a user')
121
+ end
122
+
123
+ describe "after create," do
124
+ before { create_model('a user') }
125
+
126
+ it_should_behave_like "after storing a single user"
127
+ end
128
+ end
129
+
130
+ describe "('1 user', 'foo: \"bar\", baz: \"bing bong\"')" do
131
+ it "should call user_factory.create({'foo' => 'bar', 'baz' => 'bing bong'})" do
132
+ expect(user_factory).to receive(:create).with({'foo' => 'bar', 'baz' => 'bing bong'})
133
+ create_model('1 user', 'foo: "bar", baz: "bing bong"')
134
+ end
135
+
136
+ describe "after create," do
137
+ before { create_model('1 user', 'foo: "bar", baz: "bing bong"') }
138
+
139
+ it_should_behave_like "after storing a single user"
140
+ end
141
+ end
142
+
143
+ describe "('an user: \"fred\")" do
144
+ it "should call user_factory.create({})" do
145
+ expect(user_factory).to receive(:create).with({})
146
+ create_model('an user: "fred"')
147
+ end
148
+
149
+ describe "after create," do
150
+ before { create_model('an user: "fred"') }
151
+
152
+ it_should_behave_like "after storing a single user"
153
+
154
+ it "created_model('the user: \"fred\"') should retrieve the user" do
155
+ expect(created_model('the user: "fred"')).to eq(user)
156
+ end
157
+
158
+ it "created_model?('the user: \"shirl\"') should be false" do
159
+ expect(created_model?('the user: "shirl"')).to eq(false)
160
+ end
161
+
162
+ it "model?('the user: \"shirl\"') should be false" do
163
+ expect(model?('the user: "shirl"')).to eq(false)
164
+ end
165
+ end
166
+ end
167
+
168
+ describe "#create_models" do
169
+ before do
170
+ allow(user_factory).to receive(:create).and_return(user)
171
+ end
172
+
173
+ it "should call the factory's create one time for each record" do
174
+ expect(user_factory).to receive(:create).exactly(10).times
175
+ create_models(10, "a user")
176
+ end
177
+
178
+ it "should parse the fields only once across all records" do
179
+ expect(self).to receive(:parse_fields).once
180
+ create_models(10, "a user")
181
+ end
182
+ end
183
+
184
+ describe "with hash" do
185
+ it "should call user_factory.create({'foo' => 'bar'})" do
186
+ expect(user_factory).to receive(:create).with({'foo' => 'bar'})
187
+ expect(create_model('a user', {'foo' => 'bar'})).to eq(user)
188
+ end
189
+
190
+ describe "after create," do
191
+ before { create_model('a user', {'foo' => 'bar'}) }
192
+
193
+ it_should_behave_like "after storing a single user"
194
+ end
195
+ end
196
+ end
197
+
198
+ describe '#find_model' do
199
+ before do
200
+ allow(Pickle::Adapter).to receive(:find_first_model).with(user_class, anything).and_return(user)
201
+ end
202
+
203
+ it "should call user_class.find :first, :conditions => {<fields>}" do
204
+ expect(find_model('a user', 'hair: "pink"')).to eq(user)
205
+ end
206
+
207
+ describe "after find," do
208
+ before { find_model('a user', 'hair: "pink"') }
209
+
210
+ it_should_behave_like "after storing a single user"
211
+ end
212
+
213
+ describe "with hash" do
214
+ it "should call user_class.find('user', {'foo' => 'bar'})" do
215
+ find_model('a user', {'foo' => 'bar'})
216
+ end
217
+
218
+ describe "after find," do
219
+ before { find_model('a user', {'foo' => 'bar'}) }
220
+
221
+ it_should_behave_like "after storing a single user"
222
+ end
223
+ end
224
+ end
225
+
226
+ describe "create and find using plural_factory and table" do
227
+ context "when given a table without a matching pickle ref column" do
228
+ let :table do
229
+ double(:hashes => [{'name' => 'Fred'}, {'name' => 'Betty'}])
230
+ end
231
+
232
+ it "#create_models_from_table(<plural factory>, <table>) should call create_model for each of the table hashes with plain factory name and return the models" do
233
+ expect(self).to receive(:create_model).with("user", 'name' => "Fred").once.ordered.and_return(:fred)
234
+ expect(self).to receive(:create_model).with("user", 'name' => "Betty").once.ordered.and_return(:betty)
235
+ expect(create_models_from_table("users", table)).to eq([:fred, :betty])
236
+ end
237
+
238
+ it "#find_models_from_table(<plural factory>, <table>) should call find_model for each of the table hashes with plain factory name and return the models" do
239
+ expect(self).to receive(:find_model).with("user", 'name' => "Fred").once.ordered.and_return(:fred)
240
+ expect(self).to receive(:find_model).with("user", 'name' => "Betty").once.ordered.and_return(:betty)
241
+ expect(find_models_from_table("users", table)).to eq([:fred, :betty])
242
+ end
243
+ end
244
+
245
+ context "when given a table with a matching pickle ref column" do
246
+ let :table do
247
+ double(:hashes => [{'user' => "fred", 'name' => 'Fred'}, {'user' => "betty", 'name' => 'Betty'}])
248
+ end
249
+
250
+ it "#create_models_from_table(<plural factory>, <table>) should call create_model for each of the table hashes with labelled pickle ref" do
251
+ expect(self).to receive(:create_model).with("user \"fred\"", 'name' => "Fred").once.ordered.and_return(:fred)
252
+ expect(self).to receive(:create_model).with("user \"betty\"", 'name' => "Betty").once.ordered.and_return(:betty)
253
+ expect(create_models_from_table("users", table)).to eq([:fred, :betty])
254
+ end
255
+
256
+ it "#find_models_from_table(<plural factory>, <table>) should call find_model for each of the table hashes with labelled pickle ref" do
257
+ expect(self).to receive(:find_model).with("user \"fred\"", 'name' => "Fred").once.ordered.and_return(:fred)
258
+ expect(self).to receive(:find_model).with("user \"betty\"", 'name' => "Betty").once.ordered.and_return(:betty)
259
+ expect(find_models_from_table("users", table)).to eq([:fred, :betty])
260
+ end
261
+ end
262
+ end
263
+
264
+ describe "#find_model!" do
265
+ it "should call find_model" do
266
+ expect(self).to receive(:find_model).with('name', 'fields').and_return(user)
267
+ find_model!('name', 'fields')
268
+ end
269
+
270
+ it "should call raise error if find_model returns nil" do
271
+ expect(self).to receive(:find_model).with('name', 'fields').and_return(nil)
272
+ expect { find_model!('name', 'fields') }.to raise_error(Pickle::Session::ModelNotFoundError)
273
+ end
274
+ end
275
+
276
+ describe "#find_models" do
277
+ before do
278
+ allow(Pickle::Adapter).to receive(:find_all_models).with(user_class, anything).and_return([user])
279
+ end
280
+
281
+ it "should call User.find :all, :conditions => {'hair' => 'pink'}" do
282
+ expect(find_models('user', 'hair: "pink"')).to eq([user])
283
+ end
284
+
285
+ describe "after find," do
286
+ before { find_models('user', 'hair: "pink"') }
287
+
288
+ it_should_behave_like "after storing a single user"
289
+ end
290
+
291
+ it "should cope with spaces in the factory name (ie. it should make it canonical)" do
292
+ allow(pickle_parser).to receive(:canonical).and_return('user')
293
+ expect(pickle_parser).to receive(:canonical).with('u ser').and_return('user')
294
+ expect(find_models('u ser', 'hair: "pink"')).to eq([user])
295
+ end
296
+ end
297
+
298
+ describe 'creating \'a super admin: "fred"\', then \'a user: "shirl"\', \'then 1 super_admin\' (super_admin is factory that returns users)' do
299
+ let(:fred) { double("fred", :class => user_class, :id => 2) }
300
+ let(:shirl) { double("shirl", :class => user_class, :id => 3) }
301
+ let(:noname) { double("noname", :class => user_class, :id => 4) }
302
+
303
+ if defined? ::FactoryGirl
304
+ let(:super_admin_factory) { Pickle::Adapter::FactoryGirl.new(double(:build_class => user_class, :name => :super_admin), :super_admin) }
305
+ else
306
+ let(:super_admin_factory) { Pickle::Adapter::FactoryGirl.new(double(:build_class => user_class, :factory_name => :super_admin), :super_admin) }
307
+ end
308
+
309
+ before do
310
+ allow(config).to receive(:factories).and_return(user_factory.name => user_factory, super_admin_factory.name => super_admin_factory)
311
+ allow(user_factory).to receive(:create).and_return(shirl)
312
+ allow(super_admin_factory).to receive(:create).and_return(fred, noname)
313
+ end
314
+
315
+ def do_create_users
316
+ create_model('a super admin: "fred"')
317
+ create_model('a user: "shirl"')
318
+ create_model('1 super_admin')
319
+ end
320
+
321
+ it "should call Factory.create with <'super_admin'>, <'user'>, <'super_admin'>" do
322
+ expect(super_admin_factory).to receive(:create).with({}).twice
323
+ expect(user_factory).to receive(:create).with({}).once
324
+ do_create_users
325
+ end
326
+
327
+ describe "after create," do
328
+ before do
329
+ do_create_users
330
+ end
331
+
332
+ it "created_models('user') should == [fred, shirl, noname]" do
333
+ expect(created_models('user')).to eq([fred, shirl, noname])
334
+ end
335
+
336
+ it "created_models('super_admin') should == [fred, noname]" do
337
+ expect(created_models('super_admin')).to eq([fred, noname])
338
+ end
339
+
340
+ describe "#created_model" do
341
+ it "'that user' should be noname (the last user created - as super_admins are users)" do
342
+ expect(created_model('that user')).to eq(noname)
343
+ end
344
+
345
+ it "'the super admin' should be noname (the last super admin created)" do
346
+ expect(created_model('that super admin')).to eq(noname)
347
+ end
348
+
349
+ it "'the 1st super admin' should be fred" do
350
+ expect(created_model('the 1st super admin')).to eq(fred)
351
+ end
352
+
353
+ it "'the first user' should be fred" do
354
+ expect(created_model('the first user')).to eq(fred)
355
+ end
356
+
357
+ it "'the 2nd user' should be shirl" do
358
+ expect(created_model('the 2nd user')).to eq(shirl)
359
+ end
360
+
361
+ it "'the last user' should be noname" do
362
+ expect(created_model('the last user')).to eq(noname)
363
+ end
364
+
365
+ it "'the user: \"fred\" should be fred" do
366
+ expect(created_model('the user: "fred"')).to eq(fred)
367
+ end
368
+
369
+ it "'the user: \"shirl\" should be shirl" do
370
+ expect(created_model('the user: "shirl"')).to eq(shirl)
371
+ end
372
+ end
373
+ end
374
+ end
375
+
376
+ describe "when 'the user: \"me\"' exists and there is a mapping from 'I', 'myself' => 'user: \"me\"" do
377
+ before do
378
+ self.pickle_parser = Pickle::Parser.new(:config => Pickle::Config.new {|c| c.map 'I', 'myself', :to => 'user: "me"'})
379
+ allow(config).to receive(:factories).and_return('user' => user_factory)
380
+ allow(Pickle::Adapter).to receive(:get_model).with(user_class, anything).and_return(user)
381
+ allow(user_factory).to receive(:create).and_return(user)
382
+ create_model('the user: "me"')
383
+ end
384
+
385
+ it 'model("I") should return the user' do
386
+ expect(model('I')).to eq(user)
387
+ end
388
+
389
+ it 'model("myself") should return the user' do
390
+ expect(model('myself')).to eq(user)
391
+ end
392
+
393
+ it "#parser.parse_fields 'author: user \"JIM\"' should raise Error, as model deos not refer" do
394
+ expect { pickle_parser.parse_fields('author: user "JIM"') }.to raise_error
395
+ end
396
+
397
+ it "#parser.parse_fields 'author: the user' should return {\"author\" => <user>}" do
398
+ expect(pickle_parser.parse_fields('author: the user')).to eq({"author" => user})
399
+ end
400
+
401
+ it "#parser.parse_fields 'author: myself' should return {\"author\" => <user>}" do
402
+ expect(pickle_parser.parse_fields('author: myself')).to eq({"author" => user})
403
+ end
404
+
405
+ it "#parser.parse_fields 'author: the user, approver: I, rating: \"5\"' should return {'author' => <user>, 'approver' => <user>, 'rating' => '5'}" do
406
+ expect(pickle_parser.parse_fields('author: the user, approver: I, rating: "5"')).to eq({'author' => user, 'approver' => user, 'rating' => '5'})
407
+ end
408
+
409
+ it "#parser.parse_fields 'author: user: \"me\", approver: \"\"' should return {'author' => <user>, 'approver' => \"\"}" do
410
+ expect(pickle_parser.parse_fields('author: user: "me", approver: ""')).to eq({'author' => user, 'approver' => ""})
411
+ end
412
+ end
413
+
414
+ describe "convert_models_to_attributes(ar_class, :user => <a user>)" do
415
+ before do
416
+ allow(user).to receive(:is_a?).with(ActiveRecord::Base).and_return(true)
417
+ end
418
+
419
+ describe "(when ar_class has column 'user_id')" do
420
+ let :ar_class do
421
+ double('ActiveRecord', :column_names => ['user_id'], :const_get => ActiveRecord::Base::PickleAdapter)
422
+ end
423
+
424
+ it "should return {'user_id' => <the user.id>}" do
425
+ expect(convert_models_to_attributes(ar_class, :user => user)).to eq({'user_id' => user.id})
426
+ end
427
+ end
428
+
429
+ describe "(when ar_class has columns 'user_id', 'user_type')" do
430
+ let :ar_class do
431
+ double('ActiveRecord', :column_names => ['user_id', 'user_type'], :const_get => ActiveRecord::Base::PickleAdapter)
432
+ end
433
+
434
+ it "should return {'user_id' => <the user.id>, 'user_type' => <the user.base_class>}" do
435
+ expect(user.class).to receive(:base_class).and_return(double('User base class', :name => 'UserBase'))
436
+ expect(convert_models_to_attributes(ar_class, :user => user)).to eq({'user_id' => user.id, 'user_type' => 'UserBase'})
437
+ end
438
+ end
439
+ end
440
+
441
+ it "#model!('unknown') should raise informative error message" do
442
+ expect { model!('unknown') }.to raise_error(Pickle::Session::ModelNotKnownError, "The model: 'unknown' is not known in this scenario. Use #create_model to create, or #find_model to find, and store a reference in this scenario.")
443
+ end
444
+
445
+ it "#created_model!('unknown') should raise informative error message" do
446
+ expect { created_model!('unknown') }.to raise_error(Pickle::Session::ModelNotKnownError)
447
+ end
448
+ end