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

#create_button_text(obj) ⇒ Object



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

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

#destroy_td(obj) ⇒ Object



123
124
125
126
127
128
129
# File 'app/helpers/cmtool/application_helper.rb', line 123

def destroy_td(obj)
  (
    :td, 
    link_to((:span, t('cmtool.table.index.destroy'), class: [:destroy, 'icon-trash']), cmtool.url_for(obj), method: :delete, confirm: are_you_sure(obj), class: [:destroy, :btn, 'btn-danger', 'btn-mini']),
    class: [:action, :destroy]
  )
end

#edit_td(obj) ⇒ Object



116
117
118
119
120
121
122
# File 'app/helpers/cmtool/application_helper.rb', line 116

def edit_td(obj)
  (
    :td, 
    link_to(((:span, t('cmtool.table.index.edit'), class: [:edit, 'icon-pencil'])), cmtool.url_for([:edit, obj]), class: [:edit, :btn, 'btn-warning', 'btn-mini']), 
    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



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

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) ⇒ Object



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

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

#warningsObject



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

def warnings
  @cmtool_warnings || []
end