Module: ViewFu::TagHelper

Defined in:
lib/view_fu/tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_class(css_class, options = {}) ⇒ Object

provides a slick way to add classes inside haml attribute collections

examples:

%div{add_class("current")}
#=> adds the "current" class to the div

%div{add_class("current", :if => current?)}
#=> adds the "current" class to the div if current? method

%div{add_class("highlight", :unless => logged_in?)}
#=> adds the "highlight" class to the div unless logged_in? method returns true


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/view_fu/tag_helper.rb', line 57

def add_class(css_class, options = {})
  return {} unless css_class

  attributes = {:class => css_class}

  if options.has_key?(:unless)
    return options[:unless] ? {} : attributes
  end

  if options.has_key?(:if)
    return options[:if] ? attributes : {}
  end

  attributes
end

#add_class_if(css_class, condition) ⇒ Object



73
74
75
# File 'lib/view_fu/tag_helper.rb', line 73

def add_class_if(css_class, condition)
  add_class(css_class, :if => condition)
end

#add_class_unless(css_class, condition) ⇒ Object



77
78
79
# File 'lib/view_fu/tag_helper.rb', line 77

def add_class_unless(css_class, condition)
  add_class(css_class, :unless => condition)
end

#anchor(anchor_name, options = {}) ⇒ Object

Writes an anchor tag



19
20
21
# File 'lib/view_fu/tag_helper.rb', line 19

def anchor(anchor_name, options = {})
  (:a, "", options.merge(:name => anchor_name))
end

#brObject

Writes a br tag



4
5
6
# File 'lib/view_fu/tag_helper.rb', line 4

def br
  "<br />".html_safe
end

#clear(direction = nil) ⇒ Object

Writes a clear div tag



37
38
39
# File 'lib/view_fu/tag_helper.rb', line 37

def clear(direction = nil)
  clear_tag(:div, direction)
end

#clear_tag(tag, direction = nil) ⇒ Object

Writes a clear tag



24
25
26
27
28
29
30
# File 'lib/view_fu/tag_helper.rb', line 24

def clear_tag(tag, direction = nil)
  if tag == :br
    "<br class=\"clear#{direction}\" />".html_safe
  else
    "<#{tag} class=\"clear#{direction}\"></#{tag}>".html_safe
  end
end

#clearbit_icon(icon, color, options = {}) ⇒ Object

clearbit icons



130
131
132
# File 'lib/view_fu/tag_helper.rb', line 130

def clearbit_icon(icon, color, options = {})
  image_tag "clearbits/#{icon}.gif", {:class => "clearbits #{color}", :alt => icon}.merge(options)
end

#current_yearObject



32
33
34
# File 'lib/view_fu/tag_helper.rb', line 32

def current_year
  Time.now.strftime("%Y")
end

Wrap a delete link



112
113
114
115
116
# File 'lib/view_fu/tag_helper.rb', line 112

def delete_link(*args)
  options = {:method => :delete, :confirm => "Are you sure you want to delete this?"}.merge(extract_options_from_args!(args)||{})
  args << options
  link_to(*args)
end

#hide(options = {}) ⇒ Object Also known as: hidden

Return a hidden attribute hash (useful in Haml tags - %div#hidden)



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/view_fu/tag_helper.rb', line 82

def hide(options = {})
  attributes = {:style => "display:none"}

  if options.has_key?(:unless)
    return options[:unless] ? {} : attributes
  end

  if options.has_key?(:if)
    return options[:if] ? attributes : {}
  end

  attributes
end

#hide_if(condition) ⇒ Object Also known as: hidden_if, show_unless

Return a hidden attribute hash if a condition evaluates to true



98
99
100
# File 'lib/view_fu/tag_helper.rb', line 98

def hide_if(condition)
  hide(:if => condition)
end

#hide_unless(condition) ⇒ Object Also known as: hidden_unless, show_if

Return a hidden attribute hash if a condition evaluates to false



105
106
107
# File 'lib/view_fu/tag_helper.rb', line 105

def hide_unless(condition)
  hide(:unless => condition)
end

#hrObject

Writes an hr tag



9
10
11
# File 'lib/view_fu/tag_helper.rb', line 9

def hr
  "<hr />".html_safe
end

#is_first(i) ⇒ Object

check to see if an index is the first item in a collection



140
141
142
# File 'lib/view_fu/tag_helper.rb', line 140

def is_first(i)
  i.to_i.zero? ? {:class => "first"} : {}
end

Wrap a block with a link



119
120
121
122
# File 'lib/view_fu/tag_helper.rb', line 119

def link_to_block(*args, &block)
  content = capture(&block)
  return link_to(content, *args)
end

#loremObject

Return some lorem text



42
43
44
# File 'lib/view_fu/tag_helper.rb', line 42

def lorem
  "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
end

#nbspObject

Writes a nonbreaking space



14
15
16
# File 'lib/view_fu/tag_helper.rb', line 14

def nbsp
  "&nbsp;".html_safe
end

#pixel(options = {}) ⇒ Object

pixel spacing helper



135
136
137
# File 'lib/view_fu/tag_helper.rb', line 135

def pixel(options = {})
  image_tag "pixel.png", options
end

#production?Boolean

Check if we’re on production environment

Returns:

  • (Boolean)


125
126
127
# File 'lib/view_fu/tag_helper.rb', line 125

def production?
  Rails.env.production?
end