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

Ticket #11084: add_readonly_option_to_has_many_associations.diff

File add_readonly_option_to_has_many_associations.diff, 4.2 kB (added by miloops, 5 months ago)
  • test/models/company.rb

    old new  
    4040           :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = 1000' 
    4141  has_many :clients_using_finder_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE 1=1' 
    4242  has_many :plain_clients, :class_name => 'Client' 
     43  has_many :readonly_clients, :class_name => 'Client', :readonly => true 
    4344 
    4445  has_one :account, :foreign_key => "firm_id", :dependent => :destroy 
    4546end 
  • test/cases/reflection_test.rb

    old new  
    159159  end 
    160160 
    161161  def test_reflection_of_all_associations 
    162     assert_equal 17, Firm.reflect_on_all_associations.size 
    163     assert_equal 15, Firm.reflect_on_all_associations(:has_many).size 
     162    assert_equal 18, Firm.reflect_on_all_associations.size 
     163    assert_equal 16, Firm.reflect_on_all_associations(:has_many).size 
    164164    assert_equal 2, Firm.reflect_on_all_associations(:has_one).size 
    165165    assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size 
    166166  end 
  • test/cases/associations_test.rb

    old new  
    544544    assert_equal 2, companies(:first_firm).limited_clients.find_all_by_type('Client', :limit => 9_000).length 
    545545  end 
    546546 
     547  def test_dynamic_find_all_should_respect_readonly_access 
     548    companies(:first_firm).readonly_clients.find(:all).each { |c| assert_raise(ActiveRecord::ReadOnlyRecord) { c.save  } } 
     549    companies(:first_firm).readonly_clients.find(:all).each { |c| assert c.readonly? } 
     550  end 
     551 
    547552  def test_triple_equality 
    548553    assert !(Array === Firm.find(:first).clients) 
    549554    assert Firm.find(:first).clients === Array 
  • lib/active_record/associations/association_proxy.rb

    old new  
    114114            :offset  => @reflection.options[:offset], 
    115115            :joins   => @reflection.options[:joins], 
    116116            :include => @reflection.options[:include], 
    117             :select  => @reflection.options[:select] 
     117            :select  => @reflection.options[:select], 
     118            :readonly  => @reflection.options[:readonly] 
    118119          ) 
    119120        end 
    120121 
  • lib/active_record/associations.rb

    old new  
    669669      # * <tt>:source_type</tt>: Specifies type of the source association used by <tt>has_many :through</tt> queries where the source 
    670670      #   association is a polymorphic +belongs_to+. 
    671671      # * <tt>:uniq</tt> - if set to +true+, duplicates will be omitted from the collection. Useful in conjunction with <tt>:through</tt>. 
     672      # * <tt>:readonly</tt> - if set to +true+, all the associated objects are readonly through the association. Default is +false+. 
    672673      # 
    673674      # Option examples: 
    674675      #   has_many :comments, :order => "posted_on" 
     
    677678      #   has_many :tracks, :order => "position", :dependent => :destroy 
    678679      #   has_many :comments, :dependent => :nullify 
    679680      #   has_many :tags, :as => :taggable 
     681      #   has_many :reports, :readonly => true 
    680682      #   has_many :subscribers, :through => :subscriptions, :source => :user 
    681683      #   has_many :subscribers, :class_name => "Person", :finder_sql => 
    682684      #       'SELECT DISTINCT people.* ' + 
     
    12341236            :uniq, 
    12351237            :finder_sql, :counter_sql, 
    12361238            :before_add, :after_add, :before_remove, :after_remove, 
    1237             :extend 
     1239            :extend, :readonly 
    12381240          ) 
    12391241 
    12401242          options[:extend] = create_extension_modules(association_id, extension, options[:extend])