aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/CHANGELOG.md214
-rw-r--r--activemodel/lib/active_model/attribute_methods.rb60
-rw-r--r--activemodel/lib/active_model/version.rb2
3 files changed, 35 insertions, 241 deletions
diff --git a/activemodel/CHANGELOG.md b/activemodel/CHANGELOG.md
index a1f3d081db..eb54b58888 100644
--- a/activemodel/CHANGELOG.md
+++ b/activemodel/CHANGELOG.md
@@ -1,213 +1,3 @@
-## Rails 4.0.0 (unreleased) ##
+* No changes.
-* Add `ActiveModel::Errors#full_messages_for`, to return all the error messages
- for a given attribute.
-
- Example:
-
- class Person
- include ActiveModel::Validations
-
- attr_reader :name, :email
- validates_presence_of :name, :email
- end
-
- person = Person.new
- person.valid? # => false
- person.errors.full_messages_for(:name) # => ["Name can't be blank"]
-
- *Volodymyr Shatsky*
-
-* Added a method so that validations can be easily cleared on a model.
- For example:
-
- class Person
- include ActiveModel::Validations
-
- validates_uniqueness_of :first_name
- validate :cannot_be_robot
-
- def cannot_be_robot
- errors.add(:base, 'A person cannot be a robot') if person_is_robot
- end
- end
-
- Now, if someone runs `Person.clear_validators!`, then the following occurs:
-
- Person.validators # => []
- Person._validate_callbacks.empty? # => true
-
- *John Wang*
-
-* `has_secure_password` does not fail the confirmation validation
- when assigning empty String to `password` and `password_confirmation`.
- Fixes #9535.
-
- Example:
-
- # Given User has_secure_password.
- @user.password = ""
- @user.password_confirmation = ""
- @user.valid?(:update) # used to be false
-
- *Yves Senn*
-
-* `validates_confirmation_of` does not override writer methods for
- the confirmation attribute if no reader is defined.
-
- Example:
-
- class Blog
- def title=(new_title)
- @title = new_title.downcase
- end
-
- # previously this would override the setter above.
- validates_confirmation_of :title
- end
-
- *Yves Senn*
-
-## Rails 4.0.0.beta1 (February 25, 2013) ##
-
-* Add `ActiveModel::Validations::AbsenceValidator`, a validator to check the
- absence of attributes.
-
- class Person
- include ActiveModel::Validations
-
- attr_accessor :first_name
- validates_absence_of :first_name
- end
-
- person = Person.new
- person.first_name = "John"
- person.valid?
- # => false
- person.errors.messages
- # => {:first_name=>["must be blank"]}
-
- *Roberto Vasquez Angel*
-
-* `[attribute]_changed?` now returns `false` after a call to `reset_[attribute]!`.
-
- *Renato Mascarenhas*
-
-* Observers was extracted from Active Model as `rails-observers` gem.
-
- *Rafael Mendonça França*
-
-* Specify type of singular association during serialization.
-
- *Steve Klabnik*
-
-* Fixed length validator to correctly handle `nil`. Fixes #7180.
-
- *Michal Zima*
-
-* Removed dispensable `require` statements. Make sure to require `active_model` before requiring
- individual parts of the framework.
-
- *Yves Senn*
-
-* Use BCrypt's `MIN_COST` in the test environment for speedier tests when using `has_secure_password`.
-
- *Brian Cardarella + Jeremy Kemper + Trevor Turk*
-
-* Add `ActiveModel::ForbiddenAttributesProtection`, a simple module to
- protect attributes from mass assignment when non-permitted attributes are passed.
-
- *DHH + Guillermo Iguaran*
-
-* `ActiveModel::MassAssignmentSecurity` has been extracted from Active Model and the
- `protected_attributes` gem should be added to Gemfile in order to use
- `attr_accessible` and `attr_protected` macros in your models.
-
- *Guillermo Iguaran*
-
-* Due to a change in builder, `nil` and empty strings now generate
- closed tags, so instead of this:
-
- <pseudonyms nil=\"true\"></pseudonyms>
-
- it generates this:
-
- <pseudonyms nil=\"true\"/>
-
- *Carlos Antonio da Silva*
-
-* Inclusion/exclusion validators accept a method name passed as a symbol to the
- `:in` option.
-
- This allows to use dynamic inclusion/exclusion values using methods, besides
- the current lambda/proc support.
-
- *Gabriel Sobrinho*
-
-* `ActiveModel::Validation#validates` ability to pass custom exception to the
- `:strict` option.
-
- *Bogdan Gusiev*
-
-* Changed `ActiveModel::Serializers::Xml::Serializer#add_associations` to by default
- propagate `:skip_types, :dasherize, :camelize` keys to included associations.
- It can be overridden on each association by explicitly specifying the option on one
- or more associations
-
- *Anthony Alberto*
-
-* Changed `ActiveModel::Serializers::JSON.include_root_in_json` default value to false.
- Now, AM Serializers and AR objects have the same default behaviour. Fixes #6578.
-
- class User < ActiveRecord::Base; end
-
- class Person
- include ActiveModel::Model
- include ActiveModel::AttributeMethods
- include ActiveModel::Serializers::JSON
-
- attr_accessor :name, :age
-
- def attributes
- instance_values
- end
- end
-
- user.as_json
- => {"id"=>1, "name"=>"Konata Izumi", "age"=>16, "awesome"=>true}
- # root is not included
-
- person.as_json
- => {"name"=>"Francesco", "age"=>22}
- # root is not included
-
- *Francesco Rodriguez*
-
-* Passing false hash values to `validates` will no longer enable the corresponding validators.
-
- *Steve Purcell*
-
-* `ConfirmationValidator` error messages will attach to `:#{attribute}_confirmation` instead of `attribute`.
-
- *Brian Cardarella*
-
-* Added `ActiveModel::Model`, a mixin to make Ruby objects work with AP out of box.
-
- *Guillermo Iguaran*
-
-* `AM::Errors#to_json`: support `:full_messages` parameter.
-
- *Bogdan Gusiev*
-
-* Trim down Active Model API by removing `valid?` and `errors.full_messages`.
-
- *José Valim*
-
-* When `^` or `$` are used in the regular expression provided to `validates_format_of`
- and the `:multiline` option is not set to true, an exception will be raised. This is
- to prevent security vulnerabilities when using `validates_format_of`. The problem is
- described in detail in the Rails security guide.
-
- *Jan Berdajs + Egor Homakov*
-
-Please check [3-2-stable](https://github.com/rails/rails/blob/3-2-stable/activemodel/CHANGELOG.md) for previous changes.
+Please check [4-0-stable](https://github.com/rails/rails/blob/4-0-stable/activemodel/CHANGELOG.md) for previous changes.
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb
index 6d11c0fbdc..5db898b33a 100644
--- a/activemodel/lib/active_model/attribute_methods.rb
+++ b/activemodel/lib/active_model/attribute_methods.rb
@@ -12,19 +12,21 @@ module ActiveModel
# # => ActiveModel::MissingAttributeError: missing attribute: user_id
class MissingAttributeError < NoMethodError
end
+
# == Active \Model Attribute Methods
#
# <tt>ActiveModel::AttributeMethods</tt> provides a way to add prefixes and
- # suffixes to your methods as well as handling the creation of Active Record
- # like class methods such as +table_name+.
+ # suffixes to your methods as well as handling the creation of
+ # <tt>ActiveRecord::Base</tt>-like class methods such as +table_name+.
#
- # The requirements to implement ActiveModel::AttributeMethods are to:
+ # The requirements to implement <tt>ActiveModel::AttributeMethods</tt> are to:
#
- # * <tt>include ActiveModel::AttributeMethods</tt> in your object.
- # * Call each Attribute Method module method you want to add, such as
- # +attribute_method_suffix+ or +attribute_method_prefix+.
+ # * <tt>include ActiveModel::AttributeMethods</tt> in your class.
+ # * Call each of its method you want to add, such as +attribute_method_suffix+
+ # or +attribute_method_prefix+.
# * Call +define_attribute_methods+ after the other methods are called.
# * Define the various generic +_attribute+ methods that you have declared.
+ # * Define an +attributes+ method, see below.
#
# A minimal implementation could be:
#
@@ -38,6 +40,10 @@ module ActiveModel
#
# attr_accessor :name
#
+ # def attributes
+ # {'name' => @name}
+ # end
+ #
# private
#
# def attribute_contrived?(attr)
@@ -53,10 +59,10 @@ module ActiveModel
# end
# end
#
- # Note that whenever you include ActiveModel::AttributeMethods in your class,
- # it requires you to implement an +attributes+ method which returns a hash
- # with each attribute name in your model as hash key and the attribute value as
- # hash value.
+ # Note that whenever you include <tt>ActiveModel::AttributeMethods</tt> in
+ # your class, it requires you to implement an +attributes+ method which
+ # returns a hash with each attribute name in your model as hash key and the
+ # attribute value as hash value.
#
# Hash keys must be strings.
module AttributeMethods
@@ -179,7 +185,6 @@ module ActiveModel
undefine_attribute_methods
end
-
# Allows you to make aliases for attributes.
#
# class Person
@@ -413,17 +418,16 @@ module ActiveModel
end
end
- # Allows access to the object attributes, which are held in the
- # <tt>@attributes</tt> hash, as though they were first-class methods. So a
- # Person class with a name attribute can use Person#name and Person#name=
- # and never directly use the attributes hash -- except for multiple assigns
- # with ActiveRecord#attributes=. A Milestone class can also ask
- # Milestone#completed? to test that the completed attribute is not +nil+
- # or 0.
+ # Allows access to the object attributes, which are held in the hash
+ # returned by <tt>attributes</tt>, as though they were first-class
+ # methods. So a +Person+ class with a +name+ attribute can for example use
+ # <tt>Person#name</tt> and <tt>Person#name=</tt> and never directly use
+ # the attributes hash -- except for multiple assigns with
+ # <tt>ActiveRecord::Base#attributes=</tt>.
#
- # It's also possible to instantiate related objects, so a Client class
- # belonging to the clients table with a +master_id+ foreign key can
- # instantiate master through Client#master.
+ # It's also possible to instantiate related objects, so a <tt>Client</tt>
+ # class belonging to the +clients+ table with a +master_id+ foreign key
+ # can instantiate master through <tt>Client#master</tt>.
def method_missing(method, *args, &block)
if respond_to_without_attributes?(method, true)
super
@@ -433,17 +437,17 @@ module ActiveModel
end
end
- # attribute_missing is like method_missing, but for attributes. When method_missing is
- # called we check to see if there is a matching attribute method. If so, we call
- # attribute_missing to dispatch the attribute. This method can be overloaded to
- # customize the behavior.
+ # +attribute_missing+ is like +method_missing+, but for attributes. When
+ # +method_missing+ is called we check to see if there is a matching
+ # attribute method. If so, we tell +attribute_missing+ to dispatch the
+ # attribute. This method can be overloaded to customize the behavior.
def attribute_missing(match, *args, &block)
__send__(match.target, match.attr_name, *args, &block)
end
- # A Person object with a name attribute can ask <tt>person.respond_to?(:name)</tt>,
- # <tt>person.respond_to?(:name=)</tt>, and <tt>person.respond_to?(:name?)</tt>
- # which will all return +true+.
+ # A +Person+ instance with a +name+ attribute can ask
+ # <tt>person.respond_to?(:name)</tt>, <tt>person.respond_to?(:name=)</tt>,
+ # and <tt>person.respond_to?(:name?)</tt> which will all return +true+.
alias :respond_to_without_attributes? :respond_to?
def respond_to?(method, include_private_methods = false)
if super
diff --git a/activemodel/lib/active_model/version.rb b/activemodel/lib/active_model/version.rb
index 35c4b30999..86340bba37 100644
--- a/activemodel/lib/active_model/version.rb
+++ b/activemodel/lib/active_model/version.rb
@@ -1,7 +1,7 @@
module ActiveModel
# Returns the version of the currently loaded ActiveModel as a Gem::Version
def self.version
- Gem::Version.new "4.0.0.beta1"
+ Gem::Version.new "4.1.0.beta"
end
module VERSION #:nodoc: