-
Notifications
You must be signed in to change notification settings - Fork 987
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #16725 - add plugin extension point for smart proxies
- Loading branch information
1 parent
ad94991
commit bf610a4
Showing
34 changed files
with
347 additions
and
176 deletions.
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
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
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
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,42 @@ | ||
module SharedSmartProxiesHelper | ||
def smart_proxy_fields(f, options = {}) | ||
object = options.fetch(:object, f.object) | ||
|
||
safe_join(object.smart_proxies.map do |proxy_name, proxy_options| | ||
smart_proxy_select_f(f, proxy_name, options.merge(proxy_options)) | ||
end) | ||
end | ||
|
||
INHERIT_TEXT = N_("inherit") | ||
|
||
def smart_proxy_select_f(f, resource, options) | ||
required = options.fetch(:required, false) | ||
hidden = options[:if].present? && !options[:if].call(f.object) | ||
can_override = options.fetch(:can_override, false) | ||
override = options.fetch(:override, false) | ||
blank = options.fetch(:blank, blank_or_inherit_f(f, resource)) | ||
|
||
proxies = accessible_smart_proxies(f.object, resource, options[:feature]) | ||
return if !required && !proxies.present? | ||
|
||
select_options = { | ||
:disable_button => can_override ? _(INHERIT_TEXT) : nil, | ||
:disable_button_enabled => override && !explicit_value?(:"#{resource}_id"), | ||
:user_set => user_set?(:"#{resource}_id") | ||
} | ||
select_options[:include_blank] = blank unless required | ||
|
||
select_f f, :"#{resource}_id", proxies, :id, :name, | ||
select_options, | ||
:label => _(options[:label]), | ||
:help_inline => _(options[:description]), | ||
:wrapper_class => "form-group #{'hide' if hidden}" | ||
end | ||
|
||
def accessible_smart_proxies(obj, resource, feature) | ||
list = accessible_resource_records(:smart_proxy).with_features(feature).to_a | ||
current = obj.public_send(resource) if obj.respond_to?(resource) | ||
list |= [current] if current.present? | ||
list | ||
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,42 @@ | ||
module BelongsToProxies | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_attribute :smart_proxies_from_model | ||
register_smart_proxies_from_plugins | ||
end | ||
|
||
delegate :smart_proxies, :to => :class | ||
|
||
module ClassMethods | ||
def belongs_to_proxy(name, options) | ||
register_smart_proxy(name, options) | ||
self.smart_proxies_from_model = (smart_proxies_from_model || {}).merge(name => options) | ||
end | ||
|
||
def smart_proxies | ||
(smart_proxies_from_model || {}).merge(smart_proxies_from_plugins) | ||
end | ||
|
||
def register_smart_proxy(name, options) | ||
belongs_to name, :class_name => 'SmartProxy' | ||
validates name, :proxy_features => { :feature => options[:feature] } | ||
end | ||
|
||
def register_smart_proxies_from_plugins | ||
smart_proxies_from_plugins.each do |name, options| | ||
register_smart_proxy(name, options) | ||
end | ||
end | ||
|
||
private | ||
|
||
def smart_proxies_from_model | ||
@smart_proxies_from_model ||= {} | ||
end | ||
|
||
def smart_proxies_from_plugins | ||
Foreman::Plugin.all.map {|plugin| plugin.smart_proxies(self) }.inject({}, :merge) | ||
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
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
Oops, something went wrong.