Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8863

Show
Ignore:
Timestamp:
02/13/08 02:19:46 (2 years ago)
Author:
nzkoz
Message:

Remove options from the attributes method, tidy up the implementation. Closes #11093 [juanjo.bazan, Koz]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/lib/active_record/base.rb

    r8858 r8863  
    22152215      # Returns a hash of all the attributes with their names as keys and the values of the attributes as values. 
    22162216      def attributes(options = nil) 
    2217         attrs = {} 
    2218         self.attribute_names.each do |name| 
    2219           attrs[name]=read_attribute(name) 
    2220         end 
    2221  
    2222         if options.nil? 
     2217        self.attribute_names.inject({}) do |attrs, name| 
     2218          attrs[name] = read_attribute(name) 
    22232219          attrs 
    2224         else 
    2225           if except = options[:except] 
    2226             except = Array(except).collect { |attribute| attribute.to_s } 
    2227             except.each { |attribute_name| attrs.delete(attribute_name) } 
    2228             attrs 
    2229           elsif only = options[:only] 
    2230             only = Array(only).collect { |attribute| attribute.to_s } 
    2231             attrs.delete_if { |key, value| !only.include?(key) } 
    2232             attrs 
    2233           else 
    2234             raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})" 
    2235           end 
    22362220        end 
    22372221      end 
     
    22392223      # Returns a hash of attributes before typecasting and deserialization. 
    22402224      def attributes_before_type_cast 
    2241         attrs = {} 
    2242         self.attribute_names.each do |name| 
    2243           attrs[name]=read_attribute_before_type_cast(name) 
    2244         end 
    2245         attrs 
     2225        self.attribute_names.inject({}) do |attrs, name| 
     2226          attrs[name] = read_attribute_before_typecast(name) 
     2227          attrs 
     2228        end 
    22462229      end 
    22472230 
  • trunk/activerecord/test/cases/base_test.rb

    r8855 r8863  
    17791779  end 
    17801780 
    1781   def test_except_attributes 
    1782     assert_equal( 
    1783       %w( author_name type id approved replies_count bonus_time written_on content author_email_address parent_id last_read).sort, 
    1784       topics(:first).attributes(:except => :title).keys.sort 
    1785     ) 
    1786  
    1787     assert_equal( 
    1788       %w( replies_count bonus_time written_on content author_email_address parent_id last_read).sort, 
    1789       topics(:first).attributes(:except => [ :title, :id, :type, :approved, :author_name ]).keys.sort 
    1790     ) 
    1791   end 
    1792  
    1793   def test_include_attributes 
    1794     assert_equal(%w( title ), topics(:first).attributes(:only => :title).keys) 
    1795     assert_equal(%w( title author_name type id approved ).sort, topics(:first).attributes(:only => [ :title, :id, :type, :approved, :author_name ]).keys.sort) 
    1796   end 
    1797  
    17981781  def test_type_name_with_module_should_handle_beginning 
    17991782    assert_equal 'ActiveRecord::Person', ActiveRecord::Base.send(:type_name_with_module, 'Person')