| Module | ApplicationHelper |
| In: |
app/helpers/application_helper.rb
|
Methods added to this helper will be available to all templates in the application.
| environment | [R] |
# File app/helpers/application_helper.rb, line 719
719: def base_url
720: environment.top_url(request.ssl?)
721: end
# File app/helpers/application_helper.rb, line 207
207: def button(type, label, url, html_options = {})
208: the_class = 'with-text'
209: if html_options.has_key?(:class)
210: the_class << ' ' << html_options[:class]
211: end
212: button_without_text type, label, url, html_options.merge(:class => the_class)
213: end
# File app/helpers/application_helper.rb, line 258
258: def button_bar(options = {}, &block)
259: concat(content_tag('div', capture(&block) + tag('br', :style => 'clear: left;'), { :class => 'button-bar' }.merge(options)), block.binding)
260: end
# File app/helpers/application_helper.rb, line 223
223: def button_to_function(type, label, js_code, html_options = {}, &block)
224: html_options[:class] = "button with-text" unless html_options[:class]
225: html_options[:class] << " icon-#{type}"
226: link_to_function(label, js_code, html_options, &block)
227: end
# File app/helpers/application_helper.rb, line 229
229: def button_to_function_without_text(type, label, js_code, html_options = {}, &block)
230: html_options[:class] = "" unless html_options[:class]
231: html_options[:class] << " button icon-#{type}"
232: link_to_function(content_tag('span', label), js_code, html_options, &block)
233: end
# File app/helpers/application_helper.rb, line 235
235: def button_to_remote_without_text(type, label, options, html_options = {})
236: html_options[:class] = "" unless html_options[:class]
237: html_options[:class] << " button icon-#{type}"
238: link_to_remote(content_tag('span', label), options, html_options)
239: end
# File app/helpers/application_helper.rb, line 215
215: def button_without_text(type, label, url, html_options = {})
216: the_class = "button icon-#{type}"
217: if html_options.has_key?(:class)
218: the_class << ' ' << html_options[:class]
219: end
220: link_to(content_tag('span', label), url, html_options.merge(:class => the_class ))
221: end
# File app/helpers/application_helper.rb, line 159
159: def category_color
160: if @category.nil?
161: ""
162: else
163: @category.top_ancestor.display_color
164: end
165: end
# File app/helpers/application_helper.rb, line 327
327: def current_theme
328: return session[:theme] if (session[:theme])
329: p = profile
330: if p
331: p.theme
332: else
333: @environment.theme
334: end
335: end
TODO: do something more useful here TODO: test this helper TODO: add an icon? TODO: the command rake test:rcov didn‘t works because of this method. See what it‘s the problem
# File app/helpers/application_helper.rb, line 105
105: def environment_identification
106: content_tag('div', @environment.name, :id => 'environment_identification')
107: end
# File app/helpers/application_helper.rb, line 556
556: def file_field_or_thumbnail(label, image, i)
557: display_form_field label, (
558: render :partial => (image && image.valid? ? 'shared/show_thumbnail' : 'shared/change_image'),
559: :locals => { :i => i, :image => image }
560: )
561: end
# File app/helpers/application_helper.rb, line 171
171: def file_manager(&block)
172: concat(
173: content_tag('div',
174: content_tag('div', capture(&block) + '<br style="clear:left;"/> '),
175: :class => 'file-manager'),
176: block.binding)
177: end
# File app/helpers/application_helper.rb, line 179
179: def file_manager_button(title, icon, url)
180: content_tag( 'div',
181: link_to(image_tag(icon, :alt => title, :title => title) +
182: content_tag('span', title),
183: url), :class => 'file-manager-button' )
184: end
# File app/helpers/application_helper.rb, line 311
311: def filename_for_stylesheet(name, in_theme)
312: result = ''
313: if in_theme
314: result << theme_path
315: end
316: result << '/stylesheets/' << name << '.css'
317: end
# File app/helpers/application_helper.rb, line 143
143: def footer
144: # FIXME: add some information from the environment
145: [
146: content_tag('div', _('This is %s, version %s') % [ link_to(Noosfero::PROJECT, 'http://www.noosfero.com.br/'), Noosfero::VERSION]),
147: ].join("\n")
148: end
# File app/helpers/application_helper.rb, line 440
440: def gravatar_url_for(email, options = {})
441: # Ta dando erro de roteamento
442: url_for( { :gravatar_id => Digest::MD5.hexdigest(email),
443: :host => 'www.gravatar.com',
444: :protocol => 'http://',
445: :only_path => false,
446: :controller => 'avatar.php'
447: }.merge(options) )
448: end
Displays context help. You can pass the content of the help message as the first parameter or using template code inside a block passed to this method. Note: the block is ignored if content is not nil
The method returns the text generated, so you can also use it inside a
<%= ... %>
Follow some examples …
Passing the text as argument:
<% help 'This your help message' %>
Using a block:
<% help do %> This is the help message to be displayed. It can contain any HTML you want: <strong>bold</strong>, <em>italic</em>. It can also contain calls to any Rails helper, like <%= link_to 'home', :controller => 'home' %>. <% end %>
You can also pass an optional argument to force the use of textile in your help message:
<% help nil, :textile do %> You can also use *textile*! <% end %>
or, using the return of the method:
<%= help 'this is your help message' %>
Formally, the type argument can be :html (i.e. no conversion of the input) or :textile (converts the message, in textile, into HTML). It defaults to :html.
TODO: implement correcly the ‘Help’ button click
# File app/helpers/application_helper.rb, line 61
61: def help(content = nil, link_name = nil, options = {}, &block)
62:
63: link_name ||= _('Help')
64:
65: @help_message_id ||= 1
66: help_id = "help_message_#{@help_message_id}"
67:
68: if content.nil?
69: return '' if block.nil?
70: content = capture(&block)
71: end
72:
73: if options[:type] == :textile
74: content = RedCloth.new(content).to_html
75: end
76:
77: options[:class] = '' if ! options[:class]
78: options[:class] += ' button icon-help' # with-text
79:
80: # TODO: implement this button, and add style='display: none' to the help
81: # message DIV
82: button = link_to_function(content_tag('span', link_name), "Element.show('#{help_id}')", options )
83: close_button = content_tag("div", link_to_function(_("Close"), "Element.hide('#{help_id}')", :class => 'close_help_button'))
84:
85: text = content_tag('div', button + content_tag('div', content_tag('div', content) + close_button, :class => 'help_message', :id => help_id, :style => 'display: none;'), :class => 'help_box')
86:
87: unless block.nil?
88: concat(text, block.binding)
89: end
90:
91: text
92: end
alias for help(content, :textile). You can pass a block in the same way you would do if you called help directly.
# File app/helpers/application_helper.rb, line 96
96: def help_textile(content = nil, link_name = nil, options = {}, &block)
97: options[:type] = :textile
98: help(content, link_name, options, &block)
99: end
# File app/helpers/application_helper.rb, line 186
186: def hide(id)
187: "Element.hide(#{id.inspect});"
188: end
# File app/helpers/application_helper.rb, line 241
241: def icon(icon_name, html_options = {})
242: the_class = "button #{icon_name}"
243: if html_options.has_key?(:class)
244: the_class << ' ' << html_options[:class]
245: end
246: content_tag('div', '', html_options.merge(:class => the_class))
247: end
# File app/helpers/application_helper.rb, line 249
249: def icon_button(type, text, url, html_options = {})
250: the_class = "button icon-button icon-#{type}"
251: if html_options.has_key?(:class)
252: the_class << ' ' << html_options[:class]
253: end
254:
255: link_to(content_tag('span', text), url, html_options.merge(:class => the_class, :title => text))
256: end
# File app/helpers/application_helper.rb, line 344
344: def is_testing_theme
345: !@controller.session[:theme].nil?
346: end
# File app/helpers/application_helper.rb, line 686
686: def labelled_fields_for(name, object = nil, options = {}, &proc)
687: object ||= instance_variable_get("@#{name}")
688: fields_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc)
689: end
create a form field structure (as if it were generated with labelled_form_for), but with a customized control and label.
If field_id is not given, the underlying implementation will try to guess it from field_html using a regular expression. In this case, make sure that the first ocurrance of id=[’"]([^’"]*)[’"] in field_html if the one you want (i.e. the correct id for the control )
# File app/helpers/application_helper.rb, line 680
680: def labelled_form_field(label, field_html, field_id = nil)
681: NoosferoFormBuilder::output_field(label, field_html, field_id)
682: end
# File app/helpers/application_helper.rb, line 691
691: def labelled_form_for(name, object = nil, options = {}, &proc)
692: object ||= instance_variable_get("@#{name}")
693: form_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc)
694: end
# File app/helpers/application_helper.rb, line 135
135: def link_if_permitted(link, permission = nil, target = nil)
136: if permission.nil? || current_user.person.has_permission?(permission, target)
137: link
138: else
139: nil
140: end
141: end
# File app/helpers/application_helper.rb, line 262
262: def link_to_category(category, full = true)
263: return _('Uncategorized product') unless category
264: name = full ? category.full_name(' → ') : category.name
265: link_to name, :controller => 'search', :action => 'category_index', :category_path => category.path.split('/')
266: end
# File app/helpers/application_helper.rb, line 109
109: def link_to_cms(text, profile = nil, options = {})
110: profile ||= current_user.login
111: link_to text, myprofile_path(:controller => 'cms', :profile => profile), options
112: end
# File app/helpers/application_helper.rb, line 129
129: def link_to_document(doc, text = nil)
130: text ||= doc.title
131: path = doc.path.split(/\//)
132: link_to text, homepage_path(:profile => doc.profile.identifier , :page => path)
133: end
# File app/helpers/application_helper.rb, line 119
119: def link_to_homepage(text, profile = nil, options = {})
120: profile ||= current_user.login
121: link_to text, homepage_path(:profile => profile) , options
122: end
# File app/helpers/application_helper.rb, line 124
124: def link_to_myprofile(text, url = {}, profile = nil, options = {})
125: profile ||= current_user.login
126: link_to text, { :profile => profile, :controller => 'profile_editor' }.merge(url), options
127: end
# File app/helpers/application_helper.rb, line 268
268: def link_to_product(product, opts={})
269: return _('No product') unless product
270: link_to content_tag( 'span', product.name ),
271: { :controller => 'catalog', :action => 'show', :id => product, :profile => product.enterprise.identifier },
272: opts
273: end
# File app/helpers/application_helper.rb, line 114
114: def link_to_profile(text, profile = nil, options = {})
115: profile ||= current_user.login
116: link_to text, profile_path(:profile => profile) , options
117: end
# File app/helpers/application_helper.rb, line 713
713: def login_url
714: options = { :controller => 'account', :action => 'login' }
715: options.merge!(:protocol => 'https://', :host => request.host) unless ENV['RAILS_ENV'] == 'development' || environment.disable_ssl
716: url_for(options)
717: end
# File app/helpers/application_helper.rb, line 275
275: def partial_for_class(klass)
276: if klass.nil?
277: raise ArgumentError, 'No partial for object. Is there a partial for any class in the inheritance hierarchy?'
278: end
279:
280: name = klass.name.underscore
281: if File.exists?(File.join(RAILS_ROOT, 'app', 'views', params[:controller], "_#{name}.rhtml"))
282: name
283: else
284: partial_for_class(klass.superclass)
285: end
286: end
returns the current profile beign viewed.
Make sure that you use this helper method only in contexts where there should be a current profile (i.e. while viewing some profile‘s pages, or the profile info, etc), because if there is no profile an exception is thrown.
# File app/helpers/application_helper.rb, line 155
155: def profile
156: @controller.send(:profile)
157: end
# File app/helpers/application_helper.rb, line 394
394: def profile_cat_icons( profile )
395: if profile.class == Enterprise
396: icons =
397: profile.product_categories.map{ |c| c.size > 1 ? c[1] : nil }.
398: compact.uniq.map{ |c|
399: cat_name = c.gsub( /[-_\s,.;'"]+/, '_' )
400: cat_icon = "/images/icons-cat/#{cat_name}.png"
401: if ! File.exists? RAILS_ROOT.to_s() + '/public/' + cat_icon
402: cat_icon = '/images/icons-cat/undefined.png'
403: end
404: content_tag 'span',
405: content_tag( 'span', c ),
406: :title => c,
407: :class => 'product-cat-icon cat_icon_' + cat_name,
408: :style => "background-image:url(#{cat_icon})"
409: }.join "\n"
410: content_tag 'div',
411: content_tag( 'span', _('Principal Product Categories'), :class => 'header' ) +"\n"+ icons,
412: :class => 'product-category-icons'
413: else
414: ''
415: end
416: end
# File app/helpers/application_helper.rb, line 363
363: def profile_icon( profile, size=:portrait )
364: if profile.image
365: profile.image.public_filename( size )
366: else
367: if profile.organization?
368: if profile.kind_of?(Community)
369: '/images/icons-app/users_size-'+ size.to_s() +'.png'
370: else
371: '/images/icons-app/enterprise-default-pic-'+ size.to_s() +'.png'
372: end
373: else
374: '/images/icons-app/user_icon_size-'+ size.to_s() +'.png'
375: end
376: end
377:
378: end
generates a image tag for the profile.
If the profile has no image set yet, then a default image is used.
# File app/helpers/application_helper.rb, line 355
355: def profile_image(profile, size=:portrait, opt={})
356: opt[:alt] ||= profile.name()
357: opt[:title] ||= ''
358: opt[:class] ||= ''
359: opt[:class] += ( profile.class == Person ? ' photo' : ' logo' )
360: image_tag(profile_icon(profile, size), opt )
361: end
displays a link to the profile homepage with its image (as generated by profile_image) and its name below it.
# File app/helpers/application_helper.rb, line 420
420: def profile_image_link( profile, size=:portrait, tag='li' )
421: if profile.class == Person
422: name = profile.short_name
423: city = content_tag 'span', content_tag( 'span', profile.city, :class => 'locality' ), :class => 'adr'
424: else
425: name = profile.short_name
426: city = ''
427: end
428: content_tag tag,
429: link_to(
430: content_tag( 'span', profile_image( profile, size ), :class => 'profile-image' ) +
431: content_tag( 'span', name, :class => ( profile.class == Person ? 'fn' : 'org' ) ) +
432: city + profile_sex_icon( profile ) + profile_cat_icons( profile ),
433: profile.url,
434: :onclick => 'document.location.href = this.href', # work-arround for ie.
435: :class => 'profile_link url',
436: :help => _('Click on this icon to go to the <b>%s</b>\'s home page') % profile.name ),
437: :class => 'vcard'
438: end
# File app/helpers/application_helper.rb, line 380
380: def profile_sex_icon( profile )
381: if profile.class == Person
382: sex = ( profile.sex ? profile.sex.to_s() : 'undef' )
383: title = ( sex == 'undef' ? _('non registered gender') : ( sex == 'male' ? _('Male') : _('Female') ) )
384: sex = content_tag 'span',
385: content_tag( 'span', sex ),
386: :class => 'sex-'+sex,
387: :title => title
388: else
389: sex = ''
390: end
391: sex
392: end
# File app/helpers/application_helper.rb, line 568
568: def role_color(role)
569: case role
570: when Profile::Roles.admin
571: 'blue'
572: when Profile::Roles.member
573: 'green'
574: when Profile::Roles.moderator
575: 'gray'
576: else
577: 'black'
578: end
579: end
# File app/helpers/application_helper.rb, line 563
563: def rolename_for(profile, resource)
564: role = profile.role_assignments.find_by_resource_id(resource.id).role
565: content_tag('span', role.name, :style => "color: #{role_color(role)}")
566: end
# File app/helpers/application_helper.rb, line 696
696: def search_page_title(title, options={})
697: title = "<h1>" + title + "</h1>"
698: title += "<h2 align='center'>" + _("Searched for '%s'") % options[:query] + "</h2>" if !options[:query].blank?
699: title += "<h2 align='center'>" + _("In category %s") % options[:category] + "</h2>" if !options[:category].blank?
700: title += "<h2 align='center'>" + _("within %d km from %s") % [options[:distance], options[:region]] + "</h2>" if !options[:distance].blank? && !options[:region].blank?
701: title += "<h2 align='center'>" + _("%d results found") % options[:total_results] + "</h2>" if !options[:total_results].blank?
702: title
703: end
# File app/helpers/application_helper.rb, line 462
462: def select_categories(object_name, title=nil, title_size=4)
463: return nil if environment.enabled?(:disable_categories)
464: if title.nil?
465: title = _('Categories')
466: end
467:
468: object = instance_variable_get("@#{object_name}")
469:
470: result = content_tag 'h'+title_size.to_s(), title
471: result << javascript_tag( 'function open_close_cat( link ) {
472: var div = link.parentNode.getElementsByTagName("div")[0];
473: var end = function(){
474: if ( div.style.display == "none" ) {
475: this.link.className="button icon-button icon-down"
476: } else {
477: this.link.className="button icon-button icon-up-red"
478: }
479: }
480: Effect.toggle( div, "slide", { link:link, div:div, afterFinish:end } )
481: }')
482: environment.top_level_categories.select{|i| !i.children.empty?}.each do |toplevel|
483: next unless object.accept_category?(toplevel)
484: # FIXME
485: ([toplevel] + toplevel.children_for_menu).each do |cat|
486: if cat.top_level?
487: result << '<div class="categorie_box">'
488: result << icon_button( :down, _('open'), '#', :onclick => 'open_close_cat(this); return false' )
489: result << content_tag('h5', toplevel.name)
490: result << '<div style="display:none"><ul class="categories">'
491: else
492: checkbox_id = "#{object_name}_#{cat.full_name.downcase.gsub(/\s+|\//, '_')}"
493: result << content_tag('li', labelled_check_box(
494: cat.full_name_without_leading(1, " → "),
495: "#{object_name}[category_ids][]", cat.id,
496: object.category_ids.include?(cat.id), :id => checkbox_id,
497: :onchange => 'this.parentNode.className=(this.checked?"cat_checked":"")' ),
498: :class => ( object.category_ids.include?(cat.id) ? 'cat_checked' : '' ) ) + "\n"
499: end
500: end
501: result << '</ul></div></div>'
502: end
503:
504: content_tag('div', result)
505: end
# File app/helpers/application_helper.rb, line 190
190: def show(id)
191: "Element.show(#{id.inspect});"
192: end
# File app/helpers/application_helper.rb, line 450
450: def str_gravatar_url_for(email, options = {})
451: url = 'http://www.gravatar.com/avatar.php?gravatar_id=' +
452: Digest::MD5.hexdigest(email)
453: { :only_path => false }.merge(options).each { |k,v|
454: url += ( '&%s=%s' % [ k,v ] )
455: }
456: # we can set the default imgage with this:
457: # :default => 'DOMAIN/images/icons-app/gravatar-minor.gif'
458: url
459: end
# File app/helpers/application_helper.rb, line 293
293: def stylesheet_import(*sources)
294: options = sources.last.is_a?(Hash) ? sources.pop : { }
295: themed_source = options.delete(:themed_source)
296: content_tag(
297: 'style',
298: "\n" +
299: sources.flatten.map do |source|
300: filename = filename_for_stylesheet(source.to_s, themed_source)
301: if File.exists?(File.join(RAILS_ROOT, 'public', filename))
302: "@import url(#{filename});\n"
303: else
304: "/* Not included: url(#{filename}) */\n"
305: end
306: end.join(),
307: { "type" => "text/css" }.merge(options)
308: )
309: end
# File app/helpers/application_helper.rb, line 705
705: def template_stylesheet_tag
706: if profile.nil?
707: stylesheet_link_tag '/designs/templates/default/stylesheets/style.css'
708: else
709: stylesheet_link_tag "/designs/templates/#{profile.layout_template}/stylesheets/style.css"
710: end
711: end
# File app/helpers/application_helper.rb, line 167
167: def text_editor(object, method, filter_type_method = nil, options = {})
168: text_area(object, method, { :rows => 10, :cols => 64 }.merge(options))
169: end
# File app/helpers/application_helper.rb, line 337
337: def theme_footer
338: footer = ('../../public' + theme_path + '/footer.rhtml')
339: if File.exists?(RAILS_ROOT + '/app/views/' + footer)
340: render :file => footer
341: end
342: end
# File app/helpers/application_helper.rb, line 540
540: def theme_javascript
541: option = theme_option(:js)
542: return if option.nil?
543: html = []
544: option.each do |file|
545: file = theme_path +
546: '/javascript/'+ file +'.js'
547: if File.exists? RAILS_ROOT.to_s() +'/public'+ file
548: html << javascript_src_tag( file, {} )
549: else
550: html << '<!-- Not included: '+ file +' -->'
551: end
552: end
553: html.join "\n"
554: end
# File app/helpers/application_helper.rb, line 518
518: def theme_opt_menu_search
519: opt = theme_option( :menu_search )
520: if opt == 'none'
521: ""
522: elsif opt == 'simple_search'
523: s = _('Search...')
524: "<form action=\"#{url_for(:controller => 'search', :action => 'index')}\" id=\"simple-search\" class=\"focus-out\""+
525: ' help="'+_('This is a search box. Click, write your query, and press enter to find')+'"'+
526: ' title="'+_('Click, write and press enter to find')+'">'+
527: '<input name="query" value="'+s+'"'+
528: ' onfocus="if(this.value==\''+s+'\'){this.value=\'\'} this.form.className=\'focus-in\'"'+
529: ' onblur="if(/^\s*$/.test(this.value)){this.value=\''+s+'\'} this.form.className=\'focus-out\'">'+
530: '</form>'
531: else #opt == 'lightbox_link' is default
532: lightbox_link_to '<span class="icon-menu-search"></span>'+ _('Search'), {
533: :controller => 'search',
534: :action => 'popup',
535: :category_path => (@category ? @category.explode_path : []) },
536: :id => 'open_search'
537: end
538: end
# File app/helpers/application_helper.rb, line 507
507: def theme_option(opt = nil)
508: conf = RAILS_ROOT.to_s() +
509: '/public' + theme_path +
510: '/theme.yml'
511: if File.exists?(conf)
512: opt ? YAML.load_file(conf)[opt.to_s()] : YAML.load_file(conf)
513: else
514: nil
515: end
516: end
# File app/helpers/application_helper.rb, line 348
348: def theme_owner
349: Theme.find(current_theme).owner.identifier
350: end
# File app/helpers/application_helper.rb, line 319
319: def theme_path
320: if session[:theme]
321: '/user_themes/' + current_theme
322: else
323: '/designs/themes/' + current_theme
324: end
325: end
# File app/helpers/application_helper.rb, line 194
194: def toggle_panel(hide_label, show_label, id)
195: hide_button_id = id + "-hide"
196: show_button_id = id + "-show"
197:
198: result = ""
199: result << button_to_function('open', show_label, show(id) + show(hide_button_id) + hide(show_button_id), :id => show_button_id, :class => 'show-button with-text', :style => 'display: none;' )
200:
201: result < " "
202: result << button_to_function('close', hide_label, hide(id) + hide(hide_button_id) + show(show_button_id), :id => hide_button_id, :class => 'hide-button with-text')
203:
204: result
205: end
# File app/helpers/application_helper.rb, line 581
581: def txt2html(txt)
582: txt.
583: gsub( /\n\s*\n/, ' <p/> ' ).
584: gsub( /\n/, ' <br/> ' ).
585: gsub( /(^|\s)(www\.[^\s])/, '\1http://\2' ).
586: gsub( /(https?:\/\/([^\s]+))/,
587: '<a href="\1" target="_blank" rel="nofolow" onclick="return confirm(\'' +
588: escape_javascript( _('Are you sure you want to visit this web site?') ) +
589: '\n\n\'+this.href)">\2</a>' )
590: end