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


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/view_fu/tag_helper.rb', line 129

def add_class(css_class, options = {})
  return {} unless css_class
  
  if options.has_key?(:unless)
    if options[:unless]
      return {}
    else
      return {:class => css_class}
    end
  end
  
  if options.has_key?(:if)        
    if options[:if]
      return {:class => css_class}
    else
      return {}
    end
  end
        
  {:class => css_class}
end

#add_class_if(css_class, condition) ⇒ Object



151
152
153
# File 'lib/view_fu/tag_helper.rb', line 151

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

#add_class_unless(css_class, condition) ⇒ Object



155
156
157
# File 'lib/view_fu/tag_helper.rb', line 155

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

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

Writes an anchor tag



89
90
91
92
93
# File 'lib/view_fu/tag_helper.rb', line 89

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

ported from rails



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/view_fu/tag_helper.rb', line 66

def auto_discovery_link_tag(type = :rss, url = nil, tag_options = {})
  
  # theres gotta be a better way of setting mimetype for a file extensionin Merb..
  unless tag_options[:type]
    if type.to_s == "rss"
      tag_options[:type] = "application/rss+xml"
    elsif type.to_s == "atom"
      tag_options[:type] = "application/atom+xml"
    end
  end

  tag(:link, :rel => (tag_options[:rel] || "alternate"),
              :type  => tag_options[:type].to_s,
              :title => (tag_options[:title] || type.to_s.upcase),
              :href  => (url || "#"))
end

#brObject

Writes a br tag



51
52
53
# File 'lib/view_fu/tag_helper.rb', line 51

def br
  "<br />"
end

#clear(direction = nil) ⇒ Object

Writes a clear div tag



109
110
111
# File 'lib/view_fu/tag_helper.rb', line 109

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

#clear_tag(tag, direction = nil) ⇒ Object

Writes a clear tag



96
97
98
99
100
101
102
# File 'lib/view_fu/tag_helper.rb', line 96

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

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

clearbit icons



212
213
214
# File 'lib/view_fu/tag_helper.rb', line 212

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

#current_yearObject



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

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

Wrap a delete link



188
189
190
191
192
# File 'lib/view_fu/tag_helper.rb', line 188

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)



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/view_fu/tag_helper.rb', line 160

def hide(options = {})
  if options.has_key?(:unless) && options[:unless]
    return {}
  end
  
  if options.has_key?(:if) && !options[:if]
    return {}
  end
  
  {:style => "display:none"}
end

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

Return a hidden attribute hash if a condition evaluates to true



174
175
176
# File 'lib/view_fu/tag_helper.rb', line 174

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



181
182
183
# File 'lib/view_fu/tag_helper.rb', line 181

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

#hrObject

Writes an hr tag



56
57
58
# File 'lib/view_fu/tag_helper.rb', line 56

def hr
  "<hr />"
end

#is_first(i) ⇒ Object

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



222
223
224
# File 'lib/view_fu/tag_helper.rb', line 222

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

Wrap a block with a link



195
196
197
198
# File 'lib/view_fu/tag_helper.rb', line 195

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

#loremObject

Return some lorem text



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

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



61
62
63
# File 'lib/view_fu/tag_helper.rb', line 61

def nbsp
  "&nbsp;"
end

#paging(page_data, style = :sabros) ⇒ Object

Display will_paginate paging links



206
207
208
209
# File 'lib/view_fu/tag_helper.rb', line 206

def paging(page_data, style = :sabros)
  return unless page_data.is_a? WillPaginate::Collection
  will_paginate(page_data, :class => "pagination #{style}", :inner_window => 3)
end

#parent_layout(layout, &block) ⇒ Object

Allows Easy Nested Layouts in Merb

Usage Example:

Parent Layout: layout/application.html.erb


<html>

<head>
  <title>Title</title>
</head>
<body>
  <%= catch_content %>
</body>

</html>

SubLayout: layout/alternate.html.erb


<%= parent_layout “application” do %>

<div class="inner_layout">
  <%= catch_content %>
</div>

<% end =%>

Now you can use the alternate layout in any of your views as normal and it will reuse the wrapping html on application.html.erb



46
47
48
# File 'lib/view_fu/tag_helper.rb', line 46

def parent_layout(layout, &block)
  render capture(&block), :layout => layout
end

#partial_block(template, options = {}, &block) ⇒ Object

Calls a Merb Partial with a block, which you can catch content from

Usage Example: <%= partial_block :fieldset, :legend => “Login” do %>

.. inner partial content

<% end =%>

Associated Partial (_fieldset.html.erb) <fieldset>

<legend><%= locals[:legend] %></legend>
<%= catch_content %>

</fieldset>



16
17
18
19
# File 'lib/view_fu/tag_helper.rb', line 16

def partial_block(template, options={}, &block)
  throw_content(:for_layout, block_given? ? capture(&block) : "")
  partial(template, options)
end

#pixel(options = {}) ⇒ Object

pixel spacing helper



217
218
219
# File 'lib/view_fu/tag_helper.rb', line 217

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

#production?Boolean

Check if we’re on production environment

Returns:

  • (Boolean)


201
202
203
# File 'lib/view_fu/tag_helper.rb', line 201

def production?
  Merb.env?(:production)
end

#spaceObject

Writes an hr space tag



84
85
86
# File 'lib/view_fu/tag_helper.rb', line 84

def space
  "<hr class='space' />"
end