From b95d6e84b00bd926b1118f6a820eca7a870b8c35 Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Sat, 14 Aug 2010 02:13:00 -0300 Subject: Deletes trailing whitespaces (over text files only find * -type f -exec sed 's/[ \t]*$//' -i {} \;) --- activemodel/CHANGELOG | 2 +- activemodel/README.rdoc | 54 +++++++------- activemodel/lib/active_model/attribute_methods.rb | 84 +++++++++++----------- activemodel/lib/active_model/callbacks.rb | 58 +++++++-------- activemodel/lib/active_model/conversion.rb | 12 ++-- activemodel/lib/active_model/dirty.rb | 36 +++++----- activemodel/lib/active_model/errors.rb | 60 ++++++++-------- activemodel/lib/active_model/lint.rb | 2 +- activemodel/lib/active_model/naming.rb | 10 +-- activemodel/lib/active_model/observing.rb | 14 ++-- activemodel/lib/active_model/serialization.rb | 44 ++++++------ activemodel/lib/active_model/serializers/xml.rb | 2 +- activemodel/lib/active_model/translation.rb | 14 ++-- activemodel/lib/active_model/validations.rb | 32 ++++----- .../lib/active_model/validations/acceptance.rb | 22 +++--- .../lib/active_model/validations/confirmation.rb | 20 +++--- activemodel/lib/active_model/validations/length.rb | 2 +- .../lib/active_model/validations/validates.rb | 10 +-- activemodel/lib/active_model/validator.rb | 22 +++--- activemodel/test/cases/attribute_methods_test.rb | 4 +- .../serializeration/json_serialization_test.rb | 10 +-- activemodel/test/cases/translation_test.rb | 4 +- .../test/cases/validations/validates_test.rb | 8 +-- activemodel/test/cases/validations_test.rb | 2 +- activemodel/test/models/custom_reader.rb | 4 +- activemodel/test/models/person_with_validator.rb | 2 +- activemodel/test/models/sheep.rb | 1 - 27 files changed, 267 insertions(+), 268 deletions(-) (limited to 'activemodel') diff --git a/activemodel/CHANGELOG b/activemodel/CHANGELOG index 8374853231..3ad8ac75aa 100644 --- a/activemodel/CHANGELOG +++ b/activemodel/CHANGELOG @@ -16,7 +16,7 @@ *Rails 3.0.0 [beta 2] (April 1st, 2010)* * #new_record? and #destroyed? were removed from ActiveModel::Lint. Use - persisted? instead. A model is persisted if it's not a new_record? and it was + persisted? instead. A model is persisted if it's not a new_record? and it was not destroyed? [MG] * Added validations reflection in ActiveModel::Validations [JV] diff --git a/activemodel/README.rdoc b/activemodel/README.rdoc index 73c58a87db..9b96bfaba7 100644 --- a/activemodel/README.rdoc +++ b/activemodel/README.rdoc @@ -18,38 +18,38 @@ modules: class Person include ActiveModel::AttributeMethods - + attribute_method_prefix 'clear_' define_attribute_methods [:name, :age] - + attr_accessor :name, :age - + def clear_attribute(attr) send("#{attr}=", nil) end end - + person.clear_name person.clear_age - + {Learn more}[link:classes/ActiveModel/AttributeMethods.html] - + * Callbacks for certain operations class Person extend ActiveModel::Callbacks define_model_callbacks :create - + def create _run_create_callbacks do # Your create action methods here end end end - + This generates +before_create+, +around_create+ and +after_create+ class methods that wrap your create method. - + {Learn more}[link:classes/ActiveModel/CallBacks.html] * Tracking value changes @@ -66,36 +66,36 @@ modules: person.name = 'robert' person.save person.previous_changes # => {'name' => ['bob, 'robert']} - + {Learn more}[link:classes/ActiveModel/Dirty.html] * Adding +errors+ interface to objects Exposing error messages allows objects to interact with Action Pack helpers seamlessly. - + class Person - + def initialize @errors = ActiveModel::Errors.new(self) end - + attr_accessor :name attr_reader :errors - + def validate! errors.add(:name, "can not be nil") if name == nil end - + def ErrorsPerson.human_attribute_name(attr, options = {}) "Name" end - + end - + person.errors.full_messages # => ["Name Can not be nil"] - + person.errors.full_messages # => ["Name Can not be nil"] @@ -106,7 +106,7 @@ modules: class NamedPerson extend ActiveModel::Naming end - + NamedPerson.model_name # => "NamedPerson" NamedPerson.model_name.human # => "Named person" @@ -117,19 +117,19 @@ modules: ActiveModel::Observers allows your object to implement the Observer pattern in a Rails App and take advantage of all the standard observer functions. - + {Learn more}[link:classes/ActiveModel/Observer.html] * Making objects serializable ActiveModel::Serialization provides a standard interface for your object to provide +to_json+ or +to_xml+ serialization. - + s = SerialPerson.new s.serializable_hash # => {"name"=>nil} s.to_json # => "{\"name\":null}" s.to_xml # => "\n "My attribute" - + {Learn more}[link:classes/ActiveModel/Translation.html] * Validation support @@ -160,7 +160,7 @@ modules: person.valid? # => false {Learn more}[link:classes/ActiveModel/Validations.html] - + * Custom validators class Person @@ -168,13 +168,13 @@ modules: validates_with HasNameValidator attr_accessor :name end - + class HasNameValidator < ActiveModel::Validator def validate(record) record.errors[:name] = "must exist" if record.name.blank? end end - + p = ValidatorPerson.new p.valid? # => false p.errors.full_messages # => ["Name must exist"] diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index a43436e008..b8126fb67e 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -9,46 +9,46 @@ module ActiveModel # ActiveModel::AttributeMethods 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+. - # + # # The requirements to implement ActiveModel::AttributeMethods are to: # # * include ActiveModel::AttributeMethods in your object - # * Call each Attribute Method module method you want to add, such as + # * Call each Attribute Method module 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 - # + # # A minimal implementation could be: - # + # # class Person # include ActiveModel::AttributeMethods - # + # # attribute_method_affix :prefix => 'reset_', :suffix => '_to_default!' # attribute_method_suffix '_contrived?' # attribute_method_prefix 'clear_' # define_attribute_methods ['name'] - # + # # attr_accessor :name - # + # # private - # + # # def attribute_contrived?(attr) # true # end - # + # # def clear_attribute(attr) # send("#{attr}=", nil) # end - # + # # def reset_attribute_to_default!(attr) # send("#{attr}=", "Default Name") # end # end # # Notice that whenever you include ActiveModel::AttributeMethods in your class, - # it requires you to implement a attributes methods which returns a hash - # with each attribute name in your model as hash key and the attribute value as + # it requires you to implement a attributes methods 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. @@ -57,34 +57,34 @@ module ActiveModel extend ActiveSupport::Concern module ClassMethods - # Defines an "attribute" method (like +inheritance_column+ or +table_name+). - # A new (class) method will be created with the given name. If a value is - # specified, the new method will return that value (as a string). - # Otherwise, the given block will be used to compute the value of the + # Defines an "attribute" method (like +inheritance_column+ or +table_name+). + # A new (class) method will be created with the given name. If a value is + # specified, the new method will return that value (as a string). + # Otherwise, the given block will be used to compute the value of the # method. # # The original method will be aliased, with the new name being prefixed - # with "original_". This allows the new method to access the original + # with "original_". This allows the new method to access the original # value. # # Example: # # class Person - # + # # include ActiveModel::AttributeMethods - # + # # cattr_accessor :primary_key # cattr_accessor :inheritance_column - # + # # define_attr_method :primary_key, "sysid" # define_attr_method( :inheritance_column ) do # original_inheritance_column + "_id" # end - # + # # end - # + # # Provides you with: - # + # # AttributePerson.primary_key # # => "sysid" # AttributePerson.inheritance_column = 'address' @@ -118,20 +118,20 @@ module ActiveModel # # #{prefix}attribute(#{attr}, *args, &block) # - # An instance method #{prefix}attribute must exist and accept + # An instance method #{prefix}attribute must exist and accept # at least the +attr+ argument. # # For example: # # class Person - # + # # include ActiveModel::AttributeMethods # attr_accessor :name # attribute_method_prefix 'clear_' # define_attribute_methods [:name] # # private - # + # # def clear_attribute(attr) # send("#{attr}=", nil) # end @@ -162,14 +162,14 @@ module ActiveModel # For example: # # class Person - # + # # include ActiveModel::AttributeMethods # attr_accessor :name # attribute_method_suffix '_short?' # define_attribute_methods [:name] # # private - # + # # def attribute_short?(attr) # send(attr).length < 5 # end @@ -200,14 +200,14 @@ module ActiveModel # For example: # # class Person - # + # # include ActiveModel::AttributeMethods # attr_accessor :name # attribute_method_affix :prefix => 'reset_', :suffix => '_to_default!' # define_attribute_methods [:name] # # private - # + # # def reset_attribute_to_default!(attr) # ... # end @@ -232,15 +232,15 @@ module ActiveModel end end - # Declares a the attributes that should be prefixed and suffixed by + # Declares a the attributes that should be prefixed and suffixed by # ActiveModel::AttributeMethods. - # + # # To use, pass in an array of attribute names (as strings or symbols), # be sure to declare +define_attribute_methods+ after you define any # prefix, suffix or affix methods, or they will not hook in. - # + # # class Person - # + # # include ActiveModel::AttributeMethods # attr_accessor :name, :age, :address # attribute_method_prefix 'clear_' @@ -251,7 +251,7 @@ module ActiveModel # define_attribute_methods [:name, :age, :address] # # private - # + # # def clear_attribute(attr) # ... # end @@ -344,16 +344,16 @@ module ActiveModel end end - # Allows access to the object attributes, which are held in the - # @attributes hash, as though they were first-class methods. So a - # Person class with a name attribute can use Person#name and Person#name= + # Allows access to the object attributes, which are held in the + # @attributes 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+ + # with ActiveRecord#attributes=. A Milestone class can also ask + # Milestone#completed? to test that the completed attribute is not +nil+ # or 0. # - # It's also possible to instantiate related objects, so a Client class - # belonging to the clients table with a +master_id+ foreign key can + # 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. def method_missing(method_id, *args, &block) method_name = method_id.to_s diff --git a/activemodel/lib/active_model/callbacks.rb b/activemodel/lib/active_model/callbacks.rb index b150fc60f7..aaa41f5ec6 100644 --- a/activemodel/lib/active_model/callbacks.rb +++ b/activemodel/lib/active_model/callbacks.rb @@ -3,49 +3,49 @@ require 'active_support/callbacks' module ActiveModel # == Active Model Callbacks - # + # # Provides an interface for any class to have Active Record like callbacks. - # + # # Like the Active Record methods, the callback chain is aborted as soon as # one of the methods in the chain returns false. # # First, extend ActiveModel::Callbacks from the class you are creating: - # + # # class MyModel # extend ActiveModel::Callbacks # end - # + # # Then define a list of methods that you want callbacks attached to: - # + # # define_model_callbacks :create, :update - # + # # This will provide all three standard callbacks (before, around and after) for - # both the :create and :update methods. To implement, you need to wrap the methods + # both the :create and :update methods. To implement, you need to wrap the methods # you want callbacks on in a block so that the callbacks get a chance to fire: - # + # # def create # _run_create_callbacks do # # Your create action methods here # end # end - # + # # The _run__callbacks methods are dynamically created when you extend # the ActiveModel::Callbacks module. - # + # # Then in your class, you can use the +before_create+, +after_create+ and +around_create+ # methods, just as you would in an Active Record module. - # + # # before_create :action_before_create - # + # # def action_before_create # # Your code here # end - # - # You can choose not to have all three callbacks by passing a hash to the + # + # You can choose not to have all three callbacks by passing a hash to the # define_model_callbacks method. - # + # # define_model_callbacks :create, :only => :after, :before - # + # # Would only create the after_create and before_create callback methods in your # class. module Callbacks @@ -56,44 +56,44 @@ module ActiveModel end # define_model_callbacks accepts the same options define_callbacks does, in case - # you want to overwrite a default. Besides that, it also accepts an :only option, + # you want to overwrite a default. Besides that, it also accepts an :only option, # where you can choose if you want all types (before, around or after) or just some. # # define_model_callbacks :initializer, :only => :after - # + # # Note, the :only => hash will apply to all callbacks defined on # that method call. To get around this you can call the define_model_callbacks # method as many times as you need. - # + # # define_model_callbacks :create, :only => :after # define_model_callbacks :update, :only => :before # define_model_callbacks :destroy, :only => :around - # + # # Would create +after_create+, +before_update+ and +around_destroy+ methods only. - # + # # You can pass in a class to before_, after_ and around_, in which # case the callback will call that class's _ method passing the object # that the callback is being called on. - # + # # class MyModel # extend ActiveModel::Callbacks # define_model_callbacks :create - # + # # before_create AnotherClass # end - # + # # class AnotherClass # def self.before_create( obj ) # # obj is the MyModel instance that the callback is being called on # end # end - # + # def define_model_callbacks(*callbacks) options = callbacks.extract_options! - options = { - :terminator => "result == false", - :scope => [:kind, :name], - :only => [:before, :around, :after] + options = { + :terminator => "result == false", + :scope => [:kind, :name], + :only => [:before, :around, :after] }.merge(options) types = Array.wrap(options.delete(:only)) diff --git a/activemodel/lib/active_model/conversion.rb b/activemodel/lib/active_model/conversion.rb index d2bd160dc7..b998a59e81 100644 --- a/activemodel/lib/active_model/conversion.rb +++ b/activemodel/lib/active_model/conversion.rb @@ -22,18 +22,18 @@ module ActiveModel # cm.to_param # => nil # module Conversion - # If your object is already designed to implement all of the Active Model - # you can use the default to_model implementation, which simply returns + # If your object is already designed to implement all of the Active Model + # you can use the default to_model implementation, which simply returns # self. - # - # If your model does not act like an Active Model object, then you should - # define :to_model yourself returning a proxy object that wraps + # + # If your model does not act like an Active Model object, then you should + # define :to_model yourself returning a proxy object that wraps # your object with Active Model compliant methods. def to_model self end - # Returns an Enumerable of all (primary) key attributes or nil if + # Returns an Enumerable of all (primary) key attributes or nil if # persisted? is false def to_key persisted? ? [id] : nil diff --git a/activemodel/lib/active_model/dirty.rb b/activemodel/lib/active_model/dirty.rb index 2516377afd..1361d327b3 100644 --- a/activemodel/lib/active_model/dirty.rb +++ b/activemodel/lib/active_model/dirty.rb @@ -6,48 +6,48 @@ require 'active_support/core_ext/object/duplicable' module ActiveModel # == Active Model Dirty # - # Provides a way to track changes in your object in the same way as + # Provides a way to track changes in your object in the same way as # Active Record does. - # + # # The requirements to implement ActiveModel::Dirty are to: # # * include ActiveModel::Dirty in your object - # * Call define_attribute_methods passing each method you want to + # * Call define_attribute_methods passing each method you want to # track - # * Call attr_name_will_change! before each change to the tracked + # * Call attr_name_will_change! before each change to the tracked # attribute - # - # If you wish to also track previous changes on save or update, you need to + # + # If you wish to also track previous changes on save or update, you need to # add - # + # # @previously_changed = changes - # + # # inside of your save or update method. - # + # # A minimal implementation could be: - # + # # class Person - # + # # include ActiveModel::Dirty - # + # # define_attribute_methods [:name] - # + # # def name # @name # end - # + # # def name=(val) # name_will_change! unless val == @name # @name = val # end - # + # # def save # @previously_changed = changes # @changed_attributes.clear # end - # + # # end - # + # # == Examples: # # A newly instantiated object is unchanged: @@ -79,7 +79,7 @@ module ActiveModel # person.changes # => { 'name' => ['Bill', 'Bob'] } # # If an attribute is modified in-place then make use of [attribute_name]_will_change! - # to mark that the attribute is changing. Otherwise ActiveModel can't track changes to + # to mark that the attribute is changing. Otherwise ActiveModel can't track changes to # in-place attributes. # # person.name_will_change! diff --git a/activemodel/lib/active_model/errors.rb b/activemodel/lib/active_model/errors.rb index 272ddb1554..14312283d1 100644 --- a/activemodel/lib/active_model/errors.rb +++ b/activemodel/lib/active_model/errors.rb @@ -12,50 +12,50 @@ module ActiveModel # # Provides a modified +OrderedHash+ that you can include in your object # for handling error messages and interacting with Action Pack helpers. - # + # # A minimal implementation could be: - # + # # class Person - # + # # # Required dependency for ActiveModel::Errors # extend ActiveModel::Naming - # + # # def initialize # @errors = ActiveModel::Errors.new(self) # end - # + # # attr_accessor :name # attr_reader :errors - # + # # def validate! # errors.add(:name, "can not be nil") if name == nil # end - # + # # # The following methods are needed to be minimally implemented # # def read_attribute_for_validation(attr) # send(attr) # end - # + # # def Person.human_attribute_name(attr, options = {}) # attr # end - # + # # def Person.lookup_ancestors # [self] # end - # + # # end - # + # # The last three methods are required in your object for Errors to be # able to generate error messages correctly and also handle multiple # languages. Of course, if you extend your object with ActiveModel::Translations # you will not need to implement the last two. Likewise, using # ActiveModel::Validations will handle the validation related methods # for you. - # + # # The above allows you to do: - # + # # p = Person.new # p.validate! # => ["can not be nil"] # p.errors.full_messages # => ["name can not be nil"] @@ -66,7 +66,7 @@ module ActiveModel CALLBACKS_OPTIONS = [:if, :unless, :on, :allow_nil, :allow_blank] # Pass in the instance of the object that is using the errors object. - # + # # class Person # def initialize # @errors = ActiveModel::Errors.new(self) @@ -80,9 +80,9 @@ module ActiveModel alias_method :get, :[] alias_method :set, :[]= - # When passed a symbol or a name of a method, returns an array of errors + # When passed a symbol or a name of a method, returns an array of errors # for the method. - # + # # p.errors[:name] # => ["can not be nil"] # p.errors['name'] # => ["can not be nil"] def [](attribute) @@ -90,7 +90,7 @@ module ActiveModel end # Adds to the supplied attribute the supplied error message. - # + # # p.errors[:name] = "must be set" # p.errors[:name] # => ['must be set'] def []=(attribute, error) @@ -100,12 +100,12 @@ module ActiveModel # Iterates through each error key, value pair in the error messages hash. # Yields the attribute and the error for that attribute. If the attribute # has more than one error message, yields once for each error message. - # + # # p.errors.add(:name, "can't be blank") # p.errors.each do |attribute, errors_array| # # Will yield :name and "can't be blank" # end - # + # # p.errors.add(:name, "must be specified") # p.errors.each do |attribute, errors_array| # # Will yield :name and "can't be blank" @@ -118,7 +118,7 @@ module ActiveModel end # Returns the number of error messages. - # + # # p.errors.add(:name, "can't be blank") # p.errors.size # => 1 # p.errors.add(:name, "must be specified") @@ -128,7 +128,7 @@ module ActiveModel end # Returns an array of error messages, with the attribute name included - # + # # p.errors.add(:name, "can't be blank") # p.errors.add(:name, "must be specified") # p.errors.to_a # => ["name can't be blank", "name must be specified"] @@ -151,7 +151,7 @@ module ActiveModel end # Returns an xml formatted representation of the Errors hash. - # + # # p.errors.add(:name, "can't be blank") # p.errors.add(:name, "must be specified") # p.errors.to_xml @@ -167,14 +167,14 @@ module ActiveModel # Returns an ActiveSupport::OrderedHash that can be used as the JSON representation for this object. def as_json(options=nil) - self + self end # Adds +message+ to the error messages on +attribute+, which will be returned on a call to # on(attribute) for the same attribute. More than one error can be added to the same # +attribute+ in which case an array will be returned on a call to on(attribute). # If no +message+ is supplied, :invalid is assumed. - # + # # If +message+ is a symbol, it will be translated using the appropriate scope (see +translate_error+). # If +message+ is a proc, it will be called, allowing for things like Time.now to be used within an error. def add(attribute, message = nil, options = {}) @@ -253,19 +253,19 @@ module ActiveModel full_messages end - # Translates an error message in its default scope + # Translates an error message in its default scope # (activemodel.errors.messages). # - # Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, - # if it's not there, it's looked up in models.MODEL.MESSAGE and if that is not - # there also, it returns the translation of the default message + # Error messages are first looked up in models.MODEL.attributes.ATTRIBUTE.MESSAGE, + # if it's not there, it's looked up in models.MODEL.MESSAGE and if that is not + # there also, it returns the translation of the default message # (e.g. activemodel.errors.messages.MESSAGE). The translated model name, # translated attribute name and the value are available for interpolation. # # When using inheritance in your models, it will check all the inherited # models too, but only if the model itself hasn't been found. Say you have - # class Admin < User; end and you wanted the translation for - # the :blank error +message+ for the title +attribute+, + # class Admin < User; end and you wanted the translation for + # the :blank error +message+ for the title +attribute+, # it looks for these translations: # #
    diff --git a/activemodel/lib/active_model/lint.rb b/activemodel/lib/active_model/lint.rb index fe650053d9..26eb4a3c41 100644 --- a/activemodel/lib/active_model/lint.rb +++ b/activemodel/lib/active_model/lint.rb @@ -79,7 +79,7 @@ module ActiveModel end # == Errors Testing - # + # # Returns an object that has :[] and :full_messages defined on it. See below # for more details. # diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb index b74d669f0a..d79635cfb3 100644 --- a/activemodel/lib/active_model/naming.rb +++ b/activemodel/lib/active_model/naming.rb @@ -41,16 +41,16 @@ module ActiveModel # == Active Model Naming # # Creates a +model_name+ method on your object. - # + # # To implement, just extend ActiveModel::Naming in your object: - # + # # class BookCover # extend ActiveModel::Naming # end - # + # # BookCover.model_name # => "BookCover" # BookCover.model_name.human # => "Book cover" - # + # # Providing the functionality that ActiveModel::Naming provides in your object # is required to pass the Active Model Lint test. So either extending the provided # method below, or rolling your own is required.. @@ -90,5 +90,5 @@ module ActiveModel (record_or_class.is_a?(Class) ? record_or_class : record_or_class.class).model_name end end - + end diff --git a/activemodel/lib/active_model/observing.rb b/activemodel/lib/active_model/observing.rb index c6a79acf81..62d2694da5 100644 --- a/activemodel/lib/active_model/observing.rb +++ b/activemodel/lib/active_model/observing.rb @@ -10,7 +10,7 @@ module ActiveModel module ClassMethods # == Active Model Observers Activation - # + # # Activates the observers assigned. Examples: # # # Calls PersonObserver.instance @@ -22,7 +22,7 @@ module ActiveModel # # Same as above, just using explicit class references # ActiveRecord::Base.observers = Cacher, GarbageCollector # - # Note: Setting this does not instantiate the observers yet. + # Note: Setting this does not instantiate the observers yet. # +instantiate_observers+ is called during startup, and before # each development request. def observers=(*values) @@ -123,9 +123,9 @@ module ActiveModel # # Observers will by default be mapped to the class with which they share a # name. So CommentObserver will be tied to observing Comment, ProductManagerObserver - # to ProductManager, and so on. If you want to name your observer differently than - # the class you're interested in observing, you can use the Observer.observe class - # method which takes either the concrete class (Product) or a symbol for that + # to ProductManager, and so on. If you want to name your observer differently than + # the class you're interested in observing, you can use the Observer.observe class + # method which takes either the concrete class (Product) or a symbol for that # class (:product): # # class AuditObserver < ActiveModel::Observer @@ -136,7 +136,7 @@ module ActiveModel # end # end # - # If the audit observer needs to watch more than one kind of object, this can be + # If the audit observer needs to watch more than one kind of object, this can be # specified with multiple arguments: # # class AuditObserver < ActiveModel::Observer @@ -147,7 +147,7 @@ module ActiveModel # end # end # - # The AuditObserver will now act on both updates to Account and Balance by treating + # The AuditObserver will now act on both updates to Account and Balance by treating # them both as records. # class Observer diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index e675937f4d..d4c6c15e01 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -3,25 +3,25 @@ require 'active_support/core_ext/hash/slice' module ActiveModel # == Active Model Serialization - # + # # Provides a basic serialization to a serializable_hash for your object. - # + # # A minimal implementation could be: - # + # # class Person - # + # # include ActiveModel::Serialization - # + # # attr_accessor :name - # + # # def attributes # @attributes ||= {'name' => 'nil'} # end - # + # # end - # + # # Which would provide you with: - # + # # person = Person.new # person.serializable_hash # => {"name"=>nil} # person.name = "Bob" @@ -29,40 +29,40 @@ module ActiveModel # # You need to declare some sort of attributes hash which contains the attributes # you want to serialize and their current value. - # - # Most of the time though, you will want to include the JSON or XML - # serializations. Both of these modules automatically include the + # + # Most of the time though, you will want to include the JSON or XML + # serializations. Both of these modules automatically include the # ActiveModel::Serialization module, so there is no need to explicitly # include it. - # + # # So a minimal implementation including XML and JSON would be: - # + # # class Person - # + # # include ActiveModel::Serializers::JSON # include ActiveModel::Serializers::Xml - # + # # attr_accessor :name - # + # # def attributes # @attributes ||= {'name' => 'nil'} # end - # + # # end - # + # # Which would provide you with: - # + # # person = Person.new # person.serializable_hash # => {"name"=>nil} # person.to_json # => "{\"name\":null}" # person.to_xml # => "\n {"name"=>"Bob"} # person.to_json # => "{\"name\":\"Bob\"}" # person.to_xml # => "\n:only, :except and :methods . + # Valid options are :only, :except and :methods . module Serialization def serializable_hash(options = nil) options ||= {} diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index ed64434b8f..16ab8e7928 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -52,7 +52,7 @@ module ActiveModel @options[:except] = Array.wrap(@options[:except]).map { |n| n.to_s } end - # To replicate the behavior in ActiveRecord#attributes, :except + # To replicate the behavior in ActiveRecord#attributes, :except # takes precedence over :only. If :only is not set # for a N level model but is set for the N+1 level models, # then because :except is set to a default value, the second diff --git a/activemodel/lib/active_model/translation.rb b/activemodel/lib/active_model/translation.rb index 0facbd6ce1..6c1cecd9b7 100644 --- a/activemodel/lib/active_model/translation.rb +++ b/activemodel/lib/active_model/translation.rb @@ -3,19 +3,19 @@ require 'active_support/core_ext/hash/reverse_merge' module ActiveModel # == Active Model Translation - # + # # Provides integration between your object and the Rails internationalization # (i18n) framework. - # + # # A minimal implementation could be: - # + # # class TranslatedPerson # extend ActiveModel::Translation # end - # + # # TranslatedPerson.human_attribute_name('my_attribute') # # => "My attribute" - # + # # This also provides the required class methods for hooking into the # Rails internationalization API, including being able to define a # class based i18n_scope and lookup_ancestors to find translations in @@ -28,9 +28,9 @@ module ActiveModel :activemodel end - # When localizing a string, it goes through the lookup returned by this + # When localizing a string, it goes through the lookup returned by this # method, which is used in ActiveModel::Name#human, - # ActiveModel::Errors#full_messages and + # ActiveModel::Errors#full_messages and # ActiveModel::Translation#human_attribute_name. def lookup_ancestors self.ancestors.select { |x| x.respond_to?(:model_name) } diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb index 37429f2bb8..cd37925292 100644 --- a/activemodel/lib/active_model/validations.rb +++ b/activemodel/lib/active_model/validations.rb @@ -9,24 +9,24 @@ require 'active_model/validations/callbacks' module ActiveModel # == Active Model Validations - # + # # Provides a full validation framework to your objects. - # + # # A minimal implementation could be: - # + # # class Person # include ActiveModel::Validations - # + # # attr_accessor :first_name, :last_name # # validates_each :first_name, :last_name do |record, attr, value| # record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z # end # end - # + # # Which provides you with the full standard validation stack that you # know from Active Record: - # + # # person = Person.new # person.valid? # => true # person.invalid? # => false @@ -35,11 +35,11 @@ module ActiveModel # person.valid? # => false # person.invalid? # => true # person.errors # => #["starts with z."]}> - # + # # Note that ActiveModel::Validations automatically adds an +errors+ method # to your instances initialized with a new ActiveModel::Errors object, so # there is no need for you to do this manually. - # + # module Validations extend ActiveSupport::Concern include ActiveSupport::Callbacks @@ -62,7 +62,7 @@ module ActiveModel # # class Person # include ActiveModel::Validations - # + # # attr_accessor :first_name, :last_name # # validates_each :first_name, :last_name do |record, attr, value| @@ -71,7 +71,7 @@ module ActiveModel # end # # Options: - # * :on - Specifies when this validation is active (default is + # * :on - Specifies when this validation is active (default is # :save, other options :create, :update). # * :allow_nil - Skip validation if attribute is +nil+. # * :allow_blank - Skip validation if attribute is blank. @@ -96,7 +96,7 @@ module ActiveModel # # class Comment # include ActiveModel::Validations - # + # # validate :must_be_friends # # def must_be_friends @@ -129,7 +129,7 @@ module ActiveModel set_callback(:validate, *args, &block) end - # List all validators that are being used to validate the model using + # List all validators that are being used to validate the model using # +validates_with+ method. def validators _validators.values.flatten.uniq @@ -169,14 +169,14 @@ module ActiveModel self.validation_context = current_context end - # Performs the opposite of valid?. Returns true if errors were added, + # Performs the opposite of valid?. Returns true if errors were added, # false otherwise. def invalid?(context = nil) !valid?(context) end - # Hook method defining how an attribute value should be retrieved. By default - # this is assumed to be an instance named after the attribute. Override this + # Hook method defining how an attribute value should be retrieved. By default + # this is assumed to be an instance named after the attribute. Override this # method in subclasses should you need to retrieve the value for a given # attribute differently: # @@ -195,7 +195,7 @@ module ActiveModel alias :read_attribute_for_validation :send protected - + def run_validations! _run_validate_callbacks errors.empty? diff --git a/activemodel/lib/active_model/validations/acceptance.rb b/activemodel/lib/active_model/validations/acceptance.rb index 99b8966def..d9dfe0c855 100644 --- a/activemodel/lib/active_model/validations/acceptance.rb +++ b/activemodel/lib/active_model/validations/acceptance.rb @@ -24,7 +24,7 @@ module ActiveModel end module HelperMethods - # Encapsulates the pattern of wanting to validate the acceptance of a + # Encapsulates the pattern of wanting to validate the acceptance of a # terms of service check box (or similar agreement). Example: # # class Person < ActiveRecord::Base @@ -32,33 +32,33 @@ module ActiveModel # validates_acceptance_of :eula, :message => "must be abided" # end # - # If the database column does not exist, the +terms_of_service+ attribute + # If the database column does not exist, the +terms_of_service+ attribute # is entirely virtual. This check is performed only if +terms_of_service+ # is not +nil+ and by default on save. # # Configuration options: - # * :message - A custom error message (default is: "must be + # * :message - A custom error message (default is: "must be # accepted"). # * :on - Specifies when this validation is active (default is - # :save, other options are :create and + # :save, other options are :create and # :update). # * :allow_nil - Skip validation if attribute is +nil+ (default # is true). - # * :accept - Specifies value that is considered accepted. + # * :accept - Specifies value that is considered accepted. # The default value is a string "1", which makes it easy to relate to - # an HTML checkbox. This should be set to +true+ if you are validating + # an HTML checkbox. This should be set to +true+ if you are validating # a database column, since the attribute is typecast from "1" to +true+ # before validation. # * :if - Specifies a method, proc or string to call to determine # if the validation should occur (e.g. :if => :allow_validation, # or :if => Proc.new { |user| user.signup_step > 2 }). The - # method, proc or string should return or evaluate to a true or false + # method, proc or string should return or evaluate to a true or false # value. - # * :unless - Specifies a method, proc or string to call to - # determine if the validation should not occur (for example, - # :unless => :skip_validation, or + # * :unless - Specifies a method, proc or string to call to + # determine if the validation should not occur (for example, + # :unless => :skip_validation, or # :unless => Proc.new { |user| user.signup_step <= 2 }). - # The method, proc or string should return or evaluate to a true or + # The method, proc or string should return or evaluate to a true or # false value. def validates_acceptance_of(*attr_names) validates_with AcceptanceValidator, _merge_attributes(attr_names) diff --git a/activemodel/lib/active_model/validations/confirmation.rb b/activemodel/lib/active_model/validations/confirmation.rb index 3a80893866..a31966d0c2 100644 --- a/activemodel/lib/active_model/validations/confirmation.rb +++ b/activemodel/lib/active_model/validations/confirmation.rb @@ -15,13 +15,13 @@ module ActiveModel end module HelperMethods - # Encapsulates the pattern of wanting to validate a password or email + # Encapsulates the pattern of wanting to validate a password or email # address field with a confirmation. For example: # # Model: # class Person < ActiveRecord::Base # validates_confirmation_of :user_name, :password - # validates_confirmation_of :email_address, + # validates_confirmation_of :email_address, # :message => "should match confirmation" # end # @@ -29,12 +29,12 @@ module ActiveModel # <%= password_field "person", "password" %> # <%= password_field "person", "password_confirmation" %> # - # The added +password_confirmation+ attribute is virtual; it exists only + # The added +password_confirmation+ attribute is virtual; it exists only # as an in-memory attribute for validating the password. To achieve this, - # the validation adds accessors to the model for the confirmation + # the validation adds accessors to the model for the confirmation # attribute. - # - # NOTE: This check is performed only if +password_confirmation+ is not + # + # NOTE: This check is performed only if +password_confirmation+ is not # +nil+, and by default only on save. To require confirmation, make sure # to add a presence check for the confirmation attribute: # @@ -48,11 +48,11 @@ module ActiveModel # * :if - Specifies a method, proc or string to call to determine # if the validation should occur (e.g. :if => :allow_validation, # or :if => Proc.new { |user| user.signup_step > 2 }). The - # method, proc or string should return or evaluate to a true or false + # method, proc or string should return or evaluate to a true or false # value. - # * :unless - Specifies a method, proc or string to call to - # determine if the validation should not occur (e.g. - # :unless => :skip_validation, or + # * :unless - Specifies a method, proc or string to call to + # determine if the validation should not occur (e.g. + # :unless => :skip_validation, or # :unless => Proc.new { |user| user.signup_step <= 2 }). The # method, proc or string should return or evaluate to a true or false value. def validates_confirmation_of(*attr_names) diff --git a/activemodel/lib/active_model/validations/length.rb b/activemodel/lib/active_model/validations/length.rb index ecae73a66e..5a46ecb4ac 100644 --- a/activemodel/lib/active_model/validations/length.rb +++ b/activemodel/lib/active_model/validations/length.rb @@ -34,7 +34,7 @@ module ActiveModel end end end - + def validate_each(record, attribute, value) value = options[:tokenizer].call(value) if value.kind_of?(String) diff --git a/activemodel/lib/active_model/validations/validates.rb b/activemodel/lib/active_model/validations/validates.rb index 3260e6bc5a..3242e49269 100644 --- a/activemodel/lib/active_model/validations/validates.rb +++ b/activemodel/lib/active_model/validations/validates.rb @@ -9,7 +9,7 @@ module ActiveModel # validator classes ending in 'Validator'. Note that Rails default # validators can be overridden inside specific classes by creating # custom validator classes in their place such as PresenceValidator. - # + # # Examples of using the default rails validators: # # validates :terms, :acceptance => true @@ -21,7 +21,7 @@ module ActiveModel # validates :age, :numericality => true # validates :username, :presence => true # validates :username, :uniqueness => true - # + # # The power of the +validates+ method comes when using custom validators # and default validators in one call for a given attribute e.g. # @@ -31,15 +31,15 @@ module ActiveModel # value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i # end # end - # + # # class Person # include ActiveModel::Validations # attr_accessor :name, :email - # + # # validates :name, :presence => true, :uniqueness => true, :length => { :maximum => 100 } # validates :email, :presence => true, :email => true # end - # + # # Validator classes may also exist within the class being validated # allowing custom modules of validators to be included as needed e.g. # diff --git a/activemodel/lib/active_model/validator.rb b/activemodel/lib/active_model/validator.rb index 163124d531..1262d305d1 100644 --- a/activemodel/lib/active_model/validator.rb +++ b/activemodel/lib/active_model/validator.rb @@ -5,8 +5,8 @@ require 'active_support/core_ext/object/blank' module ActiveModel #:nodoc: # == Active Model Validator - # - # A simple base class that can be used along with + # + # A simple base class that can be used along with # +ActiveModel::Validations::ClassMethods.validates_with+ # # class Person @@ -61,31 +61,31 @@ module ActiveModel #:nodoc: # @my_custom_field = options[:field_name] || :first_name # end # end - # + # # The easiest way to add custom validators for validating individual attributes # is with the convenient ActiveModel::EachValidator for example: - # + # # class TitleValidator < ActiveModel::EachValidator # def validate_each(record, attribute, value) # record.errors[attribute] << 'must be Mr. Mrs. or Dr.' unless ['Mr.', 'Mrs.', 'Dr.'].include?(value) # end # end - # + # # This can now be used in combination with the +validates+ method # (see ActiveModel::Validations::ClassMethods.validates for more on this) - # + # # class Person # include ActiveModel::Validations # attr_accessor :title - # + # # validates :title, :presence => true, :title => true # end - # + # # Validator may also define a +setup+ instance method which will get called # with the class that using that validator as it's argument. This can be # useful when there are prerequisites such as an attr_accessor being present # for example: - # + # # class MyValidator < ActiveModel::Validator # def setup(klass) # klass.send :attr_accessor, :custom_attribute @@ -94,7 +94,7 @@ module ActiveModel #:nodoc: # # This setup method is only called when used with validation macros or the # class level validates_with method. - # + # class Validator attr_reader :options @@ -133,7 +133,7 @@ module ActiveModel #:nodoc: # All Active Model validations are built on top of this Validator. class EachValidator < Validator attr_reader :attributes - + # Returns a new validator instance. All options will be available via the # +options+ reader, however the :attributes option will be removed # and instead be made available through the +attributes+ reader. diff --git a/activemodel/test/cases/attribute_methods_test.rb b/activemodel/test/cases/attribute_methods_test.rb index c60caf90d7..54cf8380ab 100644 --- a/activemodel/test/cases/attribute_methods_test.rb +++ b/activemodel/test/cases/attribute_methods_test.rb @@ -2,7 +2,7 @@ require 'cases/helper' class ModelWithAttributes include ActiveModel::AttributeMethods - + attribute_method_suffix '' def attributes @@ -17,7 +17,7 @@ end class ModelWithAttributes2 include ActiveModel::AttributeMethods - + attribute_method_suffix '_test' end diff --git a/activemodel/test/cases/serializeration/json_serialization_test.rb b/activemodel/test/cases/serializeration/json_serialization_test.rb index 1ac991a8f1..91e123e655 100644 --- a/activemodel/test/cases/serializeration/json_serialization_test.rb +++ b/activemodel/test/cases/serializeration/json_serialization_test.rb @@ -105,15 +105,15 @@ class JsonSerializationTest < ActiveModel::TestCase test "should return OrderedHash for errors" do car = Automobile.new - + # run the validation - car.valid? - + car.valid? + hash = ActiveSupport::OrderedHash.new hash[:make] = "can't be blank" hash[:model] = "is too short (minimum is 2 characters)" assert_equal hash.to_json, car.errors.to_json end - - + + end diff --git a/activemodel/test/cases/translation_test.rb b/activemodel/test/cases/translation_test.rb index e25d308ca1..d6942a5475 100644 --- a/activemodel/test/cases/translation_test.rb +++ b/activemodel/test/cases/translation_test.rb @@ -6,7 +6,7 @@ class ActiveModelI18nTests < ActiveModel::TestCase def setup I18n.backend = I18n::Backend::Simple.new end - + def test_translated_model_attributes I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } } assert_equal 'person name attribute', Person.human_attribute_name('name') @@ -16,7 +16,7 @@ class ActiveModelI18nTests < ActiveModel::TestCase I18n.backend.store_translations 'en', :attributes => { :name => 'name default attribute' } assert_equal 'name default attribute', Person.human_attribute_name('name') end - + def test_translated_model_attributes_with_symbols I18n.backend.store_translations 'en', :activemodel => {:attributes => {:person => {:name => 'person name attribute'} } } assert_equal 'person name attribute', Person.human_attribute_name(:name) diff --git a/activemodel/test/cases/validations/validates_test.rb b/activemodel/test/cases/validations/validates_test.rb index b85e491a9c..db023f6169 100644 --- a/activemodel/test/cases/validations/validates_test.rb +++ b/activemodel/test/cases/validations/validates_test.rb @@ -26,7 +26,7 @@ class ValidatesTest < ActiveModel::TestCase person.valid? assert_equal ['my custom message'], person.errors[:salary] end - + def test_validates_with_validator_class Person.validates :karma, :email => true person = Person.new @@ -93,18 +93,18 @@ class ValidatesTest < ActiveModel::TestCase person.valid? assert_equal ['my custom message'], person.errors[:karma] end - + def test_validates_with_unknown_validator assert_raise(ArgumentError) { Person.validates :karma, :unknown => true } end - + def test_validates_with_included_validator PersonWithValidator.validates :title, :presence => true person = PersonWithValidator.new person.valid? assert_equal ['Local validator'], person.errors[:title] end - + def test_validates_with_included_validator_and_options PersonWithValidator.validates :title, :presence => { :custom => ' please' } person = PersonWithValidator.new diff --git a/activemodel/test/cases/validations_test.rb b/activemodel/test/cases/validations_test.rb index 8d6bdeb6a5..f9fc6613f4 100644 --- a/activemodel/test/cases/validations_test.rb +++ b/activemodel/test/cases/validations_test.rb @@ -170,7 +170,7 @@ class ValidationsTest < ActiveModel::TestCase assert_match %r{}, xml assert_match %r{Title can't be blank}, xml assert_match %r{Content can't be blank}, xml - + hash = ActiveSupport::OrderedHash.new hash[:title] = "can't be blank" hash[:content] = "can't be blank" diff --git a/activemodel/test/models/custom_reader.rb b/activemodel/test/models/custom_reader.rb index 14a8be9ebc..2fbcf79c3d 100644 --- a/activemodel/test/models/custom_reader.rb +++ b/activemodel/test/models/custom_reader.rb @@ -4,11 +4,11 @@ class CustomReader def initialize(data = {}) @data = data end - + def []=(key, value) @data[key] = value end - + def read_attribute_for_validation(key) @data[key] end diff --git a/activemodel/test/models/person_with_validator.rb b/activemodel/test/models/person_with_validator.rb index f9763ea853..f6f665ccee 100644 --- a/activemodel/test/models/person_with_validator.rb +++ b/activemodel/test/models/person_with_validator.rb @@ -1,6 +1,6 @@ class PersonWithValidator include ActiveModel::Validations - + class PresenceValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) record.errors[attribute] << "Local validator#{options[:custom]}" if value.blank? diff --git a/activemodel/test/models/sheep.rb b/activemodel/test/models/sheep.rb index 175dbe6477..7aba055c4f 100644 --- a/activemodel/test/models/sheep.rb +++ b/activemodel/test/models/sheep.rb @@ -1,4 +1,3 @@ class Sheep extend ActiveModel::Naming end - \ No newline at end of file -- cgit v1.2.3