Module: LayoutHelper

Defined in:
app/helpers/layout_helper.rb

Instance Method Summary collapse

Instance Method Details

#alert(opts = {}) ⇒ Object


224
225
226
227
228
229
230
231
232
233
234
235
# File 'app/helpers/layout_helper.rb', line 224

def alert opts = {}
  opts[:close] ||= true
  opts[:header] ||= _("Warning!")
  opts[:text] ||= _("Alert")
   :div, :class => "alert #{opts[:class]}" do
    result = "".html_safe
    result += alert_close if opts[:close]
    result += alert_header(opts[:header])
    result += opts[:text].html_safe
    result
  end
end

#alert_closeObject


241
242
243
# File 'app/helpers/layout_helper.rb', line 241

def alert_close
  "<a class='close' href='#' data-dismiss='alert'>&times;</a>".html_safe
end

#alert_header(text) ⇒ Object


237
238
239
# File 'app/helpers/layout_helper.rb', line 237

def alert_header text
"<h4 class='alert-heading'>#{text}</h4>".html_safe
end

#base_errors_for(obj) ⇒ Object


163
164
165
166
167
168
169
# File 'app/helpers/layout_helper.rb', line 163

def base_errors_for obj
  unless obj.errors[:base].blank?
    (:div, :class => "alert alert-message alert-block alert-error base in fade") do
      ("<a class='close' href='#' data-dismiss='alert'>&times;</a><h4>" + _("Unable to save") + "</h4> " + obj.errors[:base].map {|e| "<li>".html_safe + e + "</li>".html_safe}.join).html_safe
    end
  end
end

#button_group(*elements) ⇒ Object


11
12
13
# File 'app/helpers/layout_helper.rb', line 11

def button_group *elements
    (:div,:class=>"btn-group") { elements.join(" ").html_safe }
end

#checkbox_f(f, attr, options = {}, checked_value = "1", unchecked_value = "0") ⇒ Object


53
54
55
56
57
58
59
60
61
62
# File 'app/helpers/layout_helper.rb', line 53

def checkbox_f(f, attr, options = {}, checked_value="1", unchecked_value="0")
  text = options.delete(:help_text)
  inline = options.delete(:help_inline)
  field(f, attr, options) do
    label_tag('', :class=>'checkbox') do
      help_inline = inline.blank? ? '' : (:span, inline, :class => "help-inline")
      f.check_box(attr, options, checked_value, unchecked_value) + " #{text} " + help_inline.html_safe
    end
  end
end

#field(f, attr, options = {}) ⇒ Object


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/helpers/layout_helper.rb', line 118

def field(f, attr, options = {})
  fluid = options[:fluid]
  error = f.object.errors[attr] if f && f.object.respond_to?(:errors)
  help_inline = help_inline(options.delete(:help_inline), error)

  help_block  = (:span, options.delete(:help_block), :class => "help-block")
   :div, :class => "control-group #{fluid ? "row-fluid" : ""} #{error.empty? ? "" : 'error'}" do
    label   = options.delete(:label)

    label ||= ((clazz = gettext_key(f.object.class)).respond_to?(:gettext_translation_for_attribute_name) &&
                s_(clazz.gettext_translation_for_attribute_name attr)) if f

    label_tag(attr, label, :class=>"control-label").html_safe +
      (:div, :class => "controls") do
        yield.html_safe + help_inline.html_safe + help_block.html_safe
      end.html_safe
  end
end

#file_field_f(f, attr, options = {}) ⇒ Object


112
113
114
115
116
# File 'app/helpers/layout_helper.rb', line 112

def file_field_f(f, attr, options = {})
  field(f, attr, options) do
    f.file_field attr, options
  end
end

#form_for(record_or_name_or_array, *args, &proc) ⇒ Object


205
206
207
208
209
210
211
212
# File 'app/helpers/layout_helper.rb', line 205

def form_for(record_or_name_or_array, *args, &proc)
  if args.last.is_a?(Hash)
    args.last[:html] = {:class=>"form-horizontal well"}.merge(args.last[:html]||{})
  else
    args << {:html=>{:class=>"form-horizontal well"}}
  end
  super record_or_name_or_array, *args, &proc
end

#gettext_key(aclass) ⇒ Object


137
138
139
# File 'app/helpers/layout_helper.rb', line 137

def gettext_key(aclass)
  aclass.respond_to?(:base_class) ? aclass.base_class : aclass
end

#help_inline(inline, error) ⇒ Object


141
142
143
144
145
146
147
148
149
150
151
# File 'app/helpers/layout_helper.rb', line 141

def help_inline(inline, error)
  help_inline = error.empty? ? inline : error.to_sentence.html_safe
  case help_inline
    when blank?
      ""
    when :indicator
      (:span, image_tag('spinner.gif', :class => 'hide'), :class => "help-inline")
    else
      (:span, help_inline, :class => "help-inline")
  end
end

#icon_text(i, text = "", opts = {}) ⇒ Object


220
221
222
# File 'app/helpers/layout_helper.rb', line 220

def icon_text(i, text="", opts = {})
  ((:i,"", :class=>"icon-#{i} #{opts[:class]}") + " " + text).html_safe
end

#icons(i) ⇒ Object


214
215
216
217
218
# File 'app/helpers/layout_helper.rb', line 214

def icons i
   :i, :class=>"icon-#{i}" do
    yield
  end
end

#javascript(*args) ⇒ Object


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

def javascript(*args)
  content_for(:head) { javascript_include_tag(*args) }
end

#line_count(f, attr) ⇒ Object


33
34
35
36
# File 'app/helpers/layout_helper.rb', line 33

def line_count (f, attr)
  rows = f.object.try(attr).to_s.lines.count rescue 1
  rows == 0 ? 1 : rows
end

#multiple_checkboxes(f, attr, klass, associations, options = {}, html_options = {}) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/layout_helper.rb', line 65

def multiple_checkboxes(f, attr, klass, associations, options = {}, html_options={})
  if associations.count > 5
    associated_obj = klass.send(ActiveModel::Naming.plural(associations.first))
    selected_ids = associated_obj.select("#{associations.first.class.table_name}.id").map(&:id)
    multiple_selects(f, attr, associations, selected_ids, options, html_options)
  else
    field(f, attr, options) do
      authorized_edit_habtm klass, associations, options[:prefix], html_options
    end
  end
end

#multiple_selects(f, attr, associations, selected_ids, options = {}, html_options = {}) ⇒ Object

add hidden field for options


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/helpers/layout_helper.rb', line 78

def multiple_selects(f, attr, associations, selected_ids, options={}, html_options={})
  field(f, attr,options) do
    attr_ids = (attr.to_s.singularize+"_ids").to_sym
    hidden_fields = f.hidden_field(attr_ids, :multiple => true, :value => '', :id=>'')
    options[:disabled] ||=[]
    options[:disabled].each do |disabled_value|
      hidden_fields += f.hidden_field(attr_ids, :multiple => true, :value => disabled_value, :id=>'' )
    end
    hidden_fields + f.collection_select(attr_ids, associations.all.sort_by { |a| a.to_s },
                                        :id, :to_s ,options.merge(:selected => selected_ids),
                                        html_options.merge(:multiple => true))
  end
end

#page_entries_info(collection, options = {}) ⇒ Object


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'app/helpers/layout_helper.rb', line 185

def page_entries_info(collection, options = {})
  html = if collection.total_entries == 0
           _("No entries found")
         else
           if collection.total_pages < 2
             n_("Displaying <b>%{count}</b> entry", "Displaying <b>all %{count}</b> entries", collection.total_entries) % {:count => collection.total_entries}
           else
             _("Displaying entries <b>%{from} - %{to}</b> of <b>%{count}</b> in total") %
                 { :from => collection.offset + 1, :to => collection.offset + collection.length, :count => collection.total_entries }
           end
         end.html_safe
  html += options[:more].html_safe if options[:more]
  (
    :div,(
        :ul, (
            :li, link_to(html, "#")
        ), :style=>"float: left;"
    ), :class => "span4 pagination")
end

#password_f(f, attr, options = {}) ⇒ Object


45
46
47
48
49
50
51
# File 'app/helpers/layout_helper.rb', line 45

def password_f(f, attr, options = {})
  field(f, attr, options) do
    options[:autocomplete] ||= "off"
    options[:placeholder] ||= password_placeholder(f.object)
    f.password_field attr, options
  end
end

#popover(title, msg, options = {}) ⇒ Object


171
172
173
# File 'app/helpers/layout_helper.rb', line 171

def popover title, msg, options = {}
  link_to icon_text("info-sign"), {}, {:remote => true, :rel => "popover", :data => {"content" => msg, "original-title" => title} }.merge(options)
end

#radio_button_f(f, attr, options = {}) ⇒ Object


92
93
94
95
96
97
98
# File 'app/helpers/layout_helper.rb', line 92

def radio_button_f(f, attr, options = {})
  text = options.delete(:text)
  value = options.delete(:value)
  label_tag('', :class=>"radio inline") do
    f.radio_button(attr, value, options) + " #{text} "
  end
end

#search_bar(*elements) ⇒ Object


15
16
17
# File 'app/helpers/layout_helper.rb', line 15

def search_bar *elements
  content_for(:search_bar) { elements.join(" ").html_safe }
end

#select_f(f, attr, array, id, method, select_options = {}, html_options = {}) ⇒ Object


100
101
102
103
104
# File 'app/helpers/layout_helper.rb', line 100

def select_f(f, attr, array, id, method, select_options = {}, html_options = {})
  field(f, attr, html_options) do
    f.collection_select attr, array, id, method, select_options, html_options
  end
end

#selectable_f(f, attr, array, select_options = {}, html_options = {}) ⇒ Object


106
107
108
109
110
# File 'app/helpers/layout_helper.rb', line 106

def selectable_f(f, attr, array, select_options = {}, html_options = {})
  field(f, attr, html_options) do
    f.select attr, array, select_options, html_options
  end
end

#stylesheet(*args) ⇒ Object


19
20
21
# File 'app/helpers/layout_helper.rb', line 19

def stylesheet(*args)
  content_for(:head) { stylesheet_link_tag(*args) }
end

#submit_or_cancel(f, overwrite = false, args = { }) ⇒ Object


153
154
155
156
157
158
159
160
161
# File 'app/helpers/layout_helper.rb', line 153

def submit_or_cancel f, overwrite = false, args = { }
  args[:cancel_path] ||= send("#{controller_name}_path")
  (:div, :class => "form-actions") do
    text    = overwrite ? _("Overwrite") : _("Submit")
    options = overwrite ? {:class => "btn btn-danger"} : {:class => "btn btn-primary"}
    link_to(_("Cancel"), args[:cancel_path], :class => "btn") + " " +
    f.submit(text, options)
  end
end

#text_f(f, attr, options = {}) ⇒ Object


27
28
29
30
31
# File 'app/helpers/layout_helper.rb', line 27

def text_f(f, attr, options = {})
  field(f, attr, options) do
    f.text_field attr, options
  end
end

#textarea_f(f, attr, options = {}) ⇒ Object


38
39
40
41
42
43
# File 'app/helpers/layout_helper.rb', line 38

def textarea_f(f, attr, options = {})
  field(f, attr, options) do
    options[:rows] = line_count(f, attr) if options[:rows] == :auto
    f.text_area attr, options
  end
end

#title(page_title, page_header = nil) ⇒ Object


2
3
4
5
# File 'app/helpers/layout_helper.rb', line 2

def title(page_title, page_header = nil)
  content_for(:title, page_title.to_s)
  @page_header       = page_header || @content_for_title || page_title.to_s
end

#title_actions(*elements) ⇒ Object


7
8
9
# File 'app/helpers/layout_helper.rb', line 7

def title_actions *elements
  content_for(:title_actions) { elements.join(" ").html_safe }
end

#will_paginate(collection = nil, options = {}) ⇒ Object


175
176
177
178
179
180
181
182
183
# File 'app/helpers/layout_helper.rb', line 175

def will_paginate(collection = nil, options = {})
  options.merge!(:class=>"span7  pagination")
  options[:renderer] ||= "WillPaginate::ActionView::BootstrapLinkRenderer"
  options[:inner_window] ||= 2
  options[:outer_window] ||= 0
  options[:previous_label] ||= _('&#8592; Previous')
  options[:next_label] ||= _('Next &#8594;')
  super collection, options
end