Module: SchnitzelPress::Helpers

Defined in:
lib/schnitzelpress/helpers.rb

Instance Method Summary collapse

Instance Method Details

#admin_logged_in?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/schnitzelpress/helpers.rb', line 46

def admin_logged_in?
  user_logged_in? && session[:auth] == settings.administrator
end

#admin_only!Object



50
51
52
# File 'lib/schnitzelpress/helpers.rb', line 50

def admin_only!
  redirect '/login' unless admin_logged_in?
end

#base_urlObject



11
12
13
# File 'lib/schnitzelpress/helpers.rb', line 11

def base_url
  "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
end

#find_template(views, name, engine, &block) ⇒ Object



7
8
9
# File 'lib/schnitzelpress/helpers.rb', line 7

def find_template(views, name, engine, &block)
  Array(views).each { |v| super(v, name, engine, &block) }
end

#form_field(object, attribute, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/schnitzelpress/helpers.rb', line 54

def form_field(object, attribute, options = {})
  options = {
    :label => attribute.to_s.humanize,
    :value => object.send(attribute),
    :errors => object.errors[attribute.to_sym],
    :class_name => object.class.to_s.demodulize.underscore
  }.merge(options)

  options[:name] ||= "#{options[:class_name]}[#{attribute}]"
  options[:id] ||= object.new_record? ?
    "new_#{options[:class_name]}_#{attribute}" :
    "#{options[:class_name]}_#{object.id}_#{attribute}"
  options[:class] ||= "#{options[:class_name]}_#{attribute}"

  options[:type] ||= case options[:value]
    when DateTime, Time, Date then :datetime
    when Boolean, FalseClass, TrueClass then :boolean
    else :text
  end

  partial 'form_field', :object => object, :attribute => attribute, :options => options
end

#h(*args) ⇒ Object



3
4
5
# File 'lib/schnitzelpress/helpers.rb', line 3

def h(*args)
  escape_html(*args)
end

#partial(thing, locals = {}) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/schnitzelpress/helpers.rb', line 15

def partial(thing, locals = {})
  name = case thing
    when String then thing
    else thing.class.to_s.demodulize.underscore
  end

  haml :"partials/_#{name}", :locals => { name.to_sym => thing }.merge(locals)
end

#production?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/schnitzelpress/helpers.rb', line 38

def production?
  settings.environment.to_sym == :production
end

#set_page_title(title) ⇒ Object



24
25
26
# File 'lib/schnitzelpress/helpers.rb', line 24

def set_page_title(title)
  @page_title = title
end

#show_disqus?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/schnitzelpress/helpers.rb', line 34

def show_disqus?
  settings.disqus_name.present?
end

#url_for(thing, options = {}) ⇒ Object



28
29
30
31
32
# File 'lib/schnitzelpress/helpers.rb', line 28

def url_for(thing, options = {})
  url = thing.respond_to?(:to_url) ? thing.to_url : thing.to_s
  url = "#{base_url.sub(/\/$/, '')}#{url}" if options[:absolute]
  url
end

#user_logged_in?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/schnitzelpress/helpers.rb', line 42

def user_logged_in?
  session[:auth].present?
end