diff options
author | Xavier Noria <fxn@hashref.com> | 2010-07-22 01:29:18 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-07-22 01:29:18 +0200 |
commit | 919eb200a9a0969c444b938d08eb4661d41ba986 (patch) | |
tree | 329cbe815d549275cc0dc6aa0f75c18d8954f105 /activemodel | |
parent | 56669ec3048de316918ec5ad554fff83d757911b (diff) | |
parent | b456877cfb7e0cb0bab9ffd5674abd23caba0ab4 (diff) | |
download | rails-919eb200a9a0969c444b938d08eb4661d41ba986.tar.gz rails-919eb200a9a0969c444b938d08eb4661d41ba986.tar.bz2 rails-919eb200a9a0969c444b938d08eb4661d41ba986.zip |
Merge remote branch 'rails/master'
Diffstat (limited to 'activemodel')
-rw-r--r-- | activemodel/CHANGELOG | 5 | ||||
-rw-r--r-- | activemodel/README.rdoc (renamed from activemodel/README) | 0 | ||||
-rw-r--r-- | activemodel/Rakefile | 2 | ||||
-rw-r--r-- | activemodel/activemodel.gemspec | 2 | ||||
-rw-r--r-- | activemodel/lib/active_model/naming.rb | 29 | ||||
-rw-r--r-- | activemodel/test/cases/naming_test.rb | 39 | ||||
-rw-r--r-- | activemodel/test/models/contact.rb | 1 | ||||
-rw-r--r-- | activemodel/test/models/sheep.rb | 4 |
8 files changed, 80 insertions, 2 deletions
diff --git a/activemodel/CHANGELOG b/activemodel/CHANGELOG index a5e7f300d9..33602657f5 100644 --- a/activemodel/CHANGELOG +++ b/activemodel/CHANGELOG @@ -1,3 +1,8 @@ +*Rails 3.0.0 [Release Candidate] (unreleased)* + +* Added ActiveModel::MassAssignmentSecurity [Eric Chapweske, Josh Kalderimis] + + *Rails 3.0.0 [beta 4] (June 8th, 2010)* * JSON supports a custom root option: to_json(:root => 'custom') #4515 [Jatinder Singh] diff --git a/activemodel/README b/activemodel/README.rdoc index 6f162ef408..6f162ef408 100644 --- a/activemodel/README +++ b/activemodel/README.rdoc diff --git a/activemodel/Rakefile b/activemodel/Rakefile index 1dba664539..4e4bbcee96 100644 --- a/activemodel/Rakefile +++ b/activemodel/Rakefile @@ -32,7 +32,7 @@ Rake::RDocTask.new do |rdoc| rdoc.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object' rdoc.options << '--charset' << 'utf-8' rdoc.template = ENV['template'] ? "#{ENV['template']}.rb" : '../doc/template/horo' - rdoc.rdoc_files.include("README", "CHANGELOG") + rdoc.rdoc_files.include("README.rdoc", "CHANGELOG") rdoc.rdoc_files.include("lib/**/*.rb") end diff --git a/activemodel/activemodel.gemspec b/activemodel/activemodel.gemspec index 6bd6fe49ff..c483ecbc3c 100644 --- a/activemodel/activemodel.gemspec +++ b/activemodel/activemodel.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.homepage = 'http://www.rubyonrails.org' s.rubyforge_project = 'activemodel' - s.files = Dir['CHANGELOG', 'MIT-LICENSE', 'README', 'lib/**/*'] + s.files = Dir['CHANGELOG', 'MIT-LICENSE', 'README.rdoc', 'lib/**/*'] s.require_path = 'lib' s.has_rdoc = true diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index ca1e9f0ee8..dc83932dde 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -57,6 +57,35 @@ module ActiveModel def model_name @_model_name ||= ActiveModel::Name.new(self) end + + # Returns the plural class name of a record or class. Examples: + # + # ActiveModel::Naming.plural(post) # => "posts" + # ActiveModel::Naming.plural(Highrise::Person) # => "highrise_people" + def self.plural(record_or_class) + model_name_from_record_or_class(record_or_class).plural + end + + # Returns the singular class name of a record or class. Examples: + # + # ActiveModel::Naming.singular(post) # => "post" + # ActiveModel::Naming.singular(Highrise::Person) # => "highrise_person" + def self.singular(record_or_class) + model_name_from_record_or_class(record_or_class).singular + end + + # Identifies whether the class name of a record or class is uncountable. Examples: + # + # ActiveModel::Naming.uncountable?(Sheep) # => true + # ActiveModel::Naming.uncountable?(Post) => false + def self.uncountable?(record_or_class) + plural(record_or_class) == singular(record_or_class) + end + + private + def self.model_name_from_record_or_class(record_or_class) + (record_or_class.is_a?(Class) ? record_or_class : record_or_class.class).model_name + end end end diff --git a/activemodel/test/cases/naming_test.rb b/activemodel/test/cases/naming_test.rb index dc39b84ed8..5a8bff378a 100644 --- a/activemodel/test/cases/naming_test.rb +++ b/activemodel/test/cases/naming_test.rb @@ -1,4 +1,6 @@ require 'cases/helper' +require 'models/contact' +require 'models/sheep' require 'models/track_back' class NamingTest < ActiveModel::TestCase @@ -26,3 +28,40 @@ class NamingTest < ActiveModel::TestCase assert_equal 'post/track_backs/track_back', @model_name.partial_path end end + +class NamingHelpersTest < Test::Unit::TestCase + def setup + @klass = Contact + @record = @klass.new + @singular = 'contact' + @plural = 'contacts' + @uncountable = Sheep + end + + def test_singular + assert_equal @singular, singular(@record) + end + + def test_singular_for_class + assert_equal @singular, singular(@klass) + end + + def test_plural + assert_equal @plural, plural(@record) + end + + def test_plural_for_class + assert_equal @plural, plural(@klass) + end + + def test_uncountable + assert uncountable?(@uncountable), "Expected 'sheep' to be uncoutable" + assert !uncountable?(@klass), "Expected 'contact' to be countable" + end + + private + def method_missing(method, *args) + ActiveModel::Naming.send(method, *args) + end +end + diff --git a/activemodel/test/models/contact.rb b/activemodel/test/models/contact.rb index 605e435f39..f4f3078473 100644 --- a/activemodel/test/models/contact.rb +++ b/activemodel/test/models/contact.rb @@ -1,4 +1,5 @@ class Contact + extend ActiveModel::Naming include ActiveModel::Conversion attr_accessor :id, :name, :age, :created_at, :awesome, :preferences diff --git a/activemodel/test/models/sheep.rb b/activemodel/test/models/sheep.rb new file mode 100644 index 0000000000..175dbe6477 --- /dev/null +++ b/activemodel/test/models/sheep.rb @@ -0,0 +1,4 @@ +class Sheep + extend ActiveModel::Naming +end +
\ No newline at end of file |