bakedweb-model_notifier 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. data/.gitignore +8 -0
  2. data/LICENSE +20 -0
  3. data/README.rdoc +7 -0
  4. data/Rakefile +13 -0
  5. data/VERSION +1 -0
  6. data/cucumber.yml +2 -0
  7. data/features/model_notifier.feature +24 -0
  8. data/features/step_definitions/email_steps.rb +74 -0
  9. data/features/step_definitions/model_notifier_steps.rb +18 -0
  10. data/features/support/env.rb +12 -0
  11. data/lib/model_notifier.rb +61 -0
  12. data/spec/lib/model_notifier_spec.rb +82 -0
  13. data/spec/rcov.opts +2 -0
  14. data/spec/spec.opts +4 -0
  15. data/spec/spec_helper.rb +53 -0
  16. data/test_rails_app/README +256 -0
  17. data/test_rails_app/Rakefile +10 -0
  18. data/test_rails_app/app/controllers/application.rb +15 -0
  19. data/test_rails_app/app/helpers/application_helper.rb +3 -0
  20. data/test_rails_app/app/models/contact_form.rb +2 -0
  21. data/test_rails_app/app/models/foo.rb +2 -0
  22. data/test_rails_app/app/views/model_notifier/contact_form/notification_email.erb +2 -0
  23. data/test_rails_app/app/views/model_notifier/foo/notification_email.erb +1 -0
  24. data/test_rails_app/config/boot.rb +109 -0
  25. data/test_rails_app/config/database.yml +22 -0
  26. data/test_rails_app/config/environment.rb +75 -0
  27. data/test_rails_app/config/environments/development.rb +17 -0
  28. data/test_rails_app/config/environments/production.rb +24 -0
  29. data/test_rails_app/config/environments/test.rb +24 -0
  30. data/test_rails_app/config/initializers/inflections.rb +10 -0
  31. data/test_rails_app/config/initializers/mime_types.rb +5 -0
  32. data/test_rails_app/config/initializers/new_rails_defaults.rb +17 -0
  33. data/test_rails_app/config/locales/en.yml +5 -0
  34. data/test_rails_app/config/routes.rb +43 -0
  35. data/test_rails_app/db/migrate/20090525195411_create_foos.rb +12 -0
  36. data/test_rails_app/db/migrate/20090525200701_create_contact_forms.rb +14 -0
  37. data/test_rails_app/lib/tasks/cucumber.rake +16 -0
  38. data/test_rails_app/lib/tasks/rspec.rake +167 -0
  39. data/test_rails_app/public/404.html +30 -0
  40. data/test_rails_app/public/422.html +30 -0
  41. data/test_rails_app/public/500.html +33 -0
  42. data/test_rails_app/public/dispatch.cgi +10 -0
  43. data/test_rails_app/public/dispatch.fcgi +24 -0
  44. data/test_rails_app/public/dispatch.rb +10 -0
  45. data/test_rails_app/public/favicon.ico +0 -0
  46. data/test_rails_app/public/images/rails.png +0 -0
  47. data/test_rails_app/public/index.html +274 -0
  48. data/test_rails_app/public/javascripts/application.js +2 -0
  49. data/test_rails_app/public/javascripts/controls.js +963 -0
  50. data/test_rails_app/public/javascripts/dragdrop.js +973 -0
  51. data/test_rails_app/public/javascripts/effects.js +1128 -0
  52. data/test_rails_app/public/javascripts/prototype.js +4320 -0
  53. data/test_rails_app/public/robots.txt +5 -0
  54. data/test_rails_app/script/about +4 -0
  55. data/test_rails_app/script/autospec +6 -0
  56. data/test_rails_app/script/console +3 -0
  57. data/test_rails_app/script/cucumber +8 -0
  58. data/test_rails_app/script/dbconsole +3 -0
  59. data/test_rails_app/script/destroy +3 -0
  60. data/test_rails_app/script/generate +3 -0
  61. data/test_rails_app/script/performance/benchmarker +3 -0
  62. data/test_rails_app/script/performance/profiler +3 -0
  63. data/test_rails_app/script/performance/request +3 -0
  64. data/test_rails_app/script/plugin +3 -0
  65. data/test_rails_app/script/process/inspector +3 -0
  66. data/test_rails_app/script/process/reaper +3 -0
  67. data/test_rails_app/script/process/spawner +3 -0
  68. data/test_rails_app/script/runner +3 -0
  69. data/test_rails_app/script/server +3 -0
  70. data/test_rails_app/script/spec +10 -0
  71. data/test_rails_app/script/spec_server +9 -0
  72. metadata +126 -0
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ log/*
3
+ db/*.sqlite3
4
+ tmp
5
+ *.sqlite3
6
+ *.log
7
+ schema.rb
8
+ pkg/*
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ivan Acosta-Rubio
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = model_notifier
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Ivan Acosta-Rubio. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ begin
2
+ require 'jeweler'
3
+ Jeweler::Tasks.new do |gemspec|
4
+ gemspec.name = "model_notifier"
5
+ gemspec.summary = "send an email when a watched model is created"
6
+ gemspec.email = "ivan@bakedweb.net"
7
+ gemspec.homepage = "http://github.com/bakedweb/model_notifier"
8
+ gemspec.description = "Notifies when a model is created"
9
+ gemspec.authors = ["Ivan Acosta-Rubio", "Corey Haines"]
10
+ end
11
+ rescue LoadError
12
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
13
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/cucumber.yml ADDED
@@ -0,0 +1,2 @@
1
+ default: features -r features
2
+ focus: features -r features -t focus
@@ -0,0 +1,24 @@
1
+ Feature: Externalized Observer for notifying on model creation
2
+ In order to use the email notifier for model creation
3
+ As a developer of a webapp
4
+ I want to have the externalized notifier be configured and send me an email
5
+
6
+
7
+ Background:
8
+ And the following configuration
9
+ | recipient_email | model_to_watch | subject |
10
+ | corey@example.com | :contact_form | test subject |
11
+
12
+
13
+ Scenario: Getting an email for a Contact Form
14
+ Given a new ContactForm with
15
+ | name | comment |
16
+ | ivan | please help me |
17
+
18
+ When I save the contact form
19
+ Then I should receive an email
20
+ When I open the email
21
+ Then I should see "test subject" in the subject
22
+
23
+
24
+
@@ -0,0 +1,74 @@
1
+ #Commonly used email steps
2
+ #
3
+ # To add your own steps make a custom_email_steps.rb
4
+ # The provided methods are:
5
+ #
6
+ # reset_mailer
7
+ # open_last_email
8
+ # visit_in_email
9
+ # unread_emails_for
10
+ # mailbox_for
11
+ # current_email
12
+ # open_email
13
+ # read_emails_for
14
+ # find_email
15
+
16
+ module EmailHelpers
17
+ def current_email_address
18
+ "corey@example.com" # Replace with your a way to find your current_email. e.g current_user.email
19
+ end
20
+ end
21
+ World(EmailHelpers)
22
+
23
+ # Use this step to reset the e-mail queue within a scenario.
24
+ # This is done automatically before each scenario.
25
+ Given /^(?:a clear email queue|no emails have been sent)$/ do
26
+ reset_mailer
27
+ end
28
+
29
+ # Use this step to open the most recently sent e-mail.
30
+ When /^I open the email$/ do
31
+ open_email(current_email_address)
32
+ end
33
+
34
+ When %r{^I follow "([^"]*?)" in the email$} do |link|
35
+ visit_in_email(link)
36
+ end
37
+
38
+ Then /^I should receive (an|\d+) emails?$/ do |amount|
39
+ amount = 1 if amount == "an"
40
+ unread_emails_for(current_email_address).size.should == amount.to_i
41
+ end
42
+
43
+ Then %r{^"([^"]*?)" should receive (\d+) emails?$} do |address, n|
44
+ unread_emails_for(address).size.should == n.to_i
45
+ end
46
+
47
+ Then %r{^"([^"]*?)" should have (\d+) emails?$} do |address, n|
48
+ mailbox_for(address).size.should == n.to_i
49
+ end
50
+
51
+ Then %r{^"([^"]*?)" should not receive an email$} do |address|
52
+ find_email(address).should be_nil
53
+ end
54
+
55
+ Then %r{^I should see "([^"]*?)" in the subject$} do |text|
56
+ current_email.should have_subject(Regexp.new(text))
57
+ end
58
+
59
+ Then %r{^I should see "([^"]*?)" in the email$} do |text|
60
+ current_email.body.should =~ Regexp.new(text)
61
+ end
62
+
63
+ When %r{^"([^"]*?)" opens? the email with subject "([^"]*?)"$} do |address, subject|
64
+ open_email(address, :with_subject => subject)
65
+ end
66
+
67
+ When %r{^"([^"]*?)" opens? the email with text "([^"]*?)"$} do |address, text|
68
+ open_email(address, :with_text => text)
69
+ end
70
+
71
+ When /^I click the first link in the email$/ do
72
+ click_first_link_in_email
73
+ end
74
+
@@ -0,0 +1,18 @@
1
+ Given /^the following configuration$/ do |table|
2
+ configuration = table.hashes[0]
3
+ ModelNotifier.configure do
4
+ recipients configuration[:recipient_email]
5
+ model configuration[:model_to_watch][1..-1].to_sym
6
+ subject configuration[:subject]
7
+ end
8
+ end
9
+
10
+ Given /^a new ContactForm with$/ do |form|
11
+ @contact_form = ContactForm.new(form.hashes[0])
12
+ end
13
+
14
+ When /^I save the contact form$/ do
15
+ @contact_form.save
16
+ end
17
+
18
+
@@ -0,0 +1,12 @@
1
+ # Sets up the Rails environment for Cucumber
2
+ ENV["RAILS_ENV"] ||= "test"
3
+ require File.expand_path(File.dirname(__FILE__) + '/../../test_rails_app/config/environment')
4
+ require 'cucumber/rails/world'
5
+ require 'cucumber/formatter/unicode' # Comment out this line if you don't want Cucumber Unicode support
6
+ Cucumber::Rails.use_transactional_fixtures
7
+ Cucumber::Rails.bypass_rescue # Comment out this line if you want Rails own error handling
8
+ # (e.g. rescue_action_in_public / rescue_responses / rescue_from)
9
+
10
+ require 'cucumber/rails/rspec'
11
+ require 'email_spec/cucumber'
12
+ require 'lib/model_notifier'
@@ -0,0 +1,61 @@
1
+ class ModelNotifier < ActionMailer::Base
2
+
3
+ def self.configure(&block)
4
+ @configurations ||= {}
5
+ configuration = configuration_from &block
6
+ configure_model configuration.watched_model
7
+ save_the configuration
8
+ end
9
+
10
+ def self.after_create(model)
11
+ model_name = model.class.name.underscore.to_sym
12
+ configuration = @configurations[model_name]
13
+
14
+ ModelNotifier.deliver_notification_email configuration, model
15
+ end
16
+
17
+ def notification_email(configuration, model)
18
+ recipients configuration.deliver_to
19
+ body :model => model
20
+ subject configuration.subject_line
21
+ mailer_name "#{self.class.name.underscore}/#{model.class.name.underscore}"
22
+ end
23
+
24
+
25
+ private
26
+ def self.save_the(configuration)
27
+ @configurations[configuration.watched_model] = configuration
28
+ end
29
+ def self.configure_model(model_name)
30
+ return if @configurations.has_key?(model_name)
31
+ observe_model model_name
32
+ end
33
+
34
+ def self.configuration_from(&block)
35
+ configuration = Configuration.new
36
+ configuration.instance_eval &block
37
+ configuration
38
+ end
39
+
40
+ def self.observe_model(model_name)
41
+ model = model_name.to_s.camelize.constantize
42
+ model.after_create ModelNotifier
43
+ end
44
+
45
+
46
+ class Configuration
47
+ attr_reader :deliver_to
48
+ attr_reader :watched_model
49
+ attr_reader :subject_line
50
+
51
+ def model(model_name)
52
+ @watched_model = model_name
53
+ end
54
+ def recipients(recipient)
55
+ @deliver_to = recipient
56
+ end
57
+ def subject(subject)
58
+ @subject_line = subject
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,82 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe ModelNotifier do
4
+ context "when configuring" do
5
+ it "can be configured with model, recipients, subject" do
6
+ ModelNotifier.configure do
7
+ model :foo
8
+ recipients "corey@example.com"
9
+ subject "test email from corey"
10
+ end
11
+ end
12
+ end
13
+
14
+ context "when saving the watched model" do
15
+ before(:each) do
16
+ expected_recipient = @expected_recipient = "corey@example.com"
17
+ expected_subject = @expected_subject = "this is a great email"
18
+ ModelNotifier.configure do
19
+ model :contact_form
20
+ recipients expected_recipient
21
+ subject expected_subject
22
+ end
23
+ @contact = ContactForm.new
24
+ end
25
+
26
+ it "receives the newly-created model through the after_create hook" do
27
+ ModelNotifier.should_receive(:after_create).with(@contact)
28
+ @contact.save
29
+ end
30
+ it "calls deliver_notification_email" do
31
+ ModelNotifier.should_receive(:deliver_notification_email).with(anything, @contact)
32
+ @contact.save
33
+ end
34
+
35
+ context "when email is being created and delivered" do
36
+ include EmailSpec::Helpers
37
+ include EmailSpec::Matchers
38
+ before(:each) do
39
+ @configuration = mock("configuration", :deliver_to => @expected_recipient, :subject_line => @expected_subject)
40
+ @email = ModelNotifier.deliver_notification_email(@configuration, @contact)
41
+ end
42
+
43
+ it "delivers to the recipient on the configuration" do
44
+ @email.should deliver_to(@configuration.deliver_to)
45
+ end
46
+
47
+ it "passes the contact object into the mail" do
48
+ @email.should have_text(/#{@contact}/)
49
+ end
50
+
51
+ it "sets the subject to what we have configured" do
52
+ @email.should have_subject(@expected_subject)
53
+ end
54
+
55
+ context "when rendering email according to model name" do
56
+ before(:each) do
57
+ expected_recipient = @expected_recipient
58
+ ModelNotifier.configure do
59
+ model :foo
60
+ recipients expected_recipient
61
+ end
62
+ end
63
+ it "renders contact_form/notification_email.rb for ContactForm" do
64
+ contact = ContactForm.new
65
+ @email = ModelNotifier.deliver_notification_email(@configuration, contact)
66
+ @email.should have_text(/Contact Email/)
67
+ end
68
+ it "renders foo/notification_email.rb for Foo" do
69
+ foo = Foo.new
70
+ @email = ModelNotifier.deliver_notification_email(@configuration, foo)
71
+ @email.should have_text(/Foo Template/)
72
+ end
73
+ end
74
+ end
75
+
76
+
77
+
78
+ end
79
+
80
+
81
+
82
+ end
data/spec/rcov.opts ADDED
@@ -0,0 +1,2 @@
1
+ --exclude "spec/*,gems/*"
2
+ --rails
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format progress
3
+ --loadby mtime
4
+ --reverse
@@ -0,0 +1,53 @@
1
+ # This file is copied to ~/spec when you run 'ruby script/generate rspec'
2
+ # from the project root directory.
3
+ ENV["RAILS_ENV"] ||= 'test'
4
+ require File.dirname(__FILE__) + "/../test_rails_app/config/environment" unless defined?(RAILS_ROOT)
5
+ require 'spec/autorun'
6
+ require 'spec/rails'
7
+
8
+ require "email_spec/helpers"
9
+ require "email_spec/matchers"
10
+
11
+ require 'lib/model_notifier'
12
+
13
+ Spec::Runner.configure do |config|
14
+ # If you're not using ActiveRecord you should remove these
15
+ # lines, delete config/database.yml and disable :active_record
16
+ # in your config/boot.rb
17
+ config.use_transactional_fixtures = true
18
+ config.use_instantiated_fixtures = false
19
+ config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
20
+
21
+
22
+ # == Fixtures
23
+ #
24
+ # You can declare fixtures for each example_group like this:
25
+ # describe "...." do
26
+ # fixtures :table_a, :table_b
27
+ #
28
+ # Alternatively, if you prefer to declare them only once, you can
29
+ # do so right here. Just uncomment the next line and replace the fixture
30
+ # names with your fixtures.
31
+ #
32
+ # config.global_fixtures = :table_a, :table_b
33
+ #
34
+ # If you declare global fixtures, be aware that they will be declared
35
+ # for all of your examples, even those that don't use them.
36
+ #
37
+ # You can also declare which fixtures to use (for example fixtures for test/fixtures):
38
+ #
39
+ # config.fixture_path = RAILS_ROOT + '/spec/fixtures/'
40
+ #
41
+ # == Mock Framework
42
+ #
43
+ # RSpec uses it's own mocking framework by default. If you prefer to
44
+ # use mocha, flexmock or RR, uncomment the appropriate line:
45
+ #
46
+ # config.mock_with :mocha
47
+ # config.mock_with :flexmock
48
+ # config.mock_with :rr
49
+ #
50
+ # == Notes
51
+ #
52
+ # For more information take a look at Spec::Runner::Configuration and Spec::Runner
53
+ end
@@ -0,0 +1,256 @@
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 and lighttpd if they are installed, otherwise
40
+ Rails will use WEBrick, the webserver that ships with Ruby. When you run script/server,
41
+ Rails will check if Mongrel exists, then lighttpd and finally fall back to WEBrick. This ensures
42
+ that you can always get up and running quickly.
43
+
44
+ Mongrel is a Ruby-based webserver with a C component (which requires compilation) that is
45
+ suitable for development and deployment of Rails applications. If you have Ruby Gems installed,
46
+ getting up and running with mongrel is as easy as: <tt>gem install mongrel</tt>.
47
+ More info at: http://mongrel.rubyforge.org
48
+
49
+ If Mongrel is not installed, Rails will look for lighttpd. It's considerably faster than
50
+ Mongrel and WEBrick and also suited for production use, but requires additional
51
+ installation and currently only works well on OS X/Unix (Windows users are encouraged
52
+ to start with Mongrel). We recommend version 1.4.11 and higher. You can download it from
53
+ http://www.lighttpd.net.
54
+
55
+ And finally, if neither Mongrel or lighttpd are installed, Rails will use the built-in Ruby
56
+ web server, WEBrick. WEBrick is a small Ruby web server suitable for development, but not
57
+ for production.
58
+
59
+ But of course its also possible to run Rails on any platform that supports FCGI.
60
+ Apache, LiteSpeed, IIS are just a few. For more information on FCGI,
61
+ please visit: http://wiki.rubyonrails.com/rails/pages/FastCGI
62
+
63
+
64
+ == Apache .htaccess example
65
+
66
+ # General Apache options
67
+ AddHandler fastcgi-script .fcgi
68
+ AddHandler cgi-script .cgi
69
+ Options +FollowSymLinks +ExecCGI
70
+
71
+ # If you don't want Rails to look in certain directories,
72
+ # use the following rewrite rules so that Apache won't rewrite certain requests
73
+ #
74
+ # Example:
75
+ # RewriteCond %{REQUEST_URI} ^/notrails.*
76
+ # RewriteRule .* - [L]
77
+
78
+ # Redirect all requests not available on the filesystem to Rails
79
+ # By default the cgi dispatcher is used which is very slow
80
+ #
81
+ # For better performance replace the dispatcher with the fastcgi one
82
+ #
83
+ # Example:
84
+ # RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
85
+ RewriteEngine On
86
+
87
+ # If your Rails application is accessed via an Alias directive,
88
+ # then you MUST also set the RewriteBase in this htaccess file.
89
+ #
90
+ # Example:
91
+ # Alias /myrailsapp /path/to/myrailsapp/public
92
+ # RewriteBase /myrailsapp
93
+
94
+ RewriteRule ^$ index.html [QSA]
95
+ RewriteRule ^([^.]+)$ $1.html [QSA]
96
+ RewriteCond %{REQUEST_FILENAME} !-f
97
+ RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
98
+
99
+ # In case Rails experiences terminal errors
100
+ # Instead of displaying this message you can supply a file here which will be rendered instead
101
+ #
102
+ # Example:
103
+ # ErrorDocument 500 /500.html
104
+
105
+ ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
106
+
107
+
108
+ == Debugging Rails
109
+
110
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
111
+ will help you debug it and get it back on the rails.
112
+
113
+ First area to check is the application log files. Have "tail -f" commands running
114
+ on the server.log and development.log. Rails will automatically display debugging
115
+ and runtime information to these files. Debugging info will also be shown in the
116
+ browser on requests from 127.0.0.1.
117
+
118
+ You can also log your own messages directly into the log file from your code using
119
+ the Ruby logger class from inside your controllers. Example:
120
+
121
+ class WeblogController < ActionController::Base
122
+ def destroy
123
+ @weblog = Weblog.find(params[:id])
124
+ @weblog.destroy
125
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
126
+ end
127
+ end
128
+
129
+ The result will be a message in your log file along the lines of:
130
+
131
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1
132
+
133
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
134
+
135
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/ including:
136
+
137
+ * The Learning Ruby (Pickaxe) Book: http://www.ruby-doc.org/docs/ProgrammingRuby/
138
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
139
+
140
+ These two online (and free) books will bring you up to speed on the Ruby language
141
+ and also on programming in general.
142
+
143
+
144
+ == Debugger
145
+
146
+ Debugger support is available through the debugger command when you start your Mongrel or
147
+ Webrick server with --debugger. This means that you can break out of execution at any point
148
+ in the code, investigate and change the model, AND then resume execution!
149
+ You need to install ruby-debug to run the server in debugging mode. With gems, use 'gem install ruby-debug'
150
+ Example:
151
+
152
+ class WeblogController < ActionController::Base
153
+ def index
154
+ @posts = Post.find(:all)
155
+ debugger
156
+ end
157
+ end
158
+
159
+ So the controller will accept the action, run the first line, then present you
160
+ with a IRB prompt in the server window. Here you can do things like:
161
+
162
+ >> @posts.inspect
163
+ => "[#<Post:0x14a6be8 @attributes={\"title\"=>nil, \"body\"=>nil, \"id\"=>\"1\"}>,
164
+ #<Post:0x14a6620 @attributes={\"title\"=>\"Rails you know!\", \"body\"=>\"Only ten..\", \"id\"=>\"2\"}>]"
165
+ >> @posts.first.title = "hello from a debugger"
166
+ => "hello from a debugger"
167
+
168
+ ...and even better is that you can examine how your runtime objects actually work:
169
+
170
+ >> f = @posts.first
171
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
172
+ >> f.
173
+ Display all 152 possibilities? (y or n)
174
+
175
+ Finally, when you're ready to resume execution, you enter "cont"
176
+
177
+
178
+ == Console
179
+
180
+ You can interact with the domain model by starting the console through <tt>script/console</tt>.
181
+ Here you'll have all parts of the application configured, just like it is when the
182
+ application is running. You can inspect domain models, change values, and save to the
183
+ database. Starting the script without arguments will launch it in the development environment.
184
+ Passing an argument will specify a different environment, like <tt>script/console production</tt>.
185
+
186
+ To reload your controllers and models after launching the console run <tt>reload!</tt>
187
+
188
+ == dbconsole
189
+
190
+ You can go to the command line of your database directly through <tt>script/dbconsole</tt>.
191
+ You would be connected to the database with the credentials defined in database.yml.
192
+ Starting the script without arguments will connect you to the development database. Passing an
193
+ argument will connect you to a different database, like <tt>script/dbconsole production</tt>.
194
+ Currently works for mysql, postgresql and sqlite.
195
+
196
+ == Description of Contents
197
+
198
+ app
199
+ Holds all the code that's specific to this particular application.
200
+
201
+ app/controllers
202
+ Holds controllers that should be named like weblogs_controller.rb for
203
+ automated URL mapping. All controllers should descend from ApplicationController
204
+ which itself descends from ActionController::Base.
205
+
206
+ app/models
207
+ Holds models that should be named like post.rb.
208
+ Most models will descend from ActiveRecord::Base.
209
+
210
+ app/views
211
+ Holds the template files for the view that should be named like
212
+ weblogs/index.html.erb for the WeblogsController#index action. All views use eRuby
213
+ syntax.
214
+
215
+ app/views/layouts
216
+ Holds the template files for layouts to be used with views. This models the common
217
+ header/footer method of wrapping views. In your views, define a layout using the
218
+ <tt>layout :default</tt> and create a file named default.html.erb. Inside default.html.erb,
219
+ call <% yield %> to render the view using this layout.
220
+
221
+ app/helpers
222
+ Holds view helpers that should be named like weblogs_helper.rb. These are generated
223
+ for you automatically when using script/generate for controllers. Helpers can be used to
224
+ wrap functionality for your views into methods.
225
+
226
+ config
227
+ Configuration files for the Rails environment, the routing map, the database, and other dependencies.
228
+
229
+ db
230
+ Contains the database schema in schema.rb. db/migrate contains all
231
+ the sequence of Migrations for your schema.
232
+
233
+ doc
234
+ This directory is where your application documentation will be stored when generated
235
+ using <tt>rake doc:app</tt>
236
+
237
+ lib
238
+ Application specific libraries. Basically, any kind of custom code that doesn't
239
+ belong under controllers, models, or helpers. This directory is in the load path.
240
+
241
+ public
242
+ The directory available for the web server. Contains subdirectories for images, stylesheets,
243
+ and javascripts. Also contains the dispatchers and the default HTML files. This should be
244
+ set as the DOCUMENT_ROOT of your web server.
245
+
246
+ script
247
+ Helper scripts for automation and generation.
248
+
249
+ test
250
+ Unit and functional tests along with fixtures. When using the script/generate scripts, template
251
+ test files will be generated for you and placed in this directory.
252
+
253
+ vendor
254
+ External libraries that the application depends on. Also includes the plugins subdirectory.
255
+ If the app has frozen rails, those gems also go here, under vendor/rails/.
256
+ This directory is in the load path.