Skip to content

Commit

Permalink
Added xhr_paging as an option in crudify which handles the server-sid…
Browse files Browse the repository at this point in the history
…e usage of the HTML5 History API (or some other thing that's requesting the index page via xhr request).
  • Loading branch information
parndt committed Mar 9, 2011
1 parent fb5827b commit 06fe0fb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 33 deletions.
12 changes: 4 additions & 8 deletions authentication/app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -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 = []
Expand Down
7 changes: 6 additions & 1 deletion core/lib/refinery/crud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions doc/crud.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
<ul class="<%= ['pagination_frame', pagination_css_class].compact.join(' ') %>">
<%= render :partial => 'news_item',
:collection => @news_items %>
</ul>

### ``:search_conditions``

Default value is ``nil``
Expand Down
10 changes: 2 additions & 8 deletions images/app/controllers/admin/images_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
12 changes: 4 additions & 8 deletions resources/app/controllers/admin/resources_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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?

Expand Down
10 changes: 2 additions & 8 deletions settings/app/controllers/admin/refinery_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 06fe0fb

Please sign in to comment.