Module: AjaxScaffold::ClassMethods
- Defined in:
- lib/ajax_scaffold_plugin.rb
Instance Method Summary collapse
-
#ajax_scaffold(model_id, options = {}) ⇒ Object
Adds a swath of actions to the controller.
Instance Method Details
#ajax_scaffold(model_id, options = {}) ⇒ Object
Adds a swath of actions to the controller.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/ajax_scaffold_plugin.rb', line 127 def ajax_scaffold(model_id, = {}) .assert_valid_keys(:class_name, :except, :rows_per_page, :suffix, :totals, :width, :rel_width) # set up a few variables for use in the code generation singular_name = model_id.to_s class_name = [:class_name] || singular_name.camelize plural_name = eval("#{class_name}.table_name") || singular_name.pluralize rows_per_page = [:rows_per_page] || 25 suffix = [:suffix] ? "_#{singular_name}" : "" prefix = [:suffix] ? "#{plural_name}_" : "" totals = [:totals].nil? ? nil : [:totals].join(',').to_s width = [:width].nil? ? ([:rel_width].nil? ? nil : [:rel_width]*100 ) : [:width] if (!width.nil?) dimensions = [:width].nil? ? "%" : "px" width = width.to_s + dimensions end no_create, no_edit, no_delete = false, false, false if ([:except]) no_create = [:except].include?('create') no_edit = [:except].include?('edit') no_delete = [:except].include?('delete') end module_eval <<-"end_eval", __FILE__, __LINE__ include AjaxScaffold::Common include AjaxScaffold::Controller verify :method => :post, :only => [ :destroy#{suffix}, :create#{suffix}, :update#{suffix} ], :redirect_to => { :action => :#{prefix}table } after_filter :clear_flashes @@#{prefix}scaffold_columns = nil @@#{prefix}scaffold_total_columns = nil @@#{prefix}scaffold_columns_hash = nil def #{prefix}scaffold_total_columns csv_array = "#{totals}".length >= 1 ? CSV.parse_line("#{totals}") : [] @@#{prefix}scaffold_total_columns ||= csv_array end def #{prefix}scaffold_columns @@#{prefix}scaffold_columns ||= build_#{prefix}scaffold_columns end def #{prefix}scaffold_columns_hash @@#{prefix}scaffold_columns_hash ||= build_#{prefix}scaffold_columns_hash end def #{prefix}table self.#{prefix}table_setup render#{suffix}_template(:action => 'table') end def #{prefix}table_setup update_params :default_scaffold_id => "#{singular_name}", :default_sort => nil, :default_sort_direction => default_#{prefix}sort_direction @show_wrapper = true if @show_wrapper.nil? @width = "#{width}" sort_sql = #{prefix}scaffold_columns_hash[current_sort(params)].sort_sql rescue default_#{prefix}sort sort = #{prefix}scaffold_columns_hash[current_sort(params)].sort rescue nil sort_direction = current_sort_direction(params) klass = Inflector.camelize("#{singular_name}").constantize order = sort.nil? ? sort_sql : sort order += " " + sort_direction options = { :order => order, :conditions => conditions_for_#{plural_name}_collection, :direction => current_sort_direction(params), :per_page => #{rows_per_page} } count = count_#{plural_name}_collection(klass, options) @paginator = Paginator.new(self, count, #{rows_per_page}, current_page(params)) if (sort.nil?) @collection = page_and_sort_#{plural_name}_collection(klass, options, @paginator) else @collection = page_and_sort_#{plural_name}_collection_with_method(klass, options, @paginator) end end # if multiple tables (suffix) then return to index (can be changed by overriding) # if single table return to list def #{prefix}return_to_main action = "#{suffix}" == "" ? "#{prefix}list" : "index" redirect_to :action => action end def update_#{prefix}table @show_wrapper = false # don't show the outer wrapper elements if we are just updating an existing scaffold if request.xhr? #{prefix}table_setup render#{suffix}_template(:action => 'table') else #{prefix}return_to_main end end def #{prefix}list render#{suffix}_template(:action => 'list.rhtml', :layout => true) end end_eval if(suffix=="") module_eval <<-"end_eval", __FILE__, __LINE__ def index redirect_to(:action => :list) end end_eval end # now only create these methods if not excluded unless (no_create) module_eval <<-"end_eval", __FILE__, __LINE__ def new#{suffix} do_new#{suffix} return render#{suffix}_template(:action => 'new.rjs') if request.xhr? # Javascript disabled fallback if @successful @options = { :action => "create" } render#{suffix}_template(:action => "_new_edit.rhtml", :layout => true) else #{prefix}return_to_main end end def do_new#{suffix} @#{singular_name} = #{class_name}.new @successful = true end def create#{suffix} begin do_create#{suffix} rescue flash[:error], @successful = $!.to_s, false end return render#{suffix}_template(:action => 'create.rjs') if request.xhr? if @successful #{prefix}return_to_main else @options = { :scaffold_id => params[:scaffold_id], :action => "create" } render#{suffix}_template(:action => "_new_edit.rhtml", :layout => true) end end def do_create#{suffix} @#{singular_name} = #{class_name}.new(params[:#{singular_name}]) @successful = @#{singular_name}.save end end_eval end unless (no_edit) module_eval <<-"end_eval", __FILE__, __LINE__ def edit#{suffix} begin do_edit#{suffix} rescue flash[:error], @successful = $!.to_s, false end return render#{suffix}_template(:action => 'edit.rjs') if request.xhr? if @successful @options = { :scaffold_id => params[:scaffold_id], :action => "update", :id => params[:id] } render#{suffix}_template(:action => "_new_edit.rhtml", :layout => true) else #{prefix}return_to_main end end def do_edit#{suffix} @#{singular_name} = #{class_name}.find(params[:id]) @successful = !@#{singular_name}.nil? end def update#{suffix} begin do_update#{suffix} rescue flash[:error], @successful = $!.to_s, false end return render#{suffix}_template(:action => 'update.rjs') if request.xhr? if @successful #{prefix}return_to_main else @options = { :action => "update" } render#{suffix}_template(:action => "_new_edit.rhtml", :layout => true) end end def do_update#{suffix} @#{singular_name} = #{class_name}.find(params[:id]) @successful = @#{singular_name}.update_attributes(params[:#{singular_name}]) end end_eval end unless (no_delete) module_eval <<-"end_eval", __FILE__, __LINE__ def destroy#{suffix} begin do_destroy#{suffix} rescue flash[:error], @successful = $!.to_s, false end return render#{suffix}_template(:action => 'destroy.rjs') if request.xhr? # Javascript disabled fallback #{prefix}return_to_main end def do_destroy#{suffix} @successful = #{class_name}.find(params[:id]).destroy end end_eval end module_eval <<-"end_eval", __FILE__, __LINE__ def cancel#{suffix} @successful = true return render#{suffix}_template(:action => 'cancel.rjs') if request.xhr? # Javascript disabled fallback #{prefix}return_to_main end protected # Override this for custom selection conditions def conditions_for_#{plural_name}_collection nil end # Override to change the default sort from plural_name.id def default_#{prefix}sort "#{plural_name}.id" end # Override to change the default sort direction def default_#{prefix}sort_direction "asc" end # Returns the total number of items in the collection def count_#{plural_name}_collection(model, options) count_collection_for_pagination(model, options) end # Per model collection method, using sort_collection_by_method def page_and_sort_#{plural_name}_collection_with_method(model, options, paginator) # call: sort_collection_by_method(collection, column, order) order_parts = options[:order].split(' ') collection = model.find(:all, :conditions => options[:conditions], :joins => options[:join] || options[:joins], :include => options[:include], :select => options[:select]) sort_collection_by_method(collection, order_parts[0], order_parts[1]).slice(paginator.current.offset,options[:per_page]) end # Per model collection method delegates to Rails as default def page_and_sort_#{plural_name}_collection(model, options, paginator) find_collection_for_pagination(model, options, paginator) end private def render#{suffix}_template( options = {} ) @scaffold_columns ||= #{prefix}scaffold_columns @scaffold_column_totals = #{prefix}scaffold_total_columns @scaffold_class = #{class_name} @scaffold_singular_name, @scaffold_plural_name = "#{singular_name}", "#{plural_name}" @scaffold_controller = "/" + self.class.to_s.sub(/Controller/, "").underscore @no_create = #{no_create} @no_edit = #{no_edit} @no_delete = #{no_delete} @suffix = "#{suffix}" @prefix = "#{prefix}" # check for template in following order: # views/scaffold_class/template # views/controller_class/template # views/ajax_scaffold/template action = options[:action] layout = options[:layout] || false if template_exists?("#{plural_name}/\#{action}") render(:action => "../#{plural_name}/\#{action}", :layout => layout) elsif template_exists?("\#{self.class.controller_path}/\#{action}") render(:action => action, :layout => layout) else render(:template => "ajax_scaffold/\#{action}", :layout => layout) end end def build_#{prefix}scaffold_columns scaffold_columns = Array.new #{class_name}.content_columns.each do |column| scaffold_columns << ScaffoldColumn.new(#{class_name}, { :name => column.name }) end scaffold_columns end def build_#{prefix}scaffold_columns_hash scaffold_columns_hash = Hash.new #{prefix}scaffold_columns.each do |scaffold_column| scaffold_columns_hash[scaffold_column.name] = scaffold_column end scaffold_columns_hash end end_eval # end ajax_scaffold method end |