Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 7679 Bytes
1 | class Admin::ResourceController < ApplicationController |
2 | extend Radiant::ResourceResponses |
3 | |
4 | helper_method :model, :current_object, :models, :current_objects, :model_symbol, :plural_model_symbol, :model_class, :model_name, :plural_model_name |
5 | before_filter :populate_format |
6 | before_filter :load_models, :only => :index |
7 | before_filter :load_model, :only => [:new, :create, :edit, :update, :remove, :destroy] |
8 | after_filter :clear_model_cache, :only => [:create, :update, :destroy] |
9 | |
10 | cattr_reader :paginated |
11 | cattr_accessor :default_per_page, :will_paginate_options |
12 | |
13 | responses do |r| |
14 | # Equivalent respond_to block for :plural responses: |
15 | # respond_to do |wants| |
16 | # wants.xml { render :xml => models } |
17 | # wants.json { render :json => models } |
18 | # wants.any |
19 | # end |
20 | r.plural.publish(:xml, :json) { render format_symbol => models } |
21 | |
22 | r.singular.publish(:xml, :json) { render format_symbol => model } |
23 | r.singular.default { redirect_to edit_model_path if action_name == "show" } |
24 | |
25 | r.not_found.publish(:xml, :json) { head :not_found } |
26 | r.not_found.default { announce_not_found; redirect_to :action => "index" } |
27 | |
28 | r.invalid.publish(:xml, :json) { render format_symbol => model.errors, :status => :unprocessable_entity } |
29 | r.invalid.default { announce_validation_errors; render :action => template_name } |
30 | |
31 | r.stale.publish(:xml, :json) { head :conflict } |
32 | r.stale.default { announce_update_conflict; render :action => template_name } |
33 | |
34 | r.create.publish(:xml, :json) { render format_symbol => model, :status => :created, :location => url_for(:format => format_symbol, :id => model) } |
35 | r.create.default { redirect_to continue_url(params) } |
36 | |
37 | r.update.publish(:xml, :json) { head :ok } |
38 | r.update.default { redirect_to continue_url(params) } |
39 | |
40 | r.destroy.publish(:xml, :json) { head :deleted } |
41 | r.destroy.default { redirect_to continue_url(params) } |
42 | end |
43 | |
44 | def index |
45 | response_for :plural |
46 | end |
47 | |
48 | [:show, :new, :edit, :remove].each do |action| |
49 | class_eval %{ |
50 | def #{action} # def show |
51 | response_for :singular # response_for :singular |
52 | end # end |
53 | }, __FILE__, __LINE__ |
54 | end |
55 | |
56 | [:create, :update].each do |action| |
57 | class_eval %{ |
58 | def #{action} # def create |
59 | model.update_attributes!(params[model_symbol]) # model.update_attributes!(params[model_symbol]) |
60 | response_for :#{action} # response_for :create |
61 | end # end |
62 | }, __FILE__, __LINE__ |
63 | end |
64 | |
65 | def destroy |
66 | model.destroy |
67 | response_for :destroy |
68 | end |
69 | |
70 | def self.model_class(model_class = nil) |
71 | @model_class ||= (model_class || self.controller_name).to_s.singularize.camelize.constantize |
72 | end |
73 | |
74 | # call paginate_models to declare that will_paginate should be used in the index view |
75 | # options specified here are accessible in the view by calling will_paginate_options |
76 | # eg. |
77 | # |
78 | # Class MyController < Admin::ResourceController |
79 | # paginate_models :per_page => 100 |
80 | |
81 | def self.paginate_models(options={}) |
82 | @@paginated = true |
83 | @@will_paginate_options = options.slice(:class, :previous_label, :next_label, :inner_window, :outer_window, :separator, :container).merge(:param_name => :p) |
84 | @@default_per_page = options[:per_page] |
85 | end |
86 | |
87 | # returns a hash of options that can be passed to will_paginate |
88 | # the @pagination_for@ helper method calls @will_paginate_options@ unless other options are supplied. |
89 | # |
90 | # pagination_for(@events) |
91 | |
92 | def will_paginate_options |
93 | self.class.will_paginate_options || {} |
94 | end |
95 | helper_method :will_paginate_options |
96 | |
97 | # a convenience method that returns true if paginate_models has been called on this controller class |
98 | # and can be used to make display decisions in controller and view |
99 | def paginated? |
100 | self.class.paginated == true |
101 | end |
102 | helper_method :paginated? |
103 | |
104 | # return a hash of page and per_page that can be used to build a will_paginate collection |
105 | # the per_page figure can be set in several ways: |
106 | # request parameter > declared by paginate_models > default set in config entry @admin.pagination.per_page@ > overall default of 50 |
107 | def pagination_parameters |
108 | { |
109 | :page => (params[:p] || 1).to_i, |
110 | :per_page => (params[:pp] || self.class.default_per_page || Radiant::Config['admin.pagination.per_page'] || 50).to_i |
111 | } |
112 | end |
113 | |
114 | protected |
115 | |
116 | def rescue_action(exception) |
117 | case exception |
118 | when ActiveRecord::RecordInvalid |
119 | response_for :invalid |
120 | when ActiveRecord::StaleObjectError |
121 | response_for :stale |
122 | when ActiveRecord::RecordNotFound |
123 | response_for :not_found |
124 | else |
125 | super |
126 | end |
127 | end |
128 | |
129 | def model_class |
130 | self.class.model_class |
131 | end |
132 | |
133 | def model |
134 | instance_variable_get("@#{model_symbol}") || load_model |
135 | end |
136 | alias :current_object :model |
137 | def model=(object) |
138 | instance_variable_set("@#{model_symbol}", object) |
139 | end |
140 | def load_model |
141 | self.model = if params[:id] |
142 | model_class.find(params[:id]) |
143 | else |
144 | model_class.new |
145 | end |
146 | end |
147 | |
148 | def models |
149 | instance_variable_get("@#{plural_model_symbol}") || load_models |
150 | end |
151 | alias :current_objects :models |
152 | def models=(objects) |
153 | instance_variable_set("@#{plural_model_symbol}", objects) |
154 | end |
155 | def load_models |
156 | self.models = paginated? ? model_class.paginate(pagination_parameters) : model_class.all |
157 | end |
158 | |
159 | def model_name |
160 | model_class.name |
161 | end |
162 | def plural_model_name |
163 | model_name.pluralize |
164 | end |
165 | alias :models_name :plural_model_name |
166 | |
167 | def model_symbol |
168 | model_name.underscore.intern |
169 | end |
170 | def plural_model_symbol |
171 | model_name.pluralize.underscore.intern |
172 | end |
173 | alias :models_symbol :plural_model_symbol |
174 | |
175 | def humanized_model_name |
176 | t(model_name.underscore.downcase) |
177 | end |
178 | |
179 | def continue_url(options) |
180 | options[:redirect_to] || (params[:continue] ? {:action => 'edit', :id => model.id} : index_page_for_model) |
181 | end |
182 | |
183 | def index_page_for_model |
184 | parts = {:action => "index"} |
185 | if paginated? && model && i = model_class.all.index(model) |
186 | p = (i / pagination_parameters[:per_page].to_i) + 1 |
187 | parts[:p] = p if p && p > 1 |
188 | end |
189 | parts |
190 | end |
191 | |
192 | def edit_model_path |
193 | method = "edit_admin_#{model_name.downcase}_path" |
194 | send method.to_sym, params[:id] |
195 | end |
196 | |
197 | def announce_validation_errors |
198 | flash.now[:error] = t("resource_controller.validation_errors") |
199 | end |
200 | |
201 | def announce_removed |
202 | ActiveSupport::Deprecation.warn("announce_removed is no longer encouraged in Radiant 0.9.x.", caller) |
203 | flash[:notice] = t("resource_controller.removed", :humanized_model_name => humanized_model_name) |
204 | end |
205 | |
206 | def announce_not_found |
207 | flash[:notice] = t("resource_controller.not_found", :humanized_model_name => humanized_model_name) |
208 | end |
209 | |
210 | def announce_update_conflict |
211 | flash.now[:error] = t("resource_controller.update_conflict", :humanized_model_name => humanized_model_name) |
212 | end |
213 | |
214 | def clear_model_cache |
215 | Radiant::Cache.clear if defined?(Radiant::Cache) |
216 | end |
217 | |
218 | def format_symbol |
219 | format.to_sym |
220 | end |
221 | |
222 | def format |
223 | params[:format] || 'html' |
224 | end |
225 | |
226 | # Assist with user agents that cause improper content-negotiation |
227 | # warn "Remove default HTML format, Accept header no longer used. (#{__FILE__}: #{__LINE__})" if Rails.version !~ /^2\.1/ |
228 | def populate_format |
229 | params[:format] ||= 'html' unless request.xhr? |
230 | end |
231 | |
232 | |
233 | end |