mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added observer generator.
This commit is contained in:
parent
eaef1ee09c
commit
4573fd2e06
7 changed files with 86 additions and 0 deletions
13
railties/lib/generators/rails/observer/USAGE
Normal file
13
railties/lib/generators/rails/observer/USAGE
Normal file
|
@ -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
railties/lib/generators/rails/observer/observer_generator.rb
Normal file
14
railties/lib/generators/rails/observer/observer_generator.rb
Normal file
|
@ -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
|
|
@ -0,0 +1,2 @@
|
|||
class <%= class_name %>Observer < ActiveRecord::Observer
|
||||
end
|
|
@ -8,6 +8,7 @@ module Rails
|
|||
: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)
|
||||
|
|
|
@ -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
railties/test/generators/observer_generator_test.rb
Normal file
34
railties/test/generators/observer_generator_test.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue