Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #12716 - Extensible pages for smart proxy with mount points
- Loading branch information
Showing
9 changed files
with
182 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
module PageletsHelper | ||
def pagelets_for(mountpoint) | ||
Pagelets::Manager.sorted_pagelets_at("#{controller_name}/#{action_name}", mountpoint) | ||
end | ||
|
||
def render_pagelets_for(mountpoint, opts = {}) | ||
result = "" | ||
pagelets_for(mountpoint).each do |pagelet| | ||
result += render_pagelet(pagelet, opts) | ||
end | ||
result.html_safe | ||
end | ||
|
||
def render_tab_content_for(mountpoint, opts = {}) | ||
result = "" | ||
pagelets_for(mountpoint).each do |pagelet| | ||
result += "<div id='#{pagelet.id}' class='tab-pane'>" | ||
result += render_pagelet(pagelet, opts) | ||
result += "</div>" | ||
end | ||
result.html_safe | ||
end | ||
|
||
def render_tab_header_for(mountpoint, opts = {}) | ||
result = "" | ||
pagelets_for(mountpoint).each do |pagelet| | ||
result += "<li><a href='##{pagelet.id}' data-toggle='tab'>#{pagelet.name}</a></li>" | ||
end | ||
result.html_safe | ||
end | ||
|
||
def render_pagelet(pagelet, opts) | ||
render(pagelet.partial, opts.merge!({ :pagelet => pagelet })).html_safe | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Pagelets | ||
class Manager | ||
def initialize(page_name) | ||
@page_name = page_name | ||
end | ||
|
||
def add_pagelet(mountpoint, opts) | ||
self.class.add_pagelet(@page_name, mountpoint, opts) | ||
end | ||
|
||
class << self | ||
def add_pagelet(page_name, mountpoint, opts) | ||
handle_empty_keys_for page_name, mountpoint | ||
raise ::Foreman::Exception.new(N_("Cannot add pagelet to page %s without partial", page_name)) unless opts[:partial] | ||
raise ::Foreman::Exception.new(N_("Cannot add pagelet to page %s without mountpoint", page_name)) if mountpoint.nil? | ||
priority = opts[:priority] || default_pagelet_priority(page_name, mountpoint) | ||
pagelet = Pagelets::Pagelet.new(opts.delete(:name), opts.delete(:partial), priority, opts) | ||
@pagelets[page_name][mountpoint] << pagelet | ||
end | ||
|
||
def default_pagelet_priority(page_name, mountpoint) | ||
# We need a default priority value for the first pagelet if it is not specified | ||
@pagelets[page_name][mountpoint].map(&:priority).push(0).max + 100 | ||
end | ||
|
||
def pagelets_at(page_name, mountpoint) | ||
handle_empty_keys_for page_name, mountpoint | ||
@pagelets[page_name][mountpoint] | ||
end | ||
|
||
def sorted_pagelets_at(page_name, mountpoint) | ||
pagelets_at(page_name, mountpoint).sort | ||
end | ||
|
||
private | ||
|
||
def handle_empty_keys_for(page_name, mountpoint) | ||
@pagelets ||= {}.with_indifferent_access | ||
@pagelets[page_name] ||= {} | ||
@pagelets[page_name][mountpoint] ||= [] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Pagelets | ||
class Pagelet | ||
attr_reader :name, :partial, :priority, :opts | ||
|
||
def initialize(name, partial, priority, opts) | ||
@name = name | ||
@partial = partial | ||
@priority = priority | ||
@opts = opts | ||
end | ||
|
||
def <=>(other) | ||
priority <=> other.priority | ||
end | ||
|
||
def id | ||
opts[:id] || 'pagelet-id-' + @name.gsub(/\s+/, "_").underscore | ||
end | ||
|
||
def method_missing(method_name, *arguments, &block) | ||
@opts[method_name] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'test_helper' | ||
|
||
class PageletManagerTest < ActiveSupport::TestCase | ||
test 'should assign default priority' do | ||
::Pagelets::Manager.add_pagelet("test", :test_point, :partial => "tests") | ||
assert_equal 100, ::Pagelets::Manager.pagelets_at("test", :test_point).first.priority | ||
end | ||
|
||
test 'should return sorted pagelets at mountpoint' do | ||
assert_equal 0, ::Pagelets::Manager.sorted_pagelets_at("test", :mountpoint).count | ||
::Pagelets::Manager.add_pagelet("test", :mountpoint, :partial => "tests", :priority => 20) | ||
::Pagelets::Manager.add_pagelet("test", :mountpoint, :partial => "tests", :priority => 15) | ||
::Pagelets::Manager.add_pagelet("test", :mountpoint, :partial => "tests", :priority => 5) | ||
assert_equal 3, ::Pagelets::Manager.sorted_pagelets_at("test", :mountpoint).count | ||
assert_equal 5, ::Pagelets::Manager.sorted_pagelets_at("test", :mountpoint).first.priority | ||
end | ||
|
||
test 'should add default priority' do | ||
::Pagelets::Manager.add_pagelet("test", :point, :partial => "tests") | ||
::Pagelets::Manager.add_pagelet("test", :point, :partial => "tests") | ||
|
||
assert_equal 100, ::Pagelets::Manager.sorted_pagelets_at("test", :point).first.priority | ||
assert_equal 200, ::Pagelets::Manager.sorted_pagelets_at("test", :point).last.priority | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'test_helper' | ||
|
||
class PageletTest < ActiveSupport::TestCase | ||
test 'order pagelets asc' do | ||
pagelets = [] | ||
pagelets << ::Pagelets::Pagelet.new("first", "tests/test", 40, {}) | ||
pagelets << ::Pagelets::Pagelet.new("first", "tests/test", 50, {}) | ||
pagelets << ::Pagelets::Pagelet.new("first", "tests/test", 10, {}) | ||
pagelets << ::Pagelets::Pagelet.new("first", "tests/test", 20, {}) | ||
pagelets = pagelets.sort | ||
|
||
assert_equal 10, pagelets[0].priority | ||
assert_equal 50, pagelets[3].priority | ||
end | ||
|
||
test 'should show id for pagelet' do | ||
pagelet = ::Pagelets::Pagelet.new("test pagelet", "tests/show", 50, {}) | ||
assert_equal "test pagelet", pagelet.name | ||
assert_equal "pagelet-id-test_pagelet", pagelet.id | ||
end | ||
|
||
test 'should override default id' do | ||
pagelet = ::Pagelets::Pagelet.new("test pagelet", "tests/show", 50, { :id => "custom-id"}) | ||
assert_equal "test pagelet", pagelet.name | ||
assert_equal "custom-id", pagelet.id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33f7c66
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some documentation about this?
33f7c66
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dLobatog appears to be at http://projects.theforeman.org/projects/foreman/wiki/How_to_Create_a_Plugin#Adding-a-Pagelet and the linked page.