From 2c8bc2cdcdb7b144b7a583d24922fd8064bcff3d Mon Sep 17 00:00:00 2001 From: Paul Nikitochkin Date: Fri, 6 Sep 2013 21:41:04 +0300 Subject: Use Ruby on Rails Coding Conventions for code examples in the guides * Indent after private/protected * Ruby >= 1.9 syntax for hashes * Prefer method { do_stuff } instead of method{do_stuff} for single-line blocks. [ci skip] --- guides/source/engines.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'guides/source/engines.md') diff --git a/guides/source/engines.md b/guides/source/engines.md index bc404ccb7f..ec51fb9234 100644 --- a/guides/source/engines.md +++ b/guides/source/engines.md @@ -399,9 +399,9 @@ def create end private -def comment_params - params.require(:comment).permit(:text) -end + def comment_params + params.require(:comment).permit(:text) + end ``` 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: @@ -850,7 +850,6 @@ module Blorgh::Concerns::Models::Post before_save :set_author private - def set_author self.author = User.find_or_create_by(name: author_name) end -- cgit v1.2.3 From 448aa840cb3897fdb2e96c59cc49f0a853972c01 Mon Sep 17 00:00:00 2001 From: Juanito Fatas Date: Sun, 22 Sep 2013 02:26:07 +0800 Subject: [ci skip] Fix a typo in Engines.md. --- guides/source/engines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/engines.md') diff --git a/guides/source/engines.md b/guides/source/engines.md index ec51fb9234..48d2949539 100644 --- a/guides/source/engines.md +++ b/guides/source/engines.md @@ -950,7 +950,7 @@ INFO. Remember that in order to use languages like Sass or CoffeeScript, you sho There are some situations where your engine's assets are not required by the host application. For example, say that you've created an admin functionality that only exists for your engine. In this case, the host application doesn't need to require `admin.css` -or `admin.js`. Only the gem's admin layout needs these assets. It doesn't make sense for the host app to include `"blorg/admin.css"` in it's stylesheets. In this situation, you should explicitly define these assets for precompilation. +or `admin.js`. Only the gem's admin layout needs these assets. It doesn't make sense for the host app to include `"blorgh/admin.css"` in it's stylesheets. In this situation, you should explicitly define these assets for precompilation. This tells sprockets to add your engine assets when `rake assets:precompile` is ran. You can define assets for precompilation in `engine.rb` -- cgit v1.2.3 From 53d1779b67bc49cdcf8a60ac4607f9e4f2e66cda Mon Sep 17 00:00:00 2001 From: Juanito Fatas Date: Sun, 22 Sep 2013 19:00:38 +0800 Subject: [ci skip] Add missing migrate step in generating comment resource section. --- guides/source/engines.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'guides/source/engines.md') diff --git a/guides/source/engines.md b/guides/source/engines.md index 48d2949539..c71b728ef7 100644 --- a/guides/source/engines.md +++ b/guides/source/engines.md @@ -307,7 +307,11 @@ create test/models/blorgh/comment_test.rb create test/fixtures/blorgh/comments.yml ``` -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`. +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`. Now run the migration to create our blorgh_comments table: + +```bash +$ rake db:migrate +``` To show the comments on a post, edit `app/views/blorgh/posts/show.html.erb` and add this line before the "Edit" link: -- cgit v1.2.3 From b8a538625af2c9a3442a27eff4cf8318ba812ce7 Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 24 Oct 2013 13:30:01 +0200 Subject: rephrase the overriding views part about the view path See rails/rails#12608 --- guides/source/engines.md | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'guides/source/engines.md') diff --git a/guides/source/engines.md b/guides/source/engines.md index c71b728ef7..b9be764528 100644 --- a/guides/source/engines.md +++ b/guides/source/engines.md @@ -1,4 +1,4 @@ -Getting Started with Engines +Getting Started wiith Engines ============================ 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. @@ -598,7 +598,7 @@ By outputting `@post.author` using the `<%=` tag, the `to_s` method will be call ``` # -``` +```i 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: @@ -611,7 +611,7 @@ end Now instead of the ugly Ruby object output the author's name will be displayed. #### Using a controller provided by the application - +i Because Rails controllers generally share code for things like authentication and accessing session variables, by default they inherit from `ApplicationController`. Rails engines, however are scoped to run independently from the main application, so each engine gets a scoped `ApplicationController`. This namespace prevents code collisions, but often engine controllers should access methods in the main application's `ApplicationController`. An easy way to provide this access is to change the engine's scoped `ApplicationController` to inherit from the main application's `ApplicationController`. For our Blorgh engine this would be done by changing `app/controllers/blorgh/application_controller.rb` to look like: ```ruby @@ -702,7 +702,7 @@ an application. Same goes for if you want to use a standard initializer. For locales, simply place the locale files in the `config/locales` directory, just like you would in an application. -Testing an engine +Testing an enginie ----------------- When an engine is generated there is a smaller dummy application created inside it at `test/dummy`. This application is used as a mounting point for the engine to make testing the engine extremely simple. You may extend this application by generating controllers, models or views from within the directory, and then use those to test your engine. @@ -725,7 +725,7 @@ get :index, use_route: :blorgh This tells the application that you still want to perform a `GET` request to the `index` action of this controller, just that you want to use the engine's route to get there, rather than the application. -Improving engine functionality +Improving enginie functionality ------------------------------ This section explains how to add and/or override engine MVC functionality in the main Rails application. @@ -746,7 +746,7 @@ Here is some sample code to do this: ```ruby # lib/blorgh/engine.rb -module Blorgh +module Blorghi class Engine < ::Rails::Engine isolate_namespace Blorgh @@ -763,12 +763,12 @@ This doesn't apply to just Decorators, but anything that you add in an engine that isn't referenced by your main application. #### Implementing Decorator Pattern Using Class#class_eval - +i **Adding** `Post#time_since_created`, ```ruby # MyApp/app/decorators/models/blorgh/post_decorator.rb - +i Blorgh::Post.class_eval do def time_since_created Time.current - created_at @@ -778,7 +778,7 @@ end ```ruby # Blorgh/app/models/post.rb - +i class Post < ActiveRecord::Base has_many :comments end @@ -789,7 +789,7 @@ end ```ruby # MyApp/app/decorators/models/blorgh/post_decorator.rb - +i Blorgh::Post.class_eval do def summary "#{title} - #{truncate(text)}" @@ -799,7 +799,7 @@ end ```ruby # Blorgh/app/models/post.rb - +i class Post < ActiveRecord::Base has_many :comments def summary @@ -809,14 +809,14 @@ end ``` #### Implementing Decorator Pattern Using ActiveSupport::Concern - +i Using `Class#class_eval` is great for simple adjustments, but for more complex class modifications, you might want to consider using [`ActiveSupport::Concern`](http://edgeapi.rubyonrails.org/classes/ActiveSupport/Concern.html). ActiveSupport::Concern manages load order of interlinked dependent modules and classes at run time allowing you to significantly modularize your code. **Adding** `Post#time_since_created` and **Overriding** `Post#summary` ```ruby # MyApp/app/models/blorgh/post.rb - +i class Blorgh::Post < ActiveRecord::Base include Blorgh::Concerns::Models::Post @@ -832,7 +832,7 @@ end ```ruby # Blorgh/app/models/post.rb - +i class Post < ActiveRecord::Base include Blorgh::Concerns::Models::Post end @@ -875,7 +875,7 @@ end 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. -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. +When the application is asked to render the view for `Blorgh::PostsController`'s index action, it will look the path `app/views/blorgh/posts/index.html.erb`, first within the application. If it cannot find it, it will look inside the engine. You can override this view in the application by simply creating a new file at `app/views/blorgh/posts/index.html.erb`. Then you can completely change what this view would normally output. -- cgit v1.2.3 From a0fe2f1fc8334ed083c82ca020cf91e9b1f0185b Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 24 Oct 2013 13:33:13 +0200 Subject: fix typo introduced when searching in markdown document with vim --- guides/source/engines.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'guides/source/engines.md') diff --git a/guides/source/engines.md b/guides/source/engines.md index b9be764528..af48768fe9 100644 --- a/guides/source/engines.md +++ b/guides/source/engines.md @@ -1,4 +1,4 @@ -Getting Started wiith Engines +Getting Started with Engines ============================ 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. @@ -598,7 +598,7 @@ By outputting `@post.author` using the `<%=` tag, the `to_s` method will be call ``` # -```i +``` 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: @@ -611,7 +611,7 @@ end Now instead of the ugly Ruby object output the author's name will be displayed. #### Using a controller provided by the application -i + Because Rails controllers generally share code for things like authentication and accessing session variables, by default they inherit from `ApplicationController`. Rails engines, however are scoped to run independently from the main application, so each engine gets a scoped `ApplicationController`. This namespace prevents code collisions, but often engine controllers should access methods in the main application's `ApplicationController`. An easy way to provide this access is to change the engine's scoped `ApplicationController` to inherit from the main application's `ApplicationController`. For our Blorgh engine this would be done by changing `app/controllers/blorgh/application_controller.rb` to look like: ```ruby @@ -702,7 +702,7 @@ an application. Same goes for if you want to use a standard initializer. For locales, simply place the locale files in the `config/locales` directory, just like you would in an application. -Testing an enginie +Testing an engine ----------------- When an engine is generated there is a smaller dummy application created inside it at `test/dummy`. This application is used as a mounting point for the engine to make testing the engine extremely simple. You may extend this application by generating controllers, models or views from within the directory, and then use those to test your engine. @@ -725,7 +725,7 @@ get :index, use_route: :blorgh This tells the application that you still want to perform a `GET` request to the `index` action of this controller, just that you want to use the engine's route to get there, rather than the application. -Improving enginie functionality +Improving engine functionality ------------------------------ This section explains how to add and/or override engine MVC functionality in the main Rails application. @@ -746,7 +746,7 @@ Here is some sample code to do this: ```ruby # lib/blorgh/engine.rb -module Blorghi +module Blorgh class Engine < ::Rails::Engine isolate_namespace Blorgh @@ -763,12 +763,12 @@ This doesn't apply to just Decorators, but anything that you add in an engine that isn't referenced by your main application. #### Implementing Decorator Pattern Using Class#class_eval -i + **Adding** `Post#time_since_created`, ```ruby # MyApp/app/decorators/models/blorgh/post_decorator.rb -i + Blorgh::Post.class_eval do def time_since_created Time.current - created_at @@ -778,7 +778,7 @@ end ```ruby # Blorgh/app/models/post.rb -i + class Post < ActiveRecord::Base has_many :comments end @@ -789,7 +789,7 @@ end ```ruby # MyApp/app/decorators/models/blorgh/post_decorator.rb -i + Blorgh::Post.class_eval do def summary "#{title} - #{truncate(text)}" @@ -799,7 +799,7 @@ end ```ruby # Blorgh/app/models/post.rb -i + class Post < ActiveRecord::Base has_many :comments def summary @@ -809,14 +809,14 @@ end ``` #### Implementing Decorator Pattern Using ActiveSupport::Concern -i + Using `Class#class_eval` is great for simple adjustments, but for more complex class modifications, you might want to consider using [`ActiveSupport::Concern`](http://edgeapi.rubyonrails.org/classes/ActiveSupport/Concern.html). ActiveSupport::Concern manages load order of interlinked dependent modules and classes at run time allowing you to significantly modularize your code. **Adding** `Post#time_since_created` and **Overriding** `Post#summary` ```ruby # MyApp/app/models/blorgh/post.rb -i + class Blorgh::Post < ActiveRecord::Base include Blorgh::Concerns::Models::Post @@ -832,7 +832,7 @@ end ```ruby # Blorgh/app/models/post.rb -i + class Post < ActiveRecord::Base include Blorgh::Concerns::Models::Post end -- cgit v1.2.3 From 08c72946c529f4913131d6285b35d4fc3d41673c Mon Sep 17 00:00:00 2001 From: Martin Harrigan Date: Sat, 23 Nov 2013 11:26:17 +0000 Subject: Helper should be a module in Rails guides [ci skip] --- guides/source/engines.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source/engines.md') diff --git a/guides/source/engines.md b/guides/source/engines.md index af48768fe9..2266b1fd7f 100644 --- a/guides/source/engines.md +++ b/guides/source/engines.md @@ -253,7 +253,7 @@ The helper inside `app/helpers/blorgh/posts_helper.rb` is also namespaced: ```ruby module Blorgh - class PostsHelper + module PostsHelper ... end end -- cgit v1.2.3