enumerated_attribute 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gitignore +16 -0
  2. data/CHANGELOG.rdoc +49 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +555 -0
  5. data/Rakefile +104 -0
  6. data/init.rb +1 -0
  7. data/lib/enumerated_attribute.rb +24 -0
  8. data/lib/enumerated_attribute/attribute.rb +78 -0
  9. data/lib/enumerated_attribute/attribute/arguments.rb +48 -0
  10. data/lib/enumerated_attribute/attribute/attribute_descriptor.rb +51 -0
  11. data/lib/enumerated_attribute/attribute/class_methods.rb +42 -0
  12. data/lib/enumerated_attribute/attribute/instance_methods.rb +96 -0
  13. data/lib/enumerated_attribute/integrations.rb +33 -0
  14. data/lib/enumerated_attribute/integrations/active_record.rb +109 -0
  15. data/lib/enumerated_attribute/integrations/datamapper.rb +6 -0
  16. data/lib/enumerated_attribute/integrations/default.rb +25 -0
  17. data/lib/enumerated_attribute/integrations/object.rb +47 -0
  18. data/lib/enumerated_attribute/method_definition_dsl.rb +142 -0
  19. data/lib/enumerated_attribute/rails_helpers.rb +97 -0
  20. data/lib/jeffp-enumerated_attribute.rb +1 -0
  21. data/spec/active_record/active_record_spec.rb +363 -0
  22. data/spec/active_record/association_test_classes.rb +40 -0
  23. data/spec/active_record/associations_spec.rb +130 -0
  24. data/spec/active_record/cfg.rb +6 -0
  25. data/spec/active_record/inheritance_classes.rb +19 -0
  26. data/spec/active_record/inheritance_spec.rb +60 -0
  27. data/spec/active_record/race_car.rb +15 -0
  28. data/spec/active_record/single_table_inheritance_spec.rb +0 -0
  29. data/spec/active_record/sti_classes.rb +10 -0
  30. data/spec/active_record/sti_spec.rb +41 -0
  31. data/spec/active_record/test_in_memory.rb +71 -0
  32. data/spec/car.rb +67 -0
  33. data/spec/cfg.rb +8 -0
  34. data/spec/inheritance_classes.rb +16 -0
  35. data/spec/inheritance_spec.rb +80 -0
  36. data/spec/new_and_method_missing_spec.rb +73 -0
  37. data/spec/plural.rb +8 -0
  38. data/spec/poro_spec.rb +397 -0
  39. data/spec/rails/README +243 -0
  40. data/spec/rails/Rakefile +10 -0
  41. data/spec/rails/app/controllers/application_controller.rb +10 -0
  42. data/spec/rails/app/controllers/form_test_controller.rb +38 -0
  43. data/spec/rails/app/helpers/application_helper.rb +3 -0
  44. data/spec/rails/app/helpers/form_test_helper.rb +2 -0
  45. data/spec/rails/app/models/user.rb +9 -0
  46. data/spec/rails/app/views/form_test/form.html.erb +1 -0
  47. data/spec/rails/app/views/form_test/form_for.html.erb +10 -0
  48. data/spec/rails/app/views/form_test/form_tag.html.erb +9 -0
  49. data/spec/rails/app/views/form_test/index.html.erb +6 -0
  50. data/spec/rails/app/views/layouts/application.html.erb +11 -0
  51. data/spec/rails/config/boot.rb +110 -0
  52. data/spec/rails/config/database.yml +22 -0
  53. data/spec/rails/config/environment.rb +45 -0
  54. data/spec/rails/config/environments/development.rb +17 -0
  55. data/spec/rails/config/environments/production.rb +28 -0
  56. data/spec/rails/config/environments/test.rb +28 -0
  57. data/spec/rails/config/initializers/backtrace_silencers.rb +7 -0
  58. data/spec/rails/config/initializers/inflections.rb +10 -0
  59. data/spec/rails/config/initializers/mime_types.rb +5 -0
  60. data/spec/rails/config/initializers/new_rails_defaults.rb +19 -0
  61. data/spec/rails/config/initializers/session_store.rb +15 -0
  62. data/spec/rails/config/locales/en.yml +5 -0
  63. data/spec/rails/config/routes.rb +43 -0
  64. data/spec/rails/db/development.sqlite3 +0 -0
  65. data/spec/rails/db/migrate/20090804230221_create_sessions.rb +16 -0
  66. data/spec/rails/db/migrate/20090804230546_create_users.rb +21 -0
  67. data/spec/rails/db/schema.rb +35 -0
  68. data/spec/rails/db/test.sqlite3 +0 -0
  69. data/spec/rails/public/404.html +30 -0
  70. data/spec/rails/public/422.html +30 -0
  71. data/spec/rails/public/500.html +30 -0
  72. data/spec/rails/public/favicon.ico +0 -0
  73. data/spec/rails/public/images/rails.png +0 -0
  74. data/spec/rails/public/index.html +275 -0
  75. data/spec/rails/public/javascripts/application.js +2 -0
  76. data/spec/rails/public/javascripts/controls.js +963 -0
  77. data/spec/rails/public/javascripts/dragdrop.js +973 -0
  78. data/spec/rails/public/javascripts/effects.js +1128 -0
  79. data/spec/rails/public/javascripts/prototype.js +4320 -0
  80. data/spec/rails/public/robots.txt +5 -0
  81. data/spec/rails/public/stylesheets/scaffold.css +54 -0
  82. data/spec/rails/script/about +4 -0
  83. data/spec/rails/script/autospec +6 -0
  84. data/spec/rails/script/console +3 -0
  85. data/spec/rails/script/dbconsole +3 -0
  86. data/spec/rails/script/destroy +3 -0
  87. data/spec/rails/script/generate +3 -0
  88. data/spec/rails/script/performance/benchmarker +3 -0
  89. data/spec/rails/script/performance/profiler +3 -0
  90. data/spec/rails/script/plugin +3 -0
  91. data/spec/rails/script/runner +3 -0
  92. data/spec/rails/script/server +3 -0
  93. data/spec/rails/script/spec +10 -0
  94. data/spec/rails/script/spec_server +9 -0
  95. data/spec/rails/spec/controllers/form_test_controller_spec.rb +41 -0
  96. data/spec/rails/spec/integrations/enum_select_spec.rb +75 -0
  97. data/spec/rails/spec/matchers.rb +12 -0
  98. data/spec/rails/spec/rcov.opts +2 -0
  99. data/spec/rails/spec/spec.opts +4 -0
  100. data/spec/rails/spec/spec_helper.rb +40 -0
  101. data/spec/rails/spec/views/form_test/form.html.erb_spec.rb +12 -0
  102. data/spec/rails/spec/views/form_test/form_for.html.erb_spec.rb +12 -0
  103. data/spec/rails/spec/views/form_test/form_tag.html.erb_spec.rb +12 -0
  104. data/spec/rcov.opts +2 -0
  105. data/spec/spec.opts +4 -0
  106. data/spec/tractor.rb +48 -0
  107. metadata +182 -0
data/spec/plural.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'enumerated_attribute'
2
+
3
+ class Plural
4
+ enum_attr :box, %w(small medium large)
5
+ enum_attr :batch, %w(none daily weekly)
6
+ enum_attr :cherry, %w(red green yellow)
7
+ enum_attr :guy, %w(handsome funny cool)
8
+ end
data/spec/poro_spec.rb ADDED
@@ -0,0 +1,397 @@
1
+ require 'rubygems'
2
+ require 'tractor'
3
+ require 'plural'
4
+
5
+ Spec::Matchers.define :define_instance_method do |method_name|
6
+ match do |klass_or_object|
7
+ if klass_or_object.is_a?(Class)
8
+ klass_or_object.method_defined?(method_name.to_sym)
9
+ else
10
+ klass_or_object.class.method_defined?(method_name.to_sym)
11
+ end
12
+ end
13
+ end
14
+
15
+ describe "Plural" do
16
+ it "should have plural accessor :boxes for :box" do
17
+ p=Plural.new
18
+ p.should define_instance_method(:boxes) #methods.should include("boxes")
19
+ p.boxes.should == [:small, :medium, :large]
20
+ end
21
+ it "should have plural accessor :batches for :batch" do
22
+ p=Plural.new
23
+ p.should define_instance_method(:batches) #methods.should include("batches")
24
+ p.batches.should == [:none, :daily, :weekly]
25
+ end
26
+ it "should have plural accessor :cherries for :cherry" do
27
+ p=Plural.new
28
+ p.should define_instance_method(:cherries) #methods.should include("cherries")
29
+ p.cherries.should == [:red, :green, :yellow]
30
+ end
31
+ it "should have plural accessor :guys for :guy" do
32
+ p=Plural.new
33
+ p.should define_instance_method(:guys) #methods.should include("guys")
34
+ p.guys.should == [:handsome, :funny, :cool]
35
+ end
36
+
37
+ end
38
+
39
+ describe "Tractor" do
40
+
41
+ it "should have default labels for :gear attribute" do
42
+ labels_hash = {:reverse=>'Reverse', :neutral=>'Neutral', :first=>'First', :second=>'Second', :over_drive=>'Over drive'}
43
+ labels = ['Reverse', 'Neutral', 'First', 'Second', 'Over drive']
44
+ select_options = [['Reverse', 'reverse'], ['Neutral', 'neutral'], ['First', 'first'], ['Second', 'second'], ['Over drive', 'over_drive']]
45
+ t=Tractor.new
46
+ t.gears.labels.should == labels
47
+ labels_hash.each do |k,v|
48
+ t.gears.label(k).should == v
49
+ end
50
+ t.gears.hash.should == labels_hash
51
+ t.gears.select_options.should == select_options
52
+ end
53
+
54
+ it "should retrieve :gear enums through enums method" do
55
+ t=Tractor.new
56
+ t.enums(:gear).should == t.gears
57
+ end
58
+
59
+ it "should retrieve custom labels for :side_light attribute" do
60
+ labels_hash = {:off=>'OFF', :low=>'LOW DIM', :high=>'HIGH BEAM', :super_high=>'SUPER BEAM'}
61
+ t=Tractor.new
62
+ enum = t.enums(:side_light)
63
+ t.enums(:side_light).hash.each do |k,v|
64
+ enum.label(k).should == labels_hash[k]
65
+ end
66
+ end
67
+
68
+ it "should return a Symbol type from reader methods" do
69
+ t=Tractor.new
70
+ t.gear.should be_an_instance_of(Symbol)
71
+ end
72
+
73
+ it "should not raise errors for dynamic predicate methods missing attribute name" do
74
+ t=Tractor.new
75
+ lambda{ t.neutral?.should be_true }.should_not raise_error
76
+ lambda{ t.is_neutral?.should be_true }.should_not raise_error
77
+ lambda{ t.not_neutral?.should be_false}.should_not raise_error
78
+ t.gear = :first
79
+ t.neutral?.should be_false
80
+ t.not_neutral?.should be_true
81
+ end
82
+
83
+ it "should raise AmbiguousMethod when calling :off?" do
84
+ t=Tractor.new
85
+ lambda { t.off? }.should raise_error(EnumeratedAttribute::AmbiguousMethod)
86
+ end
87
+
88
+ it "should raise AmbiguousMethod when calling :in_reverse?" do
89
+ t=Tractor.new
90
+ lambda {t.in_reverse?}.should raise_error(EnumeratedAttribute::AmbiguousMethod)
91
+ end
92
+
93
+ it "should raise AmbiguousMethod when calling :not_reverse?" do
94
+ t=Tractor.new
95
+ lambda {t.not_reverse?}.should raise_error(EnumeratedAttribute::AmbiguousMethod)
96
+ end
97
+
98
+ it "should initialize :gear for two instances of the same class" do
99
+ t=Tractor.new
100
+ t.gear.should == :neutral
101
+ s=Tractor.new
102
+ s.gear.should == :neutral
103
+ end
104
+
105
+ it "should dynamically create :plow_nil? and :plow_not_nil?" do
106
+ t=Tractor.new
107
+ t.plow_nil?.should be_false
108
+ t.plow_not_nil?.should be_true
109
+ t.plow = nil
110
+ t.plow_not_nil?.should be_false
111
+ t.plow_nil?.should be_true
112
+ Tractor.should define_instance_method(:plow_nil?)
113
+ Tractor.should define_instance_method(:plow_not_nil?)
114
+ end
115
+
116
+ it "should dynamically create :plow_is_nil? and :plow_is_not_nil?" do
117
+ t=Tractor.new
118
+ t.plow_is_nil?.should be_false
119
+ t.plow_is_not_nil?.should be_true
120
+ t.plow = nil
121
+ t.plow_is_not_nil?.should be_false
122
+ t.plow_is_nil?.should be_true
123
+ Tractor.should define_instance_method(:plow_is_nil?)
124
+ Tractor.should define_instance_method(:plow_is_not_nil?)
125
+ end
126
+
127
+ it "should negate result for not_parked? defined with is_not" do
128
+ t=Tractor.new
129
+ t.gear = :neutral
130
+ t.not_parked?.should be_false
131
+ end
132
+
133
+ it "should negate result for not_driving? defined with is_not" do
134
+ t=Tractor.new
135
+ t.gear = :neutral
136
+ t.not_driving?.should be_true
137
+ end
138
+
139
+ =begin
140
+ it "should have getter but no setter for :temperature" do
141
+ Tractor.instance_methods.should_not include('temperature=')
142
+ Tractor.instance_methods.should include('temperature')
143
+ end
144
+
145
+ it "should have setter but no getter for :ignition" do
146
+ Tractor.instance_methods.should_not include('ignition')
147
+ Tractor.instance_methods.should include('ignition=')
148
+ end
149
+ =end
150
+
151
+ it "should be able to set :plow to nil" do
152
+ t=Tractor.new
153
+ lambda { t.plow = nil }.should_not raise_error(EnumeratedAttribute::InvalidEnumeration)
154
+ end
155
+
156
+ it "should have method :plow_nil? that operates correctly" do
157
+ t=Tractor.new
158
+ t.plow.should_not be_nil
159
+ t.plow_nil?.should be_false
160
+ t.plow = nil
161
+ t.plow.should be_nil
162
+ t.plow_nil?.should be_true
163
+ end
164
+
165
+ it "should raise EnumeratedAttribute::InvalidEnumeration when setting :gear to nil" do
166
+ t=Tractor.new
167
+ lambda{ t.gear = nil }.should raise_error(EnumeratedAttribute::InvalidEnumeration)
168
+ end
169
+
170
+ it "should have respond_to? method for :gear_is_in_neutral?" do
171
+ t=Tractor.new
172
+ t.respond_to?('gear_is_in_neutral?').should be_true
173
+ end
174
+
175
+ it "should have respond_to? method for :side_light_is_super_high?" do
176
+ t=Tractor.new
177
+ t.respond_to?(:side_light_is_super_high?).should be_true
178
+ end
179
+
180
+ it "should not have respond_to? method for :gear_is_in_high?" do
181
+ t=Tractor.new
182
+ t.respond_to?(:gear_is_in_high?).should be_false
183
+ end
184
+
185
+ it "should initially set :plow to :up" do
186
+ t=Tractor.new
187
+ t.plow.should == :up
188
+ end
189
+
190
+ it "should have plowing? state method" do
191
+ t=Tractor.new
192
+ t.plowing?.should be_false
193
+ t.plow=:down
194
+ t.plowing?.should be_false
195
+ t.gear= :first
196
+ t.plowing?.should be_true
197
+ t.plow=:up
198
+ t.plowing?.should be_false
199
+ end
200
+
201
+ it "should have :side_light_up incrementor" do
202
+ t=Tractor.new
203
+ t.side_light = :off
204
+ t.side_light_up.should == :low
205
+ t.side_light_up.should == :high
206
+ t.side_light_up.should == :super_high
207
+ t.side_light_up.should == :off
208
+ end
209
+
210
+ it "should have :side_light_down incrementor" do
211
+ t=Tractor.new
212
+ t.side_light_down.should == :super_high
213
+ t.side_light_down.should == :high
214
+ t.side_light_down.should == :low
215
+ t.side_light_down.should == :off
216
+ end
217
+
218
+ it "should have :turn_lights_up incrementor" do
219
+ t=Tractor.new
220
+ t.lights = :off
221
+ t.turn_lights_up.should == :low
222
+ t.turn_lights_up.should == :high
223
+ end
224
+
225
+ it "should have :turn_lights_down decrementor" do
226
+ t=Tractor.new
227
+ t.lights=:high
228
+ t.turn_lights_down.should == :low
229
+ t.turn_lights_down.should == :off
230
+ end
231
+
232
+ it "should have :gear_previous which wraps from :neutral to :over_drive" do
233
+ t=Tractor.new
234
+ t.gear_previous.should == :reverse
235
+ t.gear.should == :reverse
236
+ t.gear_previous.should == :over_drive
237
+ t.gear.should == :over_drive
238
+ end
239
+
240
+ it "should have :gear_next which wraps from :second to :reverse" do
241
+ t=Tractor.new
242
+ t.gear = :second
243
+ t.gear_next.should == :over_drive
244
+ t.gear.should == :over_drive
245
+ t.gear_next.should == :reverse
246
+ t.gear.should == :reverse
247
+ end
248
+
249
+ it "should have :upshift which increments :gear from :neutral to :over_drive without wrapping" do
250
+ t=Tractor.new
251
+ t.upshift.should == :first
252
+ t.upshift.should == :second
253
+ t.upshift.should == :over_drive
254
+ t.upshift.should == :over_drive
255
+ end
256
+
257
+ it "should have :downshift which decrements :gear from :over_drive to :neutral without wrapping" do
258
+ t=Tractor.new
259
+ t.gear = :over_drive
260
+ t.downshift.should == :second
261
+ t.downshift.should == :first
262
+ t.downshift.should == :neutral
263
+ t.downshift.should == :neutral
264
+ end
265
+
266
+ it "should have parked? method" do
267
+ t=Tractor.new
268
+ t.parked?.should be_true
269
+ t.gear = :reverse
270
+ t.parked?.should be_false
271
+ end
272
+
273
+ it "should have driving? method" do
274
+ t=Tractor.new
275
+ t.driving?.should be_false
276
+ [:first, :second, :over_drive].each do |g|
277
+ t.gear=g
278
+ t.driving?.should be_true
279
+ end
280
+ end
281
+
282
+ it "should initially set side_light to :off" do
283
+ t=Tractor.new
284
+ t.side_light.should == :off
285
+ end
286
+
287
+ it "should have side_light_enums method" do
288
+ t = Tractor.new
289
+ t.side_light_enums.should == Tractor::SIDE_LIGHT_ENUM_VALUES
290
+ end
291
+
292
+ it "should have state method side_light_is_off?" do
293
+ t=Tractor.new
294
+ t.side_light_is_off?.should be_true
295
+ end
296
+
297
+ it "should have state method side_light_is_super_high?" do
298
+ t=Tractor.new
299
+ t.side_light_is_super_high?.should be_false
300
+ end
301
+
302
+ it "should initially set :gear to :neutral" do
303
+ t=Tractor.new
304
+ t.gear.should == :neutral
305
+ end
306
+
307
+ it "should set lights initially to :off" do
308
+ t=Tractor.new
309
+ t.lights.should == :off
310
+ end
311
+
312
+ it "should create a lights_enums method for all light enumerated values" do
313
+ t=Tractor.new
314
+ t.lights_enums.should == Tractor::LIGHTS_ENUM_VALUES
315
+ end
316
+
317
+ it "should initially set lights to :off" do
318
+ t=Tractor.new
319
+ t.lights.should equal(:off)
320
+ end
321
+
322
+ it "should have dynamic state methods for :reverse and :neutral" do
323
+ t = Tractor.new
324
+ t.gear_is_in_reverse?.should be_false
325
+ t.gear_is_in_neutral?.should be_true
326
+ end
327
+
328
+ it "should have negative dynamic state methods for :reverses and :neutral" do
329
+ t = Tractor.new
330
+ t.gear_is_not_in_reverse?.should be_true
331
+ t.gear_is_not_in_neutral?.should be_false
332
+ end
333
+
334
+ it "should have negative and positive dynamic state methods for :over_drive" do
335
+ t = Tractor.new
336
+ t.gear_is_in_over_drive?.should be_false
337
+ t.gear_is_not_in_over_drive?.should be_true
338
+ end
339
+
340
+ it "should have created instance methods for :reverse" do
341
+ Tractor.should define_instance_method(:gear_is_in_reverse?)
342
+ Tractor.should define_instance_method(:gear_is_not_in_reverse?)
343
+ end
344
+
345
+ it "should have created instance methods for :neutral" do
346
+ Tractor.should define_instance_method(:gear_is_in_neutral?)
347
+ Tractor.should define_instance_method(:gear_is_not_in_neutral?)
348
+ end
349
+
350
+ it "should have created instance methods for :over_drive" do
351
+ Tractor.should define_instance_method :gear_is_in_over_drive?
352
+ Tractor.should define_instance_method :gear_is_not_in_over_drive?
353
+ end
354
+
355
+ it "should raise NoMethodError for dynamic state methods not querying valid enumeration values" do
356
+ t = Tractor.new
357
+ lambda { t.gear_is_in_high? }.should raise_error(NoMethodError)
358
+ end
359
+
360
+ it "should convert string values to symbols for attr setters" do
361
+ t = Tractor.new
362
+ t.gear= 'reverse'
363
+ t.gear.should == :reverse
364
+ end
365
+
366
+ it "should have instance method gears equal to enumeration array" do
367
+ Tractor.new.gears.should == Tractor::GEAR_ENUM_VALUES
368
+ end
369
+
370
+ it "should have gear attribute initialized to :neutral" do
371
+ t = Tractor.new
372
+ t.gear.should == :neutral
373
+ end
374
+
375
+ it "should set gear attribute to :first" do
376
+ t = Tractor.new
377
+ t.gear = :first
378
+ t.gear.should == :first
379
+ end
380
+
381
+ it "should raise error when set gear attribute to :broken" do
382
+ t = Tractor.new
383
+ lambda { t.gear= :broken }.should raise_error(EnumeratedAttribute::InvalidEnumeration)
384
+ end
385
+
386
+ it "should have name attribute initially set to 'old faithful'" do
387
+ t = Tractor.new
388
+ t.name.should == 'old faithful'
389
+ end
390
+
391
+ it "should set name attribute to 'broke n busted'" do
392
+ t = Tractor.new
393
+ t.name = 'broke n busted'
394
+ t.name.should == 'broke n busted'
395
+ end
396
+
397
+ end
data/spec/rails/README ADDED
@@ -0,0 +1,243 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb" templates
7
+ that are primarily responsible for inserting pre-built data in between HTML tags.
8
+ The model contains the "smart" domain objects (such as Account, Product, Person,
9
+ Post) that holds all the business logic and knows how to persist themselves to
10
+ a database. The controller handles the incoming requests (such as Save New Account,
11
+ Update Product, Show Post) by manipulating the model and directing data to the view.
12
+
13
+ In Rails, the model is handled by what's called an object-relational mapping
14
+ layer entitled Active Record. This layer allows you to present the data from
15
+ database rows as objects and embellish these data objects with business logic
16
+ methods. You can read more about Active Record in
17
+ link:files/vendor/rails/activerecord/README.html.
18
+
19
+ The controller and view are handled by the Action Pack, which handles both
20
+ layers by its two parts: Action View and Action Controller. These two layers
21
+ are bundled in a single package due to their heavy interdependence. This is
22
+ unlike the relationship between the Active Record and Action Pack that is much
23
+ more separate. Each of these packages can be used independently outside of
24
+ Rails. You can read more about Action Pack in
25
+ link:files/vendor/rails/actionpack/README.html.
26
+
27
+
28
+ == Getting Started
29
+
30
+ 1. At the command prompt, start a new Rails application using the <tt>rails</tt> command
31
+ and your application name. Ex: rails myapp
32
+ 2. Change directory into myapp and start the web server: <tt>script/server</tt> (run with --help for options)
33
+ 3. Go to http://localhost:3000/ and get "Welcome aboard: You're riding the Rails!"
34
+ 4. Follow the guidelines to start developing your application
35
+
36
+
37
+ == Web Servers
38
+
39
+ By default, Rails will try to use Mongrel if it's are installed when started with script/server, otherwise Rails will use WEBrick, the webserver that ships with Ruby. But you can also use Rails
40
+ with a variety of other web servers.
41
+
42
+ Mongrel is a Ruby-based webserver with a C component (which requires compilation) that is
43
+ suitable for development and deployment of Rails applications. If you have Ruby Gems installed,
44
+ getting up and running with mongrel is as easy as: <tt>gem install mongrel</tt>.
45
+ More info at: http://mongrel.rubyforge.org
46
+
47
+ Say other Ruby web servers like Thin and Ebb or regular web servers like Apache or LiteSpeed or
48
+ Lighttpd or IIS. The Ruby web servers are run through Rack and the latter can either be setup to use
49
+ FCGI or proxy to a pack of Mongrels/Thin/Ebb servers.
50
+
51
+ == Apache .htaccess example for FCGI/CGI
52
+
53
+ # General Apache options
54
+ AddHandler fastcgi-script .fcgi
55
+ AddHandler cgi-script .cgi
56
+ Options +FollowSymLinks +ExecCGI
57
+
58
+ # If you don't want Rails to look in certain directories,
59
+ # use the following rewrite rules so that Apache won't rewrite certain requests
60
+ #
61
+ # Example:
62
+ # RewriteCond %{REQUEST_URI} ^/notrails.*
63
+ # RewriteRule .* - [L]
64
+
65
+ # Redirect all requests not available on the filesystem to Rails
66
+ # By default the cgi dispatcher is used which is very slow
67
+ #
68
+ # For better performance replace the dispatcher with the fastcgi one
69
+ #
70
+ # Example:
71
+ # RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
72
+ RewriteEngine On
73
+
74
+ # If your Rails application is accessed via an Alias directive,
75
+ # then you MUST also set the RewriteBase in this htaccess file.
76
+ #
77
+ # Example:
78
+ # Alias /myrailsapp /path/to/myrailsapp/public
79
+ # RewriteBase /myrailsapp
80
+
81
+ RewriteRule ^$ index.html [QSA]
82
+ RewriteRule ^([^.]+)$ $1.html [QSA]
83
+ RewriteCond %{REQUEST_FILENAME} !-f
84
+ RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
85
+
86
+ # In case Rails experiences terminal errors
87
+ # Instead of displaying this message you can supply a file here which will be rendered instead
88
+ #
89
+ # Example:
90
+ # ErrorDocument 500 /500.html
91
+
92
+ ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
93
+
94
+
95
+ == Debugging Rails
96
+
97
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
98
+ will help you debug it and get it back on the rails.
99
+
100
+ First area to check is the application log files. Have "tail -f" commands running
101
+ on the server.log and development.log. Rails will automatically display debugging
102
+ and runtime information to these files. Debugging info will also be shown in the
103
+ browser on requests from 127.0.0.1.
104
+
105
+ You can also log your own messages directly into the log file from your code using
106
+ the Ruby logger class from inside your controllers. Example:
107
+
108
+ class WeblogController < ActionController::Base
109
+ def destroy
110
+ @weblog = Weblog.find(params[:id])
111
+ @weblog.destroy
112
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
113
+ end
114
+ end
115
+
116
+ The result will be a message in your log file along the lines of:
117
+
118
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1
119
+
120
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
121
+
122
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/ including:
123
+
124
+ * The Learning Ruby (Pickaxe) Book: http://www.ruby-doc.org/docs/ProgrammingRuby/
125
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
126
+
127
+ These two online (and free) books will bring you up to speed on the Ruby language
128
+ and also on programming in general.
129
+
130
+
131
+ == Debugger
132
+
133
+ Debugger support is available through the debugger command when you start your Mongrel or
134
+ Webrick server with --debugger. This means that you can break out of execution at any point
135
+ in the code, investigate and change the model, AND then resume execution!
136
+ You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'
137
+ Example:
138
+
139
+ class WeblogController < ActionController::Base
140
+ def index
141
+ @posts = Post.find(:all)
142
+ debugger
143
+ end
144
+ end
145
+
146
+ So the controller will accept the action, run the first line, then present you
147
+ with a IRB prompt in the server window. Here you can do things like:
148
+
149
+ >> @posts.inspect
150
+ => "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
151
+ #<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
152
+ >> @posts.first.title = "hello from a debugger"
153
+ => "hello from a debugger"
154
+
155
+ ...and even better is that you can examine how your runtime objects actually work:
156
+
157
+ >> f = @posts.first
158
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
159
+ >> f.
160
+ Display all 152 possibilities? (y or n)
161
+
162
+ Finally, when you're ready to resume execution, you enter "cont"
163
+
164
+
165
+ == Console
166
+
167
+ You can interact with the domain model by starting the console through <tt>script/console</tt>.
168
+ Here you'll have all parts of the application configured, just like it is when the
169
+ application is running. You can inspect domain models, change values, and save to the
170
+ database. Starting the script without arguments will launch it in the development environment.
171
+ Passing an argument will specify a different environment, like <tt>script/console production</tt>.
172
+
173
+ To reload your controllers and models after launching the console run <tt>reload!</tt>
174
+
175
+ == dbconsole
176
+
177
+ You can go to the command line of your database directly through <tt>script/dbconsole</tt>.
178
+ You would be connected to the database with the credentials defined in database.yml.
179
+ Starting the script without arguments will connect you to the development database. Passing an
180
+ argument will connect you to a different database, like <tt>script/dbconsole production</tt>.
181
+ Currently works for mysql, postgresql and sqlite.
182
+
183
+ == Description of Contents
184
+
185
+ app
186
+ Holds all the code that's specific to this particular application.
187
+
188
+ app/controllers
189
+ Holds controllers that should be named like weblogs_controller.rb for
190
+ automated URL mapping. All controllers should descend from ApplicationController
191
+ which itself descends from ActionController::Base.
192
+
193
+ app/models
194
+ Holds models that should be named like post.rb.
195
+ Most models will descend from ActiveRecord::Base.
196
+
197
+ app/views
198
+ Holds the template files for the view that should be named like
199
+ weblogs/index.html.erb for the WeblogsController#index action. All views use eRuby
200
+ syntax.
201
+
202
+ app/views/layouts
203
+ Holds the template files for layouts to be used with views. This models the common
204
+ header/footer method of wrapping views. In your views, define a layout using the
205
+ <tt>layout :default</tt> and create a file named default.html.erb. Inside default.html.erb,
206
+ call <% yield %> to render the view using this layout.
207
+
208
+ app/helpers
209
+ Holds view helpers that should be named like weblogs_helper.rb. These are generated
210
+ for you automatically when using script/generate for controllers. Helpers can be used to
211
+ wrap functionality for your views into methods.
212
+
213
+ config
214
+ Configuration files for the Rails environment, the routing map, the database, and other dependencies.
215
+
216
+ db
217
+ Contains the database schema in schema.rb. db/migrate contains all
218
+ the sequence of Migrations for your schema.
219
+
220
+ doc
221
+ This directory is where your application documentation will be stored when generated
222
+ using <tt>rake doc:app</tt>
223
+
224
+ lib
225
+ Application specific libraries. Basically, any kind of custom code that doesn't
226
+ belong under controllers, models, or helpers. This directory is in the load path.
227
+
228
+ public
229
+ The directory available for the web server. Contains subdirectories for images, stylesheets,
230
+ and javascripts. Also contains the dispatchers and the default HTML files. This should be
231
+ set as the DOCUMENT_ROOT of your web server.
232
+
233
+ script
234
+ Helper scripts for automation and generation.
235
+
236
+ test
237
+ Unit and functional tests along with fixtures. When using the script/generate scripts, template
238
+ test files will be generated for you and placed in this directory.
239
+
240
+ vendor
241
+ External libraries that the application depends on. Also includes the plugins subdirectory.
242
+ If the app has frozen rails, those gems also go here, under vendor/rails/.
243
+ This directory is in the load path.