Skip to content

Commit

Permalink
Added observer generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Jun 25, 2009
1 parent eaef1ee commit 4573fd2
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
13 changes: 13 additions & 0 deletions railties/lib/generators/rails/observer/USAGE
@@ -0,0 +1,13 @@
Description:
Stubs out a new observer. Pass the observer name, either CamelCased or
under_scored, as an argument.

The generator creates an observer class in app/models and a unit test in
the configured test framework.

Example:
`./script/generate observer Account`

creates an Account observer and invoke the test framework:
Observer: app/models/account_observer.rb
TestUnit: test/unit/account_observer_test.rb
14 changes: 14 additions & 0 deletions railties/lib/generators/rails/observer/observer_generator.rb
@@ -0,0 +1,14 @@
module Rails
module Generators
class ObserverGenerator < NamedBase
# TODO Check class collisions
# class_collisions "#{class_name}Observer", "#{class_name}ObserverTest"

def create_observer_file
template 'observer.rb', File.join('app/models', class_path, "#{file_name}_observer.rb")
end

add_and_invoke_test_framework_option!
end
end
end
2 changes: 2 additions & 0 deletions railties/lib/generators/rails/observer/templates/observer.rb
@@ -0,0 +1,2 @@
class <%= class_name %>Observer < ActiveRecord::Observer
end
1 change: 1 addition & 0 deletions railties/lib/generators/rails/plugin/plugin_generator.rb
Expand Up @@ -8,6 +8,7 @@ class PluginGenerator < NamedBase
:desc => "When supplied creates generator base files."

# TODO Check class collision
# class_collision class_name

def create_root
self.root = File.expand_path("vendor/plugins/#{file_name}", root)
Expand Down
14 changes: 14 additions & 0 deletions railties/lib/generators/test_unit/observer/observer_generator.rb
@@ -0,0 +1,14 @@
module TestUnit
module Generators
class ObserverGenerator < Base
desc <<DESC
Description:
Create TestUnit files for observer generator.
DESC

def create_test_files
template 'unit_test.rb', File.join('test', 'unit', class_path, "#{file_name}_observer_test.rb")
end
end
end
end
@@ -0,0 +1,8 @@
require 'test_helper'

class <%= class_name %>ObserverTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
34 changes: 34 additions & 0 deletions railties/test/generators/observer_generator_test.rb
@@ -0,0 +1,34 @@
require 'abstract_unit'
require 'generators/generators_test_helper'
require 'generators/rails/observer/observer_generator'
require 'generators/test_unit/observer/observer_generator'

class ObserverGeneratorTest < GeneratorsTestCase

def test_observer_skeleton_is_created
run_generator
assert_file "app/models/account_observer.rb", /class AccountObserver < ActiveRecord::Observer/
end

def test_observer_with_class_path_skeleton_is_created
run_generator ["admin/account"]
assert_file "app/models/admin/account_observer.rb", /class Admin::AccountObserver < ActiveRecord::Observer/
end

def test_invokes_default_test_framework
run_generator
assert_file "test/unit/account_observer_test.rb"
end

def test_logs_if_the_test_framework_cannot_be_found
content = run_generator ["account", "--test-framework=unknown"]
assert_match /Could not find and invoke 'unknown:generators:observer'/, content
end

protected

def run_generator(args=["account"])
silence(:stdout) { Rails::Generators::ObserverGenerator.start args, :root => destination_root }
end

end

0 comments on commit 4573fd2

Please sign in to comment.