From 06fe0fb426a526860d846627756c1952236e3403 Mon Sep 17 00:00:00 2001 From: Philip Arndt Date: Wed, 9 Mar 2011 14:27:09 +1300 Subject: [PATCH] Added xhr_paging as an option in crudify which handles the server-side usage of the HTML5 History API (or some other thing that's requesting the index page via xhr request). --- .../app/controllers/admin/users_controller.rb | 12 ++++------ core/lib/refinery/crud.rb | 7 +++++- doc/crud.md | 23 +++++++++++++++++++ .../controllers/admin/images_controller.rb | 10 ++------ .../controllers/admin/resources_controller.rb | 12 ++++------ .../admin/refinery_settings_controller.rb | 10 ++------ 6 files changed, 41 insertions(+), 33 deletions(-) diff --git a/authentication/app/controllers/admin/users_controller.rb b/authentication/app/controllers/admin/users_controller.rb index fa7498ddc9..c98466c7fa 100644 --- a/authentication/app/controllers/admin/users_controller.rb +++ b/authentication/app/controllers/admin/users_controller.rb @@ -1,17 +1,13 @@ module Admin class UsersController < Admin::BaseController - crudify :user, :order => 'username ASC', :title_attribute => 'username' + crudify :user, + :order => 'username ASC', + :title_attribute => 'username', + :xhr_paging => true before_filter :load_available_plugins_and_roles, :only => [:new, :create, :edit, :update] - def index - search_all_users if searching? - paginate_all_users - - render :partial => 'users' if request.xhr? - end - def new @user = User.new @selected_plugin_names = [] diff --git a/core/lib/refinery/crud.rb b/core/lib/refinery/crud.rb index bbf7a621fd..70f38314d3 100644 --- a/core/lib/refinery/crud.rb +++ b/core/lib/refinery/crud.rb @@ -27,7 +27,8 @@ def self.default_options(model_name) :searchable => true, :search_conditions => '', :sortable => true, - :title_attribute => "title" + :title_attribute => "title", + :xhr_paging => false } end @@ -197,6 +198,8 @@ def search_all_#{plural_name} def index search_all_#{plural_name} if searching? paginate_all_#{plural_name} + + render :partial => '#{plural_name}' if #{options[:xhr_paging].inspect} && request.xhr? end ) else @@ -216,6 +219,8 @@ def index module_eval %( def index paginate_all_#{plural_name} + + render :partial => '#{plural_name}' if #{options[:xhr_paging].inspect} && request.xhr? end ) else diff --git a/doc/crud.md b/doc/crud.md index e73cad090f..5f05137826 100644 --- a/doc/crud.md +++ b/doc/crud.md @@ -134,6 +134,29 @@ Default value is ``true`` The ``:paging`` option tells ``crudify`` you don't just want one big long list but rather to break it out into pages and support paging methods uses [will_paginate](http://wiki.github.com/mislav/will_paginate/). +### ``:xhr_paging`` + +Default value is ``false`` + +The ``:xhr_paging`` option tells ``crudify`` you want to use more advanced paging. +This works using the HTML5 History API to swap out only the content that is being +paged rather than the entire webpage. To support this option you will need a partial +named after the plural name for example with ``news_items``: + + class NewsItemsController < ApplicationController + + crudify :news_item, :paging => true, :xhr_paging => true + + end + +This requires a partial called "news_items" in the appropriate directory which contains: + + <%= will_paginate @news_items %> + + ### ``:search_conditions`` Default value is ``nil`` diff --git a/images/app/controllers/admin/images_controller.rb b/images/app/controllers/admin/images_controller.rb index 245d8bc594..7c7d482a5b 100644 --- a/images/app/controllers/admin/images_controller.rb +++ b/images/app/controllers/admin/images_controller.rb @@ -5,17 +5,11 @@ class ImagesController < Admin::BaseController crudify :image, :order => "created_at DESC", - :sortable => false + :sortable => false, + :xhr_paging => true before_filter :change_list_mode_if_specified, :init_dialog - def index - search_all_images if searching? - paginate_all_images - - render :partial => 'images' if request.xhr? - end - def new @image = Image.new if @image.nil? diff --git a/resources/app/controllers/admin/resources_controller.rb b/resources/app/controllers/admin/resources_controller.rb index dbd1a84281..2cdfca935c 100644 --- a/resources/app/controllers/admin/resources_controller.rb +++ b/resources/app/controllers/admin/resources_controller.rb @@ -1,7 +1,10 @@ module Admin class ResourcesController < Admin::BaseController - crudify :resource, :order => "updated_at DESC" + crudify :resource, + :order => "updated_at DESC", + :xhr_paging => true + before_filter :init_dialog def new @@ -36,13 +39,6 @@ def create end end - def index - search_all_resources if searching? - paginate_all_resources - - render :partial => 'resources' if request.xhr? - end - def insert self.new if @resource.nil? diff --git a/settings/app/controllers/admin/refinery_settings_controller.rb b/settings/app/controllers/admin/refinery_settings_controller.rb index 0747c94413..c85b1b540c 100644 --- a/settings/app/controllers/admin/refinery_settings_controller.rb +++ b/settings/app/controllers/admin/refinery_settings_controller.rb @@ -6,18 +6,12 @@ class RefinerySettingsController < Admin::BaseController crudify :refinery_setting, :title_attribute => :title, :order => "name ASC", - :redirect_to_url => :redirect_to_where? + :redirect_to_url => :redirect_to_where?, + :xhr_paging => true before_filter :sanitise_params, :only => [:create, :update] after_filter :fire_setting_callback, :only => [:update] - def index - search_all_refinery_settings if searching? - paginate_all_refinery_settings - - render :partial => 'refinery_settings' if request.xhr? - end - def new if current_user.has_role?(:superuser) and params[:form_value_type].present? @refinery_setting = RefinerySetting.new(:form_value_type => params[:form_value_type])