Module: Schreihals::Helpers

Defined in:
lib/schreihals/helpers.rb

Instance Method Summary collapse

Instance Method Details

#admin_logged_in?Boolean

Returns:

  • (Boolean)


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

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

#admin_only!Object



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

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

#base_urlObject



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

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

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



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

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

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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/schreihals/helpers.rb', line 50

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[:type] ||= case options[:value]
    when DateTime, Time, Date then :datetime
    else :text
  end

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

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



11
12
13
14
15
16
17
18
# File 'lib/schreihals/helpers.rb', line 11

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)


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

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

#set_page_title(title) ⇒ Object



20
21
22
# File 'lib/schreihals/helpers.rb', line 20

def set_page_title(title)
  @page_title = title
end

#show_disqus?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/schreihals/helpers.rb', line 30

def show_disqus?
  settings.disqus_name.present?
end

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



24
25
26
27
28
# File 'lib/schreihals/helpers.rb', line 24

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)


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

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