Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow generators that depend on ActiveRecord Model

fixes #11
  • Loading branch information...
commit a588a290508cbefaf39b81068bae80c3b057c668 1 parent 2aa6de6
@alexrothenberg authored
View
50 features/hooking_into_other_generators.feature
@@ -0,0 +1,50 @@
+Feature: generator spec
+
+ Generator specs live in spec/generators. In order to access
+ the generator's methods you can call them on the "generator" object.
+
+ Background: A generator that uses "hook_for"
+ Given a file named "lib/generators/resourceful/resourceful_generator.rb" with:
+ """
+ class ResourcefulGenerator < Rails::Generators::NamedBase
+ source_root File.expand_path('../templates', __FILE__)
+ class_option :super, :type => :boolean, :default => false
+
+ hook_for :orm, :in => :rails, :as => :model, :required => true
+
+ def create_resourceful_controller
+ template 'controller.rb', File.join('app/controllers', "#{plural_file_name}_controller.rb")
+ end
+ end
+ """
+ And a file named "lib/generators/resourceful/templates/controller.rb" with:
+ """
+ class <%= class_name.pluralize %>Controller < ResourcefulController
+ end
+ """
+
+ Scenario: A spec that runs the entire generator
+ Given a file named "spec/generators/resourceful_generator_spec.rb" with:
+ """
+ require "spec_helper"
+ require 'generators/resourceful/resourceful_generator'
+
+ describe ResourcefulGenerator do
+ before { run_generator %w(post) }
+ describe 'app/controller/posts_controller.rb' do
+ subject { file('app/controllers/posts_controller.rb') }
+ it { should exist }
+ it { should contain 'class PostsController < ResourcefulController' }
+ end
+
+ describe 'app/models/post.rb' do
+ subject { file('app/models/post.rb') }
+ it { should exist }
+ it { should contain 'class Post < ActiveRecord::Base' }
+ end
+ end
+ """
+ When I run `rake spec`
+ Then the output should contain "4 examples, 0 failures"
+
+
View
2  lib/ammeter/init.rb
@@ -1,2 +1,4 @@
require 'ammeter/rspec/generator/example.rb'
require 'ammeter/rspec/generator/matchers.rb'
+
+Rails.application.load_generators
View
7 spec/spec_helper.rb
@@ -1,5 +1,12 @@
require 'rails/all'
require 'rspec/rails'
+
+module TestApp
+ class Application < Rails::Application
+ config.root = File.dirname(__FILE__)
+ end
+end
+
require 'ammeter/init'
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
Please sign in to comment.
Something went wrong with that request. Please try again.