Changeset 8863
- Timestamp:
- 02/13/08 02:19:46 (2 years ago)
- Files:
-
- trunk/activerecord/lib/active_record/base.rb (modified) (2 diffs)
- trunk/activerecord/test/cases/base_test.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/lib/active_record/base.rb
r8858 r8863 2215 2215 # Returns a hash of all the attributes with their names as keys and the values of the attributes as values. 2216 2216 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) 2223 2219 attrs 2224 else2225 if except = options[:except]2226 except = Array(except).collect { |attribute| attribute.to_s }2227 except.each { |attribute_name| attrs.delete(attribute_name) }2228 attrs2229 elsif only = options[:only]2230 only = Array(only).collect { |attribute| attribute.to_s }2231 attrs.delete_if { |key, value| !only.include?(key) }2232 attrs2233 else2234 raise ArgumentError, "Options does not specify :except or :only (#{options.keys.inspect})"2235 end2236 2220 end 2237 2221 end … … 2239 2223 # Returns a hash of attributes before typecasting and deserialization. 2240 2224 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 2246 2229 end 2247 2230 trunk/activerecord/test/cases/base_test.rb
r8855 r8863 1779 1779 end 1780 1780 1781 def test_except_attributes1782 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.sort1785 )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.sort1790 )1791 end1792 1793 def test_include_attributes1794 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 end1797 1798 1781 def test_type_name_with_module_should_handle_beginning 1799 1782 assert_equal 'ActiveRecord::Person', ActiveRecord::Base.send(:type_name_with_module, 'Person')