Skip to content

Commit

Permalink
Update guide Menu Presenter for Refinery 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bricesanchez committed Apr 16, 2015
1 parent 39a4306 commit 5fd285f
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions doc/guides/9 - Presenters/1 - Menu Presenter.textile
Expand Up @@ -3,6 +3,7 @@ h2. Additional Menus
This guide will show you how to:
* configure and use +Refinery::Pages::MenuPresenter+
* use a decorator to add custom functionality to +Refinery::Page+ class
* use a decorator to allow attributes in +Refinery::Admin::PagesController+
* override Refinery's view

endprologue.
Expand Down Expand Up @@ -30,7 +31,7 @@ This will generate an empty migration. Open it and add the following code:
<ruby>
class AddShowInFooterToRefineryPages < ActiveRecord::Migration
def change
add_column :refinery_pages, :show_in_footer, :boolean, :default => false
add_column :refinery_pages, :show_in_footer, :boolean, default: false
end
end
</ruby>
Expand All @@ -41,31 +42,48 @@ Run the migration:
rake db:migrate
</shell>

h3. Decorating the Refinery::Page model
h3. Decorating the +Refinery::Page+ model

Before overriding Refinery's form view, we want to decorate the +Refinery::Page+ class. Create a file +app/decorators/models/refinery/page_decorator.rb+ with this content:
We want to decorate the +Refinery::Page+ class. Create a file +app/decorators/models/refinery/page_decorator.rb+ with this content:

<ruby>
Refinery::Page.class_eval do
attr_accessible :show_in_footer

def self.footer_menu_pages
where :show_in_footer => true
where show_in_footer: true
end
end
</ruby>

We added +show_in_footer+ to the allowed attributes list so that it doesn't raise a mass-assignment error each time someone tries to save the page. We also added +footer_menu_pages+ class method to abstract away ActiveRecord query method.
We added +footer_menu_pages+ class method to abstract away ActiveRecord query method.

h3. Allow +show_in_footer+ attribute in +Refinery::Admin::PagesController+

Before overriding Refinery's form view, we want to decorate the +Refinery::Admin::PagesController+ class. Create a file +app/decorators/controllers/refinery/admin/pages_controller_decorator.rb+ with this content:

<ruby>
Refinery::Admin::PagesController.class_eval do
# work around from https://github.com/refinery/refinerycms-page-images/blob/master/app/decorators/controllers/refinery/admin/pages_controller_decorator.rb
if self.instance_methods.exclude?(:page_params_with_show_in_footer_params)
def page_params_with_show_in_footer_params
show_in_footer_params = params.require(:page).permit(:show_in_footer)
end

alias_method_chain :page_params, :show_in_footer_params
end
end
</ruby>

We added +show_in_footer+ to the allowed attributes list so that it doesn't raise a mass-assignment error each time someone tries to save the page.

h3. Overriding the form view

As I previously mentioned, let's make it so that a "Show in footer" checkbox appears right after Admin expands the "Advanced options" when editing a page. To do this, we have to override the file [_form_advaned_options.html.erb partial](https://github.com/refinery/refinerycms/blob/2-1-stable/pages/app/views/refinery/admin/pages/_form_advanced_options.html.erb). Type this in the terminal:
As I previously mentioned, let's make it so that a "Show in footer" checkbox appears right after Admin expands the "Advanced options" when editing a page. To do this, we have to override the file [_form_extra_fields_for_more_options.html.erb partial](https://github.com/refinery/refinerycms/blob/master/pages/app/views/refinery/admin/pages/_form_extra_fields_for_more_options.html.erb). Type this in the terminal:

<shell>
rake refinery:override view=refinery/admin/pages/_form_advanced_options.html
rake refinery:override view=refinery/admin/pages/_form_extra_fields_for_more_options.html
</shell>

Now open the +_form_advanced_options.html.erb+ partial and add the following code right after the h2 HTML tag:
Now open the +_form_extra_fields_for_more_options.html.erb+ partial and add the following code right after the commented line :

<html>
<div class='field'>
Expand All @@ -76,10 +94,6 @@ Now open the +_form_advanced_options.html.erb+ partial and add the following cod
</div>
</html>

The end result should look like this:

!/system/images/W1siZiIsIjIwMTMvMTAvMDMvMDZfMzdfMTNfOTMzX3Nob3dfaW5fZm9vdGVyLnBuZyJdXQ/show_in_footer.png!

h3. Creating and configuring the Presenter

Now let's focus on the presenter itself. Once instantiated, it is also possible to configure its CSS/HTML using this instance. We will use a Rails helper to instantiate a new instance of +Refinery::Pages::MenuPresenter+ and also configure it there. We're taking this approach because we don't want to pollute the view with configuration code.
Expand Down

0 comments on commit 5fd285f

Please sign in to comment.