| Class | ApplicationController |
| In: |
app/controllers/application_controller.rb
|
| Parent: | ActionController::Base |
Filters added to this controller will be run for all controllers in the application. Likewise, all the methods added will be available for all controllers.
| edit_user_url | -> | original_edit_user_url |
| user_url | -> | original_user_url |
| current_context | [RW] | Attributes |
# File app/controllers/application_controller.rb, line 250
250: def self.add_contextualized_url_method(method_id)
251: hidden_method_id = method_id.to_s.sub(/logi_/,'_hidden_')
252: eval " def \#{method_id}(logi, args = {})\n if current_context.link?\n \#{hidden_method_id}({:ll_link_string => current_context.link.to_s(\n :direction => :received, :spec_step => :logi)}.merge(args))\n else\n \#{hidden_method_id}({:ll_link_string => logi.link.to_s(\n :direction => :received, :spec_step => :logi)}.merge(args))\n end\n end\n"
253: end
Logi urls.
Specified here because REST does not take the logi-links as context correctly…
# File app/controllers/application_controller.rb, line 125
125: def self.add_logi_url(action, logi_s_p)
126: action_suffixed = (action ? action + '_' : '')
127: ["url","path"].each do |url_path|
128: if ["edit","insert","remove"].include?(action) or
129: ((logi_s_p == "logi" or logi_s_p == "formatted_logi") and
130: action == nil) # logi-show url/path
131: link_options = ":direction => :received, :spec_step => :logi"
132: optional = ""
133: else
134: link_options = ":direction => :requested, :step => -1, :force => true"
135: optional = " = {}"
136: end
137: str = " def \#{action_suffixed}\#{logi_s_p}_\#{url_path}(options\#{optional},\n extra_options = {})\n case options\n when String\n options = extra_options.merge(:ll_link_string => options)\n when Link\n options = extra_options.merge(\n :ll_link_string => options.to_s(\#{link_options}))\n when Logi\n options = extra_options.merge(\n :ll_link_string => options.link.to_s(\#{link_options}))\n else\n options = extra_options.merge(options)\n end\n if current_context.link?\n options[:ll_link_string] ||= current_context.link.to_s(\n \#{link_options})\n end\n"
138: if action == "new"
139: str += " if options[:ll_link_string]\n return \#{action_suffixed}_hidden_\#{logi_s_p}_\#{url_path}(options)\n else\n return \#{action_suffixed}_hidden_untagged_\#{logi_s_p}_\#{url_path}(options)\n end\n end\n"
140: else
141: str += " return \#{action_suffixed}_hidden_\#{logi_s_p}_\#{url_path}(options)\n end\n"
142: end
143: eval str
144: end
145: end
Keeps track of the last visited urls, like breadcrumbs, for back-buttons…
# File app/controllers/application_controller.rb, line 64
64: def append_return_url
65: session[:return_urls] ||= []
66: if params[:action] == "create" or params[:action] == "update" or
67: params[:action] == "destroy" or
68: (params[:controller] == "user" and params[:action] == "new")
69: # do nothing
70: else
71: if session[:return_urls].last == request.url
72: # Takes double out in redirect, single in back
73: session[:return_urls].pop
74: else
75: referrer = request.env["HTTP_REFERER"]
76: if session[:return_urls].last != referrer and
77: referrer =~ /\/\/[^\/]*#{request.host}(\/.*|)/ and
78: params[:controller] != "user_sessions"
79: session[:return_urls].push(referrer)
80: end
81: end
82: end
83: if session[:return_urls].size > 15
84: session[:return_urls].shift
85: end
86: session[:return_url] = session[:return_urls].last
87: end
# File app/controllers/application_controller.rb, line 272
272: def browse_ll_url(link, link_options = {}, options = {})
273: browse_logis_url(options.merge(:ll_link_string => link.to_s(
274: {:step => -1}.merge(link_options))))
275: end
Returns the current prefs-profile.
Must be called as a before_filter so the PrefsProfile.current_prefs get set.
# File app/controllers/application_controller.rb, line 355
355: def current_prefs
356: return @current_prefs if defined?(@current_prefs)
357: self.current_prefs = self.current_user.default_prefs_profile
358: # redirect for them or override them
359: if session[:just_selected_prefs_profile]
360: session[:just_selected_prefs_profile] = false
361: session[:current_peer_group_id] = nil
362: else
363: # PeerGroup from quick-settings
364: if session[:current_peer_group_id]
365: self.current_prefs.peer_group = PeerGroup.find(
366: session[:current_peer_group_id])
367: end
368: end
369: return @current_prefs
370: end
Sets the current_prefs, and also the class-methods.
Can cause troubles when rails supports threads
# File app/controllers/application_controller.rb, line 116
116: def current_prefs=(prefs_profile)
117: @current_prefs = PrefsProfile.current_prefs = prefs_profile
118: end
Returns the logged in user.
# File app/controllers/application_controller.rb, line 341
341: def current_user
342: return @current_user if defined?(@current_user)
343: if self.current_user_session
344: @current_user = current_user_session.user
345: else
346: @current_user = User.anonymous
347: end
348: end
Returns the current user session, see authlogic.
# File app/controllers/application_controller.rb, line 334
334: def current_user_session
335: return @current_user_session if defined?(@current_user_session)
336: @current_user_session = UserSession.find
337: end
# File app/controllers/application_controller.rb, line 309
309: def edit_user_url(user, options = {})
310: original_edit_user_url({:id => user.home_page_tag_string}, options)
311: end
# File app/controllers/application_controller.rb, line 223
223: def get_annotation_ids(ids)
224: if ids
225: return Annotation.find(:all,
226: :conditions => ["id IN (?)", ids.split(',')])
227: else
228: return []
229: end
230: end
# File app/controllers/application_controller.rb, line 214
214: def get_external_links_for_ids(ids)
215: if params['external_link_ids']
216: return ExternalLink.find(:all,
217: :conditions => ["id IN (?)", params['external_link_ids'].split(',')])
218: else
219: return []
220: end
221: end
# File app/controllers/application_controller.rb, line 195
195: def get_links_for_ids(ids)
196: links_hash_arr = []
197: if ids
198: links = Link.find(:all, :conditions => ["id IN (?)", ids.split(',')])
199: links.each do |link|
200: hash = {:link => link}
201: logis = link.link_matches.collect {|l_m| l_m.logi}.order_by_rating_for(
202: self.current_prefs.peer_group)
203: hash[:logi_links] = logis.collect {|logi|
204: l = Link.new(:requested_tags => link.requested_tags.dup,
205: :to_logi => logi)
206: l.resolve
207: }
208: links_hash_arr.push(hash)
209: end
210: end
211: return links_hash_arr
212: end
# File app/controllers/application_controller.rb, line 57
57: def last_non_login_url
58: return (session[:last_non_login_url] || root_url)
59: end
LL-urls, urls to logi-pages…
# File app/controllers/application_controller.rb, line 268
268: def ll_url(link, link_options = {}, options = {})
269: logi_url(options.merge(:ll_link_string => link.to_s(link_options)))
270: end
# File app/controllers/application_controller.rb, line 277
277: def logi_ll_url(logi, options = {})
278: logi_url(logi.link.to_s, options)
279: end
# File app/controllers/application_controller.rb, line 281
281: def logi_version_ll_url(logi_version, options = {})
282: link = logi_version.logi.link.new_record_dup
283: link.to_logi_version_nr = logi_version.nr
284: logi_url(link.to_s, options)
285: end
Sub-urls of logi: links, tags, votes and ratings
Happlily keeping DRY :)
# File app/controllers/application_controller.rb, line 241
241: def method_missing(method_id, *arguments)
242: if method_id.to_s =~ /^.*logi_.*_(url|path)$/
243: ApplicationController.add_contextualized_url_method(method_id)
244: self.send(method_id, *arguments)
245: else
246: super(method_id, *arguments)
247: end
248: end
# File app/controllers/application_controller.rb, line 326
326: def peer_group_ll_url(peer_group, options = {})
327: logi_url(peer_group.home_page_link.to_s, options)
328: end
# File app/controllers/application_controller.rb, line 287
287: def perma_logi_ll_url(logi, options = {})
288: return self.permatize(self.logi_ll_url(logi, options))
289: end
# File app/controllers/application_controller.rb, line 291
291: def perma_logi_version_ll_url(logi, options = {})
292: return self.permatize(self.logi_version_ll_url(logi, options))
293: end
# File app/controllers/application_controller.rb, line 295
295: def permatize(url)
296: if GlobalConfig.use_language_subdomains?
297: return url.sub("://" + GlobalConfig.site_subdomain, "://" +
298: GlobalConfig.site_subdomain + ".manta")
299: else
300: return url.sub("://", "://" + ".manta")
301: end
302: end
# File app/controllers/application_controller.rb, line 188
188: def popover_listing
189: @links_hash_arr = get_links_for_ids(params['link_ids'])
190: @external_links = get_external_links_for_ids(params['external_link_ids'])
191: @annotations = get_annotation_ids(params['annotation_ids'])
192: render :partial => 'popover_listing'
193: end
# File app/controllers/application_controller.rb, line 232
232: def rating_and_new_vote
233: @rating = @logi.rating_for(self.current_prefs.peer_group)
234: @vote = @rating.vote_for(self.current_user, request.env['REMOTE_ADDR'])
235: end
Redirects to the last visited url
# File app/controllers/application_controller.rb, line 98
98: def redirect_to_return_url_or_default(default_url = nil)
99: url = return_url_or_default(default_url)
100: # Adds an extra copy of the last url which will be popped again
101: # in append_return_url
102: session[:return_urls].push(url)
103: redirect_to url
104: end
Returns the last visited url
# File app/controllers/application_controller.rb, line 108
108: def return_url_or_default(default_url = nil)
109: return (session[:return_url] || default_url || root_url)
110: end
Disregards the last return url
# File app/controllers/application_controller.rb, line 91
91: def shift_return_url
92: session[:return_urls].pop
93: session[:return_url] = session[:return_urls].last
94: end
# File app/controllers/application_controller.rb, line 304
304: def tag_ll_url(tag)
305: browse_logis_url(tag.to_s)
306: end
Methods
# File app/controllers/application_controller.rb, line 50
50: def transfer_errors(source, target, options = {})
51: source.errors.each do |attribute, msg|
52: attribute = options[:attribute] if options[:attribute]
53: target.errors.add(attribute, msg)
54: end
55: end
# File app/controllers/application_controller.rb, line 318
318: def user_group_ll_url(user_group, options = {})
319: if user_group.personal?
320: self.user_url(user_group.personal_user, options)
321: else
322: logi_url(user_group.home_page_link.to_s, options)
323: end
324: end
# File app/controllers/application_controller.rb, line 314
314: def user_url(user, options = {})
315: original_user_url({:id => user.home_page_tag_string}, options)
316: end
# File app/controllers/application_controller.rb, line 656
656: def auto_page_name
657: return t 'c.' + params[:controller] + '.page_name.' + params[:action],
658: :default => ''
659: end
Shared insert / remove logic
# File app/controllers/application_controller.rb, line 441
441: def create_logi(logi_hash, options = {})
442: if logi_hash[:title].nil? or logi_hash[:title].empty?
443: logi_hash[:body] =~ /^<p>(.+?(\.|\?|<\/p>)+)/
444: match = $~
445: if match.nil? or match[1].size > 150
446: logi_hash[:title] = LogiVersion.strip_html(logi_hash[:body][0..50]) + '...'
447: else
448: logi_hash[:title] = LogiVersion.strip_html(match[1].gsub('.',''))
449: end
450: end
451: logi = Logi.new_with(:title => logi_hash[:title],
452: :body => logi_hash[:body],
453: :creator => current_user,
454: :user_group => current_prefs.user_group)
455: logi_version = logi.current_logi_version
456:
457: if options[:untagged] == :if_unset
458: if logi_hash[:link_string].nil? or logi_hash[:link_string].empty?
459: logi.untag
460: else
461: self.tag_with_link(logi, logi_hash[:link_string])
462: end
463: elsif options[:untagged]
464: logi.untag
465: else
466: if logi_hash[:link_string].nil? or logi_hash[:link_string].empty?
467: logi_hash[:link_string] = current_context.link.to_s
468: end
469: self.tag_with_link(logi, logi_hash[:link_string])
470: end
471:
472: return logi, logi_version
473: end
Gets the current logi and logi version from the context.
# File app/controllers/application_controller.rb, line 717
717: def find_logi_and_logi_version
718: @logi_version = self.current_context.link.volatile_to_logi_version
719: if @logi_version.nil?
720: redirect_to new_logi_url(
721: :ll_link_string => self.current_context.link.to_s,
722: :force => params[:force])
723: return false
724: end
725: @logi = @logi_version.logi
726: end
# File app/controllers/application_controller.rb, line 486
486: def insert_links(logi, logi_version, body_with_links, options = {})
487: new_link_position_range_list = self.weave_out_links(body_with_links,
488: logi_version, logi)
489: if new_link_position_range_list.empty?
490: logi.errors.add(:text, t('c.application.error_select_text'))
491: end
492:
493: old_link_list = logi.from_links.dup
494: # get the old link-positions translated to the new view
495: old_link_p_r_list = logi.from_links.position_range_list.
496: translate_to_view(logi_version.position_ranges)
497: # '-' reckons with p_r-attributes, so overlap of the same links is removed here
498: added_link_p_r_list = new_link_position_range_list - old_link_p_r_list
499: # gets links, same objects also still in the added_link_p_r_list
500: added_link_list = (added_link_p_r_list.collect {|p_r| p_r.link}.uniq)
501:
502: added_link_list.each {|added_link|
503: added_link.user = self.current_user
504: added_link.from_logi = logi
505: added_link.resolve
506: added_link.quick_validate
507: if options[:require_to_logi] and added_link.to_logi.nil?
508: added_link.errors.add(:link, t('c.application.error_specific_logi'))
509: end
510: transfer_errors(added_link, logi, :attribute => :link)
511: }
512:
513: absolute_added_link_p_r_list =
514: added_link_p_r_list.translate_from_view(logi_version.position_ranges)
515:
516: if logi.errors.empty?
517: logi.add_links_to_position_ranges(absolute_added_link_p_r_list)
518: end
519: end
Parses a string containing a cluster of links (for example from text that was just edited). Clusters of Links consist of a link-text and one or more links.
Link-cluster-syntax:
<a title="tagset/with/tags/separated/by/slashes">link text with spaces</a> <a title="tagset;second/tagset">link text with spaces</a>
# File app/controllers/application_controller.rb, line 611
611: def link_cluster_from_s(inserted_text)
612: link_cluster_array = []
613: inserted_text.split(';').each {|link_string|
614: l = Link.new_from_s(link_string)
615: link_cluster_array.push(l)
616: }
617: return link_cluster_array
618: end
Body-methods
# File app/controllers/application_controller.rb, line 757
757: def my_logilogi
758: render_body 'application/my_logilogi'
759: end
Url-bars with context.
# File app/controllers/application_controller.rb, line 767
767: def navigation_bar_edit
768: render_body 'logis/navigation_bar_edit'
769: end
# File app/controllers/application_controller.rb, line 771
771: def navigation_bar_insert
772: render_body 'logis/navigation_bar_insert'
773: end
# File app/controllers/application_controller.rb, line 707
707: def not_empty(string)
708: if string.nil? or string.empty?
709: return nil
710: else
711: return string
712: end
713: end
# File app/controllers/application_controller.rb, line 647
647: def page_id_and_class
648: @page_id = params[:controller] + '_' + params[:action]
649: @page_class = params[:controller]
650: end
# File app/controllers/application_controller.rb, line 652
652: def page_name
653: @page_name = self.auto_page_name
654: end
# File app/controllers/application_controller.rb, line 661
661: def redirect_if_wrong_subdomain
662: if GlobalConfig.use_language_subdomains?
663: subdomain = request.subdomains.first.to_s
664: if subdomain == 'www' or subdomain.empty?
665: # detect browser language
666: language = ::BrowserLanguageCode.detect(
667: request.env['HTTP_ACCEPT_LANGUAGE'])
668: if GlobalConfig.available_subdomains.include?(language)
669: subdomain = language
670: else
671: # set current site if it does not exist
672: subdomain = GlobalConfig.site_subdomain
673: end
674: # redirecting
675: domain = subdomain + '.' + GlobalConfig.domain.downcase
676: host = (request.port == request.standard_port ? "#{domain}" :
677: "#{domain}:#{request.port}")
678: redirect_to url_for(params.merge(:host => host))
679: return false
680: end
681: end
682: return true
683: end
Rewrites comma-, or space-separated tags to the logi-link format.
"Michel Foucault, History, Economics" and "Michel Foucault History Economics" =>
"History/Economics/Michel_Foucault"
# File app/controllers/application_controller.rb, line 387
387: def reformat_to_link_string(link_or_tags_string)
388: # initial cleanup of link_or_tags_string
389: link_or_tags_string.squeeze!(' ')
390: link_or_tags_string.strip!
391: # check whether the query contains spaces
392: if link_or_tags_string.index(' ') and link_or_tags_string.index(',').nil?
393: # all tags are indentified with spaces around them
394: # this eliminates the problem when a tag is a part of a larger tag
395: link_or_tags_string = ' ' + link_or_tags_string + ' '
396:
397: # sort the tags, based on the number of spaces the tag contains
398: # the tag with the most spaces will be the first
399: sorted_tags = UsedTag.find(:all).collect {|u_t| u_t.tag}.sort {|a,b|
400: b.to_s(:for => :show).scan(' ').length <=> a.to_s(:for => :show).scan(' ').length}
401:
402: sorted_tags.each do |tag|
403: tag_s = tag.to_s(:for => :show)
404: # only tags that include spaces need to be processed
405: if tag_s.index(' ')
406: index = link_or_tags_string.downcase.index(' ' + tag_s.downcase + ' ')
407: if not index.nil?
408: # replace the spaces in a tag by underscores to identify it as a single tag
409: link_or_tags_string[index+1,tag_s.length] = tag.to_s
410: end
411: end
412: end
413: # remove last and first whitespace
414: link_or_tags_string.strip!
415: link_or_tags_string.gsub!(' ',',')
416: # all spaces are correctly replaced by comma's
417: end
418: # check whether the query contains comma's
419: if link_or_tags_string.index(',')
420: tag_strings = link_or_tags_string.split(',')
421:
422: # cleanup, and remove whitespaces
423: tag_strings.collect do |tag_s|
424: tag_s.strip!
425: tag_s.gsub!(' ','_')
426: end
427: if tag_strings.size > 1
428: link_or_tags_string = tag_strings[1..-1].join('/') + '/'
429: else
430: link_or_tags_string = ''
431: end
432: link_or_tags_string += tag_strings[0]
433: # first tag last and all comma's replaced by slashes
434: end
435: # nothing (more) to be done...
436: return link_or_tags_string
437: end
# File app/controllers/application_controller.rb, line 628
628: def render_ajax_body(options)
629: respond_to do |format|
630: format.html do
631: return render_body(options)
632: end
633: format.js do
634: render :text => render_to_string(options)
635: end
636: end
637: end
Sub-Methods
# File app/controllers/application_controller.rb, line 376
376: def render_to_confirm_delete(options)
377: render :partial => 'application/confirm_delete', :layout => 'blank',
378: :locals => options
379: end
Makes sure an user is logged in.
Note that it does not check whether the logged in user also is the user specified in the url.
# File app/controllers/application_controller.rb, line 735
735: def require_login
736: if self.current_user.anonymous?
737: flash[:warning] = t 'c.application.must_be_logged_in'
738: redirect_to new_user_session_url
739: return false
740: end
741: end
Checks that the current user is anonymous.
# File app/controllers/application_controller.rb, line 745
745: def require_no_login
746: if !self.current_user.anonymous?
747: flash[:warning] = t 'c.application.must_not_be_logged_in'
748: redirect_to_return_url_or_default(user_url(current_user))
749: return false
750: end
751: end
Resolves the current context.
# File app/controllers/application_controller.rb, line 687
687: def resolve_context
688: context_link_string = not_empty(params[:new_ll_link_string])
689: if params[:top_ll_link_string]
690: context_link_string ||= not_empty(
691: params[:top_ll_link_string].gsub(' ','/'))
692: end
693: context_link_string ||= not_empty(params[:ll_link_string])
694: if params[:controller] == "logis" and
695: (params[:action] == "new" or params[:action] == "create" or
696: params[:action] == "edit" or params[:action] == "update")
697: force = true
698: replace_tag = Tag.sandbox
699: else
700: force = params[:force]
701: replace_tag = nil
702: end
703: self.current_context = Context.new(
704: context_link_string, :force => force, :replace_tag => replace_tag)
705: end
# File app/controllers/application_controller.rb, line 620
620: def select_body(selected)
621: # checks that valid method & not random code
622: if !selected.nil? and self.respond_to?(selected)
623: @body_selected = true # can be used to check that past filters
624: self.send(selected.to_sym)
625: end
626: end
Filter-methods
# File app/controllers/application_controller.rb, line 643
643: def set_locale
644: I18n.locale = GlobalConfig.language_code
645: end
Subfunctions for that
# File app/controllers/application_controller.rb, line 523
523: def tag_with_link(logi, link_string)
524: if link_string.empty?
525: if logi.comments_on_logis.empty?
526: # TODO fix better
527: logi.errors.add(:tags, t('c.application.error_empty'))
528: else
529: logi.untag
530: end
531: return
532: end
533:
534: link = Link.new_from_s(link_string)
535: transfer_errors(link, logi, :attribute => :tags)
536:
537: if logi.errors.empty?
538: logi.tag_with_link(link)
539: end
540: end
# File app/controllers/application_controller.rb, line 761
761: def tiny_mce_include_tag
762: render_body 'application/tiny_mce_include_tag'
763: end
# File app/controllers/application_controller.rb, line 475
475: def update_logi(logi, logi_hash)
476: logi_version = LogiVersion.new(:logi => logi,
477: :title => logi_hash[:title],
478: :body => logi_hash[:body],
479: :editor => self.current_user)
480:
481: self.tag_with_link(logi, logi_hash[:link_string])
482:
483: return logi, logi_version
484: end
Gets things from the given text.
# File app/controllers/application_controller.rb, line 565
565: def weave_out(body_with_inserts, logi_version, err_obj)
566: text_with_inserts = LogiVersion.clean(logi_version.raw_title + body_with_inserts)
567: global_last_insert_tag_end_p = 0
568: global_p = 0
569: global_clean_p = 0
570: link_pos_ranges = PositionRange::List.new
571: while global_p = text_with_inserts.index(Const::Logi::RECOGNIZE_INSERT_TAG_RE,global_p)
572: # the preceding text doesn't contain links
573: global_clean_p += global_p - global_last_insert_tag_end_p
574:
575: match = $~
576: insert_tag = match[0]
577: if insert_tag !~ Const::Logi::CHECK_INSERT_TAG_RE
578: raise ParsingError.new(insert_tag), t('c.application.error_tag_string')
579: end
580: insert_tag =~ Const::Logi::DISSECT_INSERT_TAG_RE
581: match = $~
582: inserted_string = match[1]
583: body_text_inserted_to = match[2]
584: if body_text_inserted_to =~ Const::LogiVersion::HTML_TAG
585: err_obj.errors.add(:selection, t('c.application.error_overlap_html'))
586: end
587:
588: begin_pos = global_clean_p
589: global_clean_p += body_text_inserted_to.size
590: end_pos = global_clean_p
591:
592: pos_range = PositionRange.new(begin_pos, end_pos)
593:
594: yield pos_range, inserted_string
595:
596: global_last_insert_tag_end_p = global_p += insert_tag.size
597: end
598: return link_pos_ranges
599: end
Gets the link-clusters + positions from the given text.
# File app/controllers/application_controller.rb, line 544
544: def weave_out_links(body_with_links, logi_version, logi)
545: link_pos_ranges = PositionRange::List.new
546: self.weave_out(body_with_links, logi_version, logi) do |pos_range, inserted_string|
547: cluster_array = self.link_cluster_from_s(inserted_string)
548: cluster_array.each do |link|
549: transfer_errors(link, logi, :attribute => :link)
550: end
551: if logi.errors.empty?
552: cluster_array.each {|link|
553: # duplicate pos_range and set the link for all links in the cluster
554: link_pos_range = pos_range.dup
555: link_pos_range.link = link
556: link_pos_ranges.push(link_pos_range)
557: }
558: end
559: end
560: return link_pos_ranges
561: end