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)
-
- (Object) current_role
Integer current_user’s roles_mask if current_user exists, 0 (Guest) otherwise.
-
- (Object) current_user_field(fieldname, default = '')
Return `current_user.field` if current_user, otherwise return `default`.
-
- (Object) current_view_sidebar_left_path
String - Path of the sidebar-partial of the current controller.
-
- (Object) errors_for(resource)
Display errors for resource.
-
- (Object) insert_extra_headers
Insert extra headers to html-head Most of this meta-tags are configurable in `application.yml`.
-
- (Object) insert_google_analytics_script
yield :google_analytics in HTML-HEAD.
-
- (Object) insert_google_site_search
insert google site search if file exists.
-
- (Object) is_on_last_page(collection)
Check if paginate is on last page.
-
- (Object) link_to_add_fields(name, f, association)
Insert a new file-field to form.
-
- (Object) link_to_remove_fields(name, f)
Remove an attached file Set the hidden field to destroy this detail-record on update-attributes.
-
- (Object) present(object, klass = nil) {|presenter| ... }
A helper to load presenters.
-
- (Object) setup_action_buttons
See the main-layout application.html.erb where this buttons will be displayed at runtime.
-
- (Boolean) sidebar_partial_exists?
Boolean - true if a partial named ‘_sidebar_left’ exists for the current controller.
-
- (Object) with_format(view, format, &block)
Force rendering of a given block with `format` and then switch back to the format defined before.
Instance Method Details
- (Object) current_role
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`
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
159 160 161 |
# File 'app/helpers/application_helper.rb', line 159 def path = "/#{controller_name.underscore}/sidebar_left" end |
- (Object) errors_for(resource)
Display errors for resource
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..map { |msg| "<li><b>"+ msg.split(" ",2)[0] + "</b>: " + msg.split(" ",2)[1] +"</li>" }.join if defined? resource. rc += "<ul>" + resource..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='#{(APPLICATION_CONFIG['name'])} | #{(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
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 |
- (Object) link_to_add_fields(name, f, association)
Insert a new file-field to form
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 |
- (Object) link_to_remove_fields(name, f)
Remove an attached file Set the hidden field to destroy this detail-record on update-attributes
79 80 81 |
# File 'app/helpers/application_helper.rb', line 79 def link_to_remove_fields(name, f) f.(:_destroy) + " ".html_safe + ui_link_to_function('delete',name,"remove_fields(this)") end |
- (Object) present(object, klass = nil) {|presenter| ... }
A helper to load presenters
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 content_for :action_buttons do render :partial => '/home/action_buttons' end end |
- (Boolean) sidebar_partial_exists?
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 ['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.
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 |