Skip to content

Commit

Permalink
Inject on toolbars
Browse files Browse the repository at this point in the history
  • Loading branch information
Nyxelio committed Nov 8, 2016
1 parent 3579b75 commit 2db859e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
21 changes: 18 additions & 3 deletions app/helpers/toolbar_helper.rb
Expand Up @@ -41,7 +41,7 @@ def export(*natures)
modal_id = nature.name.to_s + '-exporting'
if Document.of(nature.name, key).any?
@template.content_for :popover, @template.render('backend/shared/export', nature: nature, key: key, modal_id: modal_id)
menu.item nature.human_name, '#' + modal_id, data: { toggle: 'modal' }
menu.item nature.human_name, '#' + modal_id, data: {toggle: 'modal'}
else
DocumentTemplate.of_nature(nature.name).each do |template|
menu.item(template.name, @template.params.merge(format: :pdf, template: template.id, key: key))
Expand Down Expand Up @@ -78,10 +78,10 @@ def menu(name, options = {}, &block)
def destroy(options = {})
if @template.resource
if @template.resource.destroyable?
tool(options[:label] || :destroy.ta, { action: :destroy, id: @template.resource.id, redirect: options[:redirect] }, method: :delete, data: { confirm: :are_you_sure_you_want_to_delete.tl })
tool(options[:label] || :destroy.ta, {action: :destroy, id: @template.resource.id, redirect: options[:redirect]}, method: :delete, data: {confirm: :are_you_sure_you_want_to_delete.tl})
end
else
tool(options[:label] || :destroy.ta, { action: :destroy, redirect: options[:redirect] }, { method: :delete }.merge(options.except(:redirect, :label)))
tool(options[:label] || :destroy.ta, {action: :destroy, redirect: options[:redirect]}, {method: :delete}.merge(options.except(:redirect, :label)))
end
end

Expand Down Expand Up @@ -112,6 +112,17 @@ def action(name, *args)
end
end

def view_addons(options = {})
return nil unless options[:controller].present?

options[:action] ||= :index
options[:context] = :toolbar

Ekylibre::Plugin.find_addons(options).collect do |addon|
@template.render partial: addon, locals: {t: self}
end
end

def method_missing(method_name, *args)
raise ArgumentError, 'Block can not be accepted' if block_given?
options = args.extract_options!
Expand All @@ -135,6 +146,10 @@ def toolbar(options = {}, &block)
end
end
end
html << capture(toolbar) do |t|
t.view_addons(controller: controller_name, action: action_name).join.html_safe
end

unless options[:wrap].is_a?(FalseClass)
html = content_tag(:div, html, class: 'toolbar' + (options[:class] ? ' ' << options[:class].to_s : ''))
end
Expand Down
36 changes: 33 additions & 3 deletions lib/ekylibre/plugin.rb
Expand Up @@ -106,10 +106,9 @@ def generate_themes_stylesheets
plugin.themes_assets.each do |name, addons|
next unless name == theme || name == '*' || (name.respond_to?(:match) && theme.match(name))
stylesheet << "// #{plugin.name}\n"
next unless addons[:stylesheets]
addons[:stylesheets].each do |file|
stylesheet << "@import \"#{file}\";\n"
end
end if addons[:stylesheets]
end
end
# <base_dir>/themes/<theme>/plugins.scss
Expand Down Expand Up @@ -143,9 +142,25 @@ def after_login_plugin
def after_login_path(resource)
after_login_plugin.name.to_s.camelize.constantize.after_login_path(resource)
end

def find_addons(options = {})
return unless options[:context]

view_addons = []
search_path = "backend/#{options[:controller]}##{options[:action]}"

each do |plugin|
availables_addons = plugin.view_addons
view_addons << availables_addons[options[:context]][search_path] if availables_addons.key? options[:context] and availables_addons[options[:context]].key? search_path
end

view_addons

end

end

attr_reader :root, :themes_assets, :routes, :javascripts, :initializers
attr_reader :root, :themes_assets, :routes, :javascripts, :initializers, :view_addons
field_accessor :name, :summary, :description, :url, :author, :author_url, :version

# Links plugin into app
Expand All @@ -155,6 +170,7 @@ def initialize(plugfile_path)
@themes_assets = {}.with_indifferent_access
@javascripts = []
@initializers = {}
@view_addons = {}

lib = @root.join('lib')
if File.directory?(lib)
Expand Down Expand Up @@ -315,6 +331,20 @@ def add_routes(&block)
@routes = block
end

def add_toolbar_addon(partial_path, options = {})
return unless options[:to]
context = :toolbar
@view_addons[context] ||= {}
@view_addons[context][options[:to]] = partial_path
end

def add_cobble_addon(partial_path, options = {})
return unless options[:to]
context = :cobble
@view_addons[context] ||= {}
@view_addons[context][options[:to]] = partial_path
end

# Adds menus with DSL in Ekylibre backend nav
def extend_navigation(&block)
Ekylibre::Navigation.exec_dsl(&block)
Expand Down

0 comments on commit 2db859e

Please sign in to comment.