authlogic 0.10.4
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of authlogic might be problematic. Click here for more details.
- data/CHANGELOG.rdoc +47 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +100 -0
- data/README.rdoc +292 -0
- data/Rakefile +15 -0
- data/authlogic.gemspec +38 -0
- data/init.rb +1 -0
- data/lib/authlogic.rb +25 -0
- data/lib/authlogic/active_record/acts_as_authentic.rb +265 -0
- data/lib/authlogic/active_record/authenticates_many.rb +19 -0
- data/lib/authlogic/active_record/scoped_session.rb +28 -0
- data/lib/authlogic/controller_adapters/abstract_adapter.rb +25 -0
- data/lib/authlogic/controller_adapters/rails_adapter.rb +39 -0
- data/lib/authlogic/session/active_record_trickery.rb +26 -0
- data/lib/authlogic/session/base.rb +510 -0
- data/lib/authlogic/session/callbacks.rb +56 -0
- data/lib/authlogic/session/config.rb +237 -0
- data/lib/authlogic/session/errors.rb +18 -0
- data/lib/authlogic/sha512_crypto_provider.rb +18 -0
- data/lib/authlogic/version.rb +56 -0
- data/test_app/README +256 -0
- data/test_app/Rakefile +10 -0
- data/test_app/app/controllers/application.rb +72 -0
- data/test_app/app/controllers/companies_controller.rb +2 -0
- data/test_app/app/controllers/user_sessions_controller.rb +25 -0
- data/test_app/app/controllers/users_controller.rb +61 -0
- data/test_app/app/helpers/application_helper.rb +3 -0
- data/test_app/app/helpers/companies_helper.rb +2 -0
- data/test_app/app/helpers/user_sessions_helper.rb +2 -0
- data/test_app/app/helpers/users_helper.rb +2 -0
- data/test_app/app/models/company.rb +4 -0
- data/test_app/app/models/project.rb +3 -0
- data/test_app/app/models/user.rb +5 -0
- data/test_app/app/models/user_session.rb +3 -0
- data/test_app/app/views/layouts/application.html.erb +27 -0
- data/test_app/app/views/user_sessions/new.html.erb +15 -0
- data/test_app/app/views/users/_form.erb +15 -0
- data/test_app/app/views/users/edit.html.erb +8 -0
- data/test_app/app/views/users/new.html.erb +8 -0
- data/test_app/app/views/users/show.html.erb +29 -0
- data/test_app/config/boot.rb +109 -0
- data/test_app/config/database.yml +19 -0
- data/test_app/config/environment.rb +69 -0
- data/test_app/config/environments/development.rb +17 -0
- data/test_app/config/environments/production.rb +22 -0
- data/test_app/config/environments/test.rb +22 -0
- data/test_app/config/initializers/inflections.rb +10 -0
- data/test_app/config/initializers/mime_types.rb +5 -0
- data/test_app/config/initializers/new_rails_defaults.rb +17 -0
- data/test_app/config/routes.rb +11 -0
- data/test_app/db/development.sqlite3 +0 -0
- data/test_app/db/migrate/20081023040052_create_users.rb +20 -0
- data/test_app/db/migrate/20081103003828_create_companies.rb +14 -0
- data/test_app/db/migrate/20081103003834_create_projects.rb +18 -0
- data/test_app/db/schema.rb +46 -0
- data/test_app/db/test.sqlite3 +0 -0
- data/test_app/doc/README_FOR_APP +2 -0
- data/test_app/public/404.html +30 -0
- data/test_app/public/422.html +30 -0
- data/test_app/public/500.html +30 -0
- data/test_app/public/dispatch.cgi +10 -0
- data/test_app/public/dispatch.fcgi +24 -0
- data/test_app/public/dispatch.rb +10 -0
- data/test_app/public/favicon.ico +0 -0
- data/test_app/public/images/rails.png +0 -0
- data/test_app/public/javascripts/application.js +2 -0
- data/test_app/public/javascripts/controls.js +963 -0
- data/test_app/public/javascripts/dragdrop.js +972 -0
- data/test_app/public/javascripts/effects.js +1120 -0
- data/test_app/public/javascripts/prototype.js +4225 -0
- data/test_app/public/robots.txt +5 -0
- data/test_app/public/stylesheets/scaffold.css +62 -0
- data/test_app/script/about +4 -0
- data/test_app/script/console +3 -0
- data/test_app/script/dbconsole +3 -0
- data/test_app/script/destroy +3 -0
- data/test_app/script/generate +3 -0
- data/test_app/script/performance/benchmarker +3 -0
- data/test_app/script/performance/profiler +3 -0
- data/test_app/script/performance/request +3 -0
- data/test_app/script/plugin +3 -0
- data/test_app/script/process/inspector +3 -0
- data/test_app/script/process/reaper +3 -0
- data/test_app/script/process/spawner +3 -0
- data/test_app/script/runner +3 -0
- data/test_app/script/server +3 -0
- data/test_app/test/fixtures/companies.yml +7 -0
- data/test_app/test/fixtures/projects.yml +4 -0
- data/test_app/test/fixtures/users.yml +21 -0
- data/test_app/test/functional/companies_controller_test.rb +8 -0
- data/test_app/test/functional/user_sessions_controller_test.rb +36 -0
- data/test_app/test/functional/users_controller_test.rb +8 -0
- data/test_app/test/integration/company_user_session_stories_test.rb +46 -0
- data/test_app/test/integration/user_sesion_stories_test.rb +105 -0
- data/test_app/test/integration/user_session_config_test.rb +24 -0
- data/test_app/test/integration/user_session_test.rb +161 -0
- data/test_app/test/test_helper.rb +81 -0
- data/test_app/test/unit/account_test.rb +8 -0
- data/test_app/test/unit/company_test.rb +8 -0
- data/test_app/test/unit/project_test.rb +8 -0
- data/test_app/test/unit/user_test.rb +80 -0
- metadata +201 -0
@@ -0,0 +1,56 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
# = Callbacks
|
4
|
+
#
|
5
|
+
# Just like in ActiveRecord you have before_save, before_validation, etc. You have similar callbacks with Authlogic, see all callbacks below.
|
6
|
+
module Callbacks
|
7
|
+
CALLBACKS = %w(before_create after_create before_destroy after_destroy before_save after_save before_update after_update before_validation after_validation)
|
8
|
+
|
9
|
+
def self.included(base) #:nodoc:
|
10
|
+
[:destroy, :save, :valid?, :validate_credentials].each do |method|
|
11
|
+
base.send :alias_method_chain, method, :callbacks
|
12
|
+
end
|
13
|
+
|
14
|
+
base.send :include, ActiveSupport::Callbacks
|
15
|
+
base.define_callbacks *CALLBACKS
|
16
|
+
end
|
17
|
+
|
18
|
+
def destroy_with_callbacks # :nodoc:
|
19
|
+
run_callbacks(:before_destroy)
|
20
|
+
result = destroy_without_callbacks
|
21
|
+
run_callbacks(:after_destroy) if result
|
22
|
+
result
|
23
|
+
end
|
24
|
+
|
25
|
+
def save_with_callbacks # :nodoc:
|
26
|
+
if new_session?
|
27
|
+
run_callbacks(:before_create)
|
28
|
+
else
|
29
|
+
run_callbacks(:before_update)
|
30
|
+
end
|
31
|
+
run_callbacks(:before_save)
|
32
|
+
result = save_without_callbacks
|
33
|
+
if result
|
34
|
+
if new_session?
|
35
|
+
run_callbacks(:after_create)
|
36
|
+
else
|
37
|
+
run_callbacks(:after_update)
|
38
|
+
end
|
39
|
+
run_callbacks(:after_save)
|
40
|
+
end
|
41
|
+
result
|
42
|
+
end
|
43
|
+
|
44
|
+
def valid_with_callbacks?
|
45
|
+
result = valid_without_callbacks?
|
46
|
+
run_callbacks(:after_validation) if result
|
47
|
+
result
|
48
|
+
end
|
49
|
+
|
50
|
+
def validate_credentials_with_callbacks # :nodoc:
|
51
|
+
run_callbacks(:before_validation)
|
52
|
+
validate_credentials_without_callbacks
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,237 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
module Config # :nodoc:
|
4
|
+
def self.included(klass)
|
5
|
+
klass.extend(ClassMethods)
|
6
|
+
klass.send(:include, InstanceMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
# = Config
|
10
|
+
#
|
11
|
+
# Configuration is simple. The configuration options are just class methods. Just put this in your config/initializers directory
|
12
|
+
#
|
13
|
+
# UserSession.configure do |config|
|
14
|
+
# config.authenticate_with = User
|
15
|
+
# # ... more configuration
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# or you can set your configuration in the session class directly:
|
19
|
+
#
|
20
|
+
# class UserSession < Authlogic::Session::Base
|
21
|
+
# authenticate_with User
|
22
|
+
# # ... more configuration
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# See the methods belows for all configuration options.
|
26
|
+
module ClassMethods
|
27
|
+
# Lets you change which model to use for authentication.
|
28
|
+
#
|
29
|
+
# * <tt>Default:</tt> inferred from the class name. UserSession would automatically try User
|
30
|
+
# * <tt>Accepts:</tt> an ActiveRecord class
|
31
|
+
def authenticate_with(klass)
|
32
|
+
@klass_name = klass.name
|
33
|
+
@klass = klass
|
34
|
+
end
|
35
|
+
alias_method :authenticate_with=, :authenticate_with
|
36
|
+
|
37
|
+
# Convenience method that lets you easily set configuration, see examples above
|
38
|
+
def configure
|
39
|
+
yield self
|
40
|
+
end
|
41
|
+
|
42
|
+
# The name of the cookie or the key in the cookies hash. Be sure and use a unique name. If you have multiple sessions and they use the same cookie it will cause problems.
|
43
|
+
# Also, if a id is set it will be inserted into the beginning of the string. Exmaple:
|
44
|
+
#
|
45
|
+
# session = UserSession.new(:super_high_secret)
|
46
|
+
# session.cookie_key => "super_high_secret_user_credentials"
|
47
|
+
#
|
48
|
+
# * <tt>Default:</tt> "#{klass_name.underscore}_credentials"
|
49
|
+
# * <tt>Accepts:</tt> String
|
50
|
+
def cookie_key(value = nil)
|
51
|
+
if value.nil?
|
52
|
+
read_inheritable_attribute(:cookie_key) || cookie_key("#{klass_name.underscore}_credentials")
|
53
|
+
else
|
54
|
+
write_inheritable_attribute(:cookie_key, value)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
alias_method :cookie_key=, :cookie_key
|
58
|
+
|
59
|
+
# The name of the method used to find the record by the login. What's nifty about this is that you can do anything in your method, Authlogic will just pass you the login.
|
60
|
+
#
|
61
|
+
# Let's say you allow users to login by username or email. Set this to "find_login", or whatever method you want. Then in your model create a class method like:
|
62
|
+
#
|
63
|
+
# def self.find_login(login)
|
64
|
+
# find_by_login(login) || find_by_email(login)
|
65
|
+
# end
|
66
|
+
#
|
67
|
+
# * <tt>Default:</tt> "find_by_#{login_field}"
|
68
|
+
# * <tt>Accepts:</tt> Symbol or String
|
69
|
+
def find_by_login_method(value = nil)
|
70
|
+
if value.nil?
|
71
|
+
read_inheritable_attribute(:find_by_login_method) || find_by_login_method("find_by_#{login_field}")
|
72
|
+
else
|
73
|
+
write_inheritable_attribute(:find_by_login_method, value)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
alias_method :find_by_login_method=, :find_by_login_method
|
77
|
+
|
78
|
+
# Calling UserSession.find tries to find the user session by session, then cookie, then basic http auth. This option allows you to change the order or remove any of these.
|
79
|
+
#
|
80
|
+
# * <tt>Default:</tt> [:session, :cookie, :http_auth]
|
81
|
+
# * <tt>Accepts:</tt> Array, and can only use any of the 3 options above
|
82
|
+
def find_with(*values)
|
83
|
+
if values.blank?
|
84
|
+
read_inheritable_attribute(:find_with) || find_with(:session, :cookie, :http_auth)
|
85
|
+
else
|
86
|
+
values.flatten!
|
87
|
+
write_inheritable_array(:find_with, values)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
alias_method :find_with=, :find_with
|
91
|
+
|
92
|
+
# The name of the method you want Authlogic to create for storing the login / username. Keep in mind this is just for your Authlogic::Session, if you want it can be something completely different
|
93
|
+
# than the field in your model. So if you wanted people to login with a field called "login" and then find users by email this is compeltely doable. See the find_by_login_method configuration option for
|
94
|
+
# more details.
|
95
|
+
#
|
96
|
+
# * <tt>Default:</tt> Guesses based on the model columns, tries login, username, and email. If none are present it defaults to login
|
97
|
+
# * <tt>Accepts:</tt> Symbol or String
|
98
|
+
def login_field(value = nil)
|
99
|
+
if value.nil?
|
100
|
+
read_inheritable_attribute(:login_field) || login_field((klass.column_names.include?("login") && :login) || (klass.column_names.include?("username") && :username) || (klass.column_names.include?("email") && :email) || :login)
|
101
|
+
else
|
102
|
+
write_inheritable_attribute(:login_field, value)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
alias_method :login_field=, :login_field
|
106
|
+
|
107
|
+
# Works exactly like login_field, but for the password instead.
|
108
|
+
#
|
109
|
+
# * <tt>Default:</tt> Guesses based on the model columns, tries password and pass. If none are present it defaults to password
|
110
|
+
# * <tt>Accepts:</tt> Symbol or String
|
111
|
+
def password_field(value = nil)
|
112
|
+
if value.nil?
|
113
|
+
read_inheritable_attribute(:password_field) || password_field((klass.column_names.include?("password") && :password) || (klass.column_names.include?("pass") && :pass) || :password)
|
114
|
+
else
|
115
|
+
write_inheritable_attribute(:password_field, value)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
alias_method :password_field=, :password_field
|
119
|
+
|
120
|
+
# If sessions should be remembered by default or not.
|
121
|
+
#
|
122
|
+
# * <tt>Default:</tt> false
|
123
|
+
# * <tt>Accepts:</tt> Boolean
|
124
|
+
def remember_me(value = nil)
|
125
|
+
if value.nil?
|
126
|
+
read_inheritable_attribute(:remember_me)
|
127
|
+
else
|
128
|
+
write_inheritable_attribute(:remember_me, value)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
alias_method :remember_me=, :remember_me
|
132
|
+
|
133
|
+
# The length of time until the cookie expires.
|
134
|
+
#
|
135
|
+
# * <tt>Default:</tt> 3.months
|
136
|
+
# * <tt>Accepts:</tt> Integer, length of time in seconds, such as 60 or 3.months
|
137
|
+
def remember_me_for(value = :_read)
|
138
|
+
if value == :_read
|
139
|
+
read_inheritable_attribute(:remember_me_for) || remember_me_for(3.months)
|
140
|
+
else
|
141
|
+
write_inheritable_attribute(:remember_me_for, value)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
alias_method :remember_me_for=, :remember_me_for
|
145
|
+
|
146
|
+
# The name of the field that the remember token is stored. This is for cookies. Let's say you set up your app and want all users to be remembered for 6 months. Then you realize that might be a little too
|
147
|
+
# long. Well they already have a cookie set to expire in 6 months. Without a token you would have to reset their password, which obviously isn't feasible. So instead of messing with their password
|
148
|
+
# just reset their remember token. Next time they access the site and try to login via a cookie it will be rejected and they will have to relogin.
|
149
|
+
#
|
150
|
+
# * <tt>Default:</tt> Guesses based on the model columns, tries remember_token, remember_key, cookie_token, and cookie_key. If none are present it defaults to remember_token
|
151
|
+
# * <tt>Accepts:</tt> Symbol or String
|
152
|
+
def remember_token_field(value = nil)
|
153
|
+
if value.nil?
|
154
|
+
read_inheritable_attribute(:remember_token_field) ||
|
155
|
+
remember_token_field(
|
156
|
+
(klass.column_names.include?("remember_token") && :remember_token) ||
|
157
|
+
(klass.column_names.include?("remember_key") && :remember_key) ||
|
158
|
+
(klass.column_names.include?("cookie_token") && :cookie_token) ||
|
159
|
+
(klass.column_names.include?("cookie_key") && :cookie_key) ||
|
160
|
+
:remember_token
|
161
|
+
)
|
162
|
+
else
|
163
|
+
write_inheritable_attribute(:remember_token_field, value)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
alias_method :remember_token_field=, :remember_token_field
|
167
|
+
|
168
|
+
# Works exactly like cookie_key, but for sessions. See cookie_key for more info.
|
169
|
+
#
|
170
|
+
# * <tt>Default:</tt> cookie_key
|
171
|
+
# * <tt>Accepts:</tt> Symbol or String
|
172
|
+
def session_key(value = nil)
|
173
|
+
if value.nil?
|
174
|
+
read_inheritable_attribute(:session_key) || session_key(cookie_key)
|
175
|
+
else
|
176
|
+
write_inheritable_attribute(:session_key, value)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
alias_method :session_key=, :session_key
|
180
|
+
|
181
|
+
# The name of the method in your model used to verify the password. This should be an instance method. It should also be prepared to accept a raw password and a crytped password.
|
182
|
+
#
|
183
|
+
# * <tt>Default:</tt> "valid_#{password_field}?"
|
184
|
+
# * <tt>Accepts:</tt> Symbol or String
|
185
|
+
def verify_password_method(value = nil)
|
186
|
+
if value.nil?
|
187
|
+
read_inheritable_attribute(:verify_password_method) || verify_password_method("valid_#{password_field}?")
|
188
|
+
else
|
189
|
+
write_inheritable_attribute(:verify_password_method, value)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
alias_method :verify_password_method=, :verify_password_method
|
193
|
+
end
|
194
|
+
|
195
|
+
module InstanceMethods # :nodoc:
|
196
|
+
def cookie_key
|
197
|
+
key_parts = [id, scope[:id], self.class.cookie_key].compact
|
198
|
+
key_parts.join("_")
|
199
|
+
end
|
200
|
+
|
201
|
+
def find_by_login_method
|
202
|
+
self.class.find_by_login_method
|
203
|
+
end
|
204
|
+
|
205
|
+
def find_with
|
206
|
+
self.class.find_with
|
207
|
+
end
|
208
|
+
|
209
|
+
def login_field
|
210
|
+
self.class.login_field
|
211
|
+
end
|
212
|
+
|
213
|
+
def password_field
|
214
|
+
self.class.password_field
|
215
|
+
end
|
216
|
+
|
217
|
+
def remember_me_for
|
218
|
+
return unless remember_me?
|
219
|
+
self.class.remember_me_for
|
220
|
+
end
|
221
|
+
|
222
|
+
def remember_token_field
|
223
|
+
self.class.remember_token_field
|
224
|
+
end
|
225
|
+
|
226
|
+
def session_key
|
227
|
+
key_parts = [id, scope[:id], self.class.session_key].compact
|
228
|
+
key_parts.join("_")
|
229
|
+
end
|
230
|
+
|
231
|
+
def verify_password_method
|
232
|
+
self.class.verify_password_method
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Authlogic
|
2
|
+
module Session
|
3
|
+
class Errors < ::ActiveRecord::Errors # :nodoc:
|
4
|
+
end
|
5
|
+
|
6
|
+
class NotActivated < ::StandardError # :nodoc:
|
7
|
+
def initialize(session)
|
8
|
+
super("You must activate the Authlogic::Session::Base.controller with a controller object before creating objects")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class SessionInvalid < ::StandardError # :nodoc:
|
13
|
+
def initialize(session)
|
14
|
+
super("Authentication failed: #{session.errors.full_messages.to_sentence}")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "digest/sha2"
|
2
|
+
|
3
|
+
module Authlogic
|
4
|
+
# = Sha512 Crypto Provider
|
5
|
+
#
|
6
|
+
# The acts_as_authentic method allows you to pass a :crypto_provider option. This allows you to use any type of encryption you like. Just create a class with a class level encrypt and decrypt method.
|
7
|
+
# The password will be passed as the single parameter to each of these methods so you can do your magic.
|
8
|
+
#
|
9
|
+
# If you are encrypting via a hash just don't include a decrypt method, since hashes can't be decrypted. Authlogic will notice this adjust accordingly.
|
10
|
+
class Sha512CryptoProvider
|
11
|
+
STRETCHES = 20
|
12
|
+
def self.encrypt(pass)
|
13
|
+
digest = pass
|
14
|
+
STRETCHES.times { digest = Digest::SHA512.hexdigest(digest) }
|
15
|
+
digest
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module Authlogic # :nodoc:
|
2
|
+
# = Version
|
3
|
+
#
|
4
|
+
# A class for describing the current version of a library. The version
|
5
|
+
# consists of three parts: the +major+ number, the +minor+ number, and the
|
6
|
+
# +tiny+ (or +patch+) number.
|
7
|
+
class Version
|
8
|
+
|
9
|
+
include Comparable
|
10
|
+
|
11
|
+
# A convenience method for instantiating a new Version instance with the
|
12
|
+
# given +major+, +minor+, and +tiny+ components.
|
13
|
+
def self.[](major, minor, tiny)
|
14
|
+
new(major, minor, tiny)
|
15
|
+
end
|
16
|
+
|
17
|
+
attr_reader :major, :minor, :tiny
|
18
|
+
|
19
|
+
# Create a new Version object with the given components.
|
20
|
+
def initialize(major, minor, tiny)
|
21
|
+
@major, @minor, @tiny = major, minor, tiny
|
22
|
+
end
|
23
|
+
|
24
|
+
# Compare this version to the given +version+ object.
|
25
|
+
def <=>(version)
|
26
|
+
to_i <=> version.to_i
|
27
|
+
end
|
28
|
+
|
29
|
+
# Converts this version object to a string, where each of the three
|
30
|
+
# version components are joined by the '.' character. E.g., 2.0.0.
|
31
|
+
def to_s
|
32
|
+
@to_s ||= [@major, @minor, @tiny].join(".")
|
33
|
+
end
|
34
|
+
|
35
|
+
# Converts this version to a canonical integer that may be compared
|
36
|
+
# against other version objects.
|
37
|
+
def to_i
|
38
|
+
@to_i ||= @major * 1_000_000 + @minor * 1_000 + @tiny
|
39
|
+
end
|
40
|
+
|
41
|
+
def to_a
|
42
|
+
[@major, @minor, @tiny]
|
43
|
+
end
|
44
|
+
|
45
|
+
MAJOR = 0
|
46
|
+
MINOR = 10
|
47
|
+
TINY = 4
|
48
|
+
|
49
|
+
# The current version as a Version instance
|
50
|
+
CURRENT = new(MAJOR, MINOR, TINY)
|
51
|
+
# The current version as a String
|
52
|
+
STRING = CURRENT.to_s
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
data/test_app/README
ADDED
@@ -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.
|