Module: TbHelpers::Base

Defined in:
lib/tb-helpers/base.rb

Constant Summary collapse

ALIGNMENT_CLASSES =
{:left => 'text-left', :center => 'text-center', :right => 'text-right'}
EMPHASIS_CLASSES =
{:success => 'text-success', :error => 'text-error', :warning => 'text-warning', :info => 'text-info', :muted => 'muted'}
TABLE_CLASSES =
{:striped => 'table-striped', :bordered => 'table-bordered', :hover => 'table-hover', :condensed => 'table-condensed'}
TABLE_ROW_CLASSES =
{:success => 'success', :error => 'error', :warning => 'warning', :info => 'info'}
FORM_CLASSES =
{:search => 'form-search', :inline => 'form-inline', :horizontal => 'form-horizontal'}
INPUT_SIZES =
{:mini => 'input-mini', :small => 'input-small', :medium => 'input-medium', :large => 'input-large', :xlarge => 'input-xlarge', :xxlarge => 'input-xxlarge'}
SPAN_SIZES =

span1 - span12

{}
BUTTON_SIZES =
{:mini => 'btn-mini', :small => 'btn-small', :large => 'btn-large'}

Instance Method Summary collapse

Instance Method Details

#tb_abbreviation(content = nil, options = nil, &block) ⇒ Object Also known as: tb_abbr



74
75
76
77
78
79
80
# File 'lib/tb-helpers/base.rb', line 74

def tb_abbreviation(content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = delete_option_if_exists!(options, :tag, :abbr)
  escape = delete_option_if_exists!(options, :escape, true)

  (tag, content, options, escape)
end

#tb_address(content = nil, options = nil, &block) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/tb-helpers/base.rb', line 83

def tb_address(content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = delete_option_if_exists!(options, :tag, :address)
  escape = delete_option_if_exists!(options, :escape, true)

  (tag, content, options, escape)
end

#tb_blockquote(content = nil, options = nil, &block) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/tb-helpers/base.rb', line 91

def tb_blockquote(content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = delete_option_if_exists!(options, :tag, :blockquote)
  escape = delete_option_if_exists!(options, :escape, true)

  (tag, content, options, escape)
end

#tb_bold(content = nil, options = nil, &block) ⇒ Object



65
66
67
# File 'lib/tb-helpers/base.rb', line 65

def tb_bold(content = nil, options = nil, &block)
  tb_emphasis(:bold, content, options, &block)
end

#tb_button(content = nil, options = nil, &block) ⇒ Object Also known as: tb_btn



340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/tb-helpers/base.rb', line 340

def tb_button(content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = delete_option_if_exists!(options, :tag, :div)
  escape = delete_option_if_exists!(options, :escape, true)

  classes = ['btn']
  context = delete_option_if_exists!(options, :context, nil)
  classes << "btn-#{context}" if context
  size = delete_option_if_exists!(options, :size, nil)
  classes << "btn-#{size}" if size
  set_or_prepend_option!(options, :class, classes.join(' '))

  (tag, content, options, escape)
end

#tb_button_group(options = nil, &block) ⇒ Object

buttons



332
333
334
335
336
337
338
# File 'lib/tb-helpers/base.rb', line 332

def tb_button_group(options = nil, &block)
  options = set_or_prepend_option!(options, :class, 'btn-group')
  tag = delete_option_if_exists!(options, :tag, :div)
  escape = delete_option_if_exists!(options, :escape, true)

  (tag, options, nil, escape, &block)
end

#tb_checkbox_label(options = nil, &block) ⇒ Object Also known as: tb_check_box_label



218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/tb-helpers/base.rb', line 218

def tb_checkbox_label(options = nil, &block)
  options ||= {} # set_or_prepend_option!(options, :class, 'checkbox')
  tag = delete_option_if_exists!(options, :tag, :label)
  escape = delete_option_if_exists!(options, :escape, true)

  classes = ['checkbox']
  inline = delete_option_if_exists!(options, :inline, false)
  classes << 'inline' if inline

  set_or_prepend_option!(options, :class, classes.join(' '))
  (tag, options, nil, escape, &block)
end

#tb_code_block(content = nil, options = nil, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/tb-helpers/base.rb', line 111

def tb_code_block(content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = delete_option_if_exists!(options, :tag, :pre)
  escape = delete_option_if_exists!(options, :escape, true)

  classes = []
  scrollable = delete_option_if_exists!(options, :scrollable, true)
  classes << 'pre-scrollable' if scrollable
  set_or_prepend_option!(options, :class, classes.join(' ')) unless classes.empty?

  (tag, content, options, escape)
end

#tb_code_inline(content = nil, options = nil, &block) ⇒ Object

code



103
104
105
106
107
108
109
# File 'lib/tb-helpers/base.rb', line 103

def tb_code_inline(content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = delete_option_if_exists!(options, :tag, :code)
  escape = delete_option_if_exists!(options, :escape, true)

  (tag, content, options, escape)
end

#tb_control_group(options = nil, &block) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/tb-helpers/base.rb', line 183

def tb_control_group(options = nil, &block)
  options ||= {}
  tag = delete_option_if_exists!(options, :tag, :div)
  escape = delete_option_if_exists!(options, :escape, true)

  classes = ['control-group']
  context = delete_option_if_exists!(options, :context, nil)
  classes << context.to_s if context

  set_or_prepend_option!(options, :class, classes.join(' '))
  (tag, options, nil, escape, &block)
end

#tb_control_label(content = nil, options = nil, &block) ⇒ Object



209
210
211
212
213
214
215
216
# File 'lib/tb-helpers/base.rb', line 209

def tb_control_label(content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  options = set_or_prepend_option!(options, :class, 'control-label')
  tag = delete_option_if_exists!(options, :tag, :label)
  escape = delete_option_if_exists!(options, :escape, true)

  (tag, content, options, escape)
end

#tb_controls(options = nil, &block) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/tb-helpers/base.rb', line 196

def tb_controls(options = nil, &block)
  options ||= {} # set_or_prepend_option!(options, :class, 'controls')
  tag = delete_option_if_exists!(options, :tag, :div)
  escape = delete_option_if_exists!(options, :escape, true)

  classes = ['controls']
  row = delete_option_if_exists!(options, :row, false)
  classes << 'controls-row' if row

  set_or_prepend_option!(options, :class, classes.join(' '))
  (tag, options, nil, escape, &block)
end

#tb_emphasis(style, content = nil, options = nil, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tb-helpers/base.rb', line 40

def tb_emphasis(style, content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = case style
    when :small then :small
    when :bold then :strong
    when :italics then :em
    when nil then delete_option_if_exists!(options, :tag, :span)
    else raise ArgumentError.new('invalid style value')
  end
  escape = delete_option_if_exists!(options, :escape, true)

  classes = []
  EMPHASIS_CLASSES.each do |class_sym, class_name|
    class_val = delete_option_if_exists!(options, class_sym, false)
    classes << class_name if class_val
  end
  set_or_prepend_option!(options, :class, classes.join(' '))

  (tag, content, options, escape)
end

#tb_fieldset(options = nil, &block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tb-helpers/base.rb', line 171

def tb_fieldset(options = nil, &block)
  options ||= {}
  tag = delete_option_if_exists!(options, :tag, :fieldset)
  escape = delete_option_if_exists!(options, :escape, true)
  legend = delete_option_if_exists!(options, :legend, nil)

  (tag, options, nil, escape) do
    concat (:legend, legend, {}, true) if legend
    concat capture(&block)
  end
end

#tb_form_actions(options = nil, &block) ⇒ Object



296
297
298
299
300
301
302
# File 'lib/tb-helpers/base.rb', line 296

def tb_form_actions(options = nil, &block)
  options = set_or_prepend_option!(options, :class, 'form-actions')
  tag = delete_option_if_exists!(options, :tag, :div)
  escape = delete_option_if_exists!(options, :escape, true)

  (tag, options, nil, escape, &block)
end

#tb_form_for(*args) ⇒ Object

forms

Raises:

  • (NotImplementedError)


167
168
169
# File 'lib/tb-helpers/base.rb', line 167

def tb_form_for(*args)
  raise NotImplementedError.new
end

#tb_icon_black(name, options = nil) ⇒ Object Also known as: tb_icon

icons by glyphicons



384
385
386
387
388
389
390
391
# File 'lib/tb-helpers/base.rb', line 384

def tb_icon_black(name, options = nil)
  icon_name = name.to_s.starts_with?('icon-') ? name.to_s : "icon-#{name}"
  options = set_or_prepend_option!(options, :class, icon_name)
  tag = delete_option_if_exists!(options, :tag, :i)
  escape = delete_option_if_exists!(options, :escape, true)

  (tag, nil, options, escape)
end

#tb_icon_white(name, options = nil) ⇒ Object



394
395
396
397
398
399
400
401
# File 'lib/tb-helpers/base.rb', line 394

def tb_icon_white(name, options = nil)
  icon_name = name.to_s.starts_with?('icon-') ? "#{name} icon-white" : "icon-#{name} icon-white"
  options = set_or_prepend_option!(options, :class, icon_name)
  tag = delete_option_if_exists!(options, :tag, :i)
  escape = delete_option_if_exists!(options, :escape, true)

  (tag, nil, options, escape)
end

#tb_input_addon(style, options = nil, &block) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/tb-helpers/base.rb', line 245

def tb_input_addon(style, options = nil, &block)
  options = case style
    when :prepend then set_or_prepend_option!(options, :class, 'input-prepend')
    when :append then set_or_prepend_option!(options, :class, 'input-append')
    when :combined then set_or_prepend_option!(options, :class, 'input-prepend input-append')
    else raise ArgumentError.new('invalid style value')
  end
  tag = delete_option_if_exists!(options, :tag, :div)
  escape = delete_option_if_exists!(options, :escape, true)
  addon = delete_option_if_exists!(options, :addon, nil)

  (tag, options, nil, escape) do
    if style == :prepend || style == :combined
      prepend = addon.is_a?(Array) ? addon[0] : addon
      concat (:span, prepend, {:class => 'add-on'}, true) if prepend
    end
    concat capture(&block)
    if style == :append || style == :combined
      append = addon.is_a?(Array) ? addon[1] : addon
      concat (:span, append, {:class => 'add-on'}, true) if append
    end
  end
end

#tb_input_addon_append(options = nil, &block) ⇒ Object



273
274
275
# File 'lib/tb-helpers/base.rb', line 273

def tb_input_addon_append(options = nil, &block)
  tb_input_addon(:append, options, &block)
end

#tb_input_addon_pre_and_append(options = nil, &block) ⇒ Object Also known as: tb_input_addon_combined



277
278
279
# File 'lib/tb-helpers/base.rb', line 277

def tb_input_addon_pre_and_append(options = nil, &block)
  tb_input_addon(:combined, options, &block)
end

#tb_input_addon_prepend(options = nil, &block) ⇒ Object



269
270
271
# File 'lib/tb-helpers/base.rb', line 269

def tb_input_addon_prepend(options = nil, &block)
  tb_input_addon(:prepend, options, &block)
end

#tb_input_help(style, content = nil, options = nil, &block) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/tb-helpers/base.rb', line 304

def tb_input_help(style, content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = delete_option_if_exists!(options, :tag, :span)
  escape = delete_option_if_exists!(options, :escape, true)

  classes = []
  classes << case style
    when :inline then 'help-inline'
    when :block then 'help-block'
    else raise ArgumentError.new('invalid style value')
  end

  set_or_prepend_option!(options, :class, classes.join(' '))
  (tag, content, options, escape)    
end

#tb_input_help_block(content = nil, options = nil, &block) ⇒ Object



324
325
326
# File 'lib/tb-helpers/base.rb', line 324

def tb_input_help_block(content = nil, options = nil, &block)
  tb_input_help(:block, content, options, &block)
end

#tb_input_help_inline(content = nil, options = nil, &block) ⇒ Object



320
321
322
# File 'lib/tb-helpers/base.rb', line 320

def tb_input_help_inline(content = nil, options = nil, &block)
  tb_input_help(:inline, content, options, &block)
end

#tb_input_uneditable(content = nil, options = nil, &block) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/tb-helpers/base.rb', line 282

def tb_input_uneditable(content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = delete_option_if_exists!(options, :tag, :span)
  escape = delete_option_if_exists!(options, :escape, true)

  classes = []
  size = delete_option_if_exists!(options, :size, nil)
  classes << "input-#{size}" if size
  classes << 'uneditable-input'

  set_or_prepend_option!(options, :class, classes.join(' '))
  (tag, content, options, escape)
end

#tb_italics(content = nil, options = nil, &block) ⇒ Object Also known as: tb_italic



69
70
71
# File 'lib/tb-helpers/base.rb', line 69

def tb_italics(content = nil, options = nil, &block)
  tb_emphasis(:italics, content, options, &block)
end

#tb_javascript_include_tag(version = nil) ⇒ Object

essentials



15
16
17
18
# File 'lib/tb-helpers/base.rb', line 15

def tb_javascript_include_tag(version=nil)
  version ||= '2.3.1'
  javascript_include_tag "//netdna.bootstrapcdn.com/twitter-bootstrap/#{version}/js/bootstrap.min.js"
end

#tb_lead(content = nil, options = nil, &block) ⇒ Object

typography



29
30
31
32
33
34
35
36
37
38
# File 'lib/tb-helpers/base.rb', line 29

def tb_lead(content = nil, options = nil, &block)
  content, options = content_or_options_with_block?(content, options, &block)
  tag = delete_option_if_exists!(options, :tag, :p)
  escape = delete_option_if_exists!(options, :escape, true)

  classes = ['lead']
  set_or_prepend_option!(options, :class, classes.join(' '))

  (tag, content, options, escape)
end


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/tb-helpers/base.rb', line 356

def tb_link_button_to(*args, &block)
  if block_given?
    options = args[1] ||= {}
  else
    options = args[2] ||= {}
  end
  classes = ['btn']
  context = delete_option_if_exists!(options, :context, nil)
  classes << "btn-#{context}" if context
  size = delete_option_if_exists!(options, :size, nil)
  classes << "btn-#{size}" if size
  disabled = delete_option_if_exists!(options, :disabled, false)
  classes << "disabled" if disabled
  set_or_prepend_option!(options, :class, classes.join(' '))
  link_to(*args, &block)
end

#tb_radio_label(options = nil, &block) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/tb-helpers/base.rb', line 232

def tb_radio_label(options = nil, &block)
  options ||= {} # set_or_prepend_option!(options, :class, 'radio')
  tag = delete_option_if_exists!(options, :tag, :label)
  escape = delete_option_if_exists!(options, :escape, true)

  classes = ['radio']
  inline = delete_option_if_exists!(options, :inline, false)
  classes << 'inline' if inline

  set_or_prepend_option!(options, :class, classes.join(' '))
  (tag, options, nil, escape, &block)
end

#tb_small(content = nil, options = nil, &block) ⇒ Object



61
62
63
# File 'lib/tb-helpers/base.rb', line 61

def tb_small(content = nil, options = nil, &block)
  tb_emphasis(:small, content, options, &block)
end


20
21
22
23
# File 'lib/tb-helpers/base.rb', line 20

def tb_stylesheet_link_tag(version=nil)
  version ||= '2.3.1'
  stylesheet_link_tag "//netdna.bootstrapcdn.com/twitter-bootstrap/#{version}/css/bootstrap-combined.min.css"
end

#tb_table(options = nil, &block) ⇒ Object

tables



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/tb-helpers/base.rb', line 128

def tb_table(options = nil, &block)
  options ||= {}
  tag = delete_option_if_exists!(options, :tag, :table)
  escape = delete_option_if_exists!(options, :escape, true)
  caption = delete_option_if_exists!(options, :caption, nil)

  classes = ['table']
  TABLE_CLASSES.each do |class_sym, class_name|
    class_val = delete_option_if_exists!(options, class_sym, false)
    classes << class_name if class_val
  end
  set_or_prepend_option!(options, :class, classes.join(' '))

  (tag, options, nil, escape) do
    concat (:caption, caption, {}, true) if caption
    concat capture(&block)
  end
end

#tb_table_row(options = nil, &block) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/tb-helpers/base.rb', line 147

def tb_table_row(options = nil, &block)
  options ||= {}
  tag = delete_option_if_exists!(options, :tag, :tr)
  escape = delete_option_if_exists!(options, :escape, true)

  # FIXME this should be done with context option
  classes = []
  TABLE_ROW_CLASSES.each do |class_sym, class_name|
    class_val = delete_option_if_exists!(options, class_sym, false)
    classes << class_name if class_val
  end
  set_or_prepend_option!(options, :class, classes.join(' '))

  (tag, options, nil, escape, &block)
end