Permalink
Browse files

Added generator from plugin

  • Loading branch information...
1 parent 4546231 commit 75c59127f7ed8b7d20b8cc8c930347e7ecfe092a @topfunky committed Mar 13, 2008
View
@@ -52,6 +52,9 @@ lib/gruff/spider.rb
lib/gruff/stacked_area.rb
lib/gruff/stacked_bar.rb
lib/gruff/stacked_mixin.rb
+rails_generators/gruff/gruff_generator.rb
+rails_generators/gruff/templates/controller.rb
+rails_generators/gruff/templates/functional_test.rb
test/gruff_test_case.rb
test/test_accumulator_bar.rb
test/test_area.rb
@@ -0,0 +1,63 @@
+class GruffGenerator < Rails::Generator::NamedBase
+
+ attr_reader :controller_name,
+ :controller_class_path,
+ :controller_file_path,
+ :controller_class_nesting,
+ :controller_class_nesting_depth,
+ :controller_class_name,
+ :controller_singular_name,
+ :controller_plural_name,
+ :parent_folder_for_require
+ alias_method :controller_file_name, :controller_singular_name
+ alias_method :controller_table_name, :controller_plural_name
+
+ def initialize(runtime_args, runtime_options = {})
+ super
+
+ # Take controller name from the next argument.
+ @controller_name = runtime_args.shift
+
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
+ @controller_class_name_without_nesting, @controller_singular_name, @controller_plural_name = inflect_names(base_name)
+
+ if @controller_class_nesting.empty?
+ @controller_class_name = @controller_class_name_without_nesting
+ else
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
+ end
+ end
+
+ def manifest
+ record do |m|
+ # Check for class naming collisions.
+ m.class_collisions controller_class_path, "#{controller_class_name}Controller",
+ "#{controller_class_name}ControllerTest"
+
+ # Controller, helper, views, and test directories.
+ m.directory File.join('app/controllers', controller_class_path)
+ m.directory File.join('test/functional', controller_class_path)
+
+ m.template 'controller.rb',
+ File.join('app/controllers',
+ controller_class_path,
+ "#{controller_file_name}_controller.rb")
+
+ # For some reason this doesn't take effect if done in initialize()
+ @parent_folder_for_require = @controller_class_path.join('/').gsub(%r%app/controllers/?%, '')
+ @parent_folder_for_require += @parent_folder_for_require.blank? ? '' : '/'
+
+ m.template 'functional_test.rb',
+ File.join('test/functional',
+ controller_class_path,
+ "#{controller_file_name}_controller_test.rb")
+
+ end
+ end
+
+ protected
+ # Override with your own usage banner.
+ def banner
+ "Usage: #{$0} gruff ControllerName"
+ end
+end
@@ -0,0 +1,32 @@
+class <%= controller_class_name %>Controller < ApplicationController
+
+ # To make caching easier, add a line like this to config/routes.rb:
+ # map.graph "graph/:action/:id/image.png", :controller => "graph"
+ #
+ # Then reference it with the named route:
+ # image_tag graph_url(:action => 'show', :id => 42)
+
+ def show
+ g = Gruff::Line.new
+ # Uncomment to use your own theme or font
+ # See http://colourlovers.com or http://www.firewheeldesign.com/widgets/ for color ideas
+# g.theme = {
+# :colors => ['#663366', '#cccc99', '#cc6633', '#cc9966', '#99cc99'],
+# :marker_color => 'white',
+# :background_colors => ['black', '#333333']
+# }
+# g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT)
+
+ g.title = "Gruff-o-Rama"
+
+ g.data("Apples", [1, 2, 3, 4, 4, 3])
+ g.data("Oranges", [4, 8, 7, 9, 8, 9])
+ g.data("Watermelon", [2, 3, 1, 5, 6, 8])
+ g.data("Peaches", [9, 9, 10, 8, 7, 9])
+
+ g.labels = {0 => '2004', 2 => '2005', 4 => '2006'}
+
+ send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png")
+ end
+
+end
@@ -0,0 +1,24 @@
+require File.dirname(__FILE__) + '<%= '/..' * controller_class_name.split("::").length %>/test_helper'
+require '<%= parent_folder_for_require %><%= controller_file_name %>_controller'
+
+# Re-raise errors caught by the controller.
+class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
+
+class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
+
+ #fixtures :data
+
+ def setup
+ @controller = <%= controller_class_name %>Controller.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ end
+
+ # TODO Replace this with your actual tests
+ def test_show
+ get :show
+ assert_response :success
+ assert_equal 'image/png', @response.headers['Content-Type']
+ end
+
+end

0 comments on commit 75c5912

Please sign in to comment.