Permalink
Browse files

Move datagrid form to partial

  • Loading branch information...
1 parent 1581686 commit ecd3a9eac20daeb34eb5d4b430ae634db894109a @bogdan committed May 15, 2013
View
@@ -162,24 +162,10 @@ route resources :skills
If you want to be a true ninja you can build everything from scratch on your own.
-In order to create form for your report you can use all set of rails built-in tools.
-More over Datagrid provides you two additional form helpers:
+In order to build frontend datagrid provides you with a number of helper:
-* datagrid\_label
-* datagrid\_filter
-
-
-The easiest way to create a report form (haml for readablity):
-
-``` haml
-# Method `GET` is recommended for all report forms by default.
-- form_for @report, :html => {:method => :get} do |f|
- - @report.filters.each do |filter|
- %div
- = f.datagrid_label filter
- = f.datagrid_filter filter
- = f.submit
-```
+* datagrid\_form\_for
+* datagrid\_table
Your controller:
@@ -197,6 +183,9 @@ There is a simple helper set of helpers that allows you display report:
(require any pagination gem, will\_paginate is used as an example)
``` haml
+# Method `GET` is recommended for all report forms by default.
+= datagrid_form_for @report, :url => simple_reports_path, :html => {:method => :get}
+
- assets = @report.assets.paginate(:page => params[:page])
%div== Total #{assets.total_entries}
@@ -0,0 +1,9 @@
+<%= form_for grid, options do |f| -%>
+ <% grid.filters.each do |filter| %>
+ <div class="filter">
+ <%= f.datagrid_label filter %>
+ <%= f.datagrid_filter filter %>
+ </div>
+ <% end %>
+ <%= f.submit I18n.t("datgrid.form.submit", :default => "Search").html_safe %>
+<% end -%>
View
@@ -24,6 +24,10 @@ def datagrid_order_for(grid, column)
datagrid_renderer.order_for(grid, column)
end
+ def datagrid_form_for(grid, options = {})
+ datagrid_renderer.form_for(grid, options)
+ end
+
protected
def datagrid_renderer
View
@@ -43,10 +43,17 @@ def format_value(grid, column, asset)
end
end
+ def form_for(grid, options = {})
+ options[:method] ||= :get
+ options[:html] ||= {}
+ options[:html][:class] ||= "datagrid-form #{html_class(grid)}"
+ @template.render :partial => "datagrid/form", :locals => {:grid => grid, :options => options}
+ end
+
def table(grid, *args)
options = args.extract_options!
options[:html] ||= {}
- options[:html][:class] ||= "datagrid #{grid.class.to_s.underscore.demodulize}"
+ options[:html][:class] ||= "datagrid #{html_class(grid)}"
assets = args.any? ? args.shift : grid.assets
paginate = options[:paginate]
if paginate
@@ -75,6 +82,9 @@ def order_for(grid, column)
@template.render :partial => "datagrid/order_for", :locals => { :grid => grid, :column => column }
end
+ def html_class(grid)
+ grid.class.to_s.underscore.demodulize
+ end
def _safe(string)
string.respond_to?(:html_safe) ? string.html_safe : string
@@ -3,18 +3,13 @@ namespace :datagrid do
desc "Copy table partials into rails application"
task :copy_partials do
require "fileutils"
- def copy_template(path)
- gem_app = File.expand_path("../../../app", __FILE__)
- rails_app = (Rails.root + "app").to_s
- full_path = "#{rails_app}/#{File.dirname path}"
- puts "* copy #{full_path}"
- FileUtils.mkdir_p full_path
- FileUtils.cp "#{gem_app}/#{path}", full_path
+ views_path = "app/views/datagrid"
+ destination_dir = (Rails.root + views_path).to_s
+ pattern = File.expand_path(File.dirname(__FILE__) + "/../../#{views_path}") + "/*"
+ Dir[pattern].each do |template|
+ puts "* copy #{template} => #{destination_dir}"
+ FileUtils.mkdir_p destination_dir
+ FileUtils.cp template, destination_dir
end
- copy_template "views/datagrid/_table.html.erb"
- copy_template "views/datagrid/_head.html.erb"
- copy_template "views/datagrid/_order_for.html.erb"
- copy_template "views/datagrid/_row.html.erb"
end
-
end
@@ -278,5 +278,22 @@ class OrderedGrid
HTML
end
end
+ describe ".datagrid_form_for" do
+ it "should render ordering layout" do
+ class FormForGrid
+ include Datagrid
+ scope { Entry }
+ filter(:category)
+ end
+ grid = FormForGrid.new(:category => "hello")
+ subject.datagrid_form_for(grid, :url => "/grid").should match_css_pattern(
+ "form.datagrid-form.form_for_grid[action='/grid']" => 1,
+ "form input[name=utf8]" => 1,
+ "form .filter label" => "Category",
+ "form .filter input.category.default_filter[name='form_for_grid[category]'][value=hello]" => 1,
+ "form input[name=commit][value=Search]" => 1
+ )
+ end
+ end
end
end
View
@@ -1,12 +1,4 @@
-<%%= form_for @grid, :method => :get, :url => <%= grid_route_name %> do |f| %>
- <%% @grid.filters.each do |filter| %>
- <div class="field">
- <%%= f.datagrid_label filter %>
- <%%= f.datagrid_filter filter %>
- </div>
- <%% end %>
- <%%= f.submit %>
-<%% end %>
+<%%= datagrid_form_for @grid, :method => :get, :url => <%= grid_route_name %> %>
<%%= <%=pagination_helper_code%> %>
<%%= datagrid_table @grid, @assets %>

0 comments on commit ecd3a9e

Please sign in to comment.