Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add template whitelist for page tabs. #1943

Merged
merged 2 commits into from

3 participants

Uģis Ozols Philip Arndt Johan
Uģis Ozols
Owner

Original pull request - #1753

jokklan and others added some commits
Johan jokklan Add template whitelist for page tabs. 3b2633c
Uģis Ozols ugisozols When registering tab return an instance of itself.
Main reason for this change is specs. It's so much easier to get ahold
of the tab I just registered than to select it from Refinery::Pages.tabs array.

* Add specs for Refinery::Pages::Tab.
* Return tabs incase user isn't using view_templates.
* Removed attr_accessor from Refinery::Pages module because it's not
needed and it's not even working that way.
feb2a7a
Philip Arndt parndt merged commit feb2a7a into from
Uģis Ozols ugisozols referenced this pull request from a commit
Uģis Ozols ugisozols Add note to changelog about #1943. 63ab0a7
tsemana tsemana referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 14, 2012
  1. Johan Philip Arndt

    Add template whitelist for page tabs.

    jokklan authored parndt committed
  2. Uģis Ozols Philip Arndt

    When registering tab return an instance of itself.

    ugisozols authored parndt committed
    Main reason for this change is specs. It's so much easier to get ahold
    of the tab I just registered than to select it from Refinery::Pages.tabs array.
    
    * Add specs for Refinery::Pages::Tab.
    * Return tabs incase user isn't using view_templates.
    * Removed attr_accessor from Refinery::Pages module because it's not
    needed and it's not even working that way.
This page is out of date. Refresh to see the latest.
4 pages/app/views/refinery/admin/pages/_form_page_parts.html.erb
View
@@ -31,7 +31,7 @@
<%= link_to part.title, "##{@page.persisted? ? part.to_param : "page_part_new_#{part_index}"}" %>
</li>
<% end %>
- <% ::Refinery::Pages.tabs.each_with_index do |tab, tab_index| %>
+ <% ::Refinery::Pages.tabs_for_template(@page.view_template).each_with_index do |tab, tab_index| %>
<li class='ui-state-default' id="custom_<%= tab.name %>_tab">
<%= link_to tab.name.titleize, "#custom_tab_#{tab_index}" %>
</li>
@@ -46,7 +46,7 @@
:part_index => (part_index += 1),
:new_part => @page.new_record? -%>
<% end %>
- <% ::Refinery::Pages.tabs.each_with_index do |tab, tab_index| %>
+ <% ::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>
19 pages/lib/refinery/pages/tab.rb
View
@@ -1,14 +1,20 @@
module Refinery
module Pages
- attr_accessor :tabs
-
def self.tabs
@tabs ||= []
end
+
+ def self.tabs_for_template(template)
+ return tabs unless template
+
+ tabs.select do |tab|
+ tab.templates.include?('all') || tab.templates.include?(template)
+ end
+ end
class Tab
- attr_accessor :name, :partial
+ attr_accessor :name, :partial, :templates
def self.register(&block)
tab = self.new
@@ -17,12 +23,17 @@ def self.register(&block)
raise "A tab MUST have a name!: #{tab.inspect}" if tab.name.blank?
raise "A tab MUST have a partial!: #{tab.inspect}" if tab.partial.blank?
+
+ tab.templates = %w[all] if tab.templates.blank?
+ tab.templates = Array(tab.templates)
+
+ tab
end
protected
def initialize
- ::Refinery::Pages.tabs << self # add me to the collection of registered page tabs
+ Refinery::Pages.tabs << self # add me to the collection of registered page tabs
end
end
89 pages/spec/lib/pages/tab_spec.rb
View
@@ -0,0 +1,89 @@
+require "spec_helper"
+
+module Refinery
+ module Pages
+
+ describe ".tabs" do
+ after do
+ Refinery::Pages.instance_eval { @tabs = [] }
+ end
+
+ it "returns an array of registered tabs" do
+ rspec_tab = Refinery::Pages::Tab.register do |tab|
+ tab.name = "rspec"
+ tab.partial = "rspec"
+ end
+
+ Refinery::Pages.tabs.should include(rspec_tab)
+ end
+ end
+
+ describe ".tabs_for_template" do
+ after do
+ Refinery::Pages.instance_eval { @tabs = [] }
+ end
+
+ it "returns all tabs for which #templates hasn't been set" do
+ rspec_tab = Refinery::Pages::Tab.register do |tab|
+ tab.name = "rspec"
+ tab.partial = "rspec"
+ end
+
+ Refinery::Pages.tabs_for_template("huh").should include(rspec_tab)
+ end
+
+ it "returns tabs with matched template" do
+ rspec_tab = Refinery::Pages::Tab.register do |tab|
+ tab.name = "rspec"
+ tab.partial = "rspec"
+ tab.templates = "rspec"
+ end
+
+ Refinery::Pages.tabs_for_template("rspec").should include(rspec_tab)
+ end
+ end
+
+ describe Tab do
+ after do
+ Refinery::Pages.instance_eval { @tabs = [] }
+ end
+
+ describe ".register" do
+ it "requires name to be set" do
+ lambda {
+ Refinery::Pages::Tab.register do |tab|
+ tab.partial = "rspec"
+ end
+ }.should raise_error(/A tab MUST have a name!/)
+ end
+
+ it "requires partial to be set" do
+ lambda {
+ Refinery::Pages::Tab.register do |tab|
+ tab.name = "rspec"
+ end
+ }.should raise_error(/A tab MUST have a partial!/)
+ end
+
+ it "sets #templates if it's not set" do
+ rspec_tab = Refinery::Pages::Tab.register do |tab|
+ tab.name = "rspec"
+ tab.partial = "rspec"
+ end
+
+ rspec_tab.templates.should eq(["all"])
+ end
+
+ it "converts #templates to array if it's not an array already" do
+ rspec_tab = Refinery::Pages::Tab.register do |tab|
+ tab.name = "rspec"
+ tab.partial = "rspec"
+ tab.templates = "rspec"
+ end
+
+ rspec_tab.templates.should eq(["rspec"])
+ end
+ end
+ end
+ end
+end
Something went wrong with that request. Please try again.