authgasm 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/Manifest +85 -0
  3. data/README.rdoc +164 -0
  4. data/Rakefile +15 -0
  5. data/authgasm.gemspec +183 -0
  6. data/init.rb +2 -0
  7. data/lib/authgasm.rb +18 -0
  8. data/lib/authgasm/acts_as_authentic.rb +200 -0
  9. data/lib/authgasm/controller.rb +16 -0
  10. data/lib/authgasm/session/active_record_trickery.rb +30 -0
  11. data/lib/authgasm/session/base.rb +365 -0
  12. data/lib/authgasm/session/callbacks.rb +47 -0
  13. data/lib/authgasm/session/config.rb +193 -0
  14. data/lib/authgasm/session/errors.rb +12 -0
  15. data/lib/authgasm/sha256_crypto_provider.rb +13 -0
  16. data/lib/authgasm/version.rb +56 -0
  17. data/test_app/README +256 -0
  18. data/test_app/Rakefile +10 -0
  19. data/test_app/app/controllers/application.rb +46 -0
  20. data/test_app/app/controllers/user_sessions_controller.rb +25 -0
  21. data/test_app/app/controllers/users_controller.rb +37 -0
  22. data/test_app/app/helpers/application_helper.rb +3 -0
  23. data/test_app/app/helpers/user_sessions_helper.rb +2 -0
  24. data/test_app/app/helpers/users_helper.rb +2 -0
  25. data/test_app/app/models/user.rb +3 -0
  26. data/test_app/app/models/user_session.rb +3 -0
  27. data/test_app/app/views/asses/edit.html.erb +12 -0
  28. data/test_app/app/views/asses/index.html.erb +18 -0
  29. data/test_app/app/views/asses/new.html.erb +11 -0
  30. data/test_app/app/views/asses/show.html.erb +3 -0
  31. data/test_app/app/views/layouts/application.html.erb +25 -0
  32. data/test_app/app/views/user_sessions/new.html.erb +13 -0
  33. data/test_app/app/views/users/_form.erb +15 -0
  34. data/test_app/app/views/users/edit.html.erb +8 -0
  35. data/test_app/app/views/users/new.html.erb +8 -0
  36. data/test_app/app/views/users/show.html.erb +19 -0
  37. data/test_app/config/boot.rb +109 -0
  38. data/test_app/config/database.yml +19 -0
  39. data/test_app/config/environment.rb +69 -0
  40. data/test_app/config/environments/development.rb +17 -0
  41. data/test_app/config/environments/production.rb +22 -0
  42. data/test_app/config/environments/test.rb +22 -0
  43. data/test_app/config/initializers/inflections.rb +10 -0
  44. data/test_app/config/initializers/mime_types.rb +5 -0
  45. data/test_app/config/initializers/new_rails_defaults.rb +17 -0
  46. data/test_app/config/routes.rb +7 -0
  47. data/test_app/db/development.sqlite3 +0 -0
  48. data/test_app/db/migrate/20081023040052_create_users.rb +17 -0
  49. data/test_app/db/schema.rb +25 -0
  50. data/test_app/db/test.sqlite3 +0 -0
  51. data/test_app/doc/README_FOR_APP +2 -0
  52. data/test_app/public/404.html +30 -0
  53. data/test_app/public/422.html +30 -0
  54. data/test_app/public/500.html +30 -0
  55. data/test_app/public/dispatch.cgi +10 -0
  56. data/test_app/public/dispatch.fcgi +24 -0
  57. data/test_app/public/dispatch.rb +10 -0
  58. data/test_app/public/favicon.ico +0 -0
  59. data/test_app/public/images/rails.png +0 -0
  60. data/test_app/public/javascripts/application.js +2 -0
  61. data/test_app/public/javascripts/controls.js +963 -0
  62. data/test_app/public/javascripts/dragdrop.js +972 -0
  63. data/test_app/public/javascripts/effects.js +1120 -0
  64. data/test_app/public/javascripts/prototype.js +4225 -0
  65. data/test_app/public/robots.txt +5 -0
  66. data/test_app/public/stylesheets/scaffold.css +62 -0
  67. data/test_app/script/about +4 -0
  68. data/test_app/script/console +3 -0
  69. data/test_app/script/dbconsole +3 -0
  70. data/test_app/script/destroy +3 -0
  71. data/test_app/script/generate +3 -0
  72. data/test_app/script/performance/benchmarker +3 -0
  73. data/test_app/script/performance/profiler +3 -0
  74. data/test_app/script/performance/request +3 -0
  75. data/test_app/script/plugin +3 -0
  76. data/test_app/script/process/inspector +3 -0
  77. data/test_app/script/process/reaper +3 -0
  78. data/test_app/script/process/spawner +3 -0
  79. data/test_app/script/runner +3 -0
  80. data/test_app/script/server +3 -0
  81. data/test_app/test/fixtures/users.yml +6 -0
  82. data/test_app/test/functional/user_sessions_controller_test.rb +15 -0
  83. data/test_app/test/functional/users_controller_test.rb +8 -0
  84. data/test_app/test/test_helper.rb +38 -0
  85. data/test_app/test/unit/ass_test.rb +8 -0
  86. data/test_app/test/unit/user_test.rb +8 -0
  87. metadata +182 -0
@@ -0,0 +1,47 @@
1
+ module Authgasm
2
+ module Session
3
+ # = Callbacks
4
+ #
5
+ # Just like in ActiveRecord you have before_save, before_validation, etc. You have similar callbacks with Authgasm, see all callbacks below.
6
+ module Callbacks
7
+ CALLBACKS = %w(before_create after_create before_destroy after_destroy before_update after_update before_validation after_validation)
8
+
9
+ def self.included(base) #:nodoc:
10
+ [:create, :destroy, :update, :valid?].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 create_with_callbacks(updating = false) # :nodoc:
19
+ run_callbacks(:before_create)
20
+ result = create_without_callbacks(updating)
21
+ run_callbacks(:after_create)
22
+ result
23
+ end
24
+
25
+ def destroy_with_callbacks # :nodoc:
26
+ run_callbacks(:before_destroy)
27
+ result = destroy_without_callbacks
28
+ run_callbacks(:after_destroy)
29
+ result
30
+ end
31
+
32
+ def update_with_callbacks # :nodoc:
33
+ run_callbacks(:before_update)
34
+ result = update_without_callbacks
35
+ run_callbacks(:after_update)
36
+ result
37
+ end
38
+
39
+ def valid_with_callbacks?(set_session = false) # :nodoc:
40
+ run_callbacks(:before_validation)
41
+ result = valid_without_callbacks?(set_session)
42
+ run_callbacks(:after_validation)
43
+ result
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,193 @@
1
+ module Authgasm
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 < Authgasm::Session::Base
21
+ # self.authenticate_with = User
22
+ # # ... more configuration
23
+ # end
24
+ #
25
+ # or...
26
+ #
27
+ # class UserSession < Authgasm::Session::Base
28
+ # configure do |config|
29
+ # config.authenticate_with = User
30
+ # # ... more configuration
31
+ # end
32
+ # end
33
+ #
34
+ # See the methods belows for all configuration options.
35
+ module ClassMethods
36
+ # Lets you change which model to use for authentication.
37
+ #
38
+ # * <tt>Default:</tt> inferred from the class name. UserSession would automatically try User
39
+ # * <tt>Accepts:</tt> an ActiveRecord class
40
+ def authenticate_with=(klass)
41
+ @klass_name = klass.name
42
+ @klass = klass
43
+ end
44
+
45
+ # Convenience method that lets you easily set configuration, see examples above
46
+ def configure
47
+ yield self
48
+ end
49
+
50
+ # The authentication credentials are stored in a cookie as: user#{cookie_separator}crypted_password.
51
+ #
52
+ # * <tt>Default:</tt> ":::"
53
+ # * <tt>Accepts:</tt> String
54
+ def cookie_separator
55
+ @cookie_separator ||= ":::"
56
+ end
57
+ attr_writer :cookie_separator
58
+
59
+ # 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.
60
+ # Also, if a scope is set it will be inserted into the beginning of the string. Exmaple:
61
+ #
62
+ # session = UserSession.new(:super_high_secret)
63
+ # session.cookie_key => "super_high_secret_user_credentials"
64
+ #
65
+ # * <tt>Default:</tt> "#{klass_name.underscore}_credentials"
66
+ # * <tt>Accepts:</tt> String
67
+ def cookie_key
68
+ @cookie_key ||= "#{klass_name.underscore}_credentials"
69
+ end
70
+ attr_writer :cookie_key
71
+
72
+ # 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, Authgasm will just pass you the login.
73
+ #
74
+ # 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:
75
+ #
76
+ # def self.find_login(login)
77
+ # find_by_login(login) || find_by_email(login)
78
+ # end
79
+ #
80
+ # * <tt>Default:</tt> "find_by_#{login_field}"
81
+ # * <tt>Accepts:</tt> Symbol or String
82
+ def find_by_login_method
83
+ @find_by_login_method ||= "find_by_#{login_field}"
84
+ end
85
+ attr_writer :find_by_login_method
86
+
87
+ # The name of the method you want Authgasm to create for storing the login / username. Keep in mind this is just for your Authgasm::Session, if you want it can be something completely different
88
+ # 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
89
+ # more details.
90
+ #
91
+ # * <tt>Default:</tt> Guesses based on the model columns, tries login, username, and email. If none are present it defaults to login
92
+ # * <tt>Accepts:</tt> Symbol or String
93
+ def login_field
94
+ @login_field ||= (klass.columns.include?("login") && :login) || (klass.columns.include?("username") && :username) || (klass.columns.include?("email") && :email) || :login
95
+ end
96
+ attr_writer :login_field
97
+
98
+ # Works exactly like login_field, but for the password instead.
99
+ #
100
+ # * <tt>Default:</tt> Guesses based on the model columns, tries password and pass. If none are present it defaults to password
101
+ # * <tt>Accepts:</tt> Symbol or String
102
+ def password_field
103
+ @password_field ||= (klass.columns.include?("password") && :password) || (klass.columns.include?("pass") && :pass) || :password
104
+ end
105
+ attr_writer :password_field
106
+
107
+ # The length of time until the cookie expires.
108
+ #
109
+ # * <tt>Default:</tt> 3.months
110
+ # * <tt>Accepts:</tt> Integer, length of time in seconds, such as 60 or 3.months
111
+ def remember_me_for
112
+ return @remember_me_for if @set_remember_me_for
113
+ @remember_me_for ||= 3.months
114
+ end
115
+
116
+ def remember_me_for=(value) # :nodoc:
117
+ @set_remember_me_for = true
118
+ @remember_me_for = value
119
+ end
120
+
121
+ # 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
122
+ # 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
123
+ # 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.
124
+ #
125
+ # * <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
126
+ # * <tt>Accepts:</tt> Symbol or String
127
+ def remember_token_field
128
+ @remember_token_field ||=
129
+ (klass.columns.include?("remember_token") && :remember_token) ||
130
+ (klass.columns.include?("remember_key") && :remember_key) ||
131
+ (klass.columns.include?("cookie_token") && :cookie_token) ||
132
+ (klass.columns.include?("cookie_key") && :cookie_key) ||
133
+ :remember_token
134
+ end
135
+ attr_writer :remember_token_field
136
+
137
+ # Works exactly like cookie_key, but for sessions. See cookie_key for more info.
138
+ #
139
+ # * <tt>Default:</tt> :#{klass_name.underscore}_id
140
+ # * <tt>Accepts:</tt> Symbol or String
141
+ def session_key
142
+ @session_key ||= "#{klass_name.underscore}_id".to_sym
143
+ end
144
+ attr_writer :session_key
145
+
146
+ # 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.
147
+ #
148
+ # * <tt>Default:</tt> "valid_#{password_field}?"
149
+ # * <tt>Accepts:</tt> Symbol or String
150
+ def verify_password_method
151
+ @verify_password_method ||= "valid_#{password_field}?"
152
+ end
153
+ attr_writer :verify_password_method
154
+ end
155
+
156
+ module InstanceMethods # :nodoc:
157
+ def cookie_key
158
+ key_parts = [scope, self.class.cookie_key].compact
159
+ key_parts.join("_")
160
+ end
161
+
162
+ def find_by_login_method
163
+ self.class.find_by_login_method
164
+ end
165
+
166
+ def login_field
167
+ self.class.login_field
168
+ end
169
+
170
+ def password_field
171
+ self.class.password_field
172
+ end
173
+
174
+ def remember_me_for
175
+ self.class.remember_me_for
176
+ end
177
+
178
+ def remember_token_field
179
+ self.class.remember_token_field
180
+ end
181
+
182
+ def session_key
183
+ key_parts = [scope, self.class.session_key].compact
184
+ key_parts.join("_")
185
+ end
186
+
187
+ def verify_password_method
188
+ self.class.verify_password_method
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
@@ -0,0 +1,12 @@
1
+ module Authgasm
2
+ module Session
3
+ class Errors < ::ActiveRecord::Errors # :nodoc:
4
+ end
5
+
6
+ class SessionInvalid < ::StandardError # :nodoc:
7
+ def initialize(session)
8
+ super("Authentication failed: #{session.errors.full_messages.to_sentence}")
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ module Authgasm
2
+ # = Sha256 Crypto Provider
3
+ #
4
+ # 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.
5
+ # The password will be passed as the single parameter to each of these methods so you can do your magic.
6
+ #
7
+ # If you are encrypting via a hash just don't include a decrypt method, since hashes can't be decrypted. Authgasm will notice this adjust accordingly.
8
+ class Sha256CryptoProvider
9
+ def self.encrypt(pass)
10
+ Digest::SHA256.hexdigest(pass)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,56 @@
1
+ module Authgasm # :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 = 9
47
+ TINY = 0
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.