redirector 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +71 -0
- data/Rakefile +19 -0
- data/app/models/redirect_rule.rb +61 -0
- data/app/models/request_environment_rule.rb +23 -0
- data/db/migrate/20120815212612_create_redirect_rules.rb +14 -0
- data/db/migrate/20120823163756_create_request_environment_rules.rb +13 -0
- data/lib/redirector/engine.rb +7 -0
- data/lib/redirector/middleware.rb +66 -0
- data/lib/redirector/regex_attribute.rb +38 -0
- data/lib/redirector/version.rb +3 -0
- data/lib/redirector.rb +6 -0
- data/redirector.gemspec +29 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/news_controller.rb +9 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config/application.rb +59 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +17 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/schema.rb +41 -0
- data/spec/dummy/log/development.log +128 -0
- data/spec/dummy/log/test.log +8699 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/factories/redirect_rules.rb +16 -0
- data/spec/factories/request_environment_rules.rb +15 -0
- data/spec/models/redirect_rule_spec.rb +153 -0
- data/spec/models/request_environment_rule_spec.rb +64 -0
- data/spec/requests/middleware_spec.rb +18 -0
- data/spec/spec_helper.rb +27 -0
- metadata +255 -0
data/HISTORY
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
== 0.1.0 / 2012-08-24
|
2
|
+
* Middleware for redirecting users based on rules stored in the database.
|
3
|
+
* Rules can have a regex or string source to match against.
|
4
|
+
* Regex rules can evaluate the destination using groupings from the source.
|
5
|
+
* Rules can have further request environment conditions to match based on HTTP Headers or Rack environment variables.
|
6
|
+
* RequestEnvironmentRules can be exact string matches or regex matches.
|
7
|
+
* Make regex's case sensitivity configurable
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Brian Landau
|
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.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Redirector
|
2
|
+
|
3
|
+
Redirector is a Rails engine that adds a piece of middleware to the top of your middleware stack that looks for redirect rules stored in your database and redirects you accordingly.
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
1. Add this to your Gemfile and then `bundle install`:
|
8
|
+
<pre><code>gem 'redirector'</code></pre>
|
9
|
+
2. `$ rake redirector:install:migrations`
|
10
|
+
3. `$ rake db:migrate`
|
11
|
+
4. Create an interface for admins to manage the redirect rules.
|
12
|
+
|
13
|
+
|
14
|
+
## Redirect Rule definitions
|
15
|
+
|
16
|
+
Redirect rules have 3 parts:
|
17
|
+
|
18
|
+
1. A Source
|
19
|
+
2. A Destination
|
20
|
+
3. Request environment conditions
|
21
|
+
|
22
|
+
The source defines how to match the incoming request path and the destination is where to send the visitor if the match is made. A source can be a strict string equality match or it can be a regular expression that is matched. If a regular expression is used and it uses groupings, you can reference those groupings inside of the destination. For instance a regex like `/my_custom_path\/([0-9]+)/` could use that grouping in the destination like this `"/my_destination/$1"`. So, if the request path was `"/my_custom_path/10"` then the destination for that rule would be `"/my_destination/10"`.
|
23
|
+
|
24
|
+
Redirect rules can also have further Rack/HTTP environment (mainly HTTP headers) conditions via RequestEnvironmentRules. These define a key in the rack environment passed into the middleware and a value match you require for the redirect rule it's tied too. Similar to the redirect rules these RequestEnvironmentRules can be string matches or regex matches. A redirect rule can have as many of these environment rules as you need.
|
25
|
+
|
26
|
+
When using regex matching on either a redirect rule source or a request environment rule environment value you can specify if you want the matching to be case sensitive or case insensitive with a boolean column that's on the table.
|
27
|
+
|
28
|
+
### Schema Definition
|
29
|
+
|
30
|
+
Here's the schema definition used for the two tables:
|
31
|
+
|
32
|
+
create_table "redirect_rules", :force => true do |t|
|
33
|
+
t.string "source", :null => false # Matched against the request path
|
34
|
+
t.boolean "source_is_regex", :default => false, :null => false # Is the source a regular expression or not
|
35
|
+
t.boolean "source_is_case_sensitive", :default => false, :null => false # Is the source regex cas sensitive or not
|
36
|
+
t.string "destination", :null => false
|
37
|
+
t.boolean "active", :default => false # Should this rule be applied or not
|
38
|
+
t.datetime "created_at", :null => false
|
39
|
+
t.datetime "updated_at", :null => false
|
40
|
+
end
|
41
|
+
|
42
|
+
create_table "request_environment_rules", :force => true do |t|
|
43
|
+
t.integer "redirect_rule_id", :null => false
|
44
|
+
t.string "environment_key_name", :null => false # Name of the enviornment key (e.g. "QUERY_STRING", "HTTP_HOST")
|
45
|
+
t.string "environment_value", :null => false # What to match the value of the specified environment attribute against
|
46
|
+
t.boolean "environment_value_is_regex", :default => false, :null => false # Is the value match a regex or not
|
47
|
+
t.boolean "environment_value_is_case_sensitive", :default => true, :null => false # is the value regex case sensitive or not
|
48
|
+
t.datetime "created_at", :null => false
|
49
|
+
t.datetime "updated_at", :null => false
|
50
|
+
end
|
51
|
+
|
52
|
+
## Databases supported
|
53
|
+
|
54
|
+
* MySQL
|
55
|
+
* PostgreSQL
|
56
|
+
|
57
|
+
If you require support for another database, the only thing that needs to be added is a definition for a SQL regular expression conditional (see `app/models/redirect_rule.rb`). If you create a pull request that adds support for another database, it will most likely be merged in.
|
58
|
+
|
59
|
+
## Contributing to Redirector
|
60
|
+
|
61
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
62
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
63
|
+
* Fork the project.
|
64
|
+
* Start a feature/bugfix branch.
|
65
|
+
* Commit and push until you are happy with your contribution.
|
66
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
67
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
68
|
+
|
69
|
+
## Copyright
|
70
|
+
|
71
|
+
Copyright (c) 2012 Brian Landau (Viget). See MIT_LICENSE for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
|
8
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
9
|
+
load 'rails/tasks/engine.rake'
|
10
|
+
|
11
|
+
Bundler::GemHelper.install_tasks
|
12
|
+
|
13
|
+
require 'rspec/core'
|
14
|
+
require 'rspec/core/rake_task'
|
15
|
+
|
16
|
+
desc "Run all specs in spec directory (excluding plugin specs)"
|
17
|
+
RSpec::Core::RakeTask.new(:spec)
|
18
|
+
|
19
|
+
task :default => :spec
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class RedirectRule < ActiveRecord::Base
|
2
|
+
extend Redirector::RegexAttribute
|
3
|
+
regex_attribute :source
|
4
|
+
|
5
|
+
has_many :request_environment_rules
|
6
|
+
|
7
|
+
attr_accessible :source,
|
8
|
+
:source_is_regex,
|
9
|
+
:destination,
|
10
|
+
:active,
|
11
|
+
:source_is_case_sensitive
|
12
|
+
|
13
|
+
validates :source, :destination, :active, :presence => true
|
14
|
+
|
15
|
+
def self.regex_expression
|
16
|
+
case connection.adapter_name
|
17
|
+
when 'PostgreSQL'
|
18
|
+
'(redirect_rules.source_is_case_sensitive = :true AND :source ~ redirect_rules.source) OR '+
|
19
|
+
'(redirect_rules.source_is_case_sensitive = :false AND :source ~* redirect_rules.source)'
|
20
|
+
when /mysql/i
|
21
|
+
'(redirect_rules.source_is_case_sensitive = :true AND :source REGEXP BINARY redirect_rules.source) OR '+
|
22
|
+
'(redirect_rules.source_is_case_sensitive = :false AND :source REGEXP redirect_rules.source)'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.match_sql_condition
|
27
|
+
<<-SQL
|
28
|
+
redirect_rules.active = :true AND
|
29
|
+
((source_is_regex = :false AND redirect_rules.source = :source) OR
|
30
|
+
(source_is_regex = :true AND (#{regex_expression})))
|
31
|
+
SQL
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.match_for(source, environment)
|
35
|
+
where(match_sql_condition.strip, {:true => true, :false => false, :source => source}).detect do |rule|
|
36
|
+
rule.request_environment_rules.all? {|env_rule| env_rule.matches?(environment) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.destination_for(source, environment)
|
41
|
+
rule = match_for(source, environment)
|
42
|
+
rule.evaluated_destination_for(source) if rule
|
43
|
+
end
|
44
|
+
|
45
|
+
def evaluated_destination_for(request_path)
|
46
|
+
if source_is_regex? && request_path =~ source_regex
|
47
|
+
matches = $~
|
48
|
+
number_of_grouped_matches = matches.length - 1
|
49
|
+
final_destination = destination.dup
|
50
|
+
|
51
|
+
number_of_grouped_matches.downto(1) do |index|
|
52
|
+
final_destination.gsub!(/\$#{index}/, matches[index])
|
53
|
+
end
|
54
|
+
|
55
|
+
final_destination
|
56
|
+
else
|
57
|
+
destination
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class RequestEnvironmentRule < ActiveRecord::Base
|
2
|
+
extend Redirector::RegexAttribute
|
3
|
+
regex_attribute :environment_value
|
4
|
+
|
5
|
+
belongs_to :redirect_rule
|
6
|
+
|
7
|
+
attr_accessible :redirect_rule_id,
|
8
|
+
:environment_key_name,
|
9
|
+
:environment_value,
|
10
|
+
:environment_value_is_regex,
|
11
|
+
:environment_value_is_case_sensitive
|
12
|
+
|
13
|
+
validates :redirect_rule_id, :environment_key_name, :environment_value, :presence => true
|
14
|
+
|
15
|
+
def matches?(environment)
|
16
|
+
if environment_value_is_regex?
|
17
|
+
environment[environment_key_name] && environment[environment_key_name] =~ environment_value_regex
|
18
|
+
else
|
19
|
+
environment[environment_key_name] == environment_value
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class CreateRedirectRules < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :redirect_rules do |t|
|
4
|
+
t.string :source, :null => false
|
5
|
+
t.boolean :source_is_regex, :null => false, :default => false
|
6
|
+
t.boolean :source_is_case_sensitive, :null => false, :default => false
|
7
|
+
t.string :destination, :null => false
|
8
|
+
t.boolean :active, :default => false
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
add_index :redirect_rules, :source
|
12
|
+
add_index :redirect_rules, :active
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
class CreateRequestEnvironmentRules < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table :request_environment_rules do |t|
|
4
|
+
t.integer :redirect_rule_id, :null => false
|
5
|
+
t.string :environment_key_name, :null => false
|
6
|
+
t.string :environment_value, :null => false
|
7
|
+
t.boolean :environment_value_is_regex, :null => false, :default => false
|
8
|
+
t.boolean :environment_value_is_case_sensitive, :null => false, :default => true
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
add_index :request_environment_rules, :redirect_rule_id
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Redirector
|
2
|
+
class Middleware
|
3
|
+
def initialize(application)
|
4
|
+
@application = application
|
5
|
+
end
|
6
|
+
|
7
|
+
def call(environment)
|
8
|
+
Responder.new(@application, environment).response
|
9
|
+
end
|
10
|
+
|
11
|
+
class Responder
|
12
|
+
attr_reader :app, :env
|
13
|
+
|
14
|
+
def initialize(application, environment)
|
15
|
+
@app = application
|
16
|
+
@env = environment
|
17
|
+
end
|
18
|
+
|
19
|
+
def response
|
20
|
+
if redirect?
|
21
|
+
redirect_response
|
22
|
+
else
|
23
|
+
app.call(env)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def redirect?
|
30
|
+
matched_destination.present?
|
31
|
+
end
|
32
|
+
|
33
|
+
def matched_destination
|
34
|
+
@matched_destination ||= RedirectRule.destination_for(request_path, env)
|
35
|
+
end
|
36
|
+
|
37
|
+
def request_path
|
38
|
+
env['PATH_INFO']
|
39
|
+
end
|
40
|
+
|
41
|
+
def request_host
|
42
|
+
env['HTTP_HOST'].split(':').first
|
43
|
+
end
|
44
|
+
|
45
|
+
def redirect_response
|
46
|
+
[301, {'Location' => redirect_url_string},
|
47
|
+
%{You are being redirected <a href="#{redirect_url_string}">#{redirect_url_string}</a>}]
|
48
|
+
end
|
49
|
+
|
50
|
+
def destination_uri
|
51
|
+
URI.parse(matched_destination)
|
52
|
+
end
|
53
|
+
|
54
|
+
def redirect_uri
|
55
|
+
destination_uri.tap do |uri|
|
56
|
+
uri.scheme ||= 'http'
|
57
|
+
uri.host ||= request_host
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def redirect_url_string
|
62
|
+
@redirect_url_string ||= redirect_uri.to_s
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Redirector
|
2
|
+
module RegexAttribute
|
3
|
+
|
4
|
+
def regex_attribute(attribute_name)
|
5
|
+
include ValidationMethod
|
6
|
+
|
7
|
+
cattr_accessor :regex_attribute_name
|
8
|
+
self.regex_attribute_name = attribute_name
|
9
|
+
|
10
|
+
validates "#{attribute_name}_is_regex".to_sym, :inclusion => { :in => ['0', '1', true, false] }
|
11
|
+
validates "#{attribute_name}_is_case_sensitive".to_sym, :inclusion => { :in => ['0', '1', true, false] }
|
12
|
+
validate :regex_attribute_is_valid_regex
|
13
|
+
|
14
|
+
define_method("#{regex_attribute_name}_regex") do
|
15
|
+
if self.send("#{regex_attribute_name}_is_case_sensitive?")
|
16
|
+
Regexp.compile(self.send(regex_attribute_name))
|
17
|
+
else
|
18
|
+
Regexp.compile(self.send(regex_attribute_name), Regexp::IGNORECASE)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ValidationMethod
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def regex_attribute_is_valid_regex
|
28
|
+
if self.send("#{regex_attribute_name}_is_regex?") && self.send("#{regex_attribute_name}?")
|
29
|
+
begin
|
30
|
+
Regexp.compile(self.send(regex_attribute_name))
|
31
|
+
rescue RegexpError
|
32
|
+
errors.add(regex_attribute_name, 'is an invalid regular expression')
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/redirector.rb
ADDED
data/redirector.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
# Maintain your gem's version:
|
4
|
+
require "redirector/version"
|
5
|
+
|
6
|
+
# Describe your gem and declare its dependencies:
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "redirector"
|
9
|
+
s.version = Redirector::VERSION
|
10
|
+
s.authors = ["Brian Landau"]
|
11
|
+
s.email = ["brian.landau@viget.com"]
|
12
|
+
s.homepage = "https://github.com/vigetlabs/redirector"
|
13
|
+
s.summary = "A Rails engine that adds a piece of middleware to the top of your middleware stack that looks for redirect rules stored in your database and redirects you accordingly."
|
14
|
+
s.description = "A Rails engine that adds a piece of middleware to the top of your middleware stack that looks for redirect rules stored in your database and redirects you accordingly."
|
15
|
+
|
16
|
+
s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile",
|
17
|
+
"README.md", "redirector.gemspec", "HISTORY"]
|
18
|
+
s.test_files = Dir["spec/**/*"]
|
19
|
+
|
20
|
+
s.add_dependency "rails", "~> 3.1"
|
21
|
+
# s.add_dependency "jquery-rails"
|
22
|
+
|
23
|
+
s.add_development_dependency "mysql2"
|
24
|
+
s.add_development_dependency 'rspec-rails'
|
25
|
+
s.add_development_dependency 'shoulda-matchers'
|
26
|
+
s.add_development_dependency 'capybara'
|
27
|
+
s.add_development_dependency 'database_cleaner'
|
28
|
+
s.add_development_dependency 'factory_girl_rails', '~> 1.7'
|
29
|
+
end
|
@@ -0,0 +1,261 @@
|
|
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"
|
7
|
+
templates that are primarily responsible for inserting pre-built data in between
|
8
|
+
HTML tags. The model contains the "smart" domain objects (such as Account,
|
9
|
+
Product, Person, Post) that holds all the business logic and knows how to
|
10
|
+
persist themselves to a database. The controller handles the incoming requests
|
11
|
+
(such as Save New Account, Update Product, Show Post) by manipulating the model
|
12
|
+
and directing data to the view.
|
13
|
+
|
14
|
+
In Rails, the model is handled by what's called an object-relational mapping
|
15
|
+
layer entitled Active Record. This layer allows you to present the data from
|
16
|
+
database rows as objects and embellish these data objects with business logic
|
17
|
+
methods. You can read more about Active Record in
|
18
|
+
link:files/vendor/rails/activerecord/README.html.
|
19
|
+
|
20
|
+
The controller and view are handled by the Action Pack, which handles both
|
21
|
+
layers by its two parts: Action View and Action Controller. These two layers
|
22
|
+
are bundled in a single package due to their heavy interdependence. This is
|
23
|
+
unlike the relationship between the Active Record and Action Pack that is much
|
24
|
+
more separate. Each of these packages can be used independently outside of
|
25
|
+
Rails. You can read more about Action Pack in
|
26
|
+
link:files/vendor/rails/actionpack/README.html.
|
27
|
+
|
28
|
+
|
29
|
+
== Getting Started
|
30
|
+
|
31
|
+
1. At the command prompt, create a new Rails application:
|
32
|
+
<tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
|
33
|
+
|
34
|
+
2. Change directory to <tt>myapp</tt> and start the web server:
|
35
|
+
<tt>cd myapp; rails server</tt> (run with --help for options)
|
36
|
+
|
37
|
+
3. Go to http://localhost:3000/ and you'll see:
|
38
|
+
"Welcome aboard: You're riding Ruby on Rails!"
|
39
|
+
|
40
|
+
4. Follow the guidelines to start developing your application. You can find
|
41
|
+
the following resources handy:
|
42
|
+
|
43
|
+
* The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
|
44
|
+
* Ruby on Rails Tutorial Book: http://www.railstutorial.org/
|
45
|
+
|
46
|
+
|
47
|
+
== Debugging Rails
|
48
|
+
|
49
|
+
Sometimes your application goes wrong. Fortunately there are a lot of tools that
|
50
|
+
will help you debug it and get it back on the rails.
|
51
|
+
|
52
|
+
First area to check is the application log files. Have "tail -f" commands
|
53
|
+
running on the server.log and development.log. Rails will automatically display
|
54
|
+
debugging and runtime information to these files. Debugging info will also be
|
55
|
+
shown in the browser on requests from 127.0.0.1.
|
56
|
+
|
57
|
+
You can also log your own messages directly into the log file from your code
|
58
|
+
using the Ruby logger class from inside your controllers. Example:
|
59
|
+
|
60
|
+
class WeblogController < ActionController::Base
|
61
|
+
def destroy
|
62
|
+
@weblog = Weblog.find(params[:id])
|
63
|
+
@weblog.destroy
|
64
|
+
logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
The result will be a message in your log file along the lines of:
|
69
|
+
|
70
|
+
Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
|
71
|
+
|
72
|
+
More information on how to use the logger is at http://www.ruby-doc.org/core/
|
73
|
+
|
74
|
+
Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
|
75
|
+
several books available online as well:
|
76
|
+
|
77
|
+
* Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
|
78
|
+
* Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
|
79
|
+
|
80
|
+
These two books will bring you up to speed on the Ruby language and also on
|
81
|
+
programming in general.
|
82
|
+
|
83
|
+
|
84
|
+
== Debugger
|
85
|
+
|
86
|
+
Debugger support is available through the debugger command when you start your
|
87
|
+
Mongrel or WEBrick server with --debugger. This means that you can break out of
|
88
|
+
execution at any point in the code, investigate and change the model, and then,
|
89
|
+
resume execution! You need to install ruby-debug to run the server in debugging
|
90
|
+
mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
|
91
|
+
|
92
|
+
class WeblogController < ActionController::Base
|
93
|
+
def index
|
94
|
+
@posts = Post.all
|
95
|
+
debugger
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
So the controller will accept the action, run the first line, then present you
|
100
|
+
with a IRB prompt in the server window. Here you can do things like:
|
101
|
+
|
102
|
+
>> @posts.inspect
|
103
|
+
=> "[#<Post:0x14a6be8
|
104
|
+
@attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
|
105
|
+
#<Post:0x14a6620
|
106
|
+
@attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
|
107
|
+
>> @posts.first.title = "hello from a debugger"
|
108
|
+
=> "hello from a debugger"
|
109
|
+
|
110
|
+
...and even better, you can examine how your runtime objects actually work:
|
111
|
+
|
112
|
+
>> f = @posts.first
|
113
|
+
=> #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
|
114
|
+
>> f.
|
115
|
+
Display all 152 possibilities? (y or n)
|
116
|
+
|
117
|
+
Finally, when you're ready to resume execution, you can enter "cont".
|
118
|
+
|
119
|
+
|
120
|
+
== Console
|
121
|
+
|
122
|
+
The console is a Ruby shell, which allows you to interact with your
|
123
|
+
application's domain model. Here you'll have all parts of the application
|
124
|
+
configured, just like it is when the application is running. You can inspect
|
125
|
+
domain models, change values, and save to the database. Starting the script
|
126
|
+
without arguments will launch it in the development environment.
|
127
|
+
|
128
|
+
To start the console, run <tt>rails console</tt> from the application
|
129
|
+
directory.
|
130
|
+
|
131
|
+
Options:
|
132
|
+
|
133
|
+
* Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
|
134
|
+
made to the database.
|
135
|
+
* Passing an environment name as an argument will load the corresponding
|
136
|
+
environment. Example: <tt>rails console production</tt>.
|
137
|
+
|
138
|
+
To reload your controllers and models after launching the console run
|
139
|
+
<tt>reload!</tt>
|
140
|
+
|
141
|
+
More information about irb can be found at:
|
142
|
+
link:http://www.rubycentral.org/pickaxe/irb.html
|
143
|
+
|
144
|
+
|
145
|
+
== dbconsole
|
146
|
+
|
147
|
+
You can go to the command line of your database directly through <tt>rails
|
148
|
+
dbconsole</tt>. You would be connected to the database with the credentials
|
149
|
+
defined in database.yml. Starting the script without arguments will connect you
|
150
|
+
to the development database. Passing an argument will connect you to a different
|
151
|
+
database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
|
152
|
+
PostgreSQL and SQLite 3.
|
153
|
+
|
154
|
+
== Description of Contents
|
155
|
+
|
156
|
+
The default directory structure of a generated Ruby on Rails application:
|
157
|
+
|
158
|
+
|-- app
|
159
|
+
| |-- assets
|
160
|
+
| |-- images
|
161
|
+
| |-- javascripts
|
162
|
+
| `-- stylesheets
|
163
|
+
| |-- controllers
|
164
|
+
| |-- helpers
|
165
|
+
| |-- mailers
|
166
|
+
| |-- models
|
167
|
+
| `-- views
|
168
|
+
| `-- layouts
|
169
|
+
|-- config
|
170
|
+
| |-- environments
|
171
|
+
| |-- initializers
|
172
|
+
| `-- locales
|
173
|
+
|-- db
|
174
|
+
|-- doc
|
175
|
+
|-- lib
|
176
|
+
| `-- tasks
|
177
|
+
|-- log
|
178
|
+
|-- public
|
179
|
+
|-- script
|
180
|
+
|-- test
|
181
|
+
| |-- fixtures
|
182
|
+
| |-- functional
|
183
|
+
| |-- integration
|
184
|
+
| |-- performance
|
185
|
+
| `-- unit
|
186
|
+
|-- tmp
|
187
|
+
| |-- cache
|
188
|
+
| |-- pids
|
189
|
+
| |-- sessions
|
190
|
+
| `-- sockets
|
191
|
+
`-- vendor
|
192
|
+
|-- assets
|
193
|
+
`-- stylesheets
|
194
|
+
`-- plugins
|
195
|
+
|
196
|
+
app
|
197
|
+
Holds all the code that's specific to this particular application.
|
198
|
+
|
199
|
+
app/assets
|
200
|
+
Contains subdirectories for images, stylesheets, and JavaScript files.
|
201
|
+
|
202
|
+
app/controllers
|
203
|
+
Holds controllers that should be named like weblogs_controller.rb for
|
204
|
+
automated URL mapping. All controllers should descend from
|
205
|
+
ApplicationController which itself descends from ActionController::Base.
|
206
|
+
|
207
|
+
app/models
|
208
|
+
Holds models that should be named like post.rb. Models descend from
|
209
|
+
ActiveRecord::Base by default.
|
210
|
+
|
211
|
+
app/views
|
212
|
+
Holds the template files for the view that should be named like
|
213
|
+
weblogs/index.html.erb for the WeblogsController#index action. All views use
|
214
|
+
eRuby syntax by default.
|
215
|
+
|
216
|
+
app/views/layouts
|
217
|
+
Holds the template files for layouts to be used with views. This models the
|
218
|
+
common header/footer method of wrapping views. In your views, define a layout
|
219
|
+
using the <tt>layout :default</tt> and create a file named default.html.erb.
|
220
|
+
Inside default.html.erb, call <% yield %> to render the view using this
|
221
|
+
layout.
|
222
|
+
|
223
|
+
app/helpers
|
224
|
+
Holds view helpers that should be named like weblogs_helper.rb. These are
|
225
|
+
generated for you automatically when using generators for controllers.
|
226
|
+
Helpers can be used to wrap functionality for your views into methods.
|
227
|
+
|
228
|
+
config
|
229
|
+
Configuration files for the Rails environment, the routing map, the database,
|
230
|
+
and other dependencies.
|
231
|
+
|
232
|
+
db
|
233
|
+
Contains the database schema in schema.rb. db/migrate contains all the
|
234
|
+
sequence of Migrations for your schema.
|
235
|
+
|
236
|
+
doc
|
237
|
+
This directory is where your application documentation will be stored when
|
238
|
+
generated using <tt>rake doc:app</tt>
|
239
|
+
|
240
|
+
lib
|
241
|
+
Application specific libraries. Basically, any kind of custom code that
|
242
|
+
doesn't belong under controllers, models, or helpers. This directory is in
|
243
|
+
the load path.
|
244
|
+
|
245
|
+
public
|
246
|
+
The directory available for the web server. Also contains the dispatchers and the
|
247
|
+
default HTML files. This should be set as the DOCUMENT_ROOT of your web
|
248
|
+
server.
|
249
|
+
|
250
|
+
script
|
251
|
+
Helper scripts for automation and generation.
|
252
|
+
|
253
|
+
test
|
254
|
+
Unit and functional tests along with fixtures. When using the rails generate
|
255
|
+
command, template test files will be generated for you and placed in this
|
256
|
+
directory.
|
257
|
+
|
258
|
+
vendor
|
259
|
+
External libraries that the application depends on. Also includes the plugins
|
260
|
+
subdirectory. If the app has frozen rails, those gems also go here, under
|
261
|
+
vendor/rails/. This directory is in the load path.
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
3
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
4
|
+
|
5
|
+
require File.expand_path('../config/application', __FILE__)
|
6
|
+
|
7
|
+
Dummy::Application.load_tasks
|