Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
14
railties/lib/generators/rails/observer/observer_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class <%= class_name %>Observer < ActiveRecord::Observer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
railties/lib/generators/test_unit/observer/observer_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
8 changes: 8 additions & 0 deletions
8
railties/lib/generators/test_unit/observer/templates/unit_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |