Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update guide Menu Presenter for Refinery 3.0 #2953

Merged
merged 2 commits into from Apr 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 30 additions & 19 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,45 @@ 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.prepend(
Module.new do
def permitted_page_params
super << :show_in_footer
end
end
)
</ruby>

We added +show_in_footer+ to the permitted 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 +91,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 All @@ -90,11 +101,11 @@ Open the ApplicationHelper and add this code:
def footer_menu
menu_items = Refinery::Menu.new(Refinery::Page.footer_menu_pages)

presenter = Refinery::Pages::MenuPresenter.new(menu_items, self)
presenter.dom_id = "footer_menu"
presenter.css = "footer_menu"
presenter.menu_tag = :div
presenter
Refinery::Pages::MenuPresenter.new(menu_items, self).tap do |presenter|
presenter.dom_id = "footer_menu"
presenter.css = "footer_menu"
presenter.menu_tag = :div
end
end
</ruby>

Expand Down
14 changes: 9 additions & 5 deletions pages/app/controllers/refinery/admin/pages_controller.rb
Expand Up @@ -90,11 +90,7 @@ def valid_view_templates
end

def page_params
params.require(:page).permit(
:browser_title, :draft, :link_url, :menu_title, :meta_description,
:parent_id, :skip_to_first_child, :show_in_menu, :title, :view_template,
:layout_template, :custom_slug, parts_attributes: [:id, :title, :body, :position]
)
params.require(:page).permit(permitted_page_params)
end

def new_page_params
Expand All @@ -103,6 +99,14 @@ def new_page_params

private

def permitted_page_params
[
:browser_title, :draft, :link_url, :menu_title, :meta_description,
:parent_id, :skip_to_first_child, :show_in_menu, :title, :view_template,
:layout_template, :custom_slug, parts_attributes: [:id, :title, :body, :position]
]
end

def save_and_continue_locals(page)
nested_url = page.nested_url
{
Expand Down