Module: Handicraft::Helper

Defined in:
lib/handicraft/helper.rb

Defined Under Namespace

Classes: TagNode

Instance Method Summary collapse

Instance Method Details



21
22
23
24
25
# File 'lib/handicraft/helper.rb', line 21

def breadcrumb( *crumb )
  p = TagNode.new( :p, :class => "breadcrumb" )
  p << crumb.join(' &gt; ')
  return p.to_s
end

#handicraft_form_for(name, *args, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/handicraft/helper.rb', line 7

def handicraft_form_for(name, *args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  options = options.merge(:builder => Handicraft::Form)

  if options[:html] && options[:html][:class].nil?
    options[:html].merge!( :class => "form right-label" )
  elsif options[:html].nil?
    options[:html] = { :class => "form right-label" }
  end

  args = (args << options)
  form_for(name, *args, &block)
end

#render_body_tagObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/handicraft/helper.rb', line 32

def render_body_tag
  class_attribute = ["#{controller_name}-controller","#{action_name}-action"].join(" ")
  id_attribute = (@body_id)? " id=\"#{@body_id}-page\"" : ""

  %Q|<!--[if lt IE 7 ]>
<body class="#{class_attribute} ie6"><![endif]-->
<!--[if gte IE 7 ]>
<body class="#{class_attribute} ie"><![endif]-->
<!--[if !IE]>-->
<body#{id_attribute} class="#{class_attribute}">
<!--<![endif]-->|
end

#render_list(list = [], options = {}) {|list| ... } ⇒ Object

.current will be added to current action, but if you want to add .current to another link, you can set @current = [‘/other_link’] TODO: hot about render_list( *args )

Yields:

  • (list)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/handicraft/helper.rb', line 96

def render_list(list=[], options={})
  if list.is_a? Hash
    options = list
    list = []
  end

  yield(list) if block_given?

  ul = TagNode.new('ul', :id => options[:id], :class => options[:class] )

  list.each_with_index do |content, i|
    item_class = []
    item_class << "first" if i == 0
    item_class << "last" if i == (list.length - 1)

    item_content = content
    item_options = {}

    if content.is_a? Array
      item_content = content[0]
      item_options = content[1]
    end

    if item_options[:class]
      item_class << item_options[:class]
    end

    link = item_content.match(/href=(["'])(.*?)(\1)/)[2] rescue nil

    if ( link && current_page?(link) ) || ( @current && @current.include?(link) )
      item_class << "current"
    end

    item_class = (item_class.empty?)? nil : item_class.join(" ")
    ul << li = TagNode.new('li', :class => item_class )
    li << item_content
  end

  return ul.to_s
end

#render_page_titleObject



27
28
29
30
# File 'lib/handicraft/helper.rb', line 27

def render_page_title
  title = @page_title ? "#{SITE_NAME} | #{@page_title}" : SITE_NAME rescue "SITE_NAME"
  ("title", title)
end

#render_table(rows, renderrers, table_options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/handicraft/helper.rb', line 49

def render_table(rows, renderrers, table_options = {})
  table_options = {
    :has_header => true,
    :has_row_info => false,
    :id => nil,
    :class_name => "auto"
  }.merge(table_options)

  table_tag_options = table_options[:id] ? { :id => table_options[:id], :class => table_options[:class_name] } : { :class => table_options[:class_name] }

  table = TagNode.new('table', table_tag_options)
  if table_options[:has_header] == true
    table << thead = TagNode.new(:thead)
    thead << tr = TagNode.new(:tr, :class => 'odd')

    renderrers.each do |renderrer|
      tr << th = TagNode.new(:th)
      th << renderrer[0]
    end
  end

  table << tbody = TagNode.new('tbody')
  row_info = {}
  row_info[:total] = rows.length
  rows.each_with_index do |row,i|
    row_info[:current] = i
    tbody << tr = TagNode.new('tr', :class => cycle("","odd") )
    renderrers.each do |renderrer|
      tr << td = TagNode.new('td')

      if renderrer[1].class == Proc
        if table_options[:has_row_info] == true
          td << renderrer[1].call(row, row_info)
        else
          td << renderrer[1].call(row)
        end
      else
        td << renderrer[1]
      end
    end
  end

  return table.to_s
end

#s(html) ⇒ Object



45
46
47
# File 'lib/handicraft/helper.rb', line 45

def s(html)
  sanitize( html, :tags => %w(table thead tbody tr td th ol ul li div span font img sup sub br hr a pre p h1 h2 h3 h4 h5 h6), :attributes => %w(id class style src href size color) )
end

#yield_or_default(message, default_message = "") ⇒ Object



3
4
5
# File 'lib/handicraft/helper.rb', line 3

def yield_or_default(message, default_message = "")
  message.nil? ? default_message : message
end