Module: ActionView::Helpers
- Defined in:
- lib/bcms_tools/view_helpers.rb
Defined Under Namespace
Modules: CaptureHelper
Class Method Summary collapse
-
.category_menu_items(aRootCategory, aOptions = {}) ⇒ Object
:base_url (String) : prepended to menu urls eg.
-
.construct_category_tree(aRootCategory) ⇒ Object
Construct tree_nodes, an array of arrays - each array a level in tree.
- .default_content_for(name, &block) ⇒ Object
- .google_analytics(aTrackingId = nil) ⇒ Object
- .mailto_a_friend(aSubject, aOptions = {}) ⇒ Object
-
.render_menu2(aOptions = nil) ⇒ Object
render_menu(), or more precisely menu_items() doesn’t seem to work well for rendering a top menu when you want the ancestor item of the current page highlighted.
Class Method Details
.category_menu_items(aRootCategory, aOptions = {}) ⇒ Object
:base_url (String) : prepended to menu urls eg. /products :category (String) : name of current category eg. ‘Shoes’ :id_prefix (String) : will be prepended to ids of menu eg. ‘section_’
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/bcms_tools/view_helpers.rb', line 138 def (aRootCategory, aOptions={}) aBaseUrl = (aOptions[:base_url] || '') aIdPrefix = (aOptions[:id_prefix] || '') category = aOptions[:category] category = category.name.urlize('+') if category.is_a?(Category) tree_nodes = construct_category_tree(aRootCategory) # now turn tree_nodes into menu items, still as array of levels tree_items = [] last_lvl = nil tree_nodes.each do |lvl| item_level = [] lvl.each do |node| name = (node.name.index('/') ? File.basename(node.name) : node.name) item = {:id => aIdPrefix+node.id.to_s, :name => name } item[:node] = node if last_lvl && parent_item = last_lvl.find {|i| i[:node].id == node.parent_id} parent_item[:children] ||= [] parent_item[:children] << item item[:url] = parent_item[:url] item[:url] += '+' unless item[:url]=='' || item[:url].ends_with?('/') || item[:url].ends_with?('+') item[:url] += name.urlize('-') else item[:url] = File.join(aBaseUrl,name.urlize('-')) end item[:selected] = true if category && (category==node.name.urlize('+')) item[:order] = aOptions[:order_proc].call(item) if aOptions.has_key?(:order_proc) item_level << item end tree_items << item_level last_lvl = item_level end # clean tree_items.each do |lvl| lvl.each do |i| i.filter_include!([:url,:selected,:id,:name,:children,:order]) i[:children].sort! {|a,b| a[:order].to_i <=> b[:order].to_i} if i[:children].is_a?(Array) end end tree_items.first.sort! {|a,b| a[:order].to_i <=> b[:order].to_i} tree_items.first end |
.construct_category_tree(aRootCategory) ⇒ Object
Construct tree_nodes, an array of arrays - each array a level in tree. Each level is a list children to the parents in the level before
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/bcms_tools/view_helpers.rb', line 115 def construct_category_tree(aRootCategory) level_nodes = case aRootCategory when String [Category.find_by_name(aRootCategory)] when Category [aRootCategory] when CategoryType [aRootCategory.categories.top_level] else CategoryType.first.categories.top_level end tree_nodes = [] begin tree_nodes << level_nodes ids = level_nodes.map {|n| n.id} level_nodes = Category.find_all_by_parent_id(ids) #Category.all({:conditions => ['parent_id in (?)',ids.join(',')]}) end while !level_nodes.empty? tree_nodes end |
.default_content_for(name, &block) ⇒ Object
71 72 73 74 75 |
# File 'lib/bcms_tools/view_helpers.rb', line 71 def default_content_for(name, &block) name = name.kind_of?(Symbol) ? ":#{name}" : name out = eval("yield #{name}", block.binding) concat(out || capture(&block)) end |
.google_analytics(aTrackingId = nil) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/bcms_tools/view_helpers.rb', line 7 def google_analytics(aTrackingId = nil) return '' if request.host.begins_with?('cms.') aTrackingId ||= APP_CONFIG[:google_analytics_tracking_id] return '' unless aTrackingId.to_nil " <script>\n (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\n (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\n m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\n })(window,document,'script','//www.google-analytics.com/analytics.js','ga');\n ga('create', '\#{aTrackingId}', 'auto');\n ga('send', 'pageview');\n </script> \n EOS\nend\n" |
.mailto_a_friend(aSubject, aOptions = {}) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/bcms_tools/view_helpers.rb', line 24 def mailto_a_friend(aSubject,aOptions={}) " <SCRIPT LANGUAGE=\"JavaScript\">\n <!-- Begin\n \n //Script by Tronn: http://come.to/tronds\n //Submitted to JavaScript Kit (http://javascriptkit.com)\n //Visit http://javascriptkit.com for this script\n \n var initialsubj=\"\#{aSubject}\"\n var initialmsg=\"Hello! You may be interested in this : \"+window.location+\" \\n \\n \\n\"\n var good;\n function checkEmailAddress(field) {\n \n var goodEmail = field.value.match(/\\b(^(\\S+@).+((\\.com)|(\\.net)|(\\.edu)|(\\.mil)|(\\.gov)|(\\.org)|(\\.info)|(\\.sex)|(\\.biz)|(\\.aero)|(\\.coop)|(\\.museum)|(\\.name)|(\\.pro)|(\\..{2,2}))$)\\b/gi);\n if (goodEmail) {\n good = true;\n }\n else {\n alert('Please enter a valid address.');\n field.focus();\n field.select();\n good = false;\n }\n }\n u = window.location;\n function mailThisUrl() {\n good = false\n checkEmailAddress(document.mailto_a_friend.email);\n if (good) {\n \n //window.location = \"mailto:\"+document.mailto_a_friend.email.value+\"?subject=\"+initialsubj+\"&body=\"+document.title+\" \"+u;\n window.location = \"mailto:\"+document.mailto_a_friend.email.value+\"?subject=\"+initialsubj+\"&body=\"+initialmsg\n }\n }\n // End -->\n </script>\n \n \n <form class=\"mailto_a_friend\" name=\"mailto_a_friend\">\n <input class=\"mailto_a_friend\" type=\"text\" name=\"email\" size=\"26\" value=\"Email Address Here\" onFocus=\"this.value=''\" onMouseOver=\"window.status='Enter email address here and tell a friend about this...'; return true\" onMouseOut=\"window.status='';return true\">\n <input class=\"mailto_a_friend\" type=\"button\" value=\"Email to a friend\" onMouseOver=\"window.status='Enter email address above and click this to send an email to a friend!'; return true\" onMouseOut=\"window.status='';return true\" onClick=\"mailThisUrl();\">\n </form> \n\n EOS\nend\n" |
.render_menu2(aOptions = nil) ⇒ Object
render_menu(), or more precisely menu_items() doesn’t seem to work well for rendering a top menu when you want the ancestor item of the current page highlighted. The problem is that it compares each menu item with the current page, when the current page may be several levels deep under one of the menu items.
Fortunately the :page option allows the current page to be given, so the correct output can be produced if we manipulate this option.
This does the trick :
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/bcms_tools/view_helpers.rb', line 88 def (aOptions=nil) opts = { :from_top => 0, # menu root is how many levels down from root (0 = roots immediate children) :depth => 1, # depth of menu from menu root :show_all_siblings => true } opts.merge!(aOptions) if aOptions selected_page = opts[:page] || @page ancestors = selected_page.ancestors top_section = ancestors[opts[:from_top].to_i] return '' unless top_section opts[:path] = top_section.path ancestors << selected_page if (selected_page.section == top_section) || (selected_page != selected_page.section.pages.first) result_i = Math.min(opts[:from_top] + opts[:depth],ancestors.length-1) opts[:page] = ancestors[result_i] opts[:items] ||= (opts) return '' if opts[:items].empty? || (opts[:items].length == 1 && !opts[:items].first[:children]) # return blank if only a single menu item opts end |