Module: Cmtool::ApplicationHelper

Defined in:
app/helpers/cmtool/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#application_titleObject

Return the title of the application



5
6
7
# File 'app/helpers/cmtool/application_helper.rb', line 5

def application_title
  'Cmtool'
end

#are_you_sure(obj = nil) ⇒ Object Also known as: are_you_sure?



48
49
50
51
52
53
54
# File 'app/helpers/cmtool/application_helper.rb', line 48

def are_you_sure(obj = nil)
  if name = obj && obj.respond_to?(:name) && obj.name.presence
    t('cmtool.general.are_you_sure_with_name', name: name, model: obj.class.model_name.human)
  else
    t('cmtool.general.are_you_sure')
  end
end

#boolean_text(yes) ⇒ Object



61
62
63
# File 'app/helpers/cmtool/application_helper.rb', line 61

def boolean_text(yes)
  yes.present? ? t('cmtool.general.yes') : t('cmtool.general.no')
end

#cmtool_userObject

Add extra helper method since cmtool layouts may be used outside the cmtool scope



129
130
131
# File 'app/helpers/cmtool/application_helper.rb', line 129

def cmtool_user
  controller.respond_to?(:cmtool_user) ? controller.send(:cmtool_user) :nil
end

#collapsible_content(options = {}, &blk) ⇒ Object

This is a wrapper to create collapsible content.



107
108
109
110
111
112
113
114
115
# File 'app/helpers/cmtool/application_helper.rb', line 107

def collapsible_content(options = {}, &blk)
  options = {title: options} if options.is_a?(String) # Single argument is title
  content = capture(&blk) if blk.present?
  content ||= options[:content]
  options[:collapsed] = true unless options.has_key?(:collapsed)
  classes = Array.wrap(options[:class]) | ["collapsible-container", options[:collapsed] ? 'collapsed' : nil]
  title_tag = (:div, "<span></span>#{options[:title]}".html_safe, class: 'collapsible-title')
  (:div, title_tag + (:div, content, class: 'collapsible-content'), class: classes)
end

#create_button_text(obj = nil) ⇒ Object



83
84
85
# File 'app/helpers/cmtool/application_helper.rb', line 83

def create_button_text(obj = nil)
  t('cmtool.action.create.label', model: obj ? obj.class.model_name.human : '')
end

#destroy_td(obj) ⇒ Object



140
141
142
143
144
145
146
# File 'app/helpers/cmtool/application_helper.rb', line 140

def destroy_td(obj)
  (
    :td,
    link_to((:span, '', class: [:destroy, 'fa fa-lg fa-trash']), cmtool.url_for(obj), method: :delete, class: 'tiny alert button', data: {confirm: are_you_sure(obj) }),
    class: [:action, :destroy]
  )
end

#edit_td(obj) ⇒ Object



133
134
135
136
137
138
139
# File 'app/helpers/cmtool/application_helper.rb', line 133

def edit_td(obj)
  (
    :td,
    link_to(((:span, '', class: [:edit, 'fa fa-lg fa-pencil'])), cmtool.url_for([:edit, obj]), class: 'tiny warning button'),
    class: [:action, :edit]
  )
end

#empty_result(model) ⇒ Object



64
65
66
# File 'app/helpers/cmtool/application_helper.rb', line 64

def empty_result(model)
  t('cmtool.general.empty_result', models: model.model_name.human_plural.downcase )
end

#human_size(n) ⇒ Object



117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/cmtool/application_helper.rb', line 117

def human_size(n)
  return '0 MB' unless n
  n = n.to_i
  count = 0
  while  n >= 1024 and count < 4
    n /= 1024.0
    count += 1
  end
  format("%.2f",n) + %w(B KB MB GB TB)[count]
end

#l(*args) ⇒ Object

overwrite i18n l, to handle nil values



18
19
20
21
# File 'app/helpers/cmtool/application_helper.rb', line 18

def l(*args)
  return '' unless args.first
  super(*args)
end


89
90
91
# File 'app/helpers/cmtool/application_helper.rb', line 89

def link_separator
  ' | '
end


79
80
81
# File 'app/helpers/cmtool/application_helper.rb', line 79

def link_to_destroy_content(obj)
  t('cmtool.action.destroy.title', model: obj.class.model_name.human)
end


70
71
72
# File 'app/helpers/cmtool/application_helper.rb', line 70

def link_to_edit_content(obj)
  t('cmtool.action.edit.title', model: obj.class.model_name.human)
end


76
77
78
# File 'app/helpers/cmtool/application_helper.rb', line 76

def link_to_index_content(model)
  t('cmtool.action.index.title', models: model.model_name.human_plural)
end


67
68
69
# File 'app/helpers/cmtool/application_helper.rb', line 67

def link_to_new_content(model)
  t('cmtool.action.new.title', model: model.model_name.human)
end


73
74
75
# File 'app/helpers/cmtool/application_helper.rb', line 73

def link_to_show_content(obj)
  t('cmtool.action.show.title', model: obj.class.model_name.human)
end

#no_content_given(model) ⇒ Object

Standard return content when empty listing is found called with the model as argument

= no_content_given Album


13
14
15
# File 'app/helpers/cmtool/application_helper.rb', line 13

def no_content_given(model)
  t('helpers.no_content_given', models: model.model_name.human_plural)
end

#site_titleObject



23
24
25
# File 'app/helpers/cmtool/application_helper.rb', line 23

def site_title
  'Cmtool'
end

#title(*args) ⇒ Object

Set the page title, allow setting a resource title like:

- title :index, Album

or

- title :show, Album

or normal text behaviour:

- title "Home"


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/cmtool/application_helper.rb', line 33

def title(*args)
  content_for :title do
    if args.first.is_a?(Symbol) && (args[1].respond_to?(:model_name) || args[1].class.respond_to?(:model_name))
      model = args[1].respond_to?(:model_name) ? args[1] : args[1].class
      if args.first == :index
        t('cmtool.action.index.title', models: model.model_name.human_plural)
      else
        t("cmtool.action.#{args.first}.title", model: model.model_name.human)
      end
    else
      raise 'Imporoper title declaration'
      args.first
    end
  end
end

#tree_options_for_select(roots, options = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/helpers/cmtool/application_helper.rb', line 92

def tree_options_for_select(roots, options = {})
  iterator = Proc.new do |add_to, elements|
    for element in elements
      if !options[:exclude].present? || (element.path_ids & Array.wrap(options[:exclude])).empty?
        add_to << ['- '*element.tree_depth + element.name, element.id]
        iterator.call(add_to, element.children) if element.children.any?
      end
    end
  end
  options_ary = []
  iterator.call(options_ary, roots)
  options_for_select(options_ary, options[:selected])
end

#update_button_text(obj = nil) ⇒ Object



86
87
88
# File 'app/helpers/cmtool/application_helper.rb', line 86

def update_button_text(obj = nil)
  t('cmtool.action.update.label', model: obj ? obj.class.model_name.human : '')
end

#warningsObject



57
58
59
# File 'app/helpers/cmtool/application_helper.rb', line 57

def warnings
  @cmtool_warnings || []
end