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

Changeset 4387

Show
Ignore:
Timestamp:
05/31/06 23:25:36 (2 years ago)
Author:
david
Message:

Added Array#to_s(:db) that'll produce a comma-separated list of ids [DHH] Split Grouping into its own file

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/test/base_test.rb

    r4314 r4387  
    933933  end 
    934934 
     935  def test_quoting_arrays 
     936    replies = Reply.find(:all, :conditions => [ "id IN (?)", topics(:first).replies.to_s(:db) ]) 
     937    assert_equal topics(:first).replies.size, replies.size 
     938 
     939    replies = Reply.find(:all, :conditions => [ "id IN (?)", [].to_s(:db) ]) 
     940    assert_equal 0, replies.size 
     941  end 
     942 
    935943  MyObject = Struct.new :attribute1, :attribute2 
    936944   
  • trunk/activesupport/CHANGELOG

    r4359 r4387  
    11*SVN* 
     2 
     3* Added Array#to_s(:db) that'll produce a comma-separated list of ids [DHH]. Example: 
     4     
     5    Purchase.find(:all, :conditions => [ "product_id IN (?)", shop.products.to_s(:db) ]) 
    26 
    37* Normalize classify's argument to a String so that it plays nice with Symbols. [Marcel Molina Jr.]  
  • trunk/activesupport/lib/active_support/core_ext/array.rb

    r4345 r4387  
    11require File.dirname(__FILE__) + '/array/conversions' 
     2require File.dirname(__FILE__) + '/array/grouping' 
    23 
    34class Array #:nodoc: 
    45  include ActiveSupport::CoreExtensions::Array::Conversions 
    5  
    6   # Iterate over an array in groups of a certain size, padding any remaining  
    7   # slots with specified value (<tt>nil</tt> by default). 
    8   #  
    9   # E.g. 
    10   #  
    11   #   %w(1 2 3 4 5 6 7).in_groups_of(3) {|g| p g} 
    12   #   ["1", "2", "3"] 
    13   #   ["4", "5", "6"] 
    14   #   ["7", nil, nil] 
    15   def in_groups_of(number, fill_with = nil, &block) 
    16     require 'enumerator' 
    17     collection = dup 
    18     collection << fill_with until collection.size.modulo(number).zero? 
    19     grouped_collection = [] unless block_given? 
    20     collection.each_slice(number) do |group| 
    21       block_given? ? yield(group) : grouped_collection << group 
    22     end 
    23     grouped_collection unless block_given? 
    24   end 
    25    
    26   # Divide the array into one or more subarrays based on a delimiting +value+ 
    27   # or the result of an optional block. 
    28   # 
    29   # ex. 
    30   # 
    31   #   [1, 2, 3, 4, 5].split(3)                # => [[1, 2], [4, 5]] 
    32   #   (1..10).to_a.split { |i| i % 3 == 0 }   # => [[1, 2], [4, 5], [7, 8], [10]] 
    33   def split(value = nil, &block) 
    34     block ||= Proc.new { |e| e == value } 
    35     inject([[]]) do |results, element| 
    36       if block.call(element) 
    37         results << [] 
    38       else 
    39         results.last << element 
    40       end 
    41       results 
    42     end 
    43   end 
     6  include ActiveSupport::CoreExtensions::Array::Grouping 
    447end 
  • trunk/activesupport/lib/active_support/core_ext/array/conversions.rb

    r3901 r4387  
    2727        end 
    2828         
     29        def self.included(klass) #:nodoc: 
     30          klass.send(:alias_method, :to_default_s, :to_s) 
     31          klass.send(:alias_method, :to_s, :to_formatted_s) 
     32        end 
     33         
     34        def to_formatted_s(format = :default) 
     35          case format 
     36            when :db 
     37              if respond_to?(:empty?) && self.empty? 
     38                "null" 
     39              else 
     40                collect { |element| element.id }.join(",") 
     41              end 
     42            else 
     43              to_default_s 
     44          end 
     45        end 
     46         
    2947        def to_xml(options = {}) 
    3048          raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml } 
  • trunk/activesupport/test/core_ext/array_ext_test.rb

    r4345 r4387  
    1414end 
    1515 
    16 class ArrayExtConversionTests < Test::Unit::TestCase 
     16class ArrayExtToSentenceTests < Test::Unit::TestCase 
    1717  def test_plain_array_to_sentence 
    1818    assert_equal "", [].to_sentence 
     
    3737  def test_one_element 
    3838    assert_equal "one", ['one'].to_sentence 
     39  end 
     40end 
     41 
     42class ArrayExtToSTests < Test::Unit::TestCase 
     43  def test_to_s_db 
     44    collection = [ 
     45      Class.new { def id() 1 end }.new, 
     46      Class.new { def id() 2 end }.new, 
     47      Class.new { def id() 3 end }.new 
     48    ] 
     49     
     50    assert_equal "null", [].to_s(:db) 
     51    assert_equal "1,2,3", collection.to_s(:db) 
    3952  end 
    4053end