Module: Lokka::Helpers

Includes:
Rack::Utils
Defined in:
lib/lokka/helpers.rb

Instance Method Summary collapse

Methods included from Rack::Utils

#escape, #escape_org, #unescape, #unescape_org

Instance Method Details



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

Returns:

  • (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_formObject



124
125
126
# File 'lib/lokka/helpers.rb', line 124

def comment_form
  haml :'system/comments/form', :layout => false
end

#current_userObject



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

Returns:

  • (Boolean)


13
# File 'lib/lokka/helpers.rb', line 13

def daily?;     @theme_types.include?(:daily); end

#entries?Boolean

Returns:

  • (Boolean)


15
# File 'lib/lokka/helpers.rb', line 15

def entries?;   @theme_types.include?(:entries); end

#entry?Boolean

Returns:

  • (Boolean)


14
# File 'lib/lokka/helpers.rb', line 14

def entry?;     @theme_types.include?(:entry); end


209
210
211
212
# File 'lib/lokka/helpers.rb', line 209

def footer
  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

#headerObject



204
205
206
207
# File 'lib/lokka/helpers.rb', line 204

def header
  s = yield_content :header
  s unless s.blank?
end

#index?Boolean

Returns:

  • (Boolean)


7
# File 'lib/lokka/helpers.rb', line 7

def index?;     @theme_types.include?(:index); end


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, options = {})
  attrs = {:href => url}
  if options[:confirm] and options[:method]
    attrs[:onclick] = "if(confirm('#{options[: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', '#{options[:method]}');f.appendChild(m);f.submit();};return false"
  end

  options.delete :confirm
  options.delete :method

  attrs.update(options)

  str = ''
  attrs.each do |key, value|
    str += %Q(#{key.to_s}="#{value}")
  end

  %Q(<a #{str}>#{name}</a>)
end


128
129
130
# File 'lib/lokka/helpers.rb', line 128

def link_to_if(cond, name, url, options = {})
  cond ? link_to(name, url, options) : name
end


132
133
134
# File 'lib/lokka/helpers.rb', line 132

def link_to_unless(cond, name, url, options = {})
  link_to_if !cond, name, url, options
end

#logged_in?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/lokka/helpers.rb', line 40

def logged_in?
  !!session[:user]
end

#login_requiredObject



22
23
24
25
26
27
28
29
30
# File 'lib/lokka/helpers.rb', line 22

def 
  if current_user.class != GuestUser
    return true
  else
    session[:return_to] = request.fullpath
    redirect '/admin/login'
    return false
  end
end

#monthly?Boolean

Returns:

  • (Boolean)


12
# File 'lib/lokka/helpers.rb', line 12

def monthly?;   @theme_types.include?(:monthly); end

#monthsObject



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, options = {})
  options[:layout] = false
  render_any(name, options)
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, options = {})
  ret = ''
  settings.supported_templates.each do |ext|
    out = rendering(ext, name, options)
    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, options = {})
  locals = options[:locals] ? {:locals => options[: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}")
    options[:layout] = layout.to_sym if options[:layout].nil?
  end
  if File.exist?("#{settings.views}/#{path}.#{ext}")
    send(ext.to_sym, path.to_sym, options, locals)
  end
end

#search?Boolean

Returns:

  • (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 = [], options = {})
  name = "#{object.class.name.downcase}[#{method}]"
  v = object.send(method)

  attrs = ''
  options.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 strip_tags(text)
  text.gsub(/<.+?>/, '')
end

#tag?Boolean

Returns:

  • (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, options = {})
  options = {:length => 30, :ommision => '...'}.merge(options)
  if options[:length] < text.length
    text[0..options[:length]] + options[:ommision]
  else
    text
  end
end

#yearly?Boolean

Returns:

  • (Boolean)


11
# File 'lib/lokka/helpers.rb', line 11

def yearly?;    @theme_types.include?(:yearly); end