Class: ToolbarHelper::Toolbar

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/toolbar_helper.rb

Overview

This class permit to register the composition of a toolbar

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Toolbar


4
5
6
# File 'app/helpers/toolbar_helper.rb', line 4

def initialize(template)
  @template = template
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Raises:

  • (ArgumentError)

94
95
96
97
98
99
100
# File 'app/helpers/toolbar_helper.rb', line 94

def method_missing(method_name, *args)
  raise ArgumentError, 'Block can not be accepted' if block_given?
  options = args.extract_options!
  name = method_name.to_s.gsub(/\_+$/, '').to_sym
  record = args.shift
  action(name, record, options)
end

Instance Method Details

#action(name, *args) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/toolbar_helper.rb', line 68

def action(name, *args)
  options = args.extract_options!
  record = args.shift
  url = {}
  url.update(options.delete(:params)) if options[:params].is_a? Hash
  url[:controller] ||= @template.controller_path
  url[:action] ||= name
  url[:id] = record.id if record && record.class < ActiveRecord::Base
  action_label = options[:label] || I18n.t(name, scope: 'rest.actions')
  if options[:variants]
    variants = options.delete(:variants)
    # variants ||= { action_label => url } if authorized?(url)
    # variants ||= {}
    menu(action_label) do |menu|
      variants.each do |name, url_options, link_options|
        variant_url = url.merge(url_options)
        if authorized?(variant_url)
          menu.item(name, variant_url, options.slice(:method, 'data-confirm').merge(link_options || {}))
        end
      end
    end
  else
    tool(action_label, url, options)
  end
end

#authorized?(*args) ⇒ Boolean


8
9
10
# File 'app/helpers/toolbar_helper.rb', line 8

def authorized?(*args)
  @template.authorized?(*args)
end

#destroy(options = {}) ⇒ Object


58
59
60
61
62
63
64
65
66
# File 'app/helpers/toolbar_helper.rb', line 58

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 })
    end
  else
    tool(options[:label] || :destroy.ta, { action: :destroy, redirect: options[:redirect] }, { method: :delete }.merge(options.except(:redirect, :label)))
  end
end

#export(*natures) ⇒ Object


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/toolbar_helper.rb', line 33

def export(*natures)
  options = natures.extract_options!
  record = options[:resource] || @template.resource
  options[:key] ||= (record ? :number : Time.zone.now.strftime('%Y%m%d%H%M%S'))
  key = (options[:key].is_a?(Symbol) ? record.send(options[:key]) : options[:key]).to_s
  @template.dropdown_menu_button(:print) do |menu|
    natures.each do |nature_name|
      nature = Nomen::DocumentNature.find(nature_name)
      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' }
      else
        DocumentTemplate.of_nature(nature.name).each do |template|
          menu.item(template.name, @template.params.merge(format: :pdf, template: template.id, key: key))
        end
      end
    end
  end
end

#group(options = {}, &_block) ⇒ Object


12
13
14
15
16
17
18
# File 'app/helpers/toolbar_helper.rb', line 12

def group(options = {}, &_block)
  raise 'Nested group are forbidden' unless @group.nil?
  options[:class] = options[:class].to_s + ' btn-group'
  @template.(:div, options) do
    yield(self)
  end
end

#mail_to(email_address, name = nil, html_options = {}, &block) ⇒ Object


24
25
26
27
28
29
30
31
# File 'app/helpers/toolbar_helper.rb', line 24

def mail_to(email_address, name = nil, html_options = {}, &block)
  if html_options[:class]
    html_options[:class] << ' btn btn-default icn btn-mail'
  else
    html_options[:class] = ' btn btn-default icn btn-mail'
  end
  @template.mail_to(email_address, name, html_options, &block)
end

54
55
56
# File 'app/helpers/toolbar_helper.rb', line 54

def menu(name, options = {}, &block)
  @template.dropdown_menu_button(name, options, &block)
end

#tool(name, url, options = {}) ⇒ Object


20
21
22
# File 'app/helpers/toolbar_helper.rb', line 20

def tool(name, url, options = {})
  @template.tool_to(name, url, options) if authorized?(url)
end