Module: Lokka::Helpers
- Includes:
- Rack::Utils
- Defined in:
- lib/lokka/helpers.rb
Instance Method Summary collapse
- #bread_crumb ⇒ Object
- #category? ⇒ Boolean
- #category_tree(categories = Category.roots) ⇒ Object
- #comment_form ⇒ Object
- #current_user ⇒ Object
- #daily? ⇒ Boolean
- #entries? ⇒ Boolean
- #entry? ⇒ Boolean
- #footer ⇒ Object
-
#hbr(str) ⇒ Object
h + n2br.
- #header ⇒ Object
- #index? ⇒ Boolean
- #link_to(name, url, options = {}) ⇒ Object
- #link_to_if(cond, name, url, options = {}) ⇒ Object
- #link_to_unless(cond, name, url, options = {}) ⇒ Object
- #logged_in? ⇒ Boolean
- #login_required ⇒ Object
- #monthly? ⇒ Boolean
- #months ⇒ Object
- #partial(name, options = {}) ⇒ Object
- #render_any(name, options = {}) ⇒ Object
- #render_detect(*names) ⇒ Object
- #rendering(ext, name, options = {}) ⇒ Object
- #search? ⇒ Boolean
- #select_field(object, method, values = [], options = {}) ⇒ Object
- #strip_tags(text) ⇒ Object
- #tag? ⇒ Boolean
- #truncate(text, options = {}) ⇒ Object
- #yearly? ⇒ Boolean
Methods included from Rack::Utils
#escape, #escape_org, #unescape, #unescape_org
Instance Method Details
#bread_crumb ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/lokka/helpers.rb', line 44 def bread_crumb html = '<ol>' @bread_crumbs.each do |bread| html += '<li>' if bread.last? html += bread.name else html += "<a href=\"#{bread.link}\">#{bread.name}</a>" end html += '</li>' end html += '</ol>' html end |
#category? ⇒ Boolean
9 |
# File 'lib/lokka/helpers.rb', line 9 def category?; @theme_types.include?(:category); end |
#category_tree(categories = Category.roots) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/lokka/helpers.rb', line 59 def category_tree(categories = Category.roots) html = '<ul>' categories.each do |category| html += '<li>' html += "<a href=\"#{category.link}\">#{category.title}</a>" if category.children.count > 0 html += category_tree(category.children) end html += '</li>' end html += '</ul>' html end |
#comment_form ⇒ Object
124 125 126 |
# File 'lib/lokka/helpers.rb', line 124 def comment_form haml :'system/comments/form', :layout => false end |
#current_user ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/lokka/helpers.rb', line 32 def current_user if session[:user] User.get(session[:user]) else GuestUser.new end end |
#daily? ⇒ Boolean
13 |
# File 'lib/lokka/helpers.rb', line 13 def daily?; @theme_types.include?(:daily); end |
#entries? ⇒ Boolean
15 |
# File 'lib/lokka/helpers.rb', line 15 def entries?; @theme_types.include?(:entries); end |
#entry? ⇒ Boolean
14 |
# File 'lib/lokka/helpers.rb', line 14 def entry?; @theme_types.include?(:entry); end |
#footer ⇒ Object
209 210 211 212 |
# File 'lib/lokka/helpers.rb', line 209 def s = yield_content :footer s unless s.blank? end |
#hbr(str) ⇒ Object
h + n2br
18 19 20 |
# File 'lib/lokka/helpers.rb', line 18 def hbr(str) h(str).gsub(/\r\n|\r|\n/, "<br />\n") end |
#header ⇒ Object
204 205 206 207 |
# File 'lib/lokka/helpers.rb', line 204 def header s = yield_content :header s unless s.blank? end |
#index? ⇒ Boolean
7 |
# File 'lib/lokka/helpers.rb', line 7 def index?; @theme_types.include?(:index); end |
#link_to(name, url, options = {}) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/lokka/helpers.rb', line 136 def link_to(name, url, = {}) attrs = {:href => url} if [:confirm] and [:method] attrs[:onclick] = "if(confirm('#{[:confirm]}')){var f = document.createElement('form');f.style.display = 'none';this.parentNode.appendChild(f);f.method = 'POST';f.action = this.href;var m = document.createElement('input');m.setAttribute('type', 'hidden');m.setAttribute('name', '_method');m.setAttribute('value', '#{[:method]}');f.appendChild(m);f.submit();};return false" end .delete :confirm .delete :method attrs.update() str = '' attrs.each do |key, value| str += %Q(#{key.to_s}="#{value}") end %Q(<a #{str}>#{name}</a>) end |
#link_to_if(cond, name, url, options = {}) ⇒ Object
128 129 130 |
# File 'lib/lokka/helpers.rb', line 128 def link_to_if(cond, name, url, = {}) cond ? link_to(name, url, ) : name end |
#link_to_unless(cond, name, url, options = {}) ⇒ Object
132 133 134 |
# File 'lib/lokka/helpers.rb', line 132 def link_to_unless(cond, name, url, = {}) link_to_if !cond, name, url, end |
#logged_in? ⇒ Boolean
40 41 42 |
# File 'lib/lokka/helpers.rb', line 40 def logged_in? !!session[:user] end |
#login_required ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/lokka/helpers.rb', line 22 def login_required if current_user.class != GuestUser return true else session[:return_to] = request.fullpath redirect '/admin/login' return false end end |
#monthly? ⇒ Boolean
12 |
# File 'lib/lokka/helpers.rb', line 12 def monthly?; @theme_types.include?(:monthly); end |
#months ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/lokka/helpers.rb', line 185 def months ms = {} Post.all.each do |post| m = post.created_at.strftime('%Y-%m') if ms[m].nil? ms[m] = 1 else ms[m] += 1 end end months = [] ms.each do |m, count| year, month = m.split('-') months << OpenStruct.new({:year => year, :month => month, :count => count}) end months.sort {|x, y| y.year + y.month <=> x.year + x.month } end |
#partial(name, options = {}) ⇒ Object
90 91 92 93 |
# File 'lib/lokka/helpers.rb', line 90 def partial(name, = {}) [:layout] = false render_any(name, ) end |
#render_any(name, options = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lokka/helpers.rb', line 95 def render_any(name, = {}) ret = '' settings.supported_templates.each do |ext| out = rendering(ext, name, ) out.force_encoding(Encoding.default_external) unless out.nil? unless out.blank? ret = out break end end ret end |
#render_detect(*names) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/lokka/helpers.rb', line 73 def render_detect(*names) ret = '' names.each do |name| out = render_any(name) unless out.blank? ret = out break end end if ret.blank? raise Lokka::NoTemplateError, "Template not found. #{[names.join(', ')]}" else ret end end |
#rendering(ext, name, options = {}) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/lokka/helpers.rb', line 108 def rendering(ext, name, = {}) locals = [:locals] ? {:locals => [:locals]} : {} dir = request.path_info =~ %r{^/admin/.*} ? 'admin' : "theme/#{@theme.name}" layout = "#{dir}/layout" path = "#{dir}/#{name}" puts "ext, name, theme, dir, file: #{ext}, #{name}, #{@theme.name}, #{dir}, #{settings.views}/#{path}.#{ext}" if File.exist?("#{settings.views}/#{layout}.#{ext}") [:layout] = layout.to_sym if [:layout].nil? end if File.exist?("#{settings.views}/#{path}.#{ext}") send(ext.to_sym, path.to_sym, , locals) end end |
#search? ⇒ Boolean
8 |
# File 'lib/lokka/helpers.rb', line 8 def search?; @theme_types.include?(:search); end |
#select_field(object, method, values = [], options = {}) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/lokka/helpers.rb', line 155 def select_field(object, method, values = [], = {}) name = "#{object.class.name.downcase}[#{method}]" v = object.send(method) attrs = '' .each do |key, value| attrs += %Q( #{key}="#{value}") end html = %Q(<select name="#{name}"#{attrs}>) values.each do |value| padding = value[0] == v ? ' selected="selected"' : '' html += %Q(<option value="#{value[0]}"#{padding}>#{value[1]}</option>) end html + '</select>' end |
#strip_tags(text) ⇒ Object
181 182 183 |
# File 'lib/lokka/helpers.rb', line 181 def (text) text.gsub(/<.+?>/, '') end |
#tag? ⇒ Boolean
10 |
# File 'lib/lokka/helpers.rb', line 10 def tag?; @theme_types.include?(:tag); end |
#truncate(text, options = {}) ⇒ Object
172 173 174 175 176 177 178 179 |
# File 'lib/lokka/helpers.rb', line 172 def truncate(text, = {}) = {:length => 30, :ommision => '...'}.merge() if [:length] < text.length text[0..[:length]] + [:ommision] else text end end |
#yearly? ⇒ Boolean
11 |
# File 'lib/lokka/helpers.rb', line 11 def yearly?; @theme_types.include?(:yearly); end |