Skip to content

Instantly share code, notes, and snippets.

@anitagraham
Last active August 29, 2015 14:03
Show Gist options
  • Save anitagraham/64261524f13a3ec89bc5 to your computer and use it in GitHub Desktop.
Save anitagraham/64261524f13a3ec89bc5 to your computer and use it in GitHub Desktop.
Integrate plugins
# refinerycms-pages/views/admin/pages/_form_page_parts.html.erb
.....
<div id='page_part_editors'>
<% part_index = -1 %>
<%= f.fields_for :parts do |p| %>
<% template = p.object[:template] || 'page_part_field' %>
<%= render template,
:part => p.object,
:part_index => (part_index += 1),
:new_part => @page.new_record? -%>
<% end %>
<% ::Refinery::Pages.tabs_for_template(@page.view_template).each_with_index do |tab, tab_index| %>
<div class='page_part' id='<%= "custom_tab_#{tab_index}" %>'>
<%= render tab.partial, :f => f %>
</div>
<% end %>
</div>
# refinerycms-testimonials/app/views/refinery/admin/pages/_position_tab.html.erb
<div class='page_part position_only' id='<%= new_part ? "page_part_new_#{part_index}" : part.to_param %>'>
<h1><%= t('.title')%></h1>
<p> <%= t('.reorder_only')%>
</div>
# refinerycms-testimonials/app/decorators/models/refinery/page_decorator.rb
# Open the Refinery::Page model for manipulation
Refinery::Page.class_eval do
# Whitelist the testimonials control fields
attr_accessible :testimonials_show, :testimonials_count, :testimonials_select
validates :testimonials_count, :numericality => { :only_integer => true, :greater_than_or_equal_to=>0 }
before_save :testimonial_page_part
def testimonial_page_part
if testimonials_show && !part_with_title('Testimonials')
parts.build(title: 'Testimonials', position: parts.count, template: 'position_tab')
end
end
end
@anitagraham
Copy link
Author

Notes

  • A new field on the page_parts table for a template to use when editing the page_part
  • Instead of a defining a tab the engine now provides a page_part_template
  • If a page_part is needed the engine adds it to the page and specifies the template to use
  • The template shown here does nothing - but has to exist so the user can drag it when reordering page parts
  • The template could be an image-picker or anything other way of manipulating content
  • An alternative approach is to store the plugin name instead of the template_name. The plugin can define the template as one its attributes (_page_part_template_name?). This could be used to supply different templates in different conditions. (Can't think of why someone might want to do this, but it could happen). It also makes it easier to change what template you are using without messing with the db.

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