| Class | ApplicationController |
| In: |
app/controllers/application.rb
|
| Parent: | ActionController::Base |
Filters added to this controller are be run for all controllers in the application. Likewise, all the methods added are be available to all controllers.
A hash is returned which can be used for allowing/restricting access (using with_scope) to give users access to the right database entries. The core of the function are the :conditions which become part of the database query and determine which rows will be returned.
# File app/controllers/application.rb, line 15
15: def accessible table_name = self.controller_name
16: if current_user && (current_user.id == 1)
17: # no restrictions for the admin user
18: return { }
19: else
20: return { :find => { :conditions => ["? ~* #{table_name}.restriction", '\'' + session[:user_string].to_s + '\''] } }
21: end
22: end
This is replaced by SearchController#results, any references to this action should be phased out.
This method renders a page which helps the user to differentiate between a number of entities with the same name. It can be used to provide a very basic search engine.
# File app/controllers/application.rb, line 28
28: def disambiguate
29: layout = params[:layout].nil? ? "application" : params[:layout]
30: layout = layout == "false" ? false : layout
31: @name = params[:name].downcase
32: klass = self.controller_name.singularize.titlecase.constantize # figure out the class from the controller name
33: @items = klass.find_like_name(@name)
34: unless @items.size > 0
35: flash[:warning] = "No " + self.controller_name + " found with the name " + @name.titlecase
36: redirect_to :action => 'list'
37: else
38: flash[:notice] = @items.size.to_s + " " + self.controller_name + " found with the name " + @name.titlecase
39: result = '<% @heading = heading; @items.each do |item| %>
40: <% link_text = item.names.find(:first, :conditions => "LOWER(names.name) LIKE \'%' + @name.downcase + '%\'").name;
41: link_text += " (" + item.primary_names + ")" if item.primary_names.downcase.scan(/' + @name.downcase + '/).empty? %>
42: <p><%= link_for item, {:name => link_text} %> <%= "(" + item.disambiguation + ")" rescue nil %> <%= "<br />" + truncate(item.description) rescue nil %></p>
43: <% end %>'
44: render :inline => result,
45: :locals => {:heading => self.controller_name.titlecase + ' with the name ' + @name.titlecase + ': ' }, :layout => layout
46: end
47: end
This method will return a list of items without any layout. It is useful for ajax methods which take a name as input but need to identify a unique entity.
# File app/controllers/application.rb, line 51
51: def disambiguate_ajax
52: @name = params[:name].downcase
53: class_name = self.controller_name.singularize
54: klass = class_name.titlecase.constantize # turn class_name into a class
55: @items = klass.find_by_name(@name)
56: object_class = params[:options][:controller].singularize.titlecase.constantize # turn string into a class
57: @object = object_class.find(params[:options][:id])
58: unless @items.size > 0
59: flash.now[:warning] = "No " + self.controller_name + " found with the name " + @name.titlecase
60: render :inline => '<%= flash_all %>'
61: else
62: flash.now[:warning] = "More than one " + self.controller_name.singularize + " found with the name " + @name.titlecase
63: result = '<%= flash_all %>
64: <% @heading = heading; @items.each do |item| %><div>
65: <% disambiguation = " (" + item.disambiguation + ")" rescue nil
66: default_form_options = { :name => params[:subject].downcase, :title => item.primary_names + disambiguation.to_s, :submit => "Add this ' + class_name + '", :hidden => {:value => item.id.to_s }}
67:
68: form_options = default_form_options
69: # perform a manual merge because params[:form_options] has strings as keys and not symbols (so merge will not work normally).
70: for option in params[:form_options].keys
71: form_options[option.to_sym] = params[:form_options][option]
72: end
73: %>
74:
75: <%= remote_form_add @object, item, form_options %>
76: </div><% end %>'
77: # <%= form_tag :controller => "' + params[:options][:controller] + '", :action => "' + params[:options][:action] + '", :id => "' + params[:options][:id] + '", :' + class_name + '_id => item %>
78: # <% disambiguation = " (" + item.disambiguation + ")" rescue nil %> <%= submit_tag item.primary_names + disambiguation.to_s %><%= end_form_tag %>
79: render :inline => result,
80: :locals => {:heading => @items.size.to_s + " " + self.controller_name.titlecase + ' with the name ' + @name.titlecase + ': ' }, :layout => false
81: end
82: end
# File app/controllers/application.rb, line 84
84: def paginate_collection(collection, options = {})
85: default_options = {:per_page => 20, :page => 1}
86: options = default_options.merge options
87:
88: pages = Paginator.new self, collection.size, options[:per_page], options[:page]
89: first = pages.current.offset
90: last = [first + options[:per_page], collection.size].min
91: slice = collection[first...last]
92: return [pages, slice]
93: end