Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Class::ModelName to Active Support module core_ext
- Loading branch information
Showing
4 changed files
with
48 additions
and
18 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
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
22 changes: 22 additions & 0 deletions
22
activesupport/lib/active_support/core_ext/module/model_naming.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,22 @@ | ||
module ActiveSupport | ||
class ModelName < String | ||
attr_reader :singular, :plural, :partial_path | ||
|
||
def initialize(name) | ||
super | ||
@singular = underscore.tr('/', '_').freeze | ||
@plural = @singular.pluralize.freeze | ||
@partial_path = "#{tableize}/#{demodulize.underscore}".freeze | ||
end | ||
end | ||
|
||
module CoreExt | ||
module Module | ||
module ModelNaming | ||
def model_name | ||
@model_name ||= ModelName.new(name) | ||
end | ||
end | ||
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,19 @@ | ||
require 'abstract_unit' | ||
|
||
class ModelNamingTest < Test::Unit::TestCase | ||
def setup | ||
@name = ActiveSupport::ModelName.new('Post::TrackBack') | ||
end | ||
|
||
def test_singular | ||
assert_equal 'post_track_back', @name.singular | ||
end | ||
|
||
def test_plural | ||
assert_equal 'post_track_backs', @name.plural | ||
end | ||
|
||
def test_partial_path | ||
assert_equal 'post/track_backs/track_back', @name.partial_path | ||
end | ||
end |