Skip to content

Instantly share code, notes, and snippets.

@jipiboily
Created April 8, 2012 18:12
Show Gist options
  • Save jipiboily/2338880 to your computer and use it in GitHub Desktop.
Save jipiboily/2338880 to your computer and use it in GitHub Desktop.
How to setup a Refinery custom content type and view (aka "page template")
<!-- path: app/views/refinery/admin/pages/_actions.html.erb -->
<!--
You could override this view, if you want to add links to each page type via the admin UI. If you don't you'll have to add a param the the URL.
WARNING: You most probably want to do it cleaner than that and find a way to hook to this view instead of overriding it if at all possible.
-->
<ul>
<li>
<%= render '/refinery/admin/search', :url => refinery.admin_pages_path %>
</li>
<li>
<%= link_to t('.create_new_page') + " (standard)", refinery.new_admin_page_path,
:class => "add_icon" %>
</li>
<% Refinery::Pages.types.each do |type| %>
<li>
<%= link_to t('.create_new_page') + " (#{type.name.to_s})", refinery.new_admin_page_path + "?view_template=" + type.name.to_s, :class => "add_icon" %>
</li>
<% end %>
<% if @pages.many? and !searching? %>
<li>
<%= link_to t('.reorder_pages'), refinery.admin_pages_path,
:id => "reorder_action",
:class => "reorder_icon" %>
<%= link_to t('.reorder_pages_done'), refinery.admin_pages_path,
:id => "reorder_action_done",
:style => "display: none;",
:class => "reorder_icon" %>
</li>
<% end %>
</ul>
<!--
path: app/views/refinery/pages/my_new_view.html.erb
usage: this is a view_template you can choose in the advanced part of a page creation, see the initializer to enable it.
-->
<%= raw @page.content_for(:intro) %>
<%= raw @page.content_for(:body) %>
<%= raw @page.content_for(:right) %>
<%= raw @page.content_for(:footer) %>
<%= raw @page.content_for(:related) %>
#config/initializers/refinery/pages.rb
Refinery::Pages.configure do |config|
# ...
# this is how to define a new type of page with custom parts.
config.types.register :my_content_type do |content_type|
content_type.parts = %w[intro body right footer related]
end
# this is the list of views that you can choose from the admin UI, see the added custom view template.
config.view_template_whitelist = ["home", "show", "my_new_view"]
# this will enable the use of custom view templates.
config.use_view_templates = true
# ...
end
@jipiboily
Copy link
Author

@Soldo: yes, Rob's #2 (best choice) is an alternative. It all depends on your needs. If you only need different fields and layout, the current gist is probably the easiest solution. If you have too much customization, you might want to do it with the standard Rails approach: route, controller and everything.

For your override of pages controller, how did you proceed? Was it with a Refinery::Pages.class_eval block in a decorator file?

What are your exact needs vs that? Is it only a few different fields with different layouts only but with different functionnality? There is multiple solutions all depending on the exact problem to solve...

@jipiboily
Copy link
Author

@Soldo: maybe create a gist of what you did, what you want and link it and we could continue the discussion there? That would be easier for me to help you if I could take a look at some pieces of your code. Thanks!

@Soldo
Copy link

Soldo commented Apr 10, 2012

@jipiboily, I am always pleasantly surprised how people like you are always ready to help. Thanks!

Concerning the code, I'd have to say that I've already posted all my code. I started to work on the new project. And these things we are discussing here (defining layouts, and "yield" parts) are, as I believe, the things that should be done very late, if not first during the development of a project.
I am just trying to get this "2. option" robyurkowski suggested to work. So, as I said before, I've overridden pages_controller and added:
def bidon
render_with_templates?
end

Then I've created "bidon.html.erb" and placed just some silly text I want to be returned in my browser, just to see that its working. I wrote "Hello world" inside h1 tags.
And then I tried to add a route inside routes.rb file (config folder). I tried with "match '/novi-template' => 'pages#bidon'" line.

I'm not sure which part I didn't do right, but I've thought that route was the wrong one. I'm not 100% sure about that. The fact is that I can't get the content I placed in "bidon.html.erb" file, that is "hello world".
Thanks one more time.

@Soldo
Copy link

Soldo commented Apr 10, 2012

Just to add this, maybe you'll think it's useful:
When I hit "rake routes" command, I got this output (it's just a part of the output):

[slasticarnica]$ rake routes
refinery / Refinery::Core::Engine
/novi-template(.:format) pages#bidon

Routes for Refinery::Core::Engine:

                  /system/resources/*dragonfly(.:format)       <Dragonfly::App name=:refinery_resources >

insert_admin_resources GET /refinery/resources/insert(.:format) refinery/admin/resources#insert
admin_resources GET /refinery/resources(.:format) refinery/admin/resources#index
POST /refinery/resources(.:format) refinery/admin/resources#create
new_admin_resource GET /refinery/resources/new(.:format) refinery/admin/resources#new
...

Looking at the route "/novi-template(.:format) pages#bidon" I notice it's not same as other routes, so maybe the problem is here. What do you think?

@Soldo
Copy link

Soldo commented Apr 10, 2012

@jipiboily, also I noticed one other thing, and that is: even if my "bidon.html.erb" file is completely empty, I still get the same result.
What I did was I deleted bidon.html.erb (my_new_view.html.erb file you posted) with this content:

<%= raw @page.content_for(:intro) %>
<%= raw @page.content_for(:body) %>
<%= raw @page.content_for(:right) %>
<%= raw @page.content_for(:footer) %>
<%= raw @page.content_for(:related) %>

And what happened was, I got a content from "intro" section, and that was the content I defined in backend (administration).

This is becoming more and more confusing. Do you have some logical explanation? Thanks!

@jipiboily
Copy link
Author

@Soldo: please create a gist with ALL the content of your routes.rb, PagesController override, pages.rb initializer, your new view (bidon or my_new_view, whichever is current). Currently, I don't have enough material to give you an explanation. I would need all your code concerning the problem. Can you also tell me what are the exact specs of what your are trying to do.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment