Module: ApplicationHelper

Defined in:
app/helpers/application_helper.rb

Overview

Helpers for the entire application STYLE: Don’t place methods here if they are used in one special controller only

Instance Method Summary (collapse)

Instance Method Details

- (Object) current_role

Integer current_user’s roles_mask if current_user exists, 0 (Guest) otherwise

Returns:

  • Integer current_user’s roles_mask if current_user exists, 0 (Guest) otherwise



121
122
123
# File 'app/helpers/application_helper.rb', line 121

def current_role
  current_user ? (current_user.roles_mask||0) : 0
end

- (Object) current_user_field(fieldname, default = '')

Return `current_user.field` if current_user, otherwise return `default`

Parameters:

  • fieldname (String)
    • field of class User

  • default (String) (defaults to: '')
    • will be returned if no current_user exists.

Returns:

  • String - current-user’s field (may be nil if User.field not exists!) or `default`



46
47
48
49
50
51
52
# File 'app/helpers/application_helper.rb', line 46

def current_user_field(fieldname,default='')
  if user_signed_in?
    current_user.try(fieldname) || ''
  else
    default
  end
end

- (Object) current_view_sidebar_left_path

String - Path of the sidebar-partial of the current controller

Returns:

  • String - Path of the sidebar-partial of the current controller



159
160
161
# File 'app/helpers/application_helper.rb', line 159

def current_view_sidebar_left_path
  path = "/#{controller_name.underscore}/sidebar_left"
end

- (Object) errors_for(resource)

Display errors for resource

Parameters:

  • resource (ActiveModel)

Returns:

  • String - html-div id=’error_explanation’



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/helpers/application_helper.rb', line 93

def errors_for(resource)
  rc = ""
  if resource.errors.any?
    rc += "<div id='error_explanation'>"+
            "<h2>"+
              t(:errors, :count => resource.errors.count) + ": " +
              t(:prohibited_this_resource_from_being_saved, :resource => t(resource.class.to_s.downcase.to_sym)) +
            "</h2>"+
            "<ul>" +
               resource.errors.full_messages.map { |msg|
                 "<li><b>"+ msg.split(" ",2)[0] + "</b>: " + msg.split(" ",2)[1] +"</li>"
               }.join
          if defined? resource.attachments
            rc += "<ul>" +
                  resource.attachments.map { |p|
                     p.errors.map { |error|
                       "<li>" + p.errors[error].join(", ".html_safe) + "</li>"
                     }.join
                  }.join +
                  "</ul>"
          end
    rc += "</ul></div>"
    rc.html_safe
  end

end

- (Object) insert_extra_headers

Insert extra headers to html-head Most of this meta-tags are configurable in `application.yml`



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/application_helper.rb', line 28

def insert_extra_headers
  "<meta name='Language' content='#{t('locales.'+I18n.locale.to_s)}, #{I18n.locale}' />
  <meta name='Author' content='#{APPLICATION_CONFIG['copyright']}' />
  <meta name='publisher' content='#{APPLICATION_CONFIG['copyright']}' />
  <meta name='robots' content='index, follow, noarchive' />
  <meta name='distribution' content='global' />
  <meta name='page-topic' content='RAILS, Programming, Mac OS X, iOS, Web-Development' />
  <meta name='description' content='#{strip_tags(APPLICATION_CONFIG['name'])} | #{strip_tags(APPLICATION_CONFIG['slogan'])}' />
  <meta name='keywords' content='RAILS, CBA, Application Template, devise, cancan, omniAuth, Programming, Mac OS X, iOS, Web-Development' />
  <meta name='revisit-after' content='2 days' />
  <meta http-equiv='reply-to' content='#{APPLICATION_CONFIG['admin_notification_address']}' />
  ".html_safe
end

- (Object) insert_google_analytics_script

yield :google_analytics in HTML-HEAD



9
10
11
12
13
14
15
# File 'app/helpers/application_helper.rb', line 9

def insert_google_analytics_script
  if File::exist?(
       filename=File::join(Rails.root,"config/google_analytics.head")
     )
     File.new(filename).read.html_safe
  end
end

- (Object) insert_google_site_search

insert google site search if file exists



18
19
20
21
22
23
24
# File 'app/helpers/application_helper.rb', line 18

def insert_google_site_search
  if File::exist?(
       filename=File::join(Rails.root,"config/google_site_search.html")
     )
     File.new(filename).read.html_safe
  end
end

- (Object) is_on_last_page(collection)

Check if paginate is on last page

Parameters:

  • collection (WillPaginate-Array)

Returns:

  • Boolean - true if on last page



86
87
88
# File 'app/helpers/application_helper.rb', line 86

def is_on_last_page(collection)
  collection.total_pages && (collection.current_page < collection.total_pages)
end

Insert a new file-field to form

Parameters:

  • name (String)
    • The name of the field

  • f (FormHelper)
    • The FormHelper of the form to insert.

  • association (String)
    • The detail-model we will insert fields for

Returns:

  • the link_to_function-html-code.



67
68
69
70
71
72
73
# File 'app/helpers/application_helper.rb', line 67

def link_to_add_fields(name,f,association)
  new_object = f.object.class.reflect_on_association(association).klass.new
  fields  = f.fields_for(association,new_object, :child_index=>"new_#{association}") do |builder|
    render(association.to_s.pluralize+"/"+association.to_s.singularize + "_fields", :f => builder)
  end
  ui_link_to_function('add',name,"add_fields(this,\"#{association}\", \"#{escape_javascript(fields)}\")")
end

Remove an attached file Set the hidden field to destroy this detail-record on update-attributes

Parameters:

  • name (String)
    • Name of the field to remove

  • f (FormHelper)
    • FormHelper to use.



79
80
81
# File 'app/helpers/application_helper.rb', line 79

def link_to_remove_fields(name, f)
  f.hidden_field(:_destroy) + "&nbsp;".html_safe + ui_link_to_function('delete',name,"remove_fields(this)")
end

- (Object) present(object, klass = nil) {|presenter| ... }

A helper to load presenters

Parameters:

  • object (Object)
    • The object to be presented

  • klass (Class) (defaults to: nil)
    • If not using OBJECTPresenter as class-name

Yields:

  • (presenter)


141
142
143
144
145
146
# File 'app/helpers/application_helper.rb', line 141

def present(object, klass=nil)
  klass ||= "#{object.class}Presenter".constantize
  presenter = klass.new(object, self)
  yield presenter if block_given?
  presenter
end

- (Object) setup_action_buttons

See the main-layout application.html.erb where this buttons will be displayed at runtime. Renders partial ‘home/action_buttons’



56
57
58
59
60
# File 'app/helpers/application_helper.rb', line 56

def setup_action_buttons
  content_for :action_buttons do
    render :partial => '/home/action_buttons'
  end
end

Boolean - true if a partial named ‘_sidebar_left’ exists for the current controller

Returns:

  • (Boolean)

    Boolean - true if a partial named ‘_sidebar_left’ exists for the current controller



149
150
151
152
153
154
155
156
# File 'app/helpers/application_helper.rb', line 149

def sidebar_partial_exists?
  ['haml', 'html', 'html.erb'].each do |ext|
    return true if File::exist?(
      File::join( Rails.root, "app", "views", controller_name.underscore, "_sidebar_left.#{ext}")
    )
  end
  false
end

- (Object) with_format(view, format, &block)

Force rendering of a given block with `format` and then switch back to the format defined before.

Parameters:

  • view (ActionView)
    • the view to use

  • format (Symbol)
    • force this format

  • - (Block)

    do this block with the forced format



131
132
133
134
135
136
# File 'app/helpers/application_helper.rb', line 131

def with_format(view, format, &block)
  old_formats = view.formats
  view.formats = [:html]
  yield
  view.formats = old_formats
end