authlogic 0.10.4 → 1.0.0

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.

Files changed (111) hide show
  1. data/CHANGELOG.rdoc +11 -0
  2. data/Manifest +18 -81
  3. data/README.rdoc +53 -17
  4. data/Rakefile +1 -1
  5. data/authlogic.gemspec +7 -6
  6. data/lib/authlogic.rb +5 -0
  7. data/lib/authlogic/active_record/acts_as_authentic.rb +90 -58
  8. data/lib/authlogic/active_record/authenticates_many.rb +37 -0
  9. data/lib/authlogic/controller_adapters/abstract_adapter.rb +5 -7
  10. data/lib/authlogic/controller_adapters/merb_adapter.rb +55 -0
  11. data/lib/authlogic/controller_adapters/rails_adapter.rb +21 -15
  12. data/lib/authlogic/session/base.rb +64 -116
  13. data/lib/authlogic/session/callbacks.rb +29 -13
  14. data/lib/authlogic/session/config.rb +5 -1
  15. data/lib/authlogic/session/scopes.rb +101 -0
  16. data/lib/authlogic/version.rb +3 -3
  17. data/test/active_record_acts_as_authentic_test.rb +213 -0
  18. data/test/active_record_authenticates_many_test.rb +28 -0
  19. data/{test_app/test → test}/fixtures/companies.yml +0 -2
  20. data/test/fixtures/employees.yml +17 -0
  21. data/{test_app/test → test}/fixtures/projects.yml +1 -2
  22. data/{test_app/test → test}/fixtures/users.yml +3 -5
  23. data/test/test_helper.rb +142 -0
  24. data/test/user_session_active_record_trickery_test.rb +12 -0
  25. data/test/user_session_base_test.rb +316 -0
  26. data/test/user_session_config_test.rb +144 -0
  27. data/test/user_session_scopes_test.rb +19 -0
  28. data/test_libs/aes128_crypto_provider.rb +17 -0
  29. data/test_libs/mock_controller.rb +19 -0
  30. data/test_libs/mock_cookie_jar.rb +6 -0
  31. data/test_libs/mock_request.rb +5 -0
  32. data/test_libs/ordered_hash.rb +9 -0
  33. metadata +32 -87
  34. data/test_app/README +0 -256
  35. data/test_app/Rakefile +0 -10
  36. data/test_app/app/controllers/application.rb +0 -72
  37. data/test_app/app/controllers/companies_controller.rb +0 -2
  38. data/test_app/app/controllers/user_sessions_controller.rb +0 -25
  39. data/test_app/app/controllers/users_controller.rb +0 -61
  40. data/test_app/app/helpers/application_helper.rb +0 -3
  41. data/test_app/app/helpers/companies_helper.rb +0 -2
  42. data/test_app/app/helpers/user_sessions_helper.rb +0 -2
  43. data/test_app/app/helpers/users_helper.rb +0 -2
  44. data/test_app/app/models/company.rb +0 -4
  45. data/test_app/app/models/project.rb +0 -3
  46. data/test_app/app/models/user.rb +0 -5
  47. data/test_app/app/models/user_session.rb +0 -3
  48. data/test_app/app/views/layouts/application.html.erb +0 -27
  49. data/test_app/app/views/user_sessions/new.html.erb +0 -15
  50. data/test_app/app/views/users/_form.erb +0 -15
  51. data/test_app/app/views/users/edit.html.erb +0 -8
  52. data/test_app/app/views/users/new.html.erb +0 -8
  53. data/test_app/app/views/users/show.html.erb +0 -29
  54. data/test_app/config/boot.rb +0 -109
  55. data/test_app/config/database.yml +0 -19
  56. data/test_app/config/environment.rb +0 -69
  57. data/test_app/config/environments/development.rb +0 -17
  58. data/test_app/config/environments/production.rb +0 -22
  59. data/test_app/config/environments/test.rb +0 -22
  60. data/test_app/config/initializers/inflections.rb +0 -10
  61. data/test_app/config/initializers/mime_types.rb +0 -5
  62. data/test_app/config/initializers/new_rails_defaults.rb +0 -17
  63. data/test_app/config/routes.rb +0 -11
  64. data/test_app/db/development.sqlite3 +0 -0
  65. data/test_app/db/migrate/20081023040052_create_users.rb +0 -20
  66. data/test_app/db/migrate/20081103003828_create_companies.rb +0 -14
  67. data/test_app/db/migrate/20081103003834_create_projects.rb +0 -18
  68. data/test_app/db/schema.rb +0 -46
  69. data/test_app/db/test.sqlite3 +0 -0
  70. data/test_app/doc/README_FOR_APP +0 -2
  71. data/test_app/public/404.html +0 -30
  72. data/test_app/public/422.html +0 -30
  73. data/test_app/public/500.html +0 -30
  74. data/test_app/public/dispatch.cgi +0 -10
  75. data/test_app/public/dispatch.fcgi +0 -24
  76. data/test_app/public/dispatch.rb +0 -10
  77. data/test_app/public/favicon.ico +0 -0
  78. data/test_app/public/images/rails.png +0 -0
  79. data/test_app/public/javascripts/application.js +0 -2
  80. data/test_app/public/javascripts/controls.js +0 -963
  81. data/test_app/public/javascripts/dragdrop.js +0 -972
  82. data/test_app/public/javascripts/effects.js +0 -1120
  83. data/test_app/public/javascripts/prototype.js +0 -4225
  84. data/test_app/public/robots.txt +0 -5
  85. data/test_app/public/stylesheets/scaffold.css +0 -62
  86. data/test_app/script/about +0 -4
  87. data/test_app/script/console +0 -3
  88. data/test_app/script/dbconsole +0 -3
  89. data/test_app/script/destroy +0 -3
  90. data/test_app/script/generate +0 -3
  91. data/test_app/script/performance/benchmarker +0 -3
  92. data/test_app/script/performance/profiler +0 -3
  93. data/test_app/script/performance/request +0 -3
  94. data/test_app/script/plugin +0 -3
  95. data/test_app/script/process/inspector +0 -3
  96. data/test_app/script/process/reaper +0 -3
  97. data/test_app/script/process/spawner +0 -3
  98. data/test_app/script/runner +0 -3
  99. data/test_app/script/server +0 -3
  100. data/test_app/test/functional/companies_controller_test.rb +0 -8
  101. data/test_app/test/functional/user_sessions_controller_test.rb +0 -36
  102. data/test_app/test/functional/users_controller_test.rb +0 -8
  103. data/test_app/test/integration/company_user_session_stories_test.rb +0 -46
  104. data/test_app/test/integration/user_sesion_stories_test.rb +0 -105
  105. data/test_app/test/integration/user_session_config_test.rb +0 -24
  106. data/test_app/test/integration/user_session_test.rb +0 -161
  107. data/test_app/test/test_helper.rb +0 -81
  108. data/test_app/test/unit/account_test.rb +0 -8
  109. data/test_app/test/unit/company_test.rb +0 -8
  110. data/test_app/test/unit/project_test.rb +0 -8
  111. data/test_app/test/unit/user_test.rb +0 -80
@@ -0,0 +1,144 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class UserSessionConfigTest < ActiveSupport::TestCase
4
+ def test_authenticate_with
5
+ UserSession.authenticate_with = Employee
6
+ assert_equal "Employee", UserSession.klass_name
7
+ assert_equal Employee, UserSession.klass
8
+
9
+ UserSession.authenticate_with User
10
+ assert_equal "User", UserSession.klass_name
11
+ assert_equal User, UserSession.klass
12
+ end
13
+
14
+ def test_cookie_key
15
+ UserSession.cookie_key = "my_cookie_key"
16
+ assert_equal "my_cookie_key", UserSession.cookie_key
17
+ session = UserSession.new
18
+ assert_equal "my_cookie_key", session.cookie_key
19
+
20
+ UserSession.cookie_key "user_credentials"
21
+ assert_equal "user_credentials", UserSession.cookie_key
22
+ session = UserSession.new
23
+ assert_equal "user_credentials", session.cookie_key
24
+ end
25
+
26
+ def test_find_by_login_method
27
+ UserSession.find_by_login_method = "my_login_method"
28
+ assert_equal "my_login_method", UserSession.find_by_login_method
29
+ session = UserSession.new
30
+ assert_equal "my_login_method", session.find_by_login_method
31
+
32
+ UserSession.find_by_login_method "find_by_login"
33
+ assert_equal "find_by_login", UserSession.find_by_login_method
34
+ session = UserSession.new
35
+ assert_equal "find_by_login", session.find_by_login_method
36
+ end
37
+
38
+ def test_find_with
39
+ UserSession.find_with = [:session]
40
+ assert_equal [:session], UserSession.find_with
41
+ session = UserSession.new
42
+ assert_equal [:session], session.find_with
43
+
44
+ set_cookie_for(users(:ben))
45
+ assert !UserSession.find
46
+
47
+ UserSession.find_with :session, :cookie, :http_auth
48
+ assert_equal [:session, :cookie, :http_auth], UserSession.find_with
49
+ session = UserSession.new
50
+ assert_equal [:session, :cookie, :http_auth], session.find_with
51
+
52
+ assert UserSession.find
53
+ end
54
+
55
+ def test_login_field
56
+ UserSession.login_field = :saweet
57
+ assert_equal :saweet, UserSession.login_field
58
+ session = UserSession.new
59
+ assert_equal :saweet, session.login_field
60
+ assert session.respond_to?(:saweet)
61
+
62
+ UserSession.login_field :login
63
+ assert_equal :login, UserSession.login_field
64
+ session = UserSession.new
65
+ assert_equal :login, session.login_field
66
+ assert session.respond_to?(:login)
67
+ end
68
+
69
+ def test_password_field
70
+ UserSession.password_field = :saweet
71
+ assert_equal :saweet, UserSession.password_field
72
+ session = UserSession.new
73
+ assert_equal :saweet, session.password_field
74
+ assert session.respond_to?(:saweet)
75
+
76
+ UserSession.password_field :password
77
+ assert_equal :password, UserSession.password_field
78
+ session = UserSession.new
79
+ assert_equal :password, session.password_field
80
+ assert session.respond_to?(:password)
81
+ end
82
+
83
+ def test_remember_me
84
+ UserSession.remember_me = true
85
+ assert_equal true, UserSession.remember_me
86
+ session = UserSession.new
87
+ assert_equal true, session.remember_me
88
+
89
+ UserSession.remember_me false
90
+ assert_equal false, UserSession.remember_me
91
+ session = UserSession.new
92
+ assert_equal false, session.remember_me
93
+ end
94
+
95
+ def test_remember_me_for
96
+ UserSession.remember_me_for = 3.years
97
+ assert_equal 3.years, UserSession.remember_me_for
98
+ session = UserSession.new
99
+ session.remember_me = true
100
+ assert_equal 3.years, session.remember_me_for
101
+
102
+ UserSession.remember_me_for 3.months
103
+ assert_equal 3.months, UserSession.remember_me_for
104
+ session = UserSession.new
105
+ session.remember_me = true
106
+ assert_equal 3.months, session.remember_me_for
107
+ end
108
+
109
+ def test_remember_token_field
110
+ UserSession.remember_token_field = :saweet
111
+ assert_equal :saweet, UserSession.remember_token_field
112
+ session = UserSession.new
113
+ assert_equal :saweet, session.remember_token_field
114
+
115
+ UserSession.remember_token_field :remember_token
116
+ assert_equal :remember_token, UserSession.remember_token_field
117
+ session = UserSession.new
118
+ assert_equal :remember_token, session.remember_token_field
119
+ end
120
+
121
+ def test_session_key
122
+ UserSession.session_key = "my_session_key"
123
+ assert_equal "my_session_key", UserSession.session_key
124
+ session = UserSession.new
125
+ assert_equal "my_session_key", session.session_key
126
+
127
+ UserSession.session_key "user_credentials"
128
+ assert_equal "user_credentials", UserSession.session_key
129
+ session = UserSession.new
130
+ assert_equal "user_credentials", session.session_key
131
+ end
132
+
133
+ def test_verify_password_method
134
+ UserSession.verify_password_method = "my_login_method"
135
+ assert_equal "my_login_method", UserSession.verify_password_method
136
+ session = UserSession.new
137
+ assert_equal "my_login_method", session.verify_password_method
138
+
139
+ UserSession.verify_password_method "valid_password?"
140
+ assert_equal "valid_password?", UserSession.verify_password_method
141
+ session = UserSession.new
142
+ assert_equal "valid_password?", session.verify_password_method
143
+ end
144
+ end
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class UserSessionBaseTest < ActiveSupport::TestCase
4
+ def test_scope
5
+ UserSession.with_scope(:find_options => {:conditions => "awesome = 1"}, :id => "some_id") do
6
+ assert_equal({:find_options => {:conditions => "awesome = 1"}, :id => "some_id"}, UserSession.scope)
7
+ end
8
+ assert_equal nil, UserSession.scope
9
+ end
10
+
11
+ def test_with_scope
12
+ assert_raise(ArgumentError) { UserSession.with_scope }
13
+ # the rest of the method was tested in test_scope
14
+ end
15
+
16
+ def test_initialize
17
+
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ require "ezcrypto"
2
+
3
+ class AES128CryptoProvider
4
+ class << self
5
+ def encrypt(term)
6
+ [key.encrypt(term)].pack("m").chomp
7
+ end
8
+
9
+ def decrypt(term)
10
+ key.decrypt(term.unpack("m").first)
11
+ end
12
+
13
+ def key
14
+ EzCrypto::Key.with_password "master_key", "some_salt"
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,19 @@
1
+ class MockController < Authlogic::ControllerAdapters::AbstractAdapter
2
+ attr_accessor :http_user, :http_password
3
+
4
+ def authenticate_with_http_basic(&block)
5
+ yield http_user, http_password
6
+ end
7
+
8
+ def cookies
9
+ @cookies ||= MockCookieJar.new
10
+ end
11
+
12
+ def request
13
+ @request ||= MockRequest.new
14
+ end
15
+
16
+ def session
17
+ @session ||= {}
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ class MockCookieJar < Hash
2
+ def [](key)
3
+ hash = super
4
+ hash && hash[:value]
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ class MockRequest
2
+ def remote_ip
3
+ "1.1.1.1"
4
+ end
5
+ end
@@ -0,0 +1,9 @@
1
+ class Hash
2
+ def each(&block)
3
+ sorted_keys = keys.sort { |a, b| a.to_s <=> b.to_s }
4
+ sorted_keys.each do |key|
5
+ yield key, self[key]
6
+ end
7
+ self
8
+ end
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: authlogic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.4
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Johnson of Binary Logic
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-11-03 00:00:00 -05:00
12
+ date: 2008-11-05 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,7 +42,7 @@ dependencies:
42
42
  - !ruby/object:Gem::Version
43
43
  version: "0"
44
44
  version:
45
- description: Rails authentication done right
45
+ description: Framework agnostic object based authentication solution that handles all of the non sense for you. It's as easy as ActiveRecord is with a database.
46
46
  email: bjohnson@binarylogic.com
47
47
  executables: []
48
48
 
@@ -54,12 +54,14 @@ extra_rdoc_files:
54
54
  - lib/authlogic/active_record/authenticates_many.rb
55
55
  - lib/authlogic/active_record/scoped_session.rb
56
56
  - lib/authlogic/controller_adapters/abstract_adapter.rb
57
+ - lib/authlogic/controller_adapters/merb_adapter.rb
57
58
  - lib/authlogic/controller_adapters/rails_adapter.rb
58
59
  - lib/authlogic/session/active_record_trickery.rb
59
60
  - lib/authlogic/session/base.rb
60
61
  - lib/authlogic/session/callbacks.rb
61
62
  - lib/authlogic/session/config.rb
62
63
  - lib/authlogic/session/errors.rb
64
+ - lib/authlogic/session/scopes.rb
63
65
  - lib/authlogic/sha512_crypto_provider.rb
64
66
  - lib/authlogic/version.rb
65
67
  - lib/authlogic.rb
@@ -71,12 +73,14 @@ files:
71
73
  - lib/authlogic/active_record/authenticates_many.rb
72
74
  - lib/authlogic/active_record/scoped_session.rb
73
75
  - lib/authlogic/controller_adapters/abstract_adapter.rb
76
+ - lib/authlogic/controller_adapters/merb_adapter.rb
74
77
  - lib/authlogic/controller_adapters/rails_adapter.rb
75
78
  - lib/authlogic/session/active_record_trickery.rb
76
79
  - lib/authlogic/session/base.rb
77
80
  - lib/authlogic/session/callbacks.rb
78
81
  - lib/authlogic/session/config.rb
79
82
  - lib/authlogic/session/errors.rb
83
+ - lib/authlogic/session/scopes.rb
80
84
  - lib/authlogic/sha512_crypto_provider.rb
81
85
  - lib/authlogic/version.rb
82
86
  - lib/authlogic.rb
@@ -84,87 +88,22 @@ files:
84
88
  - MIT-LICENSE
85
89
  - Rakefile
86
90
  - README.rdoc
87
- - test_app/app/controllers/application.rb
88
- - test_app/app/controllers/companies_controller.rb
89
- - test_app/app/controllers/user_sessions_controller.rb
90
- - test_app/app/controllers/users_controller.rb
91
- - test_app/app/helpers/application_helper.rb
92
- - test_app/app/helpers/companies_helper.rb
93
- - test_app/app/helpers/user_sessions_helper.rb
94
- - test_app/app/helpers/users_helper.rb
95
- - test_app/app/models/company.rb
96
- - test_app/app/models/project.rb
97
- - test_app/app/models/user.rb
98
- - test_app/app/models/user_session.rb
99
- - test_app/app/views/layouts/application.html.erb
100
- - test_app/app/views/user_sessions/new.html.erb
101
- - test_app/app/views/users/_form.erb
102
- - test_app/app/views/users/edit.html.erb
103
- - test_app/app/views/users/new.html.erb
104
- - test_app/app/views/users/show.html.erb
105
- - test_app/config/boot.rb
106
- - test_app/config/database.yml
107
- - test_app/config/environment.rb
108
- - test_app/config/environments/development.rb
109
- - test_app/config/environments/production.rb
110
- - test_app/config/environments/test.rb
111
- - test_app/config/initializers/inflections.rb
112
- - test_app/config/initializers/mime_types.rb
113
- - test_app/config/initializers/new_rails_defaults.rb
114
- - test_app/config/routes.rb
115
- - test_app/db/development.sqlite3
116
- - test_app/db/migrate/20081023040052_create_users.rb
117
- - test_app/db/migrate/20081103003828_create_companies.rb
118
- - test_app/db/migrate/20081103003834_create_projects.rb
119
- - test_app/db/schema.rb
120
- - test_app/db/test.sqlite3
121
- - test_app/doc/README_FOR_APP
122
- - test_app/public/404.html
123
- - test_app/public/422.html
124
- - test_app/public/500.html
125
- - test_app/public/dispatch.cgi
126
- - test_app/public/dispatch.fcgi
127
- - test_app/public/dispatch.rb
128
- - test_app/public/favicon.ico
129
- - test_app/public/images/rails.png
130
- - test_app/public/javascripts/application.js
131
- - test_app/public/javascripts/controls.js
132
- - test_app/public/javascripts/dragdrop.js
133
- - test_app/public/javascripts/effects.js
134
- - test_app/public/javascripts/prototype.js
135
- - test_app/public/robots.txt
136
- - test_app/public/stylesheets/scaffold.css
137
- - test_app/Rakefile
138
- - test_app/README
139
- - test_app/script/about
140
- - test_app/script/console
141
- - test_app/script/dbconsole
142
- - test_app/script/destroy
143
- - test_app/script/generate
144
- - test_app/script/performance/benchmarker
145
- - test_app/script/performance/profiler
146
- - test_app/script/performance/request
147
- - test_app/script/plugin
148
- - test_app/script/process/inspector
149
- - test_app/script/process/reaper
150
- - test_app/script/process/spawner
151
- - test_app/script/runner
152
- - test_app/script/server
153
- - test_app/test/fixtures/companies.yml
154
- - test_app/test/fixtures/projects.yml
155
- - test_app/test/fixtures/users.yml
156
- - test_app/test/functional/companies_controller_test.rb
157
- - test_app/test/functional/user_sessions_controller_test.rb
158
- - test_app/test/functional/users_controller_test.rb
159
- - test_app/test/integration/company_user_session_stories_test.rb
160
- - test_app/test/integration/user_sesion_stories_test.rb
161
- - test_app/test/integration/user_session_config_test.rb
162
- - test_app/test/integration/user_session_test.rb
163
- - test_app/test/test_helper.rb
164
- - test_app/test/unit/account_test.rb
165
- - test_app/test/unit/company_test.rb
166
- - test_app/test/unit/project_test.rb
167
- - test_app/test/unit/user_test.rb
91
+ - test/active_record_acts_as_authentic_test.rb
92
+ - test/active_record_authenticates_many_test.rb
93
+ - test/fixtures/companies.yml
94
+ - test/fixtures/employees.yml
95
+ - test/fixtures/projects.yml
96
+ - test/fixtures/users.yml
97
+ - test/test_helper.rb
98
+ - test/user_session_active_record_trickery_test.rb
99
+ - test/user_session_base_test.rb
100
+ - test/user_session_config_test.rb
101
+ - test/user_session_scopes_test.rb
102
+ - test_libs/aes128_crypto_provider.rb
103
+ - test_libs/mock_controller.rb
104
+ - test_libs/mock_cookie_jar.rb
105
+ - test_libs/mock_request.rb
106
+ - test_libs/ordered_hash.rb
168
107
  - authlogic.gemspec
169
108
  has_rdoc: true
170
109
  homepage: http://github.com/binarylogic/authlogic
@@ -196,6 +135,12 @@ rubyforge_project: authlogic
196
135
  rubygems_version: 1.2.0
197
136
  signing_key:
198
137
  specification_version: 2
199
- summary: Rails authentication done right
200
- test_files: []
201
-
138
+ summary: Framework agnostic object based authentication solution that handles all of the non sense for you. It's as easy as ActiveRecord is with a database.
139
+ test_files:
140
+ - test/active_record_acts_as_authentic_test.rb
141
+ - test/active_record_authenticates_many_test.rb
142
+ - test/test_helper.rb
143
+ - test/user_session_active_record_trickery_test.rb
144
+ - test/user_session_base_test.rb
145
+ - test/user_session_config_test.rb
146
+ - test/user_session_scopes_test.rb
@@ -1,256 +0,0 @@
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.