1 class Admin::AbstractModels < Application
4 def self.model_class(model_class = nil)
5 @model_class = model_class.to_s.camelize.constantize unless model_class.nil?
11 @cache = ResponseCache.instance
15 self.models = model_class.find(:all)
19 self.model = model_class.new
20 render :template => "admin/#{ model_symbol }/edit" if handle_new_or_edit_post
24 self.model = model_class.find_by_id(params[:id])
25 handle_new_or_edit_post
29 self.model = model_class.find(params[:id])
33 redirect_to model_index_url
40 self.class.model_class
44 instance_variable_get("@#{model_symbol}")
47 instance_variable_set("@#{model_symbol}", object)
51 instance_variable_get("@#{plural_model_symbol}")
54 instance_variable_set("@#{plural_model_symbol}", objects)
65 model_name.underscore.intern
67 def plural_model_symbol
68 model_name.pluralize.underscore.intern
71 def humanized_model_name
72 model_name.underscore.humanize
75 def model_index_url(params = {})
76 send("#{ model_symbol }_index_url", params)
79 def model_edit_url(params = {})
80 send("#{ model_symbol }_edit_url", params)
83 def continue_url(options)
84 options[:redirect_to] || (params[:continue] ? model_edit_url(:id => model.id) : model_index_url)
91 def announce_saved(message = nil)
92 flash[:notice] = message || "#{humanized_model_name} saved below."
95 def announce_validation_errors
96 flash[:error] = "Validation errors occurred while processing this form. Please take a moment to review the form and correct any input errors before continuing."
100 flash[:notice] = "#{humanized_model_name} has been deleted."
103 def announce_update_conflict
104 flash[:error] = "#{humanized_model_name} has been modified since it was last loaded. Changes cannot be saved without potentially losing data."
107 def clear_model_cache
111 def handle_new_or_edit_post(options = {})
112 options.symbolize_keys
114 model.attributes = params[model_symbol]
118 announce_saved(options[:saved_message])
119 redirect_to continue_url(options)
122 announce_validation_errors
124 rescue ActiveRecord::StaleObjectError
125 announce_update_conflict