Module: Broadway::Sinatra::CollectionHelper
- Defined in:
- lib/broadway/sinatra/helpers/collection_helper.rb
Instance Method Summary collapse
- #breadcrumb(current_url, options, &block) ⇒ Object
- #breadcrumb_for(item, options = {}, &block) ⇒ Object
-
#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’) }.
- #find_index_dir(dir) ⇒ Object
- #grid_for(array, options = {}, &block) ⇒ Object
-
#load_tree ⇒ Object
TODO.
- #menu_for(item, options = {}, &block) ⇒ Object
-
#menu_tag(array, options = {}, &block) ⇒ Object
-
menu_tag(c(:menu)) do |attributes, node| - attributes = “one two” - node.
-
- #row_for(array, options = {}, &block) ⇒ Object
Instance Method Details
#breadcrumb(current_url, options, &block) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 98 def (current_url, , &block) item = site.find_by_url(current_url) return "" unless item haml_tag :a, .merge(:href => current_url) do if block_given? yield item else haml_concat item.title end end if [:to] != current_url haml_concat [:delimiter] || " » " next_node = [:to].gsub(/#{current_url}\/?/, "").split("/").first ("#{current_url}/#{next_node}", , &block) end end |
#breadcrumb_for(item, options = {}, &block) ⇒ Object
92 93 94 95 96 |
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 92 def (item, = {}, &block) [:from] ||= "/" + item.url.gsub(/^\//, "").split("/").first [:to] ||= item.url ([:from], , &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
115 116 117 118 |
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 115 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, = {}, &block) return "" if array.empty? columns = [:columns] || 3 (array / columns).each do |row| yield row end end |
#load_tree ⇒ Object
TODO
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 149 150 151 152 153 154 |
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 121 def load_tree root = File.("public") dir = params[:dir] result = "<ul class=\"jqueryFileTree\" style=\"display: none;\">" begin path = File.(File.join(root, dir)).untaint if (path.split("/").length >= root.split("/").length) current_dir = Dir.pwd Dir.chdir(File.(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 |
#menu_for(item, options = {}, &block) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 45 def (item, = {}, &block) if item.is_a?(Site) (item.tree, , &block) elsif item.is_a?(Page) (item.children, , &block) elsif item.is_a?(Post) "" else (item, , &block) end end |
#menu_tag(array, options = {}, &block) ⇒ Object
-
menu_tag(c(:menu)) do |attributes, node|
- attributes = “one two” - node
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 |
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 60 def (array, = {}, &block) return "" if array.empty? = nil if .has_key?(:menu_attributes) = [:menu_attributes] .delete(:menu_attributes) end haml_tag :ul, do array.each_with_index do |node, i| attributes = .has_key?(:li_attributes) ? [:li_attributes] : {} attributes[:class] ||= "" if i == 0 and .has_key?(:first) attributes[:class] << " #{[:first]}" elsif i == array.length - 1 and .has_key?(:last) attributes[:class] << " #{[:last]}" end attributes.delete(:class) if attributes[:class].blank? haml_tag :li, attributes do if block_given? yield node else haml_concat node.title end next if (.has_key?(:show_children) and [:show_children] == false) next unless (node.respond_to?(:children) and node.show_children?) (node.children, , &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 39 40 41 42 43 |
# File 'lib/broadway/sinatra/helpers/collection_helper.rb', line 20 def row_for(array, = {}, &block) return "" if array.empty? array.each_with_index do |node, i| attributes = (.has_key?(:li_attributes) ? [:li_attributes] : {}).dup attributes[:class] ||= "" if i == 0 and .has_key?(:first) attributes[:class] << " #{[:first]}" elsif i == array.length - 1 and .has_key?(:last) attributes[:class] << " #{[:last]}" end if attributes[:class].blank? attributes.delete(:class) else attributes[:class].squeeze!(" ") end haml_tag :li, attributes do if block_given? yield node, i else haml_concat node.title end end end end |