Permalink
Browse files

Improve scaffold generator to do pagination in scope

  • Loading branch information...
1 parent 698b925 commit 9ab5885120b0a2fa2bc0494cb038ae16607f4703 @bogdan committed Jun 18, 2014
Showing with 24 additions and 12 deletions.
  1. +6 −8 lib/datagrid/scaffold.rb
  2. +14 −1 spec/datagrid/scaffold_spec.rb
  3. +3 −2 templates/controller.rb.erb
  4. +1 −1 templates/index.html.erb
View
@@ -43,22 +43,20 @@ def grid_ivar_name
end
def paginate_code
- if defined?(Kaminari)
+ if defined?(::Kaminari) || defined?(::WillPaginate)
"page(params[:page])"
- elsif defined?(WillPaginate)
- "paginate(:page => params[:page])"
else
"paginate_somehow"
end
end
def pagination_helper_code
- if defined?(Kaminari)
- "paginate(@assets)"
- elsif defined?(WillPaginate)
- "will_paginate(@assets)"
+ if defined?(::Kaminari)
+ "paginate(@grid.assets)"
+ elsif defined?(::WillPaginate)
+ "will_paginate(@grid.assets)"
else
- "some_pagination_helper(@assets)"
+ "some_pagination_helper(@grid.assets)"
end
end
@@ -11,7 +11,20 @@
describe '.pagination_helper_code' do
it 'should fall through options successfully' do
- expect(subject.pagination_helper_code).to eql('some_pagination_helper(@assets)')
+ expect(subject.pagination_helper_code).to eql('some_pagination_helper(@grid.assets)')
+ end
+
+ context "when Kaminari exists" do
+ before(:each) do
+ Object.const_set("Kaminari", 1)
+ end
+ it 'should fall through options successfully' do
+ expect(subject.pagination_helper_code).to eql('paginate(@grid.assets)')
+ end
+
+ after(:each) do
+ Object.send(:remove_const, "Kaminari")
+ end
end
end
end
@@ -1,8 +1,9 @@
class <%= grid_controller_name %> < ApplicationController
def index
- @grid = <%= grid_class_name %>.new(params[:<%= grid_ivar_name %>])
- @assets = @grid.assets.<%= paginate_code %>
+ @grid = <%= grid_class_name %>.new(params[:<%= grid_ivar_name %>]) do |scope|
+ scope.<%= paginate_code %>
+ end
end
end
View
@@ -1,5 +1,5 @@
<%%= datagrid_form_for @grid, :method => :get, :url => <%= grid_route_name %> %>
<%%= <%=pagination_helper_code%> %>
-<%%= datagrid_table @grid, @assets %>
+<%%= datagrid_table @grid %>
<%%= <%=pagination_helper_code%> %>

0 comments on commit 9ab5885

Please sign in to comment.