From 1c4d28ba314c8cdd0039becf3bc9e678219b8f46 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 17 Jun 2009 10:37:39 -0500 Subject: [PATCH] Move model naming into ActiveModel --- ...er_partial_with_record_identification_test.rb | 1 + .../test/controller/record_identifier_test.rb | 2 ++ actionpack/test/controller/redirect_test.rb | 1 + actionpack/test/lib/controller/fake_models.rb | 4 ++++ .../test/template/atom_feed_helper_test.rb | 1 + .../test/template/prototype_helper_test.rb | 3 +++ .../test/template/record_tag_helper_test.rb | 1 + actionpack/test/template/test_test.rb | 1 + actionpack/test/template/url_helper_test.rb | 2 ++ activemodel/lib/active_model.rb | 2 ++ .../lib/active_model/naming.rb | 16 ++++++++-------- .../test/cases/naming_test.rb | 7 +++---- activerecord/lib/active_record/base.rb | 1 + activeresource/lib/active_resource/base.rb | 1 + .../lib/active_support/core_ext/module.rb | 1 - 15 files changed, 31 insertions(+), 13 deletions(-) rename activesupport/lib/active_support/core_ext/module/model_naming.rb => activemodel/lib/active_model/naming.rb (68%) rename activesupport/test/core_ext/module/model_naming_test.rb => activemodel/test/cases/naming_test.rb (70%) diff --git a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb index 0a596c7ae0..2a31f3be44 100644 --- a/actionpack/test/activerecord/render_partial_with_record_identification_test.rb +++ b/actionpack/test/activerecord/render_partial_with_record_identification_test.rb @@ -126,6 +126,7 @@ class RenderPartialWithRecordIdentificationController < ActionController::Base end class Game < Struct.new(:name, :id) + extend ActiveModel::Naming def to_param id.to_s end diff --git a/actionpack/test/controller/record_identifier_test.rb b/actionpack/test/controller/record_identifier_test.rb index 12c1eaea69..28bc608d47 100644 --- a/actionpack/test/controller/record_identifier_test.rb +++ b/actionpack/test/controller/record_identifier_test.rb @@ -1,6 +1,8 @@ require 'abstract_unit' class Comment + extend ActiveModel::Naming + attr_reader :id def save; @id = 1 end def new_record?; @id.nil? end diff --git a/actionpack/test/controller/redirect_test.rb b/actionpack/test/controller/redirect_test.rb index 13247f2d08..453a77e7bc 100644 --- a/actionpack/test/controller/redirect_test.rb +++ b/actionpack/test/controller/redirect_test.rb @@ -4,6 +4,7 @@ class WorkshopsController < ActionController::Base end class Workshop + extend ActiveModel::Naming attr_accessor :id, :new_record def initialize(id, new_record) diff --git a/actionpack/test/lib/controller/fake_models.rb b/actionpack/test/lib/controller/fake_models.rb index 0b30c79b10..5e63204bad 100644 --- a/actionpack/test/lib/controller/fake_models.rb +++ b/actionpack/test/lib/controller/fake_models.rb @@ -1,4 +1,6 @@ class Customer < Struct.new(:name, :id) + extend ActiveModel::Naming + def to_param id.to_s end @@ -12,6 +14,8 @@ end module Quiz class Question < Struct.new(:name, :id) + extend ActiveModel::Naming + def to_param id.to_s end diff --git a/actionpack/test/template/atom_feed_helper_test.rb b/actionpack/test/template/atom_feed_helper_test.rb index bd97caf5d7..6f1179f359 100644 --- a/actionpack/test/template/atom_feed_helper_test.rb +++ b/actionpack/test/template/atom_feed_helper_test.rb @@ -1,6 +1,7 @@ require 'abstract_unit' Scroll = Struct.new(:id, :to_param, :title, :body, :updated_at, :created_at) +Scroll.extend ActiveModel::Naming class ScrollsController < ActionController::Base FEEDS = {} diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index f9f418aec9..02b1d137f5 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -1,8 +1,10 @@ require 'abstract_unit' Bunny = Struct.new(:Bunny, :id) +Bunny.extend ActiveModel::Naming class Author + extend ActiveModel::Naming attr_reader :id def save; @id = 1 end def new_record?; @id.nil? end @@ -12,6 +14,7 @@ class Author end class Article + extend ActiveModel::Naming attr_reader :id attr_reader :author_id def save; @id = 1; @author_id = 1 end diff --git a/actionpack/test/template/record_tag_helper_test.rb b/actionpack/test/template/record_tag_helper_test.rb index 809ed6d6af..5b840d123b 100644 --- a/actionpack/test/template/record_tag_helper_test.rb +++ b/actionpack/test/template/record_tag_helper_test.rb @@ -1,6 +1,7 @@ require 'abstract_unit' class Post + extend ActiveModel::Naming def id 45 end diff --git a/actionpack/test/template/test_test.rb b/actionpack/test/template/test_test.rb index dd07a6d438..f32d0b3d42 100644 --- a/actionpack/test/template/test_test.rb +++ b/actionpack/test/template/test_test.rb @@ -41,6 +41,7 @@ class PeopleHelperTest < ActionView::TestCase def test_link_to_person person = mock(:name => "David") + person.class.extend ActiveModel::Naming expects(:mocha_mock_path).with(person).returns("/people/1") assert_equal 'David', link_to_person(person) end diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index f3d2f87b4a..f0364fd660 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -494,6 +494,7 @@ class LinkToUnlessCurrentWithControllerTest < ActionView::TestCase end class Workshop + extend ActiveModel::Naming attr_accessor :id, :new_record def initialize(id, new_record) @@ -510,6 +511,7 @@ class Workshop end class Session + extend ActiveModel::Naming attr_accessor :id, :workshop_id, :new_record def initialize(id, new_record) diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 73cee9b88f..f8e5725e9c 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -29,6 +29,8 @@ module ActiveModel autoload :Base, 'active_model/base' autoload :DeprecatedErrorMethods, 'active_model/deprecated_error_methods' autoload :Errors, 'active_model/errors' + autoload :Name, 'active_model/naming' + autoload :Naming, 'active_model/naming' autoload :Observer, 'active_model/observing' autoload :Observing, 'active_model/observing' autoload :StateMachine, 'active_model/state_machine' diff --git a/activesupport/lib/active_support/core_ext/module/model_naming.rb b/activemodel/lib/active_model/naming.rb similarity index 68% rename from activesupport/lib/active_support/core_ext/module/model_naming.rb rename to activemodel/lib/active_model/naming.rb index 13420bab07..ffb44e3824 100644 --- a/activesupport/lib/active_support/core_ext/module/model_naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -1,7 +1,7 @@ require 'active_support/inflector' -module ActiveSupport - class ModelName < String +module ActiveModel + class Name < String attr_reader :singular, :plural, :element, :collection, :partial_path alias_method :cache_key, :collection @@ -14,12 +14,12 @@ module ActiveSupport @partial_path = "#{@collection}/#{@element}".freeze end end -end -class Module - # Returns an ActiveSupport::ModelName object for module. It can be - # used to retrieve all kinds of naming-related information. - def model_name - @model_name ||= ActiveSupport::ModelName.new(name) + module Naming + # Returns an ActiveModel::Name object for module. It can be + # used to retrieve all kinds of naming-related information. + def model_name + @_model_name ||= ActiveModel::Name.new(name) + end end end diff --git a/activesupport/test/core_ext/module/model_naming_test.rb b/activemodel/test/cases/naming_test.rb similarity index 70% rename from activesupport/test/core_ext/module/model_naming_test.rb rename to activemodel/test/cases/naming_test.rb index 37119f378a..e75d4541a3 100644 --- a/activesupport/test/core_ext/module/model_naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -1,9 +1,8 @@ -require 'abstract_unit' -require 'active_support/core_ext/module/model_naming' +require 'cases/helper' -class ModelNamingTest < Test::Unit::TestCase +class NamingTest < Test::Unit::TestCase def setup - @model_name = ActiveSupport::ModelName.new('Post::TrackBack') + @model_name = ActiveModel::Name.new('Post::TrackBack') end def test_singular diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 1fc0c93732..39fac0b7e7 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -3145,6 +3145,7 @@ module ActiveRecord #:nodoc: end Base.class_eval do + extend ActiveModel::Naming extend QueryCache::ClassMethods include Validations include Locking::Optimistic, Locking::Pessimistic diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index a4f2a7e16a..f919f911e4 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -1086,6 +1086,7 @@ module ActiveResource end class Base + extend ActiveModel::Naming include CustomMethods, Validations end end diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb index 215c47b114..fbe89fe07c 100644 --- a/activesupport/lib/active_support/core_ext/module.rb +++ b/activesupport/lib/active_support/core_ext/module.rb @@ -7,5 +7,4 @@ require 'active_support/core_ext/module/attr_internal' require 'active_support/core_ext/module/attr_accessor_with_default' require 'active_support/core_ext/module/delegation' require 'active_support/core_ext/module/loading' -require 'active_support/core_ext/module/model_naming' require 'active_support/core_ext/module/synchronization'