Module: Broadway::Helpers::CollectionHelper

Defined in:
lib/broadway/sinatra/helpers/collection_helper.rb

Instance Method Summary collapse

Instance Method Details



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 92

def breadcrumb(current_url, options, &block)
  item = site.find_by_url(current_url)
  return "" unless item
  haml_tag :a, options.merge(:href => current_url) do
    if block_given?
      yield item
    else
      haml_concat item.title
    end
  end
  if options[:to] != current_url
    haml_concat options[:delimiter] || " » "
    next_node = options[:to].gsub(/#{current_url}\/?/, "").split("/").first
    breadcrumb("#{current_url}/#{next_node}", options, &block)
  end
end


86
87
88
89
90
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 86

def breadcrumb_for(item, options = {}, &block)
  options[:from] ||= "/" + item.url.gsub(/^\//, "").split("/").first
  options[:to] ||= item.url
  breadcrumb(options[:from], options, &block)
end

#cycle(*states) ⇒ Object

comment from openmonkey.com/articles/2009/02/a-cycle-helper-for-sinatra You can then call it like this: .user_badge{ :class => cycle(‘normal’, ‘normal’, ‘last’) }



8
9
10
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 8

def cycle(*states) 
  states[@_cycle = ((@_cycle || -1) + 1) % states.size] 
end

#find_index_dir(dir) ⇒ Object



109
110
111
112
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 109

def find_index_dir(dir)
  return dir if File.exists?(File.join(dir, "index.haml"))
  return dir == "." ? dir : find_index_dir(File.dirname(dir))
end

#grid_for(array, options = {}, &block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 12

def grid_for(array, options = {}, &block)
  return "" if array.empty?
  columns = options[:columns] || 3
  (array / columns).each do |row|
    yield row
  end
end

#load_treeObject

TODO



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 115

def load_tree
  root = File.expand_path("public")
  dir = params[:dir]
  result = "<ul class=\"jqueryFileTree\" style=\"display: none;\">"
  begin
  	path = File.expand_path(File.join(root, dir)).untaint
  	if (path.split("/").length >= root.split("/").length)
    	current_dir = Dir.pwd
    	Dir.chdir(File.expand_path(path).untaint);
  		#loop through all directories
  		Dir.glob("*") {
  			|x|
  			if not File.directory?(x.untaint) then next end 
  			result << "<li class=\"directory collapsed\"><a href=\"#\" rel=\"#{dir}#{x}/\">#{x}</a></li>";
  		}

  		#loop through all files
  		Dir.glob("*") {
  			|x|
  			if not File.file?(x.untaint) then next end 
  			ext = File.extname(x)[1..-1]
  			result << "<li class=\"file ext_#{ext}\"><a href=\"#\" rel=\"#{dir}#{x}\">#{x}</a></li>"
  		}
  	else
  		#only happens when someone tries to go outside your root directory...
  		result << "You are way out of your league"
  	end 
  rescue 
  	result << "Internal Error"
  end
  Dir.chdir(current_dir)
  result << "</ul>"
  result
end


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 40

def menu_for(item, options = {}, &block)
  if item.is_a?(Site)
    menu_tag(item.tree, options, &block)
  elsif item.is_a?(Page)
    menu_tag(item.children, options, &block)
  elsif item.is_a?(Post)
    ""
  else
    menu_tag(item, options, &block)
  end
end
  • menu_tag(c(:menu)) do |attributes, node|

- attributes = “one two” - node



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
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 55

def menu_tag(array, options = {}, &block)
  return "" if array.empty?
  menu_attributes = nil
  if options.has_key?(:menu_attributes)
    menu_attributes = options[:menu_attributes]
    options.delete(:menu_attributes)
  end
  haml_tag :ul, menu_attributes do
    array.each_with_index do |node, i|
      attributes = options.has_key?(:li_attributes) ? options[:li_attributes] : {}
      attributes[:class] ||= ""
      if i == 0 and options.has_key?(:first)
        attributes[:class] << " #{options[:first]}"
      elsif i == array.length - 1 and options.has_key?(:last)
        attributes[:class] << " #{options[:last]}"
      end
      haml_tag :li, attributes do
        if block_given?
          yield node
        else
          haml_concat node.title
        end
        next if (options.has_key?(:show_children) and options[:show_children] == false)
        next unless (node.respond_to?(:children) and node.show_children?)
        
        menu_tag(node.children, options, &block)
      end
    end
  end
end

#row_for(array, options = {}, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 20

def row_for(array, options = {}, &block)
  return "" if array.empty?
  array.each_with_index do |node, i|
    attributes = options.has_key?(:li_attributes) ? options[:li_attributes] : {}
    attributes[:class] ||= ""
    if i == 0 and options.has_key?(:first)
      attributes[:class] << "#{options[:first]}"
    elsif i == array.length - 1 and options.has_key?(:last)
      attributes[:class] << "#{options[:last]}"
    end
    haml_tag :li, attributes do
      if block_given?
        yield node, i
      else
        haml_concat node.title
      end
    end
  end
end