Renamed helpers to correspond to renamed Controller classes.
[merb_radiant.git] / app / controllers / admin / .svn / text-base / abstract_model_controller.rb.svn-base
blob12e459551be48cb9a380540fcd710a466c4a14f1
1 class Admin::AbstractModelController < ApplicationController
2   attr_accessor :cache
3   
4   def self.model_class(model_class = nil)
5     @model_class = model_class.to_s.camelize.constantize unless model_class.nil?
6     @model_class
7   end
8   
9   def initialize
10     super
11     @cache = ResponseCache.instance
12   end
14   def index
15     self.models = model_class.find(:all)
16   end
18   def new
19     self.model = model_class.new
20     render :template => "admin/#{ model_symbol }/edit" if handle_new_or_edit_post
21   end
22   
23   def edit
24     self.model = model_class.find_by_id(params[:id])
25     handle_new_or_edit_post
26   end
27   
28   def remove
29     self.model = model_class.find(params[:id])
30     if request.post?
31       model.destroy
32       announce_removed
33       redirect_to model_index_url
34     end
35   end
36   
37   protected
38   
39     def model_class
40       self.class.model_class
41     end
42   
43     def model
44       instance_variable_get("@#{model_symbol}")
45     end
46     def model=(object)
47       instance_variable_set("@#{model_symbol}", object)
48     end
49     
50     def models
51       instance_variable_get("@#{plural_model_symbol}")
52     end
53     def models=(objects)
54       instance_variable_set("@#{plural_model_symbol}", objects)
55     end
56     
57     def model_name
58       model_class.name
59     end
60     def plural_model_name
61       model_name.pluralize
62     end
63     
64     def model_symbol
65       model_name.underscore.intern
66     end
67     def plural_model_symbol
68       model_name.pluralize.underscore.intern
69     end
70     
71     def humanized_model_name
72       model_name.underscore.humanize
73     end
74     
75     def model_index_url(params = {})
76       send("#{ model_symbol }_index_url", params)
77     end
78     
79     def model_edit_url(params = {})
80       send("#{ model_symbol }_edit_url", params)
81     end
82     
83     def continue_url(options)
84       options[:redirect_to] || (params[:continue] ? model_edit_url(:id => model.id) : model_index_url)
85     end
87     def save
88       model.save
89     end
90     
91     def announce_saved(message = nil)
92       flash[:notice] = message || "#{humanized_model_name} saved below."
93     end
94     
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."
97     end
98     
99     def announce_removed
100       flash[:notice] = "#{humanized_model_name} has been deleted."
101     end
102     
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."
105     end
106     
107     def clear_model_cache
108       cache.clear
109     end
110     
111     def handle_new_or_edit_post(options = {})
112       options.symbolize_keys
113       if request.post?
114         model.attributes = params[model_symbol]
115         begin
116           if save
117             clear_model_cache
118             announce_saved(options[:saved_message])
119             redirect_to continue_url(options)
120             return false
121           else
122             announce_validation_errors
123           end
124         rescue ActiveRecord::StaleObjectError
125           announce_update_conflict
126         end
127       end
128       true
129     end