railties 3.2.22.5 → 4.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +186 -78
- data/README.rdoc +4 -2
- data/lib/rails.rb +11 -44
- data/lib/rails/all.rb +0 -1
- data/lib/rails/app_rails_loader.rb +37 -0
- data/lib/rails/application.rb +176 -124
- data/lib/rails/application/bootstrap.rb +29 -23
- data/lib/rails/application/configuration.rb +54 -48
- data/lib/rails/application/finisher.rb +8 -12
- data/lib/rails/application/routes_reloader.rb +2 -2
- data/lib/rails/backtrace_cleaner.rb +1 -16
- data/lib/rails/cli.rb +2 -2
- data/lib/rails/code_statistics.rb +30 -45
- data/lib/rails/code_statistics_calculator.rb +79 -0
- data/lib/rails/commands.rb +44 -37
- data/lib/rails/commands/application.rb +14 -9
- data/lib/rails/commands/console.rb +77 -33
- data/lib/rails/commands/dbconsole.rb +115 -64
- data/lib/rails/commands/destroy.rb +2 -3
- data/lib/rails/commands/generate.rb +2 -3
- data/lib/rails/commands/plugin_new.rb +0 -2
- data/lib/rails/commands/runner.rb +4 -5
- data/lib/rails/commands/server.rb +28 -9
- data/lib/rails/commands/update.rb +1 -1
- data/lib/rails/configuration.rb +35 -8
- data/lib/rails/console/app.rb +0 -4
- data/lib/rails/deprecation.rb +19 -0
- data/lib/rails/engine.rb +111 -92
- data/lib/rails/engine/commands.rb +1 -3
- data/lib/rails/engine/configuration.rb +26 -19
- data/lib/rails/engine/railties.rb +14 -18
- data/lib/rails/generators.rb +30 -50
- data/lib/rails/generators/actions.rb +38 -103
- data/lib/rails/generators/active_model.rb +14 -6
- data/lib/rails/generators/app_base.rb +70 -83
- data/lib/rails/generators/base.rb +23 -35
- data/lib/rails/generators/css/assets/assets_generator.rb +3 -3
- data/lib/rails/generators/css/scaffold/scaffold_generator.rb +3 -3
- data/lib/rails/generators/erb.rb +2 -2
- data/lib/rails/generators/erb/controller/controller_generator.rb +4 -4
- data/lib/rails/generators/erb/mailer/mailer_generator.rb +3 -3
- data/lib/rails/generators/erb/scaffold/scaffold_generator.rb +4 -4
- data/lib/rails/generators/erb/scaffold/templates/index.html.erb +18 -14
- data/lib/rails/generators/erb/scaffold/templates/show.html.erb +1 -1
- data/lib/rails/generators/generated_attribute.rb +51 -16
- data/lib/rails/generators/js/assets/assets_generator.rb +3 -3
- data/lib/rails/generators/migration.rb +1 -5
- data/lib/rails/generators/named_base.rb +20 -25
- data/lib/rails/generators/rails/app/app_generator.rb +40 -48
- data/lib/rails/generators/rails/app/templates/Gemfile +8 -7
- data/lib/rails/generators/rails/app/templates/README.rdoc +28 -0
- data/lib/rails/generators/rails/app/templates/Rakefile +0 -1
- data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +2 -1
- data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt +5 -0
- data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +2 -2
- data/lib/rails/generators/rails/app/templates/bin/bundle +2 -0
- data/lib/rails/generators/rails/app/templates/bin/rails +3 -0
- data/lib/rails/generators/rails/app/templates/bin/rake +3 -0
- data/lib/rails/generators/rails/app/templates/config/application.rb +5 -43
- data/lib/rails/generators/rails/app/templates/config/boot.rb +0 -2
- data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +2 -2
- data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +1 -4
- data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +8 -3
- data/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml +57 -0
- data/lib/rails/generators/rails/app/templates/config/environment.rb +2 -2
- data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +9 -19
- data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +40 -29
- data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +12 -15
- data/lib/rails/generators/rails/app/templates/config/initializers/filter_parameter_logging.rb +4 -0
- data/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +6 -5
- data/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt +6 -1
- data/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +1 -6
- data/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +6 -6
- data/lib/rails/generators/rails/app/templates/config/locales/en.yml +20 -2
- data/lib/rails/generators/rails/app/templates/config/routes.rb +15 -24
- data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +2 -2
- data/lib/rails/generators/rails/app/templates/gitignore +3 -2
- data/lib/rails/generators/rails/app/templates/public/404.html +2 -1
- data/lib/rails/generators/rails/app/templates/public/422.html +1 -1
- data/lib/rails/generators/rails/app/templates/public/500.html +2 -1
- data/lib/rails/generators/rails/app/templates/public/robots.txt +1 -1
- data/lib/rails/generators/rails/app/templates/test/test_helper.rb +3 -1
- data/lib/rails/generators/rails/assets/assets_generator.rb +5 -5
- data/lib/rails/generators/rails/controller/USAGE +7 -7
- data/lib/rails/generators/rails/controller/controller_generator.rb +3 -3
- data/lib/rails/generators/rails/controller/templates/controller.rb +1 -1
- data/lib/rails/generators/rails/generator/generator_generator.rb +4 -4
- data/lib/rails/generators/rails/helper/USAGE +1 -1
- data/lib/rails/generators/rails/helper/helper_generator.rb +2 -2
- data/lib/rails/generators/rails/integration_test/integration_test_generator.rb +2 -2
- data/lib/rails/generators/rails/migration/USAGE +12 -6
- data/lib/rails/generators/rails/migration/migration_generator.rb +3 -3
- data/lib/rails/generators/rails/model/USAGE +53 -4
- data/lib/rails/generators/rails/model/model_generator.rb +3 -3
- data/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +81 -39
- data/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +2 -2
- data/lib/rails/generators/rails/plugin_new/templates/Gemfile +16 -1
- data/lib/rails/generators/rails/plugin_new/templates/Rakefile +3 -9
- data/lib/rails/generators/rails/plugin_new/templates/app/controllers/%name%/application_controller.rb.tt +0 -1
- 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/{script → bin}/rails.tt +0 -0
- data/lib/rails/generators/rails/plugin_new/templates/gitignore +3 -0
- data/lib/rails/generators/rails/plugin_new/templates/lib/%name%.rb +1 -1
- data/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +0 -1
- data/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +0 -1
- data/lib/rails/generators/rails/resource/resource_generator.rb +5 -5
- data/lib/rails/generators/rails/resource_route/resource_route_generator.rb +2 -2
- data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +11 -7
- data/lib/rails/generators/rails/scaffold_controller/USAGE +4 -4
- data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +9 -7
- data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +27 -48
- data/lib/rails/generators/rails/task/task_generator.rb +2 -2
- data/lib/rails/generators/rails/task/templates/task.rb +1 -1
- data/lib/rails/generators/resource_helpers.rb +3 -5
- data/lib/rails/generators/test_case.rb +22 -28
- data/lib/rails/generators/test_unit.rb +3 -3
- data/lib/rails/generators/test_unit/controller/controller_generator.rb +6 -6
- data/lib/rails/generators/test_unit/helper/helper_generator.rb +5 -5
- data/lib/rails/generators/test_unit/integration/integration_generator.rb +4 -4
- data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +6 -6
- data/lib/rails/generators/test_unit/model/model_generator.rb +7 -7
- data/lib/rails/generators/test_unit/model/templates/fixtures.yml +8 -2
- data/lib/rails/generators/test_unit/plugin/plugin_generator.rb +4 -4
- data/lib/rails/generators/test_unit/plugin/templates/test_helper.rb +1 -2
- data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +10 -19
- data/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +5 -5
- data/lib/rails/info.rb +3 -3
- data/lib/rails/info_controller.rb +26 -8
- data/lib/rails/initializable.rb +2 -2
- data/lib/rails/paths.rb +59 -54
- data/lib/rails/rack/debugger.rb +2 -2
- data/lib/rails/rack/logger.rb +33 -8
- data/lib/rails/railtie.rb +24 -24
- data/lib/rails/railtie/configurable.rb +1 -1
- data/lib/rails/railtie/configuration.rb +15 -5
- data/lib/rails/ruby_version_check.rb +3 -13
- data/lib/rails/rubyprof_ext.rb +1 -1
- data/lib/rails/source_annotation_extractor.rb +40 -17
- data/lib/rails/tasks/annotations.rake +1 -1
- data/lib/rails/tasks/documentation.rake +10 -57
- data/lib/rails/tasks/engine.rake +1 -1
- data/lib/rails/tasks/framework.rake +9 -9
- data/lib/rails/tasks/log.rake +18 -4
- data/lib/rails/tasks/middleware.rake +1 -1
- data/lib/rails/tasks/misc.rake +2 -9
- data/lib/rails/tasks/routes.rake +4 -6
- data/lib/rails/tasks/statistics.rake +8 -2
- data/lib/rails/tasks/tmp.rake +12 -4
- data/lib/rails/templates/layouts/application.html.erb +36 -0
- data/lib/rails/templates/rails/info/properties.html.erb +1 -0
- data/lib/rails/templates/rails/info/routes.html.erb +9 -0
- data/lib/rails/{generators/rails/app/templates/public/index.html → templates/rails/welcome/index.html.erb} +18 -14
- data/lib/rails/test_help.rb +6 -29
- data/lib/rails/test_unit/railtie.rb +2 -3
- data/lib/rails/test_unit/testing.rake +45 -33
- data/lib/rails/version.rb +4 -4
- data/lib/rails/welcome_controller.rb +7 -0
- metadata +86 -378
- data/guides/assets/images/belongs_to.png +0 -0
- data/guides/assets/images/book_icon.gif +0 -0
- data/guides/assets/images/bullet.gif +0 -0
- data/guides/assets/images/challenge.png +0 -0
- data/guides/assets/images/chapters_icon.gif +0 -0
- data/guides/assets/images/check_bullet.gif +0 -0
- data/guides/assets/images/credits_pic_blank.gif +0 -0
- data/guides/assets/images/csrf.png +0 -0
- data/guides/assets/images/customized_error_messages.png +0 -0
- data/guides/assets/images/edge_badge.png +0 -0
- data/guides/assets/images/error_messages.png +0 -0
- data/guides/assets/images/feature_tile.gif +0 -0
- data/guides/assets/images/footer_tile.gif +0 -0
- data/guides/assets/images/fxn.png +0 -0
- data/guides/assets/images/grey_bullet.gif +0 -0
- data/guides/assets/images/habtm.png +0 -0
- data/guides/assets/images/has_many.png +0 -0
- data/guides/assets/images/has_many_through.png +0 -0
- data/guides/assets/images/has_one.png +0 -0
- data/guides/assets/images/has_one_through.png +0 -0
- data/guides/assets/images/header_backdrop.png +0 -0
- data/guides/assets/images/header_tile.gif +0 -0
- data/guides/assets/images/i18n/demo_html_safe.png +0 -0
- data/guides/assets/images/i18n/demo_localized_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translated_en.png +0 -0
- data/guides/assets/images/i18n/demo_translated_pirate.png +0 -0
- data/guides/assets/images/i18n/demo_translation_missing.png +0 -0
- data/guides/assets/images/i18n/demo_untranslated.png +0 -0
- data/guides/assets/images/icons/README +0 -5
- data/guides/assets/images/icons/callouts/1.png +0 -0
- data/guides/assets/images/icons/callouts/10.png +0 -0
- data/guides/assets/images/icons/callouts/11.png +0 -0
- data/guides/assets/images/icons/callouts/12.png +0 -0
- data/guides/assets/images/icons/callouts/13.png +0 -0
- data/guides/assets/images/icons/callouts/14.png +0 -0
- data/guides/assets/images/icons/callouts/15.png +0 -0
- data/guides/assets/images/icons/callouts/2.png +0 -0
- data/guides/assets/images/icons/callouts/3.png +0 -0
- data/guides/assets/images/icons/callouts/4.png +0 -0
- data/guides/assets/images/icons/callouts/5.png +0 -0
- data/guides/assets/images/icons/callouts/6.png +0 -0
- data/guides/assets/images/icons/callouts/7.png +0 -0
- data/guides/assets/images/icons/callouts/8.png +0 -0
- data/guides/assets/images/icons/callouts/9.png +0 -0
- data/guides/assets/images/icons/caution.png +0 -0
- data/guides/assets/images/icons/example.png +0 -0
- data/guides/assets/images/icons/home.png +0 -0
- data/guides/assets/images/icons/important.png +0 -0
- data/guides/assets/images/icons/next.png +0 -0
- data/guides/assets/images/icons/note.png +0 -0
- data/guides/assets/images/icons/prev.png +0 -0
- data/guides/assets/images/icons/tip.png +0 -0
- data/guides/assets/images/icons/up.png +0 -0
- data/guides/assets/images/icons/warning.png +0 -0
- data/guides/assets/images/jaimeiniesta.jpg +0 -0
- data/guides/assets/images/nav_arrow.gif +0 -0
- data/guides/assets/images/polymorphic.png +0 -0
- data/guides/assets/images/posts_index.png +0 -0
- data/guides/assets/images/radar.png +0 -0
- data/guides/assets/images/rails_guides_kindle_cover.jpg +0 -0
- data/guides/assets/images/rails_guides_logo.gif +0 -0
- data/guides/assets/images/rails_logo_remix.gif +0 -0
- data/guides/assets/images/rails_welcome.png +0 -0
- data/guides/assets/images/session_fixation.png +0 -0
- data/guides/assets/images/tab_grey.gif +0 -0
- data/guides/assets/images/tab_info.gif +0 -0
- data/guides/assets/images/tab_note.gif +0 -0
- data/guides/assets/images/tab_red.gif +0 -0
- data/guides/assets/images/tab_yellow.gif +0 -0
- data/guides/assets/images/tab_yellow.png +0 -0
- data/guides/assets/images/validation_error_messages.png +0 -0
- data/guides/assets/images/vijaydev.jpg +0 -0
- data/guides/assets/javascripts/guides.js +0 -7
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAS3.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushAppleScript.js +0 -75
- data/guides/assets/javascripts/syntaxhighlighter/shBrushBash.js +0 -59
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCSharp.js +0 -65
- data/guides/assets/javascripts/syntaxhighlighter/shBrushColdFusion.js +0 -100
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCpp.js +0 -97
- data/guides/assets/javascripts/syntaxhighlighter/shBrushCss.js +0 -91
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDelphi.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushDiff.js +0 -41
- data/guides/assets/javascripts/syntaxhighlighter/shBrushErlang.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushGroovy.js +0 -67
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJScript.js +0 -52
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJava.js +0 -57
- data/guides/assets/javascripts/syntaxhighlighter/shBrushJavaFX.js +0 -58
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPerl.js +0 -72
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPhp.js +0 -88
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPlain.js +0 -33
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPowerShell.js +0 -74
- data/guides/assets/javascripts/syntaxhighlighter/shBrushPython.js +0 -64
- data/guides/assets/javascripts/syntaxhighlighter/shBrushRuby.js +0 -55
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSass.js +0 -94
- data/guides/assets/javascripts/syntaxhighlighter/shBrushScala.js +0 -51
- data/guides/assets/javascripts/syntaxhighlighter/shBrushSql.js +0 -66
- data/guides/assets/javascripts/syntaxhighlighter/shBrushVb.js +0 -56
- data/guides/assets/javascripts/syntaxhighlighter/shBrushXml.js +0 -69
- data/guides/assets/javascripts/syntaxhighlighter/shCore.js +0 -17
- data/guides/assets/stylesheets/fixes.css +0 -16
- data/guides/assets/stylesheets/kindle.css +0 -11
- data/guides/assets/stylesheets/main.css +0 -453
- data/guides/assets/stylesheets/print.css +0 -52
- data/guides/assets/stylesheets/reset.css +0 -43
- data/guides/assets/stylesheets/style.css +0 -13
- data/guides/assets/stylesheets/syntaxhighlighter/shCore.css +0 -226
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDefault.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreDjango.css +0 -331
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEclipse.css +0 -339
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreEmacs.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreFadeToGrey.css +0 -328
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMDUltra.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreMidnight.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shCoreRDark.css +0 -324
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDefault.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeDjango.css +0 -120
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEclipse.css +0 -128
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeEmacs.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeFadeToGrey.css +0 -117
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMDUltra.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeMidnight.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRDark.css +0 -113
- data/guides/assets/stylesheets/syntaxhighlighter/shThemeRailsGuides.css +0 -116
- data/guides/code/getting_started/Gemfile +0 -38
- data/guides/code/getting_started/README.rdoc +0 -261
- data/guides/code/getting_started/Rakefile +0 -7
- data/guides/code/getting_started/app/assets/images/rails.png +0 -0
- data/guides/code/getting_started/app/assets/javascripts/application.js +0 -15
- data/guides/code/getting_started/app/assets/javascripts/comments.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/home.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/javascripts/posts.js.coffee +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/application.css +0 -13
- data/guides/code/getting_started/app/assets/stylesheets/comments.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/home.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/posts.css.scss +0 -3
- data/guides/code/getting_started/app/assets/stylesheets/scaffolds.css.scss +0 -56
- data/guides/code/getting_started/app/controllers/application_controller.rb +0 -3
- data/guides/code/getting_started/app/controllers/comments_controller.rb +0 -16
- data/guides/code/getting_started/app/controllers/home_controller.rb +0 -5
- data/guides/code/getting_started/app/controllers/posts_controller.rb +0 -84
- data/guides/code/getting_started/app/helpers/application_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/comments_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/home_helper.rb +0 -2
- data/guides/code/getting_started/app/helpers/posts_helper.rb +0 -5
- data/guides/code/getting_started/app/models/comment.rb +0 -3
- data/guides/code/getting_started/app/models/post.rb +0 -11
- data/guides/code/getting_started/app/models/tag.rb +0 -3
- data/guides/code/getting_started/app/views/comments/_comment.html.erb +0 -15
- data/guides/code/getting_started/app/views/comments/_form.html.erb +0 -13
- data/guides/code/getting_started/app/views/home/index.html.erb +0 -2
- data/guides/code/getting_started/app/views/layouts/application.html.erb +0 -14
- data/guides/code/getting_started/app/views/posts/_form.html.erb +0 -32
- data/guides/code/getting_started/app/views/posts/edit.html.erb +0 -6
- data/guides/code/getting_started/app/views/posts/index.html.erb +0 -27
- data/guides/code/getting_started/app/views/posts/new.html.erb +0 -5
- data/guides/code/getting_started/app/views/posts/show.html.erb +0 -31
- data/guides/code/getting_started/app/views/tags/_form.html.erb +0 -12
- data/guides/code/getting_started/config.ru +0 -4
- data/guides/code/getting_started/config/application.rb +0 -59
- data/guides/code/getting_started/config/boot.rb +0 -6
- data/guides/code/getting_started/config/database.yml +0 -25
- data/guides/code/getting_started/config/environment.rb +0 -5
- data/guides/code/getting_started/config/environments/development.rb +0 -37
- data/guides/code/getting_started/config/environments/production.rb +0 -67
- data/guides/code/getting_started/config/environments/test.rb +0 -37
- data/guides/code/getting_started/config/initializers/backtrace_silencers.rb +0 -7
- data/guides/code/getting_started/config/initializers/inflections.rb +0 -15
- data/guides/code/getting_started/config/initializers/mime_types.rb +0 -5
- data/guides/code/getting_started/config/initializers/secret_token.rb +0 -7
- data/guides/code/getting_started/config/initializers/session_store.rb +0 -8
- data/guides/code/getting_started/config/initializers/wrap_parameters.rb +0 -14
- data/guides/code/getting_started/config/locales/en.yml +0 -5
- data/guides/code/getting_started/config/routes.rb +0 -64
- data/guides/code/getting_started/db/migrate/20110901012504_create_posts.rb +0 -11
- data/guides/code/getting_started/db/migrate/20110901012815_create_comments.rb +0 -12
- data/guides/code/getting_started/db/migrate/20110901013701_create_tags.rb +0 -11
- data/guides/code/getting_started/db/schema.rb +0 -43
- data/guides/code/getting_started/db/seeds.rb +0 -7
- data/guides/code/getting_started/doc/README_FOR_APP +0 -2
- data/guides/code/getting_started/public/404.html +0 -26
- data/guides/code/getting_started/public/422.html +0 -26
- data/guides/code/getting_started/public/500.html +0 -25
- data/guides/code/getting_started/public/favicon.ico +0 -0
- data/guides/code/getting_started/public/robots.txt +0 -5
- data/guides/code/getting_started/script/rails +0 -6
- data/guides/code/getting_started/test/fixtures/comments.yml +0 -11
- data/guides/code/getting_started/test/fixtures/posts.yml +0 -11
- data/guides/code/getting_started/test/fixtures/tags.yml +0 -9
- data/guides/code/getting_started/test/functional/comments_controller_test.rb +0 -7
- data/guides/code/getting_started/test/functional/home_controller_test.rb +0 -9
- data/guides/code/getting_started/test/functional/posts_controller_test.rb +0 -49
- data/guides/code/getting_started/test/performance/browsing_test.rb +0 -12
- data/guides/code/getting_started/test/test_helper.rb +0 -13
- data/guides/code/getting_started/test/unit/comment_test.rb +0 -7
- data/guides/code/getting_started/test/unit/helpers/comments_helper_test.rb +0 -4
- data/guides/code/getting_started/test/unit/helpers/home_helper_test.rb +0 -4
- data/guides/code/getting_started/test/unit/helpers/posts_helper_test.rb +0 -4
- data/guides/code/getting_started/test/unit/post_test.rb +0 -7
- data/guides/code/getting_started/test/unit/tag_test.rb +0 -7
- data/guides/rails_guides.rb +0 -50
- data/guides/rails_guides/generator.rb +0 -310
- data/guides/rails_guides/helpers.rb +0 -45
- data/guides/rails_guides/indexer.rb +0 -69
- data/guides/rails_guides/levenshtein.rb +0 -31
- data/guides/rails_guides/textile_extensions.rb +0 -63
- data/guides/source/2_2_release_notes.textile +0 -422
- data/guides/source/2_3_release_notes.textile +0 -610
- data/guides/source/3_0_release_notes.textile +0 -595
- data/guides/source/3_1_release_notes.textile +0 -553
- data/guides/source/3_2_release_notes.textile +0 -540
- data/guides/source/_license.html.erb +0 -2
- data/guides/source/_welcome.html.erb +0 -19
- data/guides/source/action_controller_overview.textile +0 -820
- data/guides/source/action_mailer_basics.textile +0 -516
- data/guides/source/action_view_overview.textile +0 -1497
- data/guides/source/active_model_basics.textile +0 -205
- data/guides/source/active_record_basics.textile +0 -218
- data/guides/source/active_record_querying.textile +0 -1433
- data/guides/source/active_record_validations_callbacks.textile +0 -1287
- data/guides/source/active_resource_basics.textile +0 -120
- data/guides/source/active_support_core_extensions.textile +0 -3715
- data/guides/source/api_documentation_guidelines.textile +0 -185
- data/guides/source/asset_pipeline.textile +0 -707
- data/guides/source/association_basics.textile +0 -1959
- data/guides/source/caching_with_rails.textile +0 -437
- data/guides/source/command_line.textile +0 -574
- data/guides/source/configuring.textile +0 -643
- data/guides/source/contributing_to_ruby_on_rails.textile +0 -448
- data/guides/source/credits.html.erb +0 -72
- data/guides/source/debugging_rails_applications.textile +0 -714
- data/guides/source/documents.yaml +0 -157
- data/guides/source/engines.textile +0 -618
- data/guides/source/form_helpers.textile +0 -798
- data/guides/source/generators.textile +0 -621
- data/guides/source/getting_started.textile +0 -1931
- data/guides/source/i18n.textile +0 -931
- data/guides/source/index.html.erb +0 -30
- data/guides/source/initialization.textile +0 -1116
- data/guides/source/kindle/KINDLE.md +0 -26
- data/guides/source/kindle/copyright.html.erb +0 -1
- data/guides/source/kindle/layout.html.erb +0 -27
- data/guides/source/kindle/rails_guides.opf.erb +0 -52
- data/guides/source/kindle/toc.html.erb +0 -24
- data/guides/source/kindle/toc.ncx.erb +0 -64
- data/guides/source/kindle/welcome.html.erb +0 -5
- data/guides/source/layout.html.erb +0 -124
- data/guides/source/layouts_and_rendering.textile +0 -1237
- data/guides/source/migrations.textile +0 -900
- data/guides/source/nested_model_forms.textile +0 -222
- data/guides/source/performance_testing.textile +0 -597
- data/guides/source/plugins.textile +0 -464
- data/guides/source/rails_application_templates.textile +0 -240
- data/guides/source/rails_on_rack.textile +0 -236
- data/guides/source/routing.textile +0 -885
- data/guides/source/ruby_on_rails_guides_guidelines.textile +0 -79
- data/guides/source/security.textile +0 -1004
- data/guides/source/testing.textile +0 -947
- data/guides/w3c_validator.rb +0 -91
- data/lib/rails/application/railties.rb +0 -13
- data/lib/rails/application/route_inspector.rb +0 -84
- data/lib/rails/commands/benchmarker.rb +0 -34
- data/lib/rails/commands/plugin.rb +0 -544
- data/lib/rails/commands/profiler.rb +0 -33
- data/lib/rails/generators/rails/app/templates/README +0 -261
- data/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +0 -3
- 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/doc/README_FOR_APP +0 -2
- data/lib/rails/generators/rails/app/templates/public/stylesheets/.empty_directory +0 -0
- data/lib/rails/generators/rails/app/templates/script/rails +0 -5
- 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/performance/browsing_test.rb +0 -12
- data/lib/rails/generators/rails/app/templates/test/unit/.empty_directory +0 -0
- data/lib/rails/generators/rails/generator/templates/templates/.empty_directory +0 -0
- data/lib/rails/generators/rails/observer/USAGE +0 -12
- data/lib/rails/generators/rails/observer/observer_generator.rb +0 -7
- data/lib/rails/generators/rails/performance_test/USAGE +0 -10
- data/lib/rails/generators/rails/performance_test/performance_test_generator.rb +0 -7
- 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/session_migration/USAGE +0 -8
- data/lib/rails/generators/rails/session_migration/session_migration_generator.rb +0 -8
- data/lib/rails/generators/test_unit/observer/observer_generator.rb +0 -13
- data/lib/rails/generators/test_unit/observer/templates/unit_test.rb +0 -9
- data/lib/rails/generators/test_unit/performance/performance_generator.rb +0 -13
- data/lib/rails/generators/test_unit/performance/templates/performance_test.rb +0 -12
- data/lib/rails/performance_test_help.rb +0 -3
- data/lib/rails/plugin.rb +0 -92
- data/lib/rails/script_rails_loader.rb +0 -29
@@ -1,157 +0,0 @@
|
|
1
|
-
-
|
2
|
-
name: Start Here
|
3
|
-
documents:
|
4
|
-
-
|
5
|
-
name: Getting Started with Rails
|
6
|
-
url: getting_started.html
|
7
|
-
description: Everything you need to know to install Rails and create your first application.
|
8
|
-
-
|
9
|
-
name: Models
|
10
|
-
documents:
|
11
|
-
-
|
12
|
-
name: Rails Database Migrations
|
13
|
-
url: migrations.html
|
14
|
-
description: This guide covers how you can use Active Record migrations to alter your database in a structured and organized manner.
|
15
|
-
-
|
16
|
-
name: Active Record Validations and Callbacks
|
17
|
-
url: active_record_validations_callbacks.html
|
18
|
-
description: This guide covers how you can use Active Record validations and callbacks.
|
19
|
-
-
|
20
|
-
name: Active Record Associations
|
21
|
-
url: association_basics.html
|
22
|
-
description: This guide covers all the associations provided by Active Record.
|
23
|
-
-
|
24
|
-
name: Active Record Query Interface
|
25
|
-
url: active_record_querying.html
|
26
|
-
description: This guide covers the database query interface provided by Active Record.
|
27
|
-
-
|
28
|
-
name: Views
|
29
|
-
documents:
|
30
|
-
-
|
31
|
-
name: Layouts and Rendering in Rails
|
32
|
-
url: layouts_and_rendering.html
|
33
|
-
description: This guide covers the basic layout features of Action Controller and Action View, including rendering and redirecting, using content_for blocks, and working with partials.
|
34
|
-
-
|
35
|
-
name: Action View Form Helpers
|
36
|
-
url: form_helpers.html
|
37
|
-
description: Guide to using built-in Form helpers.
|
38
|
-
-
|
39
|
-
name: Controllers
|
40
|
-
documents:
|
41
|
-
-
|
42
|
-
name: Action Controller Overview
|
43
|
-
url: action_controller_overview.html
|
44
|
-
description: This guide covers how controllers work and how they fit into the request cycle in your application. It includes sessions, filters, and cookies, data streaming, and dealing with exceptions raised by a request, among other topics.
|
45
|
-
-
|
46
|
-
name: Rails Routing from the Outside In
|
47
|
-
url: routing.html
|
48
|
-
description: This guide covers the user-facing features of Rails routing. If you want to understand how to use routing in your own Rails applications, start here.
|
49
|
-
-
|
50
|
-
name: Digging Deeper
|
51
|
-
documents:
|
52
|
-
-
|
53
|
-
name: Active Support Core Extensions
|
54
|
-
url: active_support_core_extensions.html
|
55
|
-
description: This guide documents the Ruby core extensions defined in Active Support.
|
56
|
-
-
|
57
|
-
name: Rails Internationalization API
|
58
|
-
url: i18n.html
|
59
|
-
description: This guide covers how to add internationalization to your applications. Your application will be able to translate content to different languages, change pluralization rules, use correct date formats for each country and so on.
|
60
|
-
-
|
61
|
-
name: Action Mailer Basics
|
62
|
-
url: action_mailer_basics.html
|
63
|
-
work_in_progress: true
|
64
|
-
description: This guide describes how to use Action Mailer to send and receive emails.
|
65
|
-
-
|
66
|
-
name: Testing Rails Applications
|
67
|
-
url: testing.html
|
68
|
-
work_in_progress: true
|
69
|
-
description: This is a rather comprehensive guide to doing both unit and functional tests in Rails. It covers everything from 'What is a test?' to the testing APIs. Enjoy.
|
70
|
-
-
|
71
|
-
name: Securing Rails Applications
|
72
|
-
url: security.html
|
73
|
-
description: This guide describes common security problems in web applications and how to avoid them with Rails.
|
74
|
-
-
|
75
|
-
name: Debugging Rails Applications
|
76
|
-
url: debugging_rails_applications.html
|
77
|
-
description: This guide describes how to debug Rails applications. It covers the different ways of achieving this and how to understand what is happening "behind the scenes" of your code.
|
78
|
-
-
|
79
|
-
name: Performance Testing Rails Applications
|
80
|
-
url: performance_testing.html
|
81
|
-
description: This guide covers the various ways of performance testing a Ruby on Rails application.
|
82
|
-
-
|
83
|
-
name: Configuring Rails Applications
|
84
|
-
url: configuring.html
|
85
|
-
description: This guide covers the basic configuration settings for a Rails application.
|
86
|
-
-
|
87
|
-
name: Rails Command Line Tools and Rake Tasks
|
88
|
-
url: command_line.html
|
89
|
-
description: This guide covers the command line tools and rake tasks provided by Rails.
|
90
|
-
-
|
91
|
-
name: Caching with Rails
|
92
|
-
work_in_progress: true
|
93
|
-
url: caching_with_rails.html
|
94
|
-
description: Various caching techniques provided by Rails.
|
95
|
-
-
|
96
|
-
name: Asset Pipeline
|
97
|
-
url: asset_pipeline.html
|
98
|
-
description: This guide documents the asset pipeline.
|
99
|
-
-
|
100
|
-
name: The Rails Initialization Process
|
101
|
-
work_in_progress: true
|
102
|
-
url: initialization.html
|
103
|
-
description: This guide explains the internals of the Rails initialization process as of Rails 3.1
|
104
|
-
-
|
105
|
-
name: Extending Rails
|
106
|
-
documents:
|
107
|
-
-
|
108
|
-
name: The Basics of Creating Rails Plugins
|
109
|
-
work_in_progress: true
|
110
|
-
url: plugins.html
|
111
|
-
description: This guide covers how to build a plugin to extend the functionality of Rails.
|
112
|
-
-
|
113
|
-
name: Rails on Rack
|
114
|
-
url: rails_on_rack.html
|
115
|
-
description: This guide covers Rails integration with Rack and interfacing with other Rack components.
|
116
|
-
-
|
117
|
-
name: Creating and Customizing Rails Generators
|
118
|
-
url: generators.html
|
119
|
-
description: This guide covers the process of adding a brand new generator to your extension or providing an alternative to an element of a built-in Rails generator (such as providing alternative test stubs for the scaffold generator).
|
120
|
-
-
|
121
|
-
name: Contributing to Ruby on Rails
|
122
|
-
documents:
|
123
|
-
-
|
124
|
-
name: Contributing to Ruby on Rails
|
125
|
-
url: contributing_to_ruby_on_rails.html
|
126
|
-
description: Rails is not 'somebody else's framework.' This guide covers a variety of ways that you can get involved in the ongoing development of Rails.
|
127
|
-
-
|
128
|
-
name: API Documentation Guidelines
|
129
|
-
url: api_documentation_guidelines.html
|
130
|
-
description: This guide documents the Ruby on Rails API documentation guidelines.
|
131
|
-
-
|
132
|
-
name: Ruby on Rails Guides Guidelines
|
133
|
-
url: ruby_on_rails_guides_guidelines.html
|
134
|
-
description: This guide documents the Ruby on Rails guides guidelines.
|
135
|
-
-
|
136
|
-
name: Release Notes
|
137
|
-
documents:
|
138
|
-
-
|
139
|
-
name: Ruby on Rails 3.2 Release Notes
|
140
|
-
url: 3_2_release_notes.html
|
141
|
-
description: Release notes for Rails 3.2.
|
142
|
-
-
|
143
|
-
name: Ruby on Rails 3.1 Release Notes
|
144
|
-
url: 3_1_release_notes.html
|
145
|
-
description: Release notes for Rails 3.1.
|
146
|
-
-
|
147
|
-
name: Ruby on Rails 3.0 Release Notes
|
148
|
-
url: 3_0_release_notes.html
|
149
|
-
description: Release notes for Rails 3.0.
|
150
|
-
-
|
151
|
-
name: Ruby on Rails 2.3 Release Notes
|
152
|
-
url: 2_3_release_notes.html
|
153
|
-
description: Release notes for Rails 2.3.
|
154
|
-
-
|
155
|
-
name: Ruby on Rails 2.2 Release Notes
|
156
|
-
url: 2_2_release_notes.html
|
157
|
-
description: Release notes for Rails 2.2.
|
@@ -1,618 +0,0 @@
|
|
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.
|