Module: Schreihals::Helpers
- Defined in:
- lib/schreihals/helpers.rb
Instance Method Summary collapse
- #admin_logged_in? ⇒ Boolean
- #admin_only! ⇒ Object
- #base_url ⇒ Object
- #find_template(views, name, engine, &block) ⇒ Object
- #form_field(object, attribute, options = {}) ⇒ Object
- #partial(thing, locals = {}) ⇒ Object
- #production? ⇒ Boolean
- #set_page_title(title) ⇒ Object
- #show_disqus? ⇒ Boolean
- #url_for(thing, options = {}) ⇒ Object
- #user_logged_in? ⇒ Boolean
Instance Method Details
#admin_logged_in? ⇒ 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_url ⇒ Object
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, = {}) = { label: attribute.to_s.humanize, value: object.send(attribute), errors: object.errors[attribute.to_sym], class_name: object.class.to_s.demodulize.underscore }.merge() [:name] ||= "#{[:class_name]}[#{attribute}]" [:id] ||= object.new_record? ? "new_#{[:class_name]}_#{attribute}" : "#{[:class_name]}_#{object.id}_#{attribute}" [:type] ||= case [:value] when DateTime, Time, Date then :datetime else :text end partial 'form_field', object: object, attribute: attribute, 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
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
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, = {}) url = thing.respond_to?(:to_url) ? thing.to_url : thing.to_s url = "#{base_url.sub(/\/$/, '')}#{url}" if [:absolute] url end |
#user_logged_in? ⇒ Boolean
38 39 40 |
# File 'lib/schreihals/helpers.rb', line 38 def user_logged_in? session[:user].present? end |