Permalink
Browse files

First version of grid generator

  • Loading branch information...
1 parent 923f2e7 commit 1d2b14e839ff9546e1e8746446e8548f0280761e @bogdan committed Jan 29, 2013
Showing with 94 additions and 11 deletions.
  1. +1 −8 lib/datagrid.rb
  2. +54 −3 lib/datagrid/generator.rb
  3. +9 −0 templates/controller.rb.erb
  4. +17 −0 templates/grid.rb.erb
  5. +13 −0 templates/index.html.erb
View
@@ -31,23 +31,16 @@ def self.included(base)
include ::Datagrid::Ordering
end
- base.send :include, InstanceMethods
end # self.included
module ClassMethods
-
-
end # ClassMethods
- module InstanceMethods
-
- end # InstanceMethods
-
class ConfigurationError < StandardError; end
class ArgumentError < ::ArgumentError; end
-
end
+require "datagrid/generator"
View
@@ -1,7 +1,58 @@
-class DatagridGenerator < Rails::Generators::NamedBase
- source_root File.expand_path(__FILE__ + "/../../../generator")
+require "rails/generators"
+
+class Datagrid::Generator < Rails::Generators::NamedBase
+ source_root File.expand_path(__FILE__ + "/../../../templates")
def create_uploader_file
- template "grid_template.rb.erb", "app/grids/#{file_name}_grid.rb"
+ template "grid.rb.erb", "app/grids/#{grid_class_name.underscore}.rb"
+ template "controller.rb.erb", "app/controllers/#{grid_controller_name.underscore}.rb"
+ template "index.html.erb", "app/views/#{grid_controller_short_name}/index.html.erb"
+ route("resources :#{grid_controller_short_name}")
+ end
+
+ def grid_class_name
+ file_name.camelize.pluralize + "Grid"
+ end
+
+ def grid_controller_name
+ grid_controller_short_name.camelize + "Controller"
+ end
+
+ def grid_controller_short_name
+ file_name.underscore.pluralize
+ end
+
+ def grid_model_name
+ file_name.camelize.singularize
+ end
+
+ def grid_ivar_name
+ grid_class_name.underscore
+ end
+
+ def paginate_code
+ if defined?(Kaminari)
+ "page(params[:page])"
+ elsif defned?(WillPaginate)
+ "paginate(:page => params[:page])"
+ else
+ "paginate_somehow"
+ end
end
+
+ def pagination_helper_code
+ if defined?(Kaminari)
+ "paginate(@assets)"
+ elsif defned?(WillPaginate)
+ "will_paginate(@assets)"
+ else
+ "some_pagination_helper(@assets)"
+ end
+
+ end
+
+ def grid_route_name
+ grid_controller_short_name + "_path"
+ end
+
end
@@ -0,0 +1,9 @@
+class <%= grid_controller_name %> < ApplicationController
+
+ def index
+ @grid = <%= grid_class_name %>.new(params[:<%= grid_ivar_name %>])
+ @assets = @grid.assets.<%= paginate_code %>
+ end
+
+end
+
View
@@ -0,0 +1,17 @@
+class <%= grid_class_name %>
+
+ include Datagrid
+
+ scope do
+ <%= grid_model_name %>
+ end
+
+ filter(:id, :integer)
+ filter(:created_at, :date, :range => true)
+
+ column(:id)
+ column(:name)
+ column(:created_at) do |model|
+ model.created_at.to_date
+ end
+end
View
@@ -0,0 +1,13 @@
+<%%= 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 %>
+
+<%%= <%=pagination_helper_code%> %>
+<%%= datagrid_table @grid, @assets %>
+<%%= <%=pagination_helper_code%> %>

0 comments on commit 1d2b14e

Please sign in to comment.