Permalink
Browse files

allow pass custom partial name

  • Loading branch information...
1 parent 9a69d2d commit 9a1489e40e9ef8c1371f5759e6764b40865dbfb5 Sergey Pchelincev committed Oct 3, 2013
Showing with 67 additions and 32 deletions.
  1. +47 −28 README.rdoc
  2. +2 −1 lib/rjax/resolver.rb
  3. +10 −2 spec/dummy/application.rb
  4. +1 −0 spec/dummy/articles/_popular.erb
  5. +7 −1 spec/rjax/rjax_spec.rb
View
@@ -31,44 +31,63 @@ Rjax will be useful if you have a lot actions which serve http and ajax requests
render :index_ajax if request.xhr?
end
-Rjax replace it with single method call
-
- def index
- @users = User.all
- rjax
- end
- # render index_rjax template
-
- def index
- @users = User.all
- rjax @users
- end
- # render _user_rjax or _user partial with variable `user`
-
- def index
- @users = User.all
- rjax :users => @users, :other => :variable
- end
- # render _users_rjax or _users partial with variables users & other
-
- def show
- @user = User.find(params[:id])
- rjax @user
+Rjax adds `rjax` methods in rails controllers.
+
+ class UsersController < ApplicationController
+ def index
+ @users = User.all
+ rjax
+ end
+ # calling rjax method without arguments will render template;
+ # the name of template will contain action_name and wrapper;
+ # in this case expected `index_rjax` template;
+
+ def index
+ @users = User.all
+ rjax @users
+ end
+ # calling rjax method is similar to view helper render %collection%;
+ # the only different is also looks for partial with wrapped name;
+ # in this case it will look for partial `_user_rjax` or `_user` and pass variable `user`;
+
+ def index
+ @users = User.all
+ rjax :users => @users, :other => :variable
+ end
+ # calling rjax method with Hash will render partial with locals specified in hash;
+ # the name of partial might be specified in :partial key;
+ # if :partial key is missed expected that view folder contains partial with plural form of controller with or without wrapper;
+ # in this case it will be `_users_rjax` or `_users` partial
+
+ def show
+ @user = User.find(params[:id])
+ rjax @user
+ end
+ # in other cases rjax will render partial and pass this variable into partial;
+ # the name of partial will be singular form of controller with or without wrapper;
+ # in this case it will be `_user_rjax` or `_user` partial
+
+ # it also support respond_with
+ def index
+ respond_with do |format|
+ format.html { rjax }
+ format.json { rjax }
+ format.xml { rjax }
+ end
+ end
end
- # render _user_rjax or _user partial with `user` variable
-The name of partial is based on controller name. If you don't like rjax suffix or it's out your team's convention
-feel free to configure wrapper:
+If you don't like rjax default wrapper you can configure it:
# config/initializers/rjax.rb
Rjax.config do |config|
config.suffix = "suffix"
config.prefix = "prefix"
end
- prefix_%action_name%_suffix
+ prefix_%template%_suffix
- _prefix_%partial_name%_suffix
+ _prefix_%partial%_suffix
== Contributing
View
@@ -18,7 +18,8 @@ def process
private
def general_partial
- { :partial => partial_name(:multi), :locals => options, :layout => false }
+ partial = options.has_key?(:partial) ? options.delete(:partial) : partial_name(:multi)
+ { :partial => partial, :locals => options, :layout => false }
end
def collection_partial
View
@@ -20,7 +20,7 @@ class Dummy < Rails::Application
get :search, :popular, :on => :collection
end
resources :articles, :only => [:index, :show] do
- get :search, :on => :collection
+ get :search, :popular, :on => :collection
end
end
end
@@ -49,6 +49,8 @@ def show
end
class ArticlesController < BaseController
+ respond_to :html
+
def index
rjax %w(Monday Tuesday Sunday)
end
@@ -57,7 +59,13 @@ def search
rjax :articles => %w(Monday Sunday)
end
+ def popular
+ rjax :articles => %w(Friday Saturday), :partial => 'popular'
+ end
+
def show
- rjax "Monday"
+ respond_with do |format|
+ format.html { rjax "Monday" }
+ end
end
end
@@ -0,0 +1 @@
+<%= articles.join(', ') %>
View
@@ -40,6 +40,12 @@
body.should == "Monday, Sunday"
end
+ it 'use custom partial' do
+ ajax '/articles/popular'
+
+ body.should == "Friday, Saturday"
+ end
+
it 'collection instance partial' do
ajax '/articles/1'
@@ -56,6 +62,6 @@
end
def ajax(path)
- get path, {}, { "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest", 'HTTP_ACCEPT' => '*/*' }
+ get path, { :format => :html }, { "HTTP_X_REQUESTED_WITH" => "XMLHttpRequest", 'HTTP_ACCEPT' => '*/*' }
end
end

0 comments on commit 9a1489e

Please sign in to comment.