rails-doorman 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +21 -0
- data/README.rdoc +94 -0
- data/Rakefile +89 -0
- data/features/doorman.feature +99 -0
- data/features/step_definitions/common_steps.rb +25 -0
- data/features/step_definitions/webrat_steps.rb +115 -0
- data/features/support/authorized_matcher.rb +29 -0
- data/features/support/env.rb +16 -0
- data/features/support/paths.rb +19 -0
- data/features/support/unauthorized_matcher.rb +29 -0
- data/lib/doorman.rb +111 -0
- data/lib/doorman/helpers.rb +17 -0
- data/lib/doorman/rule.rb +59 -0
- data/rails/init.rb +1 -0
- data/spec/fixtures/app/README +243 -0
- data/spec/fixtures/app/Rakefile +10 -0
- data/spec/fixtures/app/app/controllers/access_control_by_host_controller.rb +5 -0
- data/spec/fixtures/app/app/controllers/access_control_by_user_agent_controller.rb +4 -0
- data/spec/fixtures/app/app/controllers/allow_all_by_default_controller.rb +2 -0
- data/spec/fixtures/app/app/controllers/allowed_and_denied_roles_controller.rb +4 -0
- data/spec/fixtures/app/app/controllers/allowed_and_denied_users_controller.rb +4 -0
- data/spec/fixtures/app/app/controllers/allowed_role_controller.rb +3 -0
- data/spec/fixtures/app/app/controllers/allowed_role_with_only_controller.rb +3 -0
- data/spec/fixtures/app/app/controllers/allowed_user_controller.rb +3 -0
- data/spec/fixtures/app/app/controllers/application_controller.rb +37 -0
- data/spec/fixtures/app/app/controllers/denied_role_controller.rb +3 -0
- data/spec/fixtures/app/app/controllers/denied_user_controller.rb +4 -0
- data/spec/fixtures/app/app/controllers/deny_all_controller.rb +3 -0
- data/spec/fixtures/app/app/controllers/explicitly_allow_all_controller.rb +3 -0
- data/spec/fixtures/app/app/controllers/test_controller.rb +4 -0
- data/spec/fixtures/app/app/controllers/view_helpers_controller.rb +4 -0
- data/spec/fixtures/app/app/helpers/application_helper.rb +3 -0
- data/spec/fixtures/app/app/models/user.rb +7 -0
- data/spec/fixtures/app/app/views/layouts/application.html.erb +8 -0
- data/spec/fixtures/app/app/views/view_helpers/allow_via_role.html.erb +3 -0
- data/spec/fixtures/app/app/views/view_helpers/deny_via_role.html.erb +3 -0
- data/spec/fixtures/app/config/boot.rb +110 -0
- data/spec/fixtures/app/config/environment.rb +41 -0
- data/spec/fixtures/app/config/environments/development.rb +0 -0
- data/spec/fixtures/app/config/environments/production.rb +0 -0
- data/spec/fixtures/app/config/environments/test.rb +31 -0
- data/spec/fixtures/app/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/fixtures/app/config/initializers/inflections.rb +10 -0
- data/spec/fixtures/app/config/initializers/mime_types.rb +5 -0
- data/spec/fixtures/app/config/initializers/new_rails_defaults.rb +19 -0
- data/spec/fixtures/app/config/initializers/session_store.rb +15 -0
- data/spec/fixtures/app/config/locales/en.yml +5 -0
- data/spec/fixtures/app/config/routes.rb +43 -0
- data/spec/fixtures/app/db/foo.txt +0 -0
- data/spec/fixtures/app/doc/README_FOR_APP +2 -0
- data/spec/fixtures/app/log/test.log +11988 -0
- data/spec/fixtures/app/public/404.html +30 -0
- data/spec/fixtures/app/public/422.html +30 -0
- data/spec/fixtures/app/public/500.html +30 -0
- data/spec/fixtures/app/public/favicon.ico +0 -0
- data/spec/fixtures/app/public/images/rails.png +0 -0
- data/spec/fixtures/app/public/javascripts/application.js +2 -0
- data/spec/fixtures/app/public/javascripts/controls.js +963 -0
- data/spec/fixtures/app/public/javascripts/dragdrop.js +973 -0
- data/spec/fixtures/app/public/javascripts/effects.js +1128 -0
- data/spec/fixtures/app/public/javascripts/prototype.js +4320 -0
- data/spec/fixtures/app/public/robots.txt +5 -0
- data/spec/fixtures/app/script/about +4 -0
- data/spec/fixtures/app/script/console +3 -0
- data/spec/fixtures/app/script/dbconsole +3 -0
- data/spec/fixtures/app/script/destroy +3 -0
- data/spec/fixtures/app/script/generate +3 -0
- data/spec/fixtures/app/script/performance/benchmarker +3 -0
- data/spec/fixtures/app/script/performance/profiler +3 -0
- data/spec/fixtures/app/script/plugin +3 -0
- data/spec/fixtures/app/script/runner +3 -0
- data/spec/fixtures/app/script/server +3 -0
- data/spec/fixtures/app/test/performance/browsing_test.rb +9 -0
- data/spec/fixtures/app/test/test_helper.rb +38 -0
- data/spec/fixtures/app/vendor/plugins/doorman/init.rb +1 -0
- data/spec/rails_doorman/class_methods_spec.rb +49 -0
- data/spec/rails_doorman/rule_spec.rb +120 -0
- data/spec/spec_helper.rb +15 -0
- metadata +225 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
class BeAuthorized
|
2
|
+
def matches?(response)
|
3
|
+
@response = response
|
4
|
+
@response.status == "200 OK" && @response.body == 'Allowed Access'
|
5
|
+
end
|
6
|
+
|
7
|
+
# ==== Returns
|
8
|
+
# String:: The failure message.
|
9
|
+
def failure_message
|
10
|
+
<<-EOS
|
11
|
+
expected the response from #{@response.request.url}
|
12
|
+
to have the status 200 OK and body 'Allowed Access'"
|
13
|
+
but the status is #{@response.status} and the body is '#{@response.body}'"
|
14
|
+
EOS
|
15
|
+
end
|
16
|
+
|
17
|
+
# ==== Returns
|
18
|
+
# String:: The failure message to be displayed in negative matches.
|
19
|
+
def negative_failure_message
|
20
|
+
<<-EOS
|
21
|
+
expected the response from #{@response.request.url}
|
22
|
+
not to have the status 200 Ok and body 'Allowed Access'"
|
23
|
+
EOS
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def be_authorized
|
28
|
+
BeAuthorized.new
|
29
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
ENV["RAILS_ENV"] ||= "test"
|
2
|
+
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec/fixtures/app/config/environment')
|
4
|
+
require 'cucumber/rails/world'
|
5
|
+
require 'cucumber/rails/rspec'
|
6
|
+
require 'cucumber/formatter/unicode'
|
7
|
+
|
8
|
+
require 'webrat'
|
9
|
+
require 'webrat/core/matchers'
|
10
|
+
|
11
|
+
# grrr, finding this sucked
|
12
|
+
ActionController::Base.allow_rescue = true
|
13
|
+
|
14
|
+
Webrat.configure do |config|
|
15
|
+
config.mode = :rails
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module NavigationHelpers
|
2
|
+
# Maps a static name to a static route.
|
3
|
+
#
|
4
|
+
# This method is *not* designed to map from a dynamic name to a
|
5
|
+
# dynamic route like <tt>post_comments_path(post)</tt>. For dynamic
|
6
|
+
# routes like this you should *not* rely on #path_to, but write
|
7
|
+
# your own step definitions instead. Example:
|
8
|
+
#
|
9
|
+
# Given /I am on the comments page for the "(.+)" post/ |name|
|
10
|
+
# post = Post.find_by_name(name)
|
11
|
+
# visit post_comments_path(post)
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
def path_to(page_name)
|
15
|
+
page_name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
World(NavigationHelpers)
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class BeUnauthorized
|
2
|
+
def matches?(response)
|
3
|
+
@response = response
|
4
|
+
@response.status == '401 Unauthorized' && @response.body == 'Unauthorized'
|
5
|
+
end
|
6
|
+
|
7
|
+
# ==== Returns
|
8
|
+
# String:: The failure message.
|
9
|
+
def failure_message
|
10
|
+
<<-EOS
|
11
|
+
expected the response from #{@response.request.url}
|
12
|
+
to have the status 401 Unauthorized and body 'Unauthorized'"
|
13
|
+
but the status is #{@response.status} and the body is '#{@response.body}'"
|
14
|
+
EOS
|
15
|
+
end
|
16
|
+
|
17
|
+
# ==== Returns
|
18
|
+
# String:: The failure message to be displayed in negative matches.
|
19
|
+
def negative_failure_message
|
20
|
+
<<-EOS
|
21
|
+
expected the response from #{@response.request.url}
|
22
|
+
not to have the status 200 Ok and body 'Allowed Access'"
|
23
|
+
EOS
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def be_unauthorized
|
28
|
+
BeUnauthorized.new
|
29
|
+
end
|
data/lib/doorman.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
require 'doorman/rule'
|
2
|
+
require 'doorman/helpers'
|
3
|
+
|
4
|
+
module Doorman
|
5
|
+
class Unauthorized < StandardError; end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def options
|
9
|
+
@options ||= {
|
10
|
+
:user_identifier_method => :login,
|
11
|
+
:has_role_method => :has_role?
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
SUPPORTED_METHODS = [:block, :host, :role, :user, :user_agent].freeze
|
16
|
+
|
17
|
+
def supported_method?(method)
|
18
|
+
SUPPORTED_METHODS.include?(method)
|
19
|
+
end
|
20
|
+
|
21
|
+
def included(base)
|
22
|
+
base.extend(ClassMethods)
|
23
|
+
base.class_eval do
|
24
|
+
include InstanceMethods
|
25
|
+
include SharedMethods
|
26
|
+
helper Doorman::SharedMethods, Doorman::Helpers
|
27
|
+
before_filter :_doorman_check_acl
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module ClassMethods
|
33
|
+
def _doorman_list
|
34
|
+
@_doorman_list ||= []
|
35
|
+
end
|
36
|
+
|
37
|
+
def _doorman_default
|
38
|
+
@_doorman_default ||= :allow
|
39
|
+
end
|
40
|
+
|
41
|
+
def deny(*args, &block)
|
42
|
+
_add_acl(:deny, args, block)
|
43
|
+
end
|
44
|
+
|
45
|
+
def allow(*args, &block)
|
46
|
+
_add_acl(:allow, args, block)
|
47
|
+
end
|
48
|
+
|
49
|
+
def _clear_acl_list
|
50
|
+
@_doorman_list = nil
|
51
|
+
end
|
52
|
+
|
53
|
+
def _add_acl(type, args, block)
|
54
|
+
opts = args.is_a?(Array) ? args.first : args
|
55
|
+
if opts == :all
|
56
|
+
@_doorman_default = type
|
57
|
+
return true
|
58
|
+
end
|
59
|
+
if block
|
60
|
+
_doorman_list << Rule.from_block(type, opts, &block)
|
61
|
+
else
|
62
|
+
_doorman_list << Rule.from_hash(type, opts)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
module InstanceMethods
|
68
|
+
private
|
69
|
+
def _doorman_check_acl
|
70
|
+
allowed = false
|
71
|
+
self.class._doorman_list.each do |rule|
|
72
|
+
next unless rule.evaluate?(self.action_name)
|
73
|
+
if _check_rule(rule)
|
74
|
+
allowed = true
|
75
|
+
else
|
76
|
+
raise Unauthorized
|
77
|
+
end
|
78
|
+
end
|
79
|
+
if self.class._doorman_default == :deny && !allowed
|
80
|
+
raise Unauthorized
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
module SharedMethods
|
86
|
+
# self can either be in a controller or view context.
|
87
|
+
private
|
88
|
+
def _check_rule(rule)
|
89
|
+
match = case rule.method
|
90
|
+
when :block
|
91
|
+
rule.value.call(self)
|
92
|
+
when :host
|
93
|
+
request.host =~ Regexp.new(rule.value)
|
94
|
+
when :role
|
95
|
+
current_user && current_user.send(Doorman.options[:has_role_method], rule.value)
|
96
|
+
when :user
|
97
|
+
current_user && current_user.send(Doorman.options[:user_identifier_method]).to_sym == rule.value.to_sym
|
98
|
+
when :user_agent
|
99
|
+
request.user_agent =~ Regexp.new(rule.value)
|
100
|
+
else
|
101
|
+
false
|
102
|
+
end
|
103
|
+
rule.deny? ? !match : match
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
ActionController::Base.class_eval do
|
110
|
+
include Doorman
|
111
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
|
2
|
+
module Doorman
|
3
|
+
module Helpers
|
4
|
+
def allow(*args, &blk)
|
5
|
+
_capture_within_rule_context(:allow, args, &blk)
|
6
|
+
end
|
7
|
+
|
8
|
+
def deny(*args, &blk)
|
9
|
+
_capture_within_rule_context(:deny, args, &blk)
|
10
|
+
end
|
11
|
+
|
12
|
+
def _capture_within_rule_context(type, args, &blk)
|
13
|
+
_check_rule(Doorman::Rule.from_hash(type, args.first)) ? blk.call : ""
|
14
|
+
end
|
15
|
+
private :_capture_within_rule_context
|
16
|
+
end
|
17
|
+
end
|
data/lib/doorman/rule.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
|
2
|
+
module Doorman
|
3
|
+
class InvalidRule < StandardError
|
4
|
+
def initialize(*args)
|
5
|
+
super "rails_doorman: invalid rule #{args.inspect}"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Rule
|
10
|
+
def self.from_hash(type, opts)
|
11
|
+
rule = new(type)
|
12
|
+
h = opts.except(:only, :exclude)
|
13
|
+
if h.size > 1
|
14
|
+
raise InvalidRule.new(type, opts)
|
15
|
+
end
|
16
|
+
rule.method = h.keys.first.to_sym
|
17
|
+
unless Doorman.supported_method?(rule.method)
|
18
|
+
raise InvalidRule.new(type, opts)
|
19
|
+
end
|
20
|
+
rule.value = h.values.first
|
21
|
+
rule.limits = extract_limits(opts)
|
22
|
+
rule
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.from_block(type, opts = nil, &block)
|
26
|
+
unless block.arity == 1
|
27
|
+
raise InvalidRule.new(type, opts, block)
|
28
|
+
end
|
29
|
+
opts ||= {}
|
30
|
+
rule = new(type)
|
31
|
+
rule.method = :block
|
32
|
+
rule.value = block
|
33
|
+
rule.limits = extract_limits(opts)
|
34
|
+
rule
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.extract_limits(h)
|
38
|
+
h.slice(:only, :exclude).inject({}) do |limits, kv|
|
39
|
+
limits.merge!(kv.first => Array(kv.last).map {|limit| limit.to_sym })
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
attr_accessor :type, :method, :value, :limits
|
44
|
+
|
45
|
+
def initialize(type)
|
46
|
+
@type, @limits = type, {}
|
47
|
+
end
|
48
|
+
|
49
|
+
def deny?
|
50
|
+
type == :deny
|
51
|
+
end
|
52
|
+
|
53
|
+
def evaluate?(action_name)
|
54
|
+
(!limits.key?(:only) || limits[:only].include?(action_name.to_sym)) &&
|
55
|
+
(!limits.key?(:exclude) || !limits[:exclude].include?(action_name.to_sym))
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'doorman'
|
@@ -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.
|