railties 3.1.12 → 3.2.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +2292 -41
- data/README.rdoc +14 -5
- data/bin/rails +7 -0
- data/guides/code/getting_started/Gemfile +27 -0
- data/guides/code/getting_started/README.rdoc +261 -0
- data/guides/code/getting_started/Rakefile +7 -0
- data/guides/code/getting_started/app/assets/images/rails.png +0 -0
- data/guides/code/getting_started/app/assets/javascripts/application.js +9 -0
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +3 -0
- data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +3 -0
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +3 -0
- data/guides/code/getting_started/app/assets/stylesheets/application.css +7 -0
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +3 -0
- data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +3 -0
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +3 -0
- data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +56 -0
- data/guides/code/getting_started/app/controllers/application_controller.rb +3 -0
- data/guides/code/getting_started/app/controllers/comments_controller.rb +16 -0
- data/guides/code/getting_started/app/controllers/home_controller.rb +5 -0
- data/guides/code/getting_started/app/controllers/posts_controller.rb +84 -0
- data/guides/code/getting_started/app/helpers/application_helper.rb +2 -0
- data/guides/code/getting_started/app/helpers/comments_helper.rb +2 -0
- data/guides/code/getting_started/app/helpers/home_helper.rb +2 -0
- data/guides/code/getting_started/app/helpers/posts_helper.rb +5 -0
- data/guides/code/getting_started/app/models/comment.rb +3 -0
- data/guides/code/getting_started/app/models/post.rb +11 -0
- data/guides/code/getting_started/app/models/tag.rb +3 -0
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +15 -0
- data/guides/code/getting_started/app/views/comments/_form.html.erb +13 -0
- data/guides/code/getting_started/app/views/home/index.html.erb +2 -0
- data/guides/code/getting_started/app/views/layouts/application.html.erb +14 -0
- data/guides/code/getting_started/app/views/posts/_form.html.erb +32 -0
- data/guides/code/getting_started/app/views/posts/edit.html.erb +6 -0
- data/guides/code/getting_started/app/views/posts/index.html.erb +27 -0
- data/guides/code/getting_started/app/views/posts/new.html.erb +5 -0
- data/guides/code/getting_started/app/views/posts/show.html.erb +31 -0
- data/guides/code/getting_started/app/views/tags/_form.html.erb +12 -0
- data/guides/code/getting_started/config.ru +4 -0
- data/guides/code/getting_started/config/application.rb +53 -0
- data/guides/code/getting_started/config/boot.rb +6 -0
- data/guides/code/getting_started/config/database.yml +25 -0
- data/guides/code/getting_started/config/environment.rb +5 -0
- data/guides/code/getting_started/config/environments/development.rb +37 -0
- data/guides/code/getting_started/config/environments/production.rb +67 -0
- data/guides/code/getting_started/config/environments/test.rb +37 -0
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +7 -0
- data/guides/code/getting_started/config/initializers/inflections.rb +10 -0
- data/guides/code/getting_started/config/initializers/mime_types.rb +5 -0
- data/guides/code/getting_started/config/initializers/secret_token.rb +7 -0
- data/guides/code/getting_started/config/initializers/session_store.rb +8 -0
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +14 -0
- data/guides/code/getting_started/config/locales/en.yml +5 -0
- data/guides/code/getting_started/config/routes.rb +64 -0
- data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +11 -0
- data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +12 -0
- data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +11 -0
- data/guides/code/getting_started/db/schema.rb +43 -0
- data/guides/code/getting_started/db/seeds.rb +7 -0
- data/guides/code/getting_started/doc/README_FOR_APP +2 -0
- data/guides/code/getting_started/public/404.html +26 -0
- data/guides/code/getting_started/public/422.html +26 -0
- data/guides/code/getting_started/public/500.html +26 -0
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +5 -0
- data/guides/code/getting_started/script/rails +6 -0
- data/guides/code/getting_started/test/fixtures/comments.yml +11 -0
- data/guides/code/getting_started/test/fixtures/posts.yml +11 -0
- data/guides/code/getting_started/test/fixtures/tags.yml +9 -0
- data/guides/code/getting_started/test/functional/comments_controller_test.rb +7 -0
- data/guides/code/getting_started/test/functional/home_controller_test.rb +9 -0
- data/guides/code/getting_started/test/functional/posts_controller_test.rb +49 -0
- data/guides/code/getting_started/test/performance/browsing_test.rb +12 -0
- data/guides/code/getting_started/test/test_helper.rb +13 -0
- data/guides/code/getting_started/test/unit/comment_test.rb +7 -0
- data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +4 -0
- data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +4 -0
- data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +4 -0
- data/guides/code/getting_started/test/unit/post_test.rb +7 -0
- data/guides/code/getting_started/test/unit/tag_test.rb +7 -0
- data/guides/rails_guides/generator.rb +2 -1
- data/guides/source/3_0_release_notes.textile +2 -2
- data/guides/source/3_1_release_notes.textile +3 -110
- data/guides/source/action_controller_overview.textile +11 -13
- data/guides/source/action_mailer_basics.textile +7 -18
- data/guides/source/action_view_overview.textile +78 -9
- data/guides/source/active_model_basics.textile +205 -0
- data/guides/source/active_record_basics.textile +31 -31
- data/guides/source/active_record_querying.textile +288 -67
- data/guides/source/active_record_validations_callbacks.textile +69 -75
- data/guides/source/active_resource_basics.textile +48 -2
- data/guides/source/active_support_core_extensions.textile +145 -24
- data/guides/source/ajax_on_rails.textile +65 -7
- data/guides/source/api_documentation_guidelines.textile +0 -6
- data/guides/source/asset_pipeline.textile +2 -2
- data/guides/source/association_basics.textile +25 -34
- data/guides/source/caching_with_rails.textile +12 -17
- data/guides/source/command_line.textile +29 -19
- data/guides/source/configuring.textile +40 -18
- data/guides/source/contributing_to_ruby_on_rails.textile +11 -18
- data/guides/source/debugging_rails_applications.textile +10 -21
- data/guides/source/engines.textile +618 -0
- data/guides/source/form_helpers.textile +1 -12
- data/guides/source/generators.textile +9 -11
- data/guides/source/getting_started.textile +152 -152
- data/guides/source/i18n.textile +4 -5
- data/guides/source/index.html.erb +0 -1
- data/guides/source/initialization.textile +26 -26
- data/guides/source/layouts_and_rendering.textile +97 -61
- data/guides/source/migrations.textile +380 -161
- data/guides/source/performance_testing.textile +4 -10
- data/guides/source/plugins.textile +11 -19
- data/guides/source/rails_application_templates.textile +12 -4
- data/guides/source/rails_on_rack.textile +25 -19
- data/guides/source/routing.textile +6 -13
- data/guides/source/ruby_on_rails_guides_guidelines.textile +0 -5
- data/guides/source/security.textile +11 -15
- data/guides/source/testing.textile +1 -9
- data/lib/rails/application.rb +107 -42
- data/lib/rails/application/bootstrap.rb +12 -11
- data/lib/rails/application/configuration.rb +27 -21
- data/lib/rails/application/finisher.rb +40 -17
- data/lib/rails/application/route_inspector.rb +75 -0
- data/lib/rails/application/routes_reloader.rb +15 -4
- data/lib/rails/code_statistics.rb +16 -5
- data/lib/rails/commands.rb +6 -5
- data/lib/rails/commands/application.rb +8 -1
- data/lib/rails/commands/console.rb +2 -0
- data/lib/rails/commands/dbconsole.rb +2 -2
- data/lib/rails/commands/destroy.rb +0 -2
- data/lib/rails/commands/generate.rb +3 -3
- data/lib/rails/commands/plugin.rb +161 -159
- data/lib/rails/commands/plugin_new.rb +3 -2
- data/lib/rails/commands/runner.rb +4 -0
- data/lib/rails/console/app.rb +26 -22
- data/lib/rails/console/helpers.rb +9 -5
- data/lib/rails/engine.rb +70 -34
- data/lib/rails/engine/commands.rb +39 -0
- data/lib/rails/engine/configuration.rb +1 -1
- data/lib/rails/generators.rb +3 -14
- data/lib/rails/generators/actions.rb +36 -9
- data/lib/rails/generators/app_base.rb +34 -38
- data/lib/rails/generators/base.rb +4 -4
- data/lib/rails/generators/generated_attribute.rb +1 -1
- data/lib/rails/generators/named_base.rb +1 -3
- data/lib/rails/generators/rails/app/USAGE +6 -0
- data/lib/rails/generators/rails/app/app_generator.rb +6 -2
- data/lib/rails/generators/rails/app/templates/Gemfile +4 -3
- data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +9 -3
- data/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +11 -5
- data/lib/rails/generators/rails/app/templates/app/mailers/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/app/models/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +1 -1
- data/lib/rails/generators/rails/app/templates/config/application.rb +11 -0
- data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +1 -1
- data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +1 -1
- data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +10 -1
- data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +10 -1
- data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +6 -6
- data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +5 -0
- data/lib/rails/generators/rails/app/templates/config/routes.rb +1 -1
- data/lib/rails/generators/rails/app/templates/public/500.html +0 -1
- data/lib/rails/generators/rails/app/templates/public/index.html +1 -1
- data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/fixtures/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/functional/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/integration/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
- data/lib/rails/generators/rails/controller/templates/controller.rb +1 -1
- data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
- data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +17 -5
- data/lib/rails/generators/rails/plugin_new/templates/Rakefile +1 -0
- data/lib/rails/generators/rails/plugin_new/templates/app/mailers/.empty_directory +0 -0
- data/lib/rails/generators/rails/plugin_new/templates/app/models/.empty_directory +0 -0
- data/lib/rails/generators/rails/plugin_new/templates/app/views/layouts/%name%/application.html.erb.tt +1 -1
- data/lib/rails/generators/rails/plugin_new/templates/gitignore +4 -3
- data/lib/rails/generators/rails/plugin_new/templates/lib/%name%/engine.rb +1 -1
- data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +1 -1
- data/lib/rails/generators/rails/plugin_new/templates/script/rails.tt +5 -3
- data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +2 -2
- data/lib/rails/generators/rails/task/USAGE +9 -0
- data/lib/rails/generators/rails/task/task_generator.rb +12 -0
- data/lib/rails/generators/rails/task/templates/task.rb +8 -0
- data/lib/rails/generators/resource_helpers.rb +3 -3
- data/lib/rails/generators/test_unit/integration/templates/integration_test.rb +0 -2
- data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +4 -4
- data/lib/rails/paths.rb +11 -38
- data/lib/rails/rack/debugger.rb +3 -4
- data/lib/rails/rack/logger.rb +26 -12
- data/lib/rails/railtie.rb +6 -1
- data/lib/rails/railtie/configuration.rb +12 -5
- data/lib/rails/source_annotation_extractor.rb +12 -10
- data/lib/rails/tasks/documentation.rake +3 -1
- data/lib/rails/tasks/engine.rake +1 -0
- data/lib/rails/tasks/misc.rake +1 -1
- data/lib/rails/tasks/routes.rake +3 -23
- data/lib/rails/test_help.rb +1 -2
- data/lib/rails/test_unit/testing.rake +8 -4
- data/lib/rails/version.rb +3 -3
- metadata +131 -61
- checksums.yaml +0 -7
- data/lib/rails/generators/rails/plugin/USAGE +0 -13
- data/lib/rails/generators/rails/plugin/plugin_generator.rb +0 -54
- data/lib/rails/generators/rails/plugin/templates/MIT-LICENSE.tt +0 -20
- data/lib/rails/generators/rails/plugin/templates/README.tt +0 -13
- data/lib/rails/generators/rails/plugin/templates/Rakefile.tt +0 -23
- data/lib/rails/generators/rails/plugin/templates/init.rb +0 -1
- data/lib/rails/generators/rails/plugin/templates/install.rb +0 -1
- data/lib/rails/generators/rails/plugin/templates/lib/%file_name%.rb.tt +0 -1
- data/lib/rails/generators/rails/plugin/templates/lib/tasks/%file_name%_tasks.rake.tt +0 -4
- data/lib/rails/generators/rails/plugin/templates/uninstall.rb +0 -1
@@ -64,7 +64,7 @@ end
|
|
64
64
|
|
65
65
|
If you want a more complicated expiration scheme, you can use cache sweepers to expire cached objects when things change. This is covered in the section on Sweepers.
|
66
66
|
|
67
|
-
NOTE: Page caching ignores all parameters. For example +/products?page=1+ will be written out to the filesystem as +products.html+ with no reference to the +page+ parameter. Thus, if someone requests +/products?page=2+ later, they will get the cached first page.
|
67
|
+
NOTE: Page caching ignores all parameters. For example +/products?page=1+ will be written out to the filesystem as +products.html+ with no reference to the +page+ parameter. Thus, if someone requests +/products?page=2+ later, they will get the cached first page. A workaround for this limitation is to include the parameters in the page's path, e.g. +/productions/page/1+.
|
68
68
|
|
69
69
|
INFO: Page caching runs in an after filter. Thus, invalid requests won't generate spurious cache entries as long as you halt them. Typically, a redirection in some before filter that checks request preconditions does the job.
|
70
70
|
|
@@ -72,7 +72,7 @@ h4. Action Caching
|
|
72
72
|
|
73
73
|
One of the issues with Page Caching is that you cannot use it for pages that require to restrict access somehow. This is where Action Caching comes in. Action Caching works like Page Caching except for the fact that the incoming web request does go from the webserver to the Rails stack and Action Pack so that before filters can be run on it before the cache is served. This allows authentication and other restriction to be run while still serving the result of the output from a cached copy.
|
74
74
|
|
75
|
-
Clearing the cache works in
|
75
|
+
Clearing the cache works in a similar way to Page Caching, except you use +expire_action+ instead of +expire_page+.
|
76
76
|
|
77
77
|
Let's say you only wanted authenticated users to call actions on +ProductsController+.
|
78
78
|
|
@@ -188,7 +188,7 @@ end
|
|
188
188
|
You may notice that the actual product gets passed to the sweeper, so if we were caching the edit action for each product, we could add an expire method which specifies the page we want to expire:
|
189
189
|
|
190
190
|
<ruby>
|
191
|
-
|
191
|
+
expire_action(:controller => 'products', :action => 'edit', :id => product.id)
|
192
192
|
</ruby>
|
193
193
|
|
194
194
|
Then we add it to our controller to tell it to call the sweeper when certain actions are called. So, if we wanted to expire the cached content for the list and edit actions when the create action was called, we could do the following:
|
@@ -293,7 +293,7 @@ Note that the cache will grow until the disk is full unless you periodically cle
|
|
293
293
|
|
294
294
|
h4. ActiveSupport::Cache::MemCacheStore
|
295
295
|
|
296
|
-
This cache store uses Danga's +memcached+ server to provide a centralized cache for your application. Rails uses the bundled +
|
296
|
+
This cache store uses Danga's +memcached+ server to provide a centralized cache for your application. Rails uses the bundled +memcache-client+ gem by default. This is currently the most popular cache store for production websites. It can be used to provide a single, shared cache cluster with very a high performance and redundancy.
|
297
297
|
|
298
298
|
When initializing the cache, you need to specify the addresses for all memcached servers in your cluster. If none is specified, it will assume memcached is running on the local host on the default port, but this is not an ideal set up for larger sites.
|
299
299
|
|
@@ -332,6 +332,14 @@ caches_action :index, :expires_in => 60.seconds, :unless_exist => true
|
|
332
332
|
For more information about Ehcache, see "http://ehcache.org/":http://ehcache.org/ .
|
333
333
|
For more information about Ehcache for JRuby and Rails, see "http://ehcache.org/documentation/jruby.html":http://ehcache.org/documentation/jruby.html
|
334
334
|
|
335
|
+
h4. ActiveSupport::Cache::NullStore
|
336
|
+
|
337
|
+
This cache store implementation is meant to be used only in development or test environments and it never stores anything. This can be very useful in development when you have code that interacts directly with +Rails.cache+, but caching may interfere with being able to see the results of code changes. With this cache store, all +fetch+ and +read+ operations will result in a miss.
|
338
|
+
|
339
|
+
<ruby>
|
340
|
+
ActionController::Base.cache_store = :null
|
341
|
+
</ruby>
|
342
|
+
|
335
343
|
h4. Custom Cache Stores
|
336
344
|
|
337
345
|
You can create your own custom cache store by simply extending +ActiveSupport::Cache::Store+ and implementing the appropriate methods. In this way, you can swap in any number of caching technologies into your Rails application.
|
@@ -403,16 +411,3 @@ end
|
|
403
411
|
h3. Further reading
|
404
412
|
|
405
413
|
* "Scaling Rails Screencasts":http://railslab.newrelic.com/scaling-rails
|
406
|
-
|
407
|
-
|
408
|
-
h3. Changelog
|
409
|
-
|
410
|
-
* Feb 17, 2011: Document 3.0.0 changes to ActiveSupport::Cache
|
411
|
-
* May 02, 2009: Formatting cleanups
|
412
|
-
* April 26, 2009: Clean up typos in submitted patch
|
413
|
-
* April 1, 2009: Made a bunch of small fixes
|
414
|
-
* February 22, 2009: Beefed up the section on cache_stores
|
415
|
-
* December 27, 2008: Typo fixes
|
416
|
-
* November 23, 2008: Incremental updates with various suggested changes and formatting cleanup
|
417
|
-
* September 15, 2008: Initial version by Aditya Chadha
|
418
|
-
|
@@ -36,7 +36,7 @@ WARNING: You can install the rails gem by typing +gem install rails+, if you don
|
|
36
36
|
<shell>
|
37
37
|
$ rails new commandsapp
|
38
38
|
create
|
39
|
-
create README
|
39
|
+
create README.rdoc
|
40
40
|
create .gitignore
|
41
41
|
create Rakefile
|
42
42
|
create config.ru
|
@@ -81,6 +81,8 @@ The server can be run on a different port using the +-p+ option. The default dev
|
|
81
81
|
$ rails server -e production -p 4000
|
82
82
|
</shell>
|
83
83
|
|
84
|
+
The +-b+ option binds Rails to the specified ip, by default it is 0.0.0.0. You can run a server as a daemon by passing a +-d+ option.
|
85
|
+
|
84
86
|
h4. +rails generate+
|
85
87
|
|
86
88
|
The +rails generate+ command uses templates to create a whole lot of things. Running +rails generate+ by itself gives a list of available generators:
|
@@ -325,6 +327,8 @@ h4. +rails destroy+
|
|
325
327
|
|
326
328
|
Think of +destroy+ as the opposite of +generate+. It'll figure out what generate did, and undo it.
|
327
329
|
|
330
|
+
You can also use the alias "d" to invoke the destroy command: <tt>rails d</tt>.
|
331
|
+
|
328
332
|
<shell>
|
329
333
|
$ rails generate model Oops
|
330
334
|
exists app/models/
|
@@ -379,23 +383,24 @@ $ rake about
|
|
379
383
|
About your application's environment
|
380
384
|
Ruby version 1.8.7 (x86_64-linux)
|
381
385
|
RubyGems version 1.3.6
|
382
|
-
Rack version 1.
|
383
|
-
Rails version 3.
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
386
|
+
Rack version 1.3
|
387
|
+
Rails version 3.2.0.beta
|
388
|
+
JavaScript Runtime Node.js (V8)
|
389
|
+
Active Record version 3.2.0.beta
|
390
|
+
Action Pack version 3.2.0.beta
|
391
|
+
Active Resource version 3.2.0.beta
|
392
|
+
Action Mailer version 3.2.0.beta
|
393
|
+
Active Support version 3.2.0.beta
|
394
|
+
Middleware ActionDispatch::Static, Rack::Lock, Rack::Runtime, Rack::MethodOverride, ActionDispatch::RequestId, Rails::Rack::Logger, ActionDispatch::ShowExceptions, ActionDispatch::DebugExceptions, ActionDispatch::RemoteIp, ActionDispatch::Reloader, ActionDispatch::Callbacks, ActiveRecord::ConnectionAdapters::ConnectionManagement, ActiveRecord::QueryCache, ActionDispatch::Cookies, ActionDispatch::Session::CookieStore, ActionDispatch::Flash, ActionDispatch::ParamsParser, ActionDispatch::Head, Rack::ConditionalGet, Rack::ETag, ActionDispatch::BestStandardsSupport
|
390
395
|
Application root /home/foobar/commandsapp
|
391
396
|
Environment development
|
392
397
|
Database adapter sqlite3
|
393
|
-
Database schema version
|
398
|
+
Database schema version 20110805173523
|
394
399
|
</shell>
|
395
400
|
|
396
401
|
h4. +assets+
|
397
402
|
|
398
|
-
You can precompile the assets in <tt>app/assets</tt> using <tt>rake assets:precompile</tt> and remove compiled assets using <tt>rake assets:clean</tt>.
|
403
|
+
You can precompile the assets in <tt>app/assets</tt> using <tt>rake assets:precompile</tt> and remove those compiled assets using <tt>rake assets:clean</tt>.
|
399
404
|
|
400
405
|
h4. +db+
|
401
406
|
|
@@ -415,7 +420,7 @@ The +doc:+ namespace has the tools to generate documentation for your app, API d
|
|
415
420
|
|
416
421
|
h4. +notes+
|
417
422
|
|
418
|
-
+rake notes+ will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is
|
423
|
+
+rake notes+ will search through your code for comments beginning with FIXME, OPTIMIZE or TODO. The search is done in files with extension +.builder+, +.rb+, +.erb+, +.haml+ and +.slim+ for both default and custom annotations.
|
419
424
|
|
420
425
|
<shell>
|
421
426
|
$ rake notes
|
@@ -460,13 +465,18 @@ h4. +test+
|
|
460
465
|
|
461
466
|
INFO: A good description of unit testing in Rails is given in "A Guide to Testing Rails Applications":testing.html
|
462
467
|
|
463
|
-
Rails comes with a test suite called Test::Unit
|
464
|
-
|
465
|
-
The +test:+ namespace helps in running the different tests you will (hopefully!) write.
|
468
|
+
Rails comes with a test suite called <tt>Test::Unit</tt>. Rails owes its stability to the use of tests. The tasks available in the +test:+ namespace helps in running the different tests you will hopefully write.
|
466
469
|
|
467
470
|
h4. +tmp+
|
468
471
|
|
469
|
-
The <tt>Rails.root/tmp</tt> directory is, like the *nix /tmp directory, the holding place for temporary files like sessions (if you're using a file store for files), process id files, and cached actions.
|
472
|
+
The <tt>Rails.root/tmp</tt> directory is, like the *nix /tmp directory, the holding place for temporary files like sessions (if you're using a file store for files), process id files, and cached actions.
|
473
|
+
|
474
|
+
The +tmp:+ namespaced tasks will help you clear the <tt>Rails.root/tmp</tt> directory:
|
475
|
+
|
476
|
+
* +rake tmp:cache:clear+ clears <tt>tmp/cache</tt>.
|
477
|
+
* +rake tmp:sessions:clear+ clears <tt>tmp/sessions</tt>.
|
478
|
+
* +rake tmp:sockets:clear+ clears <tt>tmp/sockets</tt>.
|
479
|
+
* +rake tmp:clear+ clears all the three: cache, sessions and sockets.
|
470
480
|
|
471
481
|
h4. Miscellaneous
|
472
482
|
|
@@ -499,8 +509,8 @@ $ rails new . --git --database=postgresql
|
|
499
509
|
create tmp/pids
|
500
510
|
create Rakefile
|
501
511
|
add 'Rakefile'
|
502
|
-
create README
|
503
|
-
add 'README'
|
512
|
+
create README.rdoc
|
513
|
+
add 'README.rdoc'
|
504
514
|
create app/controllers/application_controller.rb
|
505
515
|
add 'app/controllers/application_controller.rb'
|
506
516
|
create app/helpers/application_helper.rb
|
@@ -513,7 +523,7 @@ We had to create the *gitapp* directory and initialize an empty git repository b
|
|
513
523
|
|
514
524
|
<shell>
|
515
525
|
$ cat config/database.yml
|
516
|
-
# PostgreSQL. Versions
|
526
|
+
# PostgreSQL. Versions 8.2 and up are supported.
|
517
527
|
#
|
518
528
|
# Install the ruby-postgres driver:
|
519
529
|
# gem install ruby-postgres
|
@@ -82,12 +82,18 @@ NOTE. The +config.asset_path+ configuration is ignored if the asset pipeline is
|
|
82
82
|
|
83
83
|
* +config.encoding+ sets up the application-wide encoding. Defaults to UTF-8.
|
84
84
|
|
85
|
+
* +config.exceptions_app+ sets the exceptions application invoked by the ShowException middleware when an exception happens. Defaults to +ActionDispatch::PublicExceptions.new(Rails.public_path)+.
|
86
|
+
|
87
|
+
* +config.file_watcher+ the class used to detect file updates in the filesystem when +config.reload_classes_only_on_change+ is true. Must conform to +ActiveSupport::FileUpdateChecker+ API.
|
88
|
+
|
85
89
|
* +config.filter_parameters+ used for filtering out the parameters that you don't want shown in the logs, such as passwords or credit card numbers.
|
86
90
|
|
87
91
|
* +config.force_ssl+ forces all requests to be under HTTPS protocol by using +Rack::SSL+ middleware.
|
88
92
|
|
89
93
|
* +config.log_level+ defines the verbosity of the Rails logger. This option defaults to +:debug+ for all modes except production, where it defaults to +:info+.
|
90
94
|
|
95
|
+
* +config.log_tags+ accepts a list of methods that respond to +request+ object. This makes it easy to tag log lines with debug information like subdomain and request id -- both very helpful in debugging multi-user production applications.
|
96
|
+
|
91
97
|
* +config.logger+ accepts a logger conforming to the interface of Log4r or the default Ruby +Logger+ class. Defaults to an instance of +ActiveSupport::BufferedLogger+, with auto flushing off in production mode.
|
92
98
|
|
93
99
|
* +config.middleware+ allows you to configure the application's middleware. This is covered in depth in the "Configuring Middleware":#configuring-middleware section below.
|
@@ -96,6 +102,8 @@ NOTE. The +config.asset_path+ configuration is ignored if the asset pipeline is
|
|
96
102
|
|
97
103
|
* +config.preload_frameworks+ enables or disables preloading all frameworks at startup. Enabled by +config.threadsafe!+. Defaults to +nil+, so is disabled.
|
98
104
|
|
105
|
+
* +config.reload_classes_only_on_change+ enables or disables reloading of classes only when tracked files change. By default tracks everything on autoload paths and is set to true. If +config.cache_classes+ is true, this option is ignored.
|
106
|
+
|
99
107
|
* +config.reload_plugins+ enables or disables plugin reloading. Defaults to false.
|
100
108
|
|
101
109
|
* +config.secret_token+ used for specifying a key which allows sessions for the application to be verified against a known secure key to prevent tampering. Applications get +config.secret_token+ initialized to a random key in +config/initializers/secret_token.rb+.
|
@@ -181,16 +189,17 @@ h4. Configuring Middleware
|
|
181
189
|
|
182
190
|
Every Rails application comes with a standard set of middleware which it uses in this order in the development environment:
|
183
191
|
|
184
|
-
* +Rack::SSL+
|
192
|
+
* +Rack::SSL+ forces every request to be under HTTPS protocol. Will be available if +config.force_ssl+ is set to +true+. Options passed to this can be configured by using +config.ssl_options+.
|
185
193
|
* +ActionDispatch::Static+ is used to serve static assets. Disabled if +config.serve_static_assets+ is +true+.
|
186
|
-
* +Rack::Lock+
|
187
|
-
* +ActiveSupport::Cache::Strategy::LocalCache+
|
188
|
-
* +Rack::Runtime+
|
189
|
-
* +Rails::Rack::Logger+
|
190
|
-
* +ActionDispatch::ShowExceptions+
|
191
|
-
* +ActionDispatch::
|
192
|
-
* +
|
193
|
-
* +
|
194
|
+
* +Rack::Lock+ wraps the app in mutex so it can only be called by a single thread at a time. Only enabled if +config.action_controller.allow_concurrency+ is set to +false+, which it is by default.
|
195
|
+
* +ActiveSupport::Cache::Strategy::LocalCache+ serves as a basic memory backed cache. This cache is not thread safe and is intended only for serving as a temporary memory cache for a single thread.
|
196
|
+
* +Rack::Runtime+ sets an +X-Runtime+ header, containing the time (in seconds) taken to execute the request.
|
197
|
+
* +Rails::Rack::Logger+ notifies the logs that the request has began. After request is complete, flushes all the logs.
|
198
|
+
* +ActionDispatch::ShowExceptions+ rescues any exception returned by the application and renders nice exception pages if the request is local or if +config.consider_all_requests_local+ is set to +true+. If +config.action_dispatch.show_exceptions+ is set to +false+, exceptions will be raised regardless.
|
199
|
+
* +ActionDispatch::RequestId+ makes a unique X-Request-Id header available to the response and enables the +ActionDispatch::Request#uuid+ method.
|
200
|
+
* +ActionDispatch::RemoteIp+ checks for IP spoofing attacks. Configurable with the +config.action_dispatch.ip_spoofing_check+ and +config.action_dispatch.trusted_proxies+ settings.
|
201
|
+
* +Rack::Sendfile+ intercepts responses whose body is being served from a file and replaces it with a server specific X-Sendfile header. Configurable with +config.action_dispatch.x_sendfile_header+.
|
202
|
+
* +ActionDispatch::Callbacks+ runs the prepare callbacks before serving the request.
|
194
203
|
* +ActiveRecord::ConnectionAdapters::ConnectionManagement+ cleans active connections after each request, unless the +rack.test+ key in the request environment is set to +true+.
|
195
204
|
* +ActiveRecord::QueryCache+ caches all SELECT queries generated in a request. If any INSERT or UPDATE takes place then the cache is cleaned.
|
196
205
|
* +ActionDispatch::Cookies+ sets cookies for the request.
|
@@ -265,6 +274,8 @@ h4. Configuring Active Record
|
|
265
274
|
|
266
275
|
* +config.active_record.identity_map+ controls whether the identity map is enabled, and is false by default.
|
267
276
|
|
277
|
+
* +config.active_record.auto_explain_threshold_in_seconds+ configures the threshold for automatic EXPLAINs (+nil+ disables this feature). Queries exceeding the threshold get their query plan logged. Default is 0.5 in development mode.
|
278
|
+
|
268
279
|
The MySQL adapter adds one additional configuration option:
|
269
280
|
|
270
281
|
* +ActiveRecord::ConnectionAdapters::MysqlAdapter.emulate_booleans+ controls whether Active Record will consider all +tinyint(1)+ columns in a MySQL database to be booleans and is true by default.
|
@@ -465,12 +476,31 @@ Rails has 5 initialization events which can be hooked into (listed in the order
|
|
465
476
|
|
466
477
|
* +before_initialize+: This is run directly before the initialization process of the application occurs with the +:bootstrap_hook+ initializer near the beginning of the Rails initialization process.
|
467
478
|
|
468
|
-
* +to_prepare+: Run after the initializers are ran for all Railties (including the application itself), but before eager loading and the middleware stack is built.
|
479
|
+
* +to_prepare+: Run after the initializers are ran for all Railties (including the application itself), but before eager loading and the middleware stack is built. More importantly, will run upon every request in +development+, but only once (during boot-up) in +production+ and +test+.
|
469
480
|
|
470
481
|
* +before_eager_load+: This is run directly before eager loading occurs, which is the default behaviour for the _production_ environment and not for the +development+ environment.
|
471
482
|
|
472
483
|
* +after_initialize+: Run directly after the initialization of the application, but before the application initializers are run.
|
473
484
|
|
485
|
+
To define an event for these hooks, use the block syntax within a +Rails::Aplication+, +Rails::Railtie+ or +Rails::Engine+ subclass:
|
486
|
+
|
487
|
+
<ruby>
|
488
|
+
module YourApp
|
489
|
+
class Application < Rails::Application
|
490
|
+
config.before_initialize do
|
491
|
+
# initialization code goes here
|
492
|
+
end
|
493
|
+
end
|
494
|
+
end
|
495
|
+
</ruby>
|
496
|
+
|
497
|
+
Alternatively, you can also do it through the +config+ method on the +Rails.application+ object:
|
498
|
+
|
499
|
+
<ruby>
|
500
|
+
Rails.application.config.before_initialize do
|
501
|
+
# initialization code goes here
|
502
|
+
end
|
503
|
+
</ruby>
|
474
504
|
|
475
505
|
WARNING: Some parts of your application, notably observers and routing, are not yet set up at the point where the +after_initialize+ block is called.
|
476
506
|
|
@@ -608,11 +638,3 @@ The error occurred while evaluating nil.each
|
|
608
638
|
*+set_routes_reloader+* Configures Action Dispatch to reload the routes file using +ActionDispatch::Callbacks.to_prepare+.
|
609
639
|
|
610
640
|
*+disable_dependency_loading+* Disables the automatic dependency loading if the +config.cache_classes+ is set to true and +config.dependency_loading+ is set to false.
|
611
|
-
|
612
|
-
h3. Changelog
|
613
|
-
|
614
|
-
* December 3, 2010: Added initialization events for Rails 3 ("Ryan Bigg":http://ryanbigg.com)
|
615
|
-
* November 26, 2010: Removed all config settings not available in Rails 3 ("Ryan Bigg":http://ryanbigg.com)
|
616
|
-
* August 13, 2009: Updated with config syntax and added general configuration options by "John Pignata"
|
617
|
-
* January 3, 2009: First reasonably complete draft by "Mike Gunderloy":credits.html#mgunderloy
|
618
|
-
* November 5, 2008: Rough outline by "Mike Gunderloy":credits.html#mgunderloy
|
@@ -104,7 +104,6 @@ $ cd railties
|
|
104
104
|
$ TEST_DIR=generators bundle exec rake test
|
105
105
|
</shell>
|
106
106
|
|
107
|
-
|
108
107
|
h4. Warnings
|
109
108
|
|
110
109
|
The test suite runs with warnings enabled. Ideally Ruby on Rails should issue no warning, but there may be a few, and also some from third-party libraries. Please ignore (or fix!) them if any, and submit patches that do not issue new warnings.
|
@@ -121,6 +120,10 @@ The test suite of Active Record attempts to run four times, once for SQLite3, on
|
|
121
120
|
|
122
121
|
WARNING: If you're working with Active Record code, you _must_ ensure that the tests pass for at least MySQL, PostgreSQL, and SQLite3. Subtle differences between the various adapters have been behind the rejection of many patches that looked OK when tested only against MySQL.
|
123
122
|
|
123
|
+
h5. Set up Database Configuration
|
124
|
+
|
125
|
+
The Active Record test suite requires a custom config file: +activerecord/test/config.yml+. An example is provided in +activerecord/test/config.example.yml+ which can be copied and used as needed for your environment.
|
126
|
+
|
124
127
|
h5. SQLite3
|
125
128
|
|
126
129
|
The gem +sqlite3-ruby+ does not belong to the "db" group indeed, if you followed the instructions above you're ready. This is how you run the Active Record test suite only for SQLite3:
|
@@ -197,22 +200,22 @@ $ bundle exec rake test
|
|
197
200
|
|
198
201
|
will now run the four of them in turn.
|
199
202
|
|
200
|
-
You can also invoke +test_jdbcmysql+, +test_jdbcsqlite3+ or +test_jdbcpostgresql+. Check out the file +activerecord/RUNNING_UNIT_TESTS+ for information on running more targeted database tests, or the file +ci/
|
203
|
+
You can also invoke +test_jdbcmysql+, +test_jdbcsqlite3+ or +test_jdbcpostgresql+. Check out the file +activerecord/RUNNING_UNIT_TESTS+ for information on running more targeted database tests, or the file +ci/travis.rb+ to see the test suite that the continuous integration server runs.
|
201
204
|
|
202
205
|
h4. Older versions of Ruby on Rails
|
203
206
|
|
204
|
-
If you want to add a fix to older versions of Ruby on Rails, you'll need to set up and switch to your own local tracking branch. Here is an example to switch to the
|
207
|
+
If you want to add a fix to older versions of Ruby on Rails, you'll need to set up and switch to your own local tracking branch. Here is an example to switch to the 3-0-stable branch:
|
205
208
|
|
206
209
|
<shell>
|
207
|
-
$ git branch --track
|
208
|
-
$ git checkout
|
210
|
+
$ git branch --track 3-0-stable origin/3-0-stable
|
211
|
+
$ git checkout 3-0-stable
|
209
212
|
</shell>
|
210
213
|
|
211
214
|
TIP: You may want to "put your git branch name in your shell prompt":http://qugstart.com/blog/git-and-svn/add-colored-git-branch-name-to-your-shell-prompt/ to make it easier to remember which version of the code you're working with.
|
212
215
|
|
213
216
|
h3. Helping to Resolve Existing Issues
|
214
217
|
|
215
|
-
As a next step beyond reporting issues, you can help the core team resolve existing issues. If you check the "Everyone's Issues":https://github.com/rails/rails/issues
|
218
|
+
As a next step beyond reporting issues, you can help the core team resolve existing issues. If you check the "Everyone's Issues":https://github.com/rails/rails/issues list in GitHub Issues, you'll find lots of issues already requiring attention. What can you do for these? Quite a bit, actually:
|
216
219
|
|
217
220
|
h4. Verifying Bug Reports
|
218
221
|
|
@@ -287,7 +290,7 @@ $ cd rails
|
|
287
290
|
$ git checkout -b my_new_branch
|
288
291
|
</shell>
|
289
292
|
|
290
|
-
It doesn’t really matter what name you use, because this branch will only exist on your local computer.
|
293
|
+
It doesn’t really matter what name you use, because this branch will only exist on your local computer and your personal repository on Github. It won't be part of the Rails git repository.
|
291
294
|
|
292
295
|
h4. Write Your Code
|
293
296
|
|
@@ -333,7 +336,7 @@ It’s pretty likely that other changes to master have happened while you were w
|
|
333
336
|
|
334
337
|
<shell>
|
335
338
|
$ git checkout master
|
336
|
-
$ git pull
|
339
|
+
$ git pull --rebase
|
337
340
|
</shell>
|
338
341
|
|
339
342
|
Now reapply your patch on top of the latest changes:
|
@@ -384,13 +387,3 @@ And then...think about your next contribution!
|
|
384
387
|
h3. Rails Contributors
|
385
388
|
|
386
389
|
All contributions, either via master or docrails, get credit in "Rails Contributors":http://contributors.rubyonrails.org.
|
387
|
-
|
388
|
-
h3. Changelog
|
389
|
-
|
390
|
-
* May 12, 2011: Modified to prefer topic branches instead of master branch for users contributions by "Guillermo Iguaran":http://quillarb.org
|
391
|
-
* April 29, 2011: Reflect GitHub Issues and Pull Request workflow by "Dan Pickett":http://www.enlightsolutions.com
|
392
|
-
* April 14, 2011: Modified Contributing to the Rails Code section to add '[#ticket_number state:commited]' on patches commit messages by "Sebastian Martinez":http://wyeworks.com
|
393
|
-
* December 28, 2010: Complete revision by "Xavier Noria":credits.html#fxn
|
394
|
-
* April 6, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
|
395
|
-
* August 1, 2009: Updates/amplifications by "Mike Gunderloy":credits.html#mgunderloy
|
396
|
-
* March 2, 2009: Initial draft by "Mike Gunderloy":credits.html#mgunderloy
|
@@ -465,7 +465,7 @@ Now you should know where you are in the running trace and be able to print the
|
|
465
465
|
|
466
466
|
Use +step+ (abbreviated +s+) to continue running your program until the next logical stopping point and return control to ruby-debug.
|
467
467
|
|
468
|
-
TIP: You can also use
|
468
|
+
TIP: You can also use <tt>step<plus> n</tt> and <tt>step- n</tt> to move forward or backward +n+ steps respectively.
|
469
469
|
|
470
470
|
You may also use +next+ which is similar to step, but function or method calls that appear within the line of code are executed without stopping. As with step, you may use plus sign to move _n_ steps.
|
471
471
|
|
@@ -480,11 +480,7 @@ class Author < ActiveRecord::Base
|
|
480
480
|
|
481
481
|
def find_recent_comments(limit = 10)
|
482
482
|
debugger
|
483
|
-
@recent_comments ||= comments.
|
484
|
-
:all,
|
485
|
-
:conditions => ["created_at > ?", 1.week.ago],
|
486
|
-
:limit => limit
|
487
|
-
)
|
483
|
+
@recent_comments ||= comments.where("created_at > ?", 1.week.ago).limit(limit)
|
488
484
|
end
|
489
485
|
end
|
490
486
|
</ruby>
|
@@ -507,15 +503,15 @@ With the code stopped, take a look around:
|
|
507
503
|
|
508
504
|
<shell>
|
509
505
|
(rdb:1) list
|
510
|
-
[
|
506
|
+
[2, 9] in /PathTo/project/app/models/author.rb
|
507
|
+
2 has_one :editorial
|
508
|
+
3 has_many :comments
|
509
|
+
4
|
510
|
+
5 def find_recent_comments(limit = 10)
|
511
511
|
6 debugger
|
512
|
-
|
513
|
-
8
|
514
|
-
9
|
515
|
-
10 :limit => limit
|
516
|
-
=> 11 )
|
517
|
-
12 end
|
518
|
-
13 end
|
512
|
+
=> 7 @recent_comments ||= comments.where("created_at > ?", 1.week.ago).limit(limit)
|
513
|
+
8 end
|
514
|
+
9 end
|
519
515
|
</shell>
|
520
516
|
|
521
517
|
You are at the end of the line, but... was this line executed? You can inspect the instance variables.
|
@@ -716,10 +712,3 @@ h3. References
|
|
716
712
|
* "ruby-debug cheat sheet":http://cheat.errtheblog.com/s/rdebug/
|
717
713
|
* "Ruby on Rails Wiki: How to Configure Logging":http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging
|
718
714
|
* "Bleak House Documentation":http://blog.evanweaver.com/files/doc/fauna/bleak_house/files/README.html
|
719
|
-
|
720
|
-
h3. Changelog
|
721
|
-
|
722
|
-
* April 4, 2010: Fixed document to validate XHTML 1.0 Strict. "Jaime Iniesta":http://jaimeiniesta.com
|
723
|
-
* November 3, 2008: Accepted for publication. Added RJS, memory leaks and plugins chapters by "Emilio Tagua":credits.html#miloops
|
724
|
-
* October 19, 2008: Copy editing pass by "Mike Gunderloy":credits.html#mgunderloy
|
725
|
-
* September 16, 2008: initial version by "Emilio Tagua":credits.html#miloops
|
@@ -0,0 +1,618 @@
|
|
1
|
+
h2. Getting Started with Engines
|
2
|
+
|
3
|
+
In this guide you will learn about engines and how they can be used to provide additional functionality to their host applications through a clean and very easy-to-use interface. You will learn the following things in this guide:
|
4
|
+
|
5
|
+
* What makes an engine
|
6
|
+
* How to generate an engine
|
7
|
+
* Building features for the engine
|
8
|
+
* Hooking the engine into an application
|
9
|
+
* Overriding engine functionality in the application
|
10
|
+
|
11
|
+
endprologue.
|
12
|
+
|
13
|
+
h3. What are engines?
|
14
|
+
|
15
|
+
Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the +Rails::Application+ class inheriting from +Rails::Engine+. Therefore, engines and applications share common functionality but are at the same time two separate beasts. Engines and applications also share a common structure, as you'll see throughout this guide.
|
16
|
+
|
17
|
+
Engines are also closely related to plugins where the two share a common +lib+ directory structure and are both generated using the +rails plugin new+ generator.
|
18
|
+
|
19
|
+
The engine that will be generated for this guide will be called "blorgh". The engine will provide blogging functionality to its host applications, allowing for new posts and comments to be created. For now, you will be working solely within the engine itself and in later sections you'll see how to hook it into an application.
|
20
|
+
|
21
|
+
Engines can also be isolated from their host applications. This means that an application is able to have a path provided by a routing helper such as +posts_path+ and use an engine also that provides a path also called +posts_path+, and the two would not clash. Along with this, controllers, models and table names are also namespaced. You'll see how to do this later in this guide.
|
22
|
+
|
23
|
+
To see demonstrations of other engines, check out "Devise":https://github.com/plataformatec/devise, an engine that provides authentication for its parent applications, or "Forem":https://github.com/radar/forem, an engine that provides forum functionality.
|
24
|
+
|
25
|
+
Finally, engines would not have be possible without the work of James Adam, Piotr Sarnacki, the Rails Core Team, and a number of other people. If you ever meet them, don't forget to say thanks!
|
26
|
+
|
27
|
+
h3. Generating an engine
|
28
|
+
|
29
|
+
To generate an engine with Rails 3.1, you will need to run the plugin generator and pass it the +--mountable+ option. To generate the beginnings of the "blorgh" engine you will need to run this command in a terminal:
|
30
|
+
|
31
|
+
<shell>
|
32
|
+
$ rails plugin new blorgh --mountable
|
33
|
+
</shell>
|
34
|
+
|
35
|
+
The +--mountable+ option tells the plugin generator that you want to create an engine (which is a mountable plugin, hence the option name), creating the basic directory structure of an engine by providing things such as the foundations of an +app+ folder, as well a +config/routes.rb+ file. This generator also provides a file at +lib/blorgh/engine.rb+ which is identical in function to an application's +config/application.rb+ file.
|
36
|
+
|
37
|
+
h4. Inside an engine
|
38
|
+
|
39
|
+
h5. Critical files
|
40
|
+
|
41
|
+
At the root of the engine's directory, lives a +blorgh.gemspec+ file. When you include the engine into the application later on, you will do so with this line in a Rails application's +Gemfile+:
|
42
|
+
|
43
|
+
<ruby>
|
44
|
+
gem 'blorgh', :path => "vendor/engines/blorgh"
|
45
|
+
</ruby>
|
46
|
+
|
47
|
+
By specifying it as a gem within the +Gemfile+, Bundler will load it as such, parsing this +blorgh.gemspec+ file and requiring a file within the +lib+ directory called +lib/blorgh.rb+. This file requires the +blorgh/engine.rb+ file (located at +lib/blorgh/engine.rb+) and defines a base module called +Blorgh+.
|
48
|
+
|
49
|
+
<ruby>
|
50
|
+
require "blorgh/engine"
|
51
|
+
|
52
|
+
module Blorgh
|
53
|
+
end
|
54
|
+
</ruby>
|
55
|
+
|
56
|
+
Within +lib/blorgh/engine.rb+ is the base class for the engine:
|
57
|
+
|
58
|
+
<ruby>
|
59
|
+
module Blorgh
|
60
|
+
class Engine < Rails::Engine
|
61
|
+
isolate_namespace Blorgh
|
62
|
+
end
|
63
|
+
end
|
64
|
+
</ruby>
|
65
|
+
|
66
|
+
By inheriting from the +Rails::Engine+ class, this engine gains all the functionality it needs, such as being able to serve requests to its controllers.
|
67
|
+
|
68
|
+
The +isolate_namespace+ method here deserves special notice. This call is responsible for isolating the controllers, models, routes and other things into their own namespace. Without this, there is a possibility that the engine's components could "leak" into the application, causing unwanted disruption. It is recommended that this line be left within this file.
|
69
|
+
|
70
|
+
h5. +app+ directory
|
71
|
+
|
72
|
+
Inside the +app+ directory there lives the standard +assets+, +controllers+, +helpers+, +mailers+, +models+ and +views+ directories that you should be familiar with from an application. The +helpers+, +mailers+ and +models+ directories are empty and so aren't described in this section. We'll look more into models in a future section.
|
73
|
+
|
74
|
+
Within the +app/assets+ directory, there is the +images+, +javascripts+ and +stylesheets+ directories which, again, you should be familiar with due to their similarities of an application. One difference here however is that each directory contains a sub-directory with the engine name. Because this engine is going to be namespaced, its assets should be too.
|
75
|
+
|
76
|
+
Within the +app/controllers+ directory there is a +blorgh+ directory and inside that a file called +application_controller.rb+. This file will provide any common functionality for the controllers of the engine. The +blorgh+ directory is where the other controllers for the engine will go. By placing them within this namespaced directory, you prevent them from possibly clashing with identically-named controllers within other engines or even within the application.
|
77
|
+
|
78
|
+
Lastly, the +app/views+ directory contains a +layouts+ folder which contains file at +blorgh/application.html.erb+ which allows you to specify a layout for the engine. If this engine is to be used as a stand-alone engine, then you would add any customization to its layout in this file, rather than the applications +app/views/layouts/application.html.erb+ file.
|
79
|
+
|
80
|
+
h5. +script+ directory
|
81
|
+
|
82
|
+
This directory contains one file, +script/rails+, which allows you to use the +rails+ sub-commands and generators just like you would within an application. This means that you will very easily be able to generate new controllers and models for this engine.
|
83
|
+
|
84
|
+
h5. +test+ directory
|
85
|
+
|
86
|
+
The +test+ directory is where tests for the engine will go. To test the engine, there is a cut-down version of a Rails application embedded within it at +test/dummy+. This application will mount the engine in the +test/dummy/config/routes.rb+ file:
|
87
|
+
|
88
|
+
<ruby>
|
89
|
+
Rails.application.routes.draw do
|
90
|
+
|
91
|
+
mount Blorgh::Engine => "/blorgh"
|
92
|
+
end
|
93
|
+
</ruby>
|
94
|
+
|
95
|
+
This line mounts the engine at the path of +/blorgh+, which will make it accessible through the application only at that path. We will look more into mounting an engine after some features have been developed.
|
96
|
+
|
97
|
+
Also in the test directory is the +test/integration+ directory, where integration tests for the engine should be placed.
|
98
|
+
|
99
|
+
h3. Providing engine functionality
|
100
|
+
|
101
|
+
The engine that this guide covers will provide posting and commenting functionality and follows a similar thread to the "Getting Started Guide":getting-started.html, with some new twists.
|
102
|
+
|
103
|
+
h4. Generating a post resource
|
104
|
+
|
105
|
+
The first thing to generate for a blog engine is the +Post+ model and related controller. To quickly generate this, you can use the Rails scaffold generator.
|
106
|
+
|
107
|
+
<shell>
|
108
|
+
$ rails generate scaffold post title:string text:text
|
109
|
+
</shell>
|
110
|
+
|
111
|
+
This command will output this information:
|
112
|
+
|
113
|
+
<shell>
|
114
|
+
invoke active_record
|
115
|
+
create db/migrate/[timestamp]_create_blorgh_posts.rb
|
116
|
+
create app/models/blorgh/post.rb
|
117
|
+
invoke test_unit
|
118
|
+
create test/unit/blorgh/post_test.rb
|
119
|
+
create test/fixtures/blorgh/posts.yml
|
120
|
+
route resources :posts
|
121
|
+
invoke scaffold_controller
|
122
|
+
create app/controllers/blorgh/posts_controller.rb
|
123
|
+
invoke erb
|
124
|
+
create app/views/blorgh/posts
|
125
|
+
create app/views/blorgh/posts/index.html.erb
|
126
|
+
create app/views/blorgh/posts/edit.html.erb
|
127
|
+
create app/views/blorgh/posts/show.html.erb
|
128
|
+
create app/views/blorgh/posts/new.html.erb
|
129
|
+
create app/views/blorgh/posts/_form.html.erb
|
130
|
+
invoke test_unit
|
131
|
+
create test/functional/blorgh/posts_controller_test.rb
|
132
|
+
invoke helper
|
133
|
+
create app/helpers/blorgh/posts_helper.rb
|
134
|
+
invoke test_unit
|
135
|
+
create test/unit/helpers/blorgh/posts_helper_test.rb
|
136
|
+
invoke assets
|
137
|
+
invoke js
|
138
|
+
create app/assets/javascripts/blorgh/posts.js
|
139
|
+
invoke css
|
140
|
+
create app/assets/stylesheets/blorgh/posts.css
|
141
|
+
invoke css
|
142
|
+
create app/assets/stylesheets/scaffold.css
|
143
|
+
</shell>
|
144
|
+
|
145
|
+
The first thing that the scaffold generator does is invoke the +active_record+ generator, which generates a migration and a model for the resource. Note here, however, that the migration is called +create_blorgh_posts+ rather than the usual +create_posts+. This is due to the +isolate_namespace+ method called in the +Blorgh::Engine+ class's definition. The model here is also namespaced, being placed at +app/models/blorgh/post.rb+ rather than +app/models/post.rb+.
|
146
|
+
|
147
|
+
Next, the +test_unit+ generator is invoked for this model, generating a unit test at +test/unit/blorgh/post_test.rb+ (rather than +test/unit/post_test.rb+) and a fixture at +test/fixtures/blorgh/posts.yml+ (rather than +test/fixtures/posts.yml+).
|
148
|
+
|
149
|
+
After that, a line for the resource is inserted into the +config/routes.rb+ file for the engine. This line is simply +resources :posts+, turning the +config/routes.rb+ file into this:
|
150
|
+
|
151
|
+
<ruby>
|
152
|
+
Blorgh::Engine.routes.draw do
|
153
|
+
resources :posts
|
154
|
+
|
155
|
+
end
|
156
|
+
</ruby>
|
157
|
+
|
158
|
+
Note here that the routes are drawn upon the +Blorgh::Engine+ object rather than the +YourApp::Application+ class. This is so that the engine routes are confined to the engine itself and can be mounted at a specific point as shown in the "test directory":#test-directory section.
|
159
|
+
|
160
|
+
Next, the +scaffold_controller+ generator is invoked, generating a controlled called +Blorgh::PostsController+ (at +app/controllers/blorgh/posts_controller.rb+) and its related views at +app/views/blorgh/posts+. This generator also generates a functional test for the controller (+test/functional/blorgh/posts_controller_test.rb+) and a helper (+app/helpers/blorgh/posts_controller.rb+).
|
161
|
+
|
162
|
+
Everything this generator has generated is neatly namespaced. The controller's class is defined within the +Blorgh+ module:
|
163
|
+
|
164
|
+
<ruby>
|
165
|
+
module Blorgh
|
166
|
+
class PostsController < ApplicationController
|
167
|
+
...
|
168
|
+
end
|
169
|
+
end
|
170
|
+
</ruby>
|
171
|
+
|
172
|
+
NOTE: The +ApplicationController+ class being inherited from here is the +Blorgh::ApplicationController+, not an application's +ApplicationController+.
|
173
|
+
|
174
|
+
The helper is also namespaced:
|
175
|
+
|
176
|
+
<ruby>
|
177
|
+
module Blorgh
|
178
|
+
class PostsHelper
|
179
|
+
...
|
180
|
+
end
|
181
|
+
end
|
182
|
+
</ruby>
|
183
|
+
|
184
|
+
This helps prevent conflicts with any other engine or application that may have a post resource also.
|
185
|
+
|
186
|
+
Finally, two files that are the assets for this resource are generated, +app/assets/javascripts/blorgh/posts.js+ and +app/assets/javascripts/blorgh/posts.css+. You'll see how to use these a little later.
|
187
|
+
|
188
|
+
By default, the scaffold styling is not applied to the engine as the engine's layout file, +app/views/blorgh/application.html.erb+ doesn't load it. To make this apply, insert this line into the +<head>+ tag of this layout:
|
189
|
+
|
190
|
+
<erb>
|
191
|
+
<%= stylesheet_link_tag "scaffold" %>
|
192
|
+
</erb>
|
193
|
+
|
194
|
+
You can see what the engine has so far by running +rake db:migrate+ at the root of our engine to run the migration generated by the scaffold generator, and then running +rails server+. When you open +http://localhost:3000/blorgh/posts+ you will see the default scaffold that has been generated.
|
195
|
+
|
196
|
+
!images/engines_scaffold.png(Blank engine scaffold)!
|
197
|
+
|
198
|
+
Click around! You've just generated your first engine's first functions.
|
199
|
+
|
200
|
+
If you'd rather play around in the console, +rails console+ will also work just like a Rails application. Remember: the +Post+ model is namespaced, so to reference it you must call it as +Blorgh::Post+.
|
201
|
+
|
202
|
+
<ruby>
|
203
|
+
>> Blorgh::Post.find(1)
|
204
|
+
=> #<Blorgh::Post id: 1 ...>
|
205
|
+
</ruby>
|
206
|
+
|
207
|
+
One final thing is that the +posts+ resource for this engine should be the root of the engine. Whenever someone goes to the root path where the engine is mounted, they should be shown a list of posts. This can be made to happen if this line is inserted into the +config/routes.rb+ file inside the engine:
|
208
|
+
|
209
|
+
<ruby>
|
210
|
+
root :to => "posts#index"
|
211
|
+
</ruby>
|
212
|
+
|
213
|
+
Now people will only need to go to the root of the engine to see all the posts, rather than visiting +/posts+.
|
214
|
+
|
215
|
+
h4. Generating a comments resource
|
216
|
+
|
217
|
+
Now that the engine has the ability to create new blog posts, it only makes sense to add commenting functionality as well. To do get this, you'll need to generate a comment model, a comment controller and then modify the posts scaffold to display comments and allow people to create new ones.
|
218
|
+
|
219
|
+
Run the model generator and tell it to generate a +Comment+ model, with the related table having two columns: a +post_id+ integer and +text+ text column.
|
220
|
+
|
221
|
+
<shell>
|
222
|
+
$ rails generate model Comment post_id:integer text:text
|
223
|
+
</shell>
|
224
|
+
|
225
|
+
This will output the following:
|
226
|
+
|
227
|
+
<shell>
|
228
|
+
invoke active_record
|
229
|
+
create db/migrate/[timestamp]_create_blorgh_comments.rb
|
230
|
+
create app/models/blorgh/comment.rb
|
231
|
+
invoke test_unit
|
232
|
+
create test/unit/blorgh/comment_test.rb
|
233
|
+
create test/fixtures/blorgh/comments.yml
|
234
|
+
</shell>
|
235
|
+
|
236
|
+
This generator call will generate just the necessary model files it needs, namespacing the files under a +blorgh+ directory and creating a model class called +Blorgh::Comment+.
|
237
|
+
|
238
|
+
To show the comments on a post, edit +app/views/posts/show.html.erb+ and add this line before the "Edit" link:
|
239
|
+
|
240
|
+
<erb>
|
241
|
+
<h3>Comments</h3>
|
242
|
+
<%= render @post.comments %>
|
243
|
+
</erb>
|
244
|
+
|
245
|
+
This line will require there to be a +has_many+ association for comments defined on the +Blorgh::Post+ model, which there isn't right now. To define one, open +app/models/blorgh/post.rb+ and add this line into the model:
|
246
|
+
|
247
|
+
<ruby>
|
248
|
+
has_many :comments
|
249
|
+
</ruby>
|
250
|
+
|
251
|
+
Turning the model into this:
|
252
|
+
|
253
|
+
<ruby>
|
254
|
+
module Blorgh
|
255
|
+
class Post < ActiveRecord::Base
|
256
|
+
has_many :comments
|
257
|
+
end
|
258
|
+
end
|
259
|
+
</ruby>
|
260
|
+
|
261
|
+
Because the +has_many+ is defined inside a class that is inside the +Blorgh+ module, Rails will know that you want to use the +Blorgh::Comment+ model for these objects.
|
262
|
+
|
263
|
+
Next, there needs to be a form so that comments can be created on a post. To add this, put this line underneath the call to +render @post.comments+ in +app/views/blorgh/posts/show.html.erb+:
|
264
|
+
|
265
|
+
<erb>
|
266
|
+
<%= render "blorgh/comments/form" %>
|
267
|
+
</erb>
|
268
|
+
|
269
|
+
Next, the partial that this line will render needs to exist. Create a new directory at +app/views/blorgh/comments+ and in it a new file called +_form.html.erb+ which has this content to create the required partial:
|
270
|
+
|
271
|
+
<erb>
|
272
|
+
<h3>New comment</h3>
|
273
|
+
<%= form_for [@post, @post.comments.build] do |f| %>
|
274
|
+
<p>
|
275
|
+
<%= f.label :text %><br />
|
276
|
+
<%= f.text_area :text %>
|
277
|
+
</p>
|
278
|
+
<%= f.submit %>
|
279
|
+
<% end %>
|
280
|
+
</erb>
|
281
|
+
|
282
|
+
This form, when submitted, is going to attempt to post to a route of +posts/:post_id/comments+ within the engine. This route doesn't exist at the moment, but can be created by changing the +resources :posts+ line inside +config/routes.rb+ into these lines:
|
283
|
+
|
284
|
+
<ruby>
|
285
|
+
resources :posts do
|
286
|
+
resources :comments
|
287
|
+
end
|
288
|
+
</ruby>
|
289
|
+
|
290
|
+
The route now will exist, but the controller that this route goes to does not. To create it, run this command:
|
291
|
+
|
292
|
+
<shell>
|
293
|
+
$ rails g controller comments
|
294
|
+
</shell>
|
295
|
+
|
296
|
+
This will generate the following things:
|
297
|
+
|
298
|
+
<shell>
|
299
|
+
create app/controllers/blorgh/comments_controller.rb
|
300
|
+
invoke erb
|
301
|
+
exist app/views/blorgh/comments
|
302
|
+
invoke test_unit
|
303
|
+
create test/functional/blorgh/comments_controller_test.rb
|
304
|
+
invoke helper
|
305
|
+
create app/helpers/blorgh/comments_helper.rb
|
306
|
+
invoke test_unit
|
307
|
+
create test/unit/helpers/blorgh/comments_helper_test.rb
|
308
|
+
invoke assets
|
309
|
+
invoke js
|
310
|
+
create app/assets/javascripts/blorgh/comments.js
|
311
|
+
invoke css
|
312
|
+
create app/assets/stylesheets/blorgh/comments.css
|
313
|
+
</shell>
|
314
|
+
|
315
|
+
The form will be making a +POST+ request to +/posts/:post_id/comments+, which will correspond with the +create+ action in +Blorgh::CommentsController+. This action needs to be created and can be done by putting the following lines inside the class definition in +app/controllers/blorgh/comments_controller.rb+:
|
316
|
+
|
317
|
+
<ruby>
|
318
|
+
def create
|
319
|
+
@post = Post.find(params[:post_id])
|
320
|
+
@comment = @post.comments.build(params[:comment])
|
321
|
+
flash[:notice] = "Comment has been created!"
|
322
|
+
redirect_to post_path
|
323
|
+
end
|
324
|
+
</ruby>
|
325
|
+
|
326
|
+
This is the final part required to get the new comment form working. Displaying the comments however, is not quite right yet. If you were to create a comment right now you would see this error:
|
327
|
+
|
328
|
+
<text>
|
329
|
+
Missing partial blorgh/comments/comment with {:handlers=>[:erb, :builder], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
|
330
|
+
* "/Users/ryan/Sites/side_projects/blorgh/test/dummy/app/views"
|
331
|
+
* "/Users/ryan/Sites/side_projects/blorgh/app/views"
|
332
|
+
</text>
|
333
|
+
|
334
|
+
The engine is unable to find the partial required for rendering the comments. Rails has looked firstly in the application's (+test/dummy+) +app/views+ directory and then in the engine's +app/views+ directory. When it can't find it, it will throw this error. The engine knows to look for +blorgh/comments/comment+ because the model object it is receiving is from the +Blorgh::Comment+ class.
|
335
|
+
|
336
|
+
This partial will be responsible for rendering just the comment text, for now. Create a new file at +app/views/blorgh/comments/_comment.html.erb+ and put this line inside it:
|
337
|
+
|
338
|
+
<erb>
|
339
|
+
<%= comment_counter + 1 %>. <%= comment.text %>
|
340
|
+
</erb>
|
341
|
+
|
342
|
+
The +comment_counter+ local variable is given to us by the +<%= render @post.comments %>+ call, as it will define this automatically and increment the counter as it iterates through each comment. It's used in this example to display a small number next to each comment when it's created.
|
343
|
+
|
344
|
+
That completes the comment function of the blogging engine. Now it's time to use it within an application.
|
345
|
+
|
346
|
+
h3. Hooking into an application
|
347
|
+
|
348
|
+
Using an engine within an application is very easy. This section covers how to mount the engine into an application and the initial setup required for it, as well as linking the engine to a +User+ class provided by the application to provide ownership for posts and comments within the engine.
|
349
|
+
|
350
|
+
h4. Mounting the engine
|
351
|
+
|
352
|
+
First, the engine needs to be specified inside the application's +Gemfile+. If there isn't an application handy to test this out in, generate one using the +rails new+ command outside of the engine directory like this:
|
353
|
+
|
354
|
+
<shell>
|
355
|
+
$ rails new unicorn
|
356
|
+
</shell>
|
357
|
+
|
358
|
+
Usually, specifying the engine inside the Gemfile would be done by specifying it as a normal, everyday gem.
|
359
|
+
|
360
|
+
<ruby>
|
361
|
+
gem 'devise'
|
362
|
+
</ruby>
|
363
|
+
|
364
|
+
Because the +blorgh+ engine is still under development, it will need to have a +:path+ option for its +Gemfile+ specification:
|
365
|
+
|
366
|
+
<ruby>
|
367
|
+
gem 'blorgh', :path => "/path/to/blorgh"
|
368
|
+
</ruby>
|
369
|
+
|
370
|
+
If the whole +blorgh+ engine directory is copied to +vendor/engines/blorgh+ then it could be specified in the +Gemfile+ like this:
|
371
|
+
|
372
|
+
<ruby>
|
373
|
+
gem 'blorgh', :path => "vendor/engines/blorgh"
|
374
|
+
</ruby>
|
375
|
+
|
376
|
+
As described earlier, by placing the gem in the +Gemfile+ it will be loaded when Rails is loaded, as it will first require +lib/blorgh.rb+ in the engine and then +lib/blorgh/engine.rb+, which is the file that defines the major pieces of functionality for the engine.
|
377
|
+
|
378
|
+
To make the engine's functionality accessible from within an application, it needs to be mounted in that application's +config/routes.rb+ file:
|
379
|
+
|
380
|
+
<ruby>
|
381
|
+
mount Blorgh::Engine, :at => "blog"
|
382
|
+
</ruby>
|
383
|
+
|
384
|
+
This line will mount the engine at +blog+ in the application. Making it accessible at +http://localhost:3000/blog+ when the application runs with +rails s+.
|
385
|
+
|
386
|
+
NOTE: Other engines, such as Devise, handle this a little differently by making you specify custom helpers such as +devise_for+ in the routes. These helpers do exactly the same thing, mounting pieces of the engines's functionality at a pre-defined path which may be customizable.
|
387
|
+
|
388
|
+
h4. Engine setup
|
389
|
+
|
390
|
+
The engine contains migrations for the +blorgh_posts+ and +blorgh_comments+ table which need to be created in the application's database so that the engine's models can query them correctly. To copy these migrations into the application use this command:
|
391
|
+
|
392
|
+
<shell>
|
393
|
+
$ rake blorgh:install:migrations
|
394
|
+
</shell>
|
395
|
+
|
396
|
+
This command, when run for the first time will copy over all the migrations from the engine. When run the next time, it will only copy over migrations that haven't been copied over already. The first run for this command will output something such as this:
|
397
|
+
|
398
|
+
<shell>
|
399
|
+
Copied migration [timestamp_1]_create_blorgh_posts.rb from blorgh
|
400
|
+
Copied migration [timestamp_2]_create_blorgh_comments.rb from blorgh
|
401
|
+
</shell>
|
402
|
+
|
403
|
+
The first timestamp (+\[timestamp_1\]+) will be the current time and the second timestamp (+\[timestamp_2\]+) will be the current time plus a second. The reason for this is so that the migrations for the engine are run after any existing migrations in the application.
|
404
|
+
|
405
|
+
To run these migrations within the context of the application, simply run +rake db:migrate+. When accessing the engine through +http://localhost:3000/blog+, the posts will be empty. This is because the table created inside the application is different from the one created within the engine. Go ahead, play around with the newly mounted engine. You'll find that it's the same as when it was only an engine.
|
406
|
+
|
407
|
+
h4. Using a class provided by the application
|
408
|
+
|
409
|
+
When an engine is created, it may want to use specific classes from an application to provide links between the pieces of the engine and the pieces of the application. In the case of the +blorgh+ engine, making posts and comments have authors would make a lot of sense.
|
410
|
+
|
411
|
+
Usually, an application would have a +User+ class that would provide the objects that would represent the posts' and comments' authors, but there could be a case where the application calls this class something different, such as +Person+. It's because of this reason that the engine should not hardcode the associations to be exactly for a +User+ class, but should allow for some flexibility around what the class is called.
|
412
|
+
|
413
|
+
To keep it simple in this case, the application will have a class called +User+ which will represent the users of the application. It can be generated using this command:
|
414
|
+
|
415
|
+
<shell>
|
416
|
+
rails g model user name:string
|
417
|
+
</shell>
|
418
|
+
|
419
|
+
The +rake db:migrate+ command needs to be run here to ensure that our application has the +users+ table for future use.
|
420
|
+
|
421
|
+
Also to keep it simple, the posts form will have a new text field called +author_name_+ where users can elect to put their name. The engine will then take this name and create a new +User+ object from it or find one that already has that name, and then associate the post with it.
|
422
|
+
|
423
|
+
First, the +author_name+ text field needs to be added to the +app/views/blorgh/posts/_form.html.erb+ partial inside the engine. This can be added above the +title+ field with this code:
|
424
|
+
|
425
|
+
<erb>
|
426
|
+
<div class="field">
|
427
|
+
<%= f.label :author_name %><br />
|
428
|
+
<%= f.text_field :author_name %>
|
429
|
+
</div>
|
430
|
+
</erb>
|
431
|
+
|
432
|
+
The +Blorgh::Post+ model should then have some code to convert the +author_name+ field into an actual +User+ object and associate it as that post's +author+ before the post is saved. It will also need to have an +attr_accessor+ setup for this field so that the setter and getter methods are defined for it.
|
433
|
+
|
434
|
+
To do all this, you'll need to add the +attr_accessor+ for +author_name+, the association for the author and the +before_save+ call into +app/models/blorgh/post.rb+. The +author+ association will be hard-coded to the +User+ class for the time being.
|
435
|
+
|
436
|
+
<ruby>
|
437
|
+
attr_accessor :author_name
|
438
|
+
belongs_to :author, :class_name => "User"
|
439
|
+
|
440
|
+
before_save :set_author
|
441
|
+
|
442
|
+
private
|
443
|
+
def set_author
|
444
|
+
self.author = User.find_or_create_by_name(author_name)
|
445
|
+
end
|
446
|
+
</ruby>
|
447
|
+
|
448
|
+
By defining that the +author+ association's object is represented by the +User+ class a link is established between the engine and the application. There needs to be a way of associating the records in the +blorgh_posts+ table with the records in the +users+ table. Because the association is called +author+, there should be an +author_id+ column added to the +blorgh_posts+ table.
|
449
|
+
|
450
|
+
To generate this new column, run this command within the engine:
|
451
|
+
|
452
|
+
<shell>
|
453
|
+
$ rails g migration add_author_id_to_blorgh_posts author_id:integer
|
454
|
+
</shell>
|
455
|
+
|
456
|
+
NOTE: Due to the migration's name and the column specification after it, Rails will automatically know that you want to add a column to a specific table and write that into the migration for you. You don't need to tell it any more than this.
|
457
|
+
|
458
|
+
This migration will need to be run on the application. To do that, it must first be copied using this command:
|
459
|
+
|
460
|
+
<shell>
|
461
|
+
$ rake blorgh:install:migrations
|
462
|
+
</shell>
|
463
|
+
|
464
|
+
Notice here that only _one_ migration was copied over here. This is because the first two migrations were copied over the first time this command was run.
|
465
|
+
|
466
|
+
<shell>
|
467
|
+
NOTE: Migration [timestamp]_create_blorgh_posts.rb from blorgh has been skipped. Migration with the same name already exists.
|
468
|
+
NOTE: Migration [timestamp]_create_blorgh_comments.rb from blorgh has been skipped. Migration with the same name already exists.
|
469
|
+
Copied migration [timestamp]_add_author_id_to_blorgh_posts.rb from blorgh
|
470
|
+
</shell>
|
471
|
+
|
472
|
+
Run this migration using this command:
|
473
|
+
|
474
|
+
<shell>
|
475
|
+
$ rake db:migrate
|
476
|
+
</shell>
|
477
|
+
|
478
|
+
Now with all the pieces in place, an action will take place that will associate an author -- represented by a record in the +users+ table -- with a post, represented by the +blorgh_posts+ table from the engine.
|
479
|
+
|
480
|
+
Finally, the author's name should be displayed on the post's page. Add this code above the "Title" output inside +app/views/blorgh/posts/show.html.erb+:
|
481
|
+
|
482
|
+
<erb>
|
483
|
+
<p>
|
484
|
+
<b>Author:</b>
|
485
|
+
<%= @post.author %>
|
486
|
+
</p>
|
487
|
+
</erb>
|
488
|
+
|
489
|
+
WARNING: For posts created previously, this will break the +show+ page for them. We recommend deleting these posts and starting again, or manually assigning an author using +rails c+.
|
490
|
+
|
491
|
+
By outputting +@post.author+ using the +<%=+ tag the +to_s+ method will be called on the object. By default, this will look quite ugly:
|
492
|
+
|
493
|
+
<text>
|
494
|
+
#<User:0x00000100ccb3b0>
|
495
|
+
</text>
|
496
|
+
|
497
|
+
This is undesirable and it would be much better to have the user's name there. To do this, add a +to_s+ method to the +User+ class within the application:
|
498
|
+
|
499
|
+
<ruby>
|
500
|
+
def to_s
|
501
|
+
name
|
502
|
+
end
|
503
|
+
</ruby>
|
504
|
+
|
505
|
+
Now instead of the ugly Ruby object output the author's name will be displayed.
|
506
|
+
|
507
|
+
h4. Configuring an engine
|
508
|
+
|
509
|
+
This section covers firstly how you can make the +user_class+ setting of the Blorgh engine configurable, followed by general configuration tips for the engine.
|
510
|
+
|
511
|
+
h5. Setting configuration settings in the application
|
512
|
+
|
513
|
+
The next step is to make the class that represents a +User+ in the application customizable for the engine. This is because, as explained before, that class may not always be +User+. To make this customizable, the engine will have a configuration setting called +user_class+ that will be used to specify what the class representing users is inside the application.
|
514
|
+
|
515
|
+
To define this configuration setting, you should use a +mattr_accessor+ inside the +Blorgh+ module for the engine, located at +lib/blorgh.rb+ inside the engine. Inside this module, put this line:
|
516
|
+
|
517
|
+
<ruby>
|
518
|
+
mattr_accessor :user_class
|
519
|
+
</ruby>
|
520
|
+
|
521
|
+
This method works like its brothers +attr_accessor+ and +cattr_accessor+, but provides a setter and getter method on the module with the specified name. To use it, it must be referenced using +Blorgh.user_class+.
|
522
|
+
|
523
|
+
The next step is switching the +Blorgh::Post+ model over to this new setting. For the +belongs_to+ association inside this model (+app/models/blorgh/post.rb+), it will now become this:
|
524
|
+
|
525
|
+
<ruby>
|
526
|
+
belongs_to :author, :class_name => Blorgh.user_class
|
527
|
+
</ruby>
|
528
|
+
|
529
|
+
The +set_author+ method also located in this class should also use this class:
|
530
|
+
|
531
|
+
<ruby>
|
532
|
+
self.author = Blorgh.user_class.constantize.find_or_create_by_name(author_name)
|
533
|
+
</ruby>
|
534
|
+
|
535
|
+
To set this configuration setting within the application, an initializer should be used. By using an initializer, the configuration will be set up before the application starts and makes references to the classes of the engine which may depend on this configuration setting existing.
|
536
|
+
|
537
|
+
Create a new initializer at +config/initializers/blorgh.rb+ inside the application where the +blorgh+ engine is installed and put this content in it:
|
538
|
+
|
539
|
+
<ruby>
|
540
|
+
Blorgh.user_class = "User"
|
541
|
+
</ruby>
|
542
|
+
|
543
|
+
WARNING: It's very important here to use the +String+ version of the class, rather than the class itself. If you were to use the class, Rails would attempt to load that class and then reference the related table, which could lead to problems if the table wasn't already existing. Therefore, a +String+ should be used and then converted to a class using +constantize+ in the engine later on.
|
544
|
+
|
545
|
+
Go ahead and try to create a new post. You will see that it works exactly in the same way as before, except this time the engine is using the configuration setting in +config/initializers/blorgh.rb+ to learn what the class is.
|
546
|
+
|
547
|
+
There are now no strict dependencies on what the class is, only what the class's API must be. The engine simply requires this class to define a +find_or_create_by_name+ method which returns an object of that class to be associated with a post when it's created.
|
548
|
+
|
549
|
+
h5. General engine configuration
|
550
|
+
|
551
|
+
Within an engine, there may come a time where you wish to use things such as initializers, internationalization or other configuration options. The great news is that these things are entirely possible because a Rails engine shares much the same functionality as a Rails application. In fact, a Rails application's functionality is actually a superset of what is provided by engines!
|
552
|
+
|
553
|
+
If you wish to use initializers (code that should run before the engine is loaded), the best place for them is the +config/initializers+ folder. This directory's functionality is explained in the "Initializers section":http://guides.rubyonrails.org/configuring.html#initializers of the Configuring guide.
|
554
|
+
|
555
|
+
For locales, simply place the locale files in the +config/locales+ directory, just like you would in an application.
|
556
|
+
|
557
|
+
h3. Extending engine functionality
|
558
|
+
|
559
|
+
This section looks at overriding or adding functionality to the views, controllers and models provided by an engine.
|
560
|
+
|
561
|
+
h4. Overriding views
|
562
|
+
|
563
|
+
When Rails looks for a view to render, it will first look in the +app/views+ directory of the application. If it cannot find the view there, then it will check in the +app/views+ directories of all engines which have this directory.
|
564
|
+
|
565
|
+
In the +blorgh+ engine, there is a currently a file at +app/views/blorgh/posts/index.html.erb+. When the engine is asked to render the view for +Blorgh::PostsController+'s +index+ action, it will first see if it can find it at +app/views/blorgh/posts/index.html.erb+ within the application and then if it cannot it will look inside the engine.
|
566
|
+
|
567
|
+
By overriding this view in the application, by simply creating a new file at +app/views/blorgh/posts/index.html.erb+, you can completely change what this view would normally output.
|
568
|
+
|
569
|
+
Try this now by creating a new file at +app/views/blorgh/posts/index.html.erb+ and put this content in it:
|
570
|
+
|
571
|
+
<erb>
|
572
|
+
<h1>Posts</h1>
|
573
|
+
<%= link_to "New Post", new_post_path %>
|
574
|
+
<% @posts.each do |post| %>
|
575
|
+
<h2><%= post.title %></h2>
|
576
|
+
<small>By <%= post.author %></small>
|
577
|
+
<%= simple_format(post.text) %>
|
578
|
+
<hr>
|
579
|
+
<% end %>
|
580
|
+
</erb>
|
581
|
+
|
582
|
+
Rather than looking like the default scaffold, the page will now look like this:
|
583
|
+
|
584
|
+
!images/engines_post_override.png(Engine scaffold overriden)!
|
585
|
+
|
586
|
+
h4. Controllers
|
587
|
+
|
588
|
+
TODO: Explain how to extend a controller.
|
589
|
+
IDEA: I like Devise's +devise :controllers => { "sessions" => "sessions" }+ idea. Perhaps we could incorporate that into the guide?
|
590
|
+
|
591
|
+
h4. Models
|
592
|
+
|
593
|
+
TODO: Explain how to extend models provided by an engine.
|
594
|
+
|
595
|
+
h4. Routes
|
596
|
+
|
597
|
+
Within the application, you may wish to link to some area within the engine. Due to the fact that the engine's routes are isolated (by the +isolate_namespace+ call within the +lib/blorgh/engine.rb+ file), you will need to prefix these routes with the engine name. This means rather than having something such as:
|
598
|
+
|
599
|
+
<erb>
|
600
|
+
<%= link_to "Blog posts", posts_path %>
|
601
|
+
</erb>
|
602
|
+
|
603
|
+
It needs to be written as:
|
604
|
+
|
605
|
+
<erb>
|
606
|
+
<%= link_to "Blog posts", blorgh.posts_path %>
|
607
|
+
</erb>
|
608
|
+
|
609
|
+
This allows for the engine _and_ the application to both have a +posts_path+ routing helper and to not interfere with each other. You may also reference another engine's routes from inside an engine using this same syntax.
|
610
|
+
|
611
|
+
If you wish to reference the application inside the engine in a similar way, use the +main_app+ helper:
|
612
|
+
|
613
|
+
<erb>
|
614
|
+
<%= link_to "Home", main_app.root_path %>
|
615
|
+
</erb>
|
616
|
+
|
617
|
+
TODO: Mention how to use assets within an engine?
|
618
|
+
TODO: Mention how to depend on external gems, like RedCarpet.
|