Skip to content
This repository
Browse code

HABTM finder sets :readonly => false. Closes #2525.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2694 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 7219e82fe600ff1268b9a89efc7a289ac0108592 1 parent 07bff0b
Jeremy Kemper authored October 20, 2005
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *1.12.1* (October 19th, 2005)
2 2
 
  3
+* HABTM finders shouldn't return readonly records.  #2525 [patrick@lenz.sh]
  4
+
3 5
 * Make all tests runnable on their own. #2521. [Blair Zajac <blair@orcaware.com>]
4 6
 
5 7
 * Always parenthesize :conditions options so they may be safely combined with STI and constraints.
1  activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -45,6 +45,7 @@ def find(*args)
45 45
           end
46 46
           options[:conditions] = conditions
47 47
           options[:joins] = @join_sql
  48
+          options[:readonly] ||= false
48 49
 
49 50
           if options[:order] && @options[:order]
50 51
             options[:order] = "#{options[:order]}, #{@options[:order]}"
8  activerecord/lib/active_record/base.rb
@@ -357,7 +357,7 @@ class << self # Class methods
357 357
       # * <tt>:offset</tt>: An integer determining the offset from where the rows should be fetched. So at 5, it would skip the first 4 rows.
358 358
       # * <tt>:joins</tt>: An SQL fragment for additional joins like "LEFT JOIN comments ON comments.post_id = id". (Rarely needed).
359 359
       #   The records will be returned read-only since they will have attributes that do not correspond to the table's columns.
360  
-      #   Use <tt>find_by_sql</tt> to circumvent this limitation.
  360
+      #   Pass :readonly => false to override.
361 361
       # * <tt>:include</tt>: Names associations that should be loaded alongside using LEFT OUTER JOINs. The symbols named refer
362 362
       #   to already defined associations. See eager loading under Associations.
363 363
       # * <tt>:select</tt>: By default, this is * as in SELECT * FROM, but can be changed if you for example want to do a join, but not
@@ -384,8 +384,10 @@ class << self # Class methods
384 384
       def find(*args)
385 385
         options = extract_options_from_args!(args)
386 386
 
387  
-        # :joins implies :readonly => true
388  
-        options[:readonly] = true if options[:joins]
  387
+        # :joins implies :readonly => true if unset.
  388
+        if options[:joins] and !options.has_key?(:readonly)
  389
+          options[:readonly] = true
  390
+        end
389 391
 
390 392
         case args.first
391 393
           when :first
38  activerecord/test/readonly_test.rb
... ...
@@ -1,31 +1,41 @@
1 1
 require 'abstract_unit'
2  
-require 'fixtures/topic'
  2
+require 'fixtures/developer'
  3
+require 'fixtures/project'
3 4
 
4 5
 class ReadOnlyTest < Test::Unit::TestCase
5  
-  fixtures :topics
  6
+  fixtures :developers, :projects, :developers_projects
6 7
 
7 8
   def test_cant_save_readonly_record
8  
-    topic = Topic.find(:first)
9  
-    assert !topic.readonly?
  9
+    dev = Developer.find(:first)
  10
+    assert !dev.readonly?
10 11
 
11  
-    topic.readonly!
12  
-    assert topic.readonly?
  12
+    dev.readonly!
  13
+    assert dev.readonly?
13 14
 
14 15
     assert_nothing_raised do
15  
-      topic.content = 'Luscious forbidden fruit.'
  16
+      dev.name = 'Luscious forbidden fruit.'
  17
+      assert !dev.save
  18
+      dev.name = 'Forbidden.'
16 19
     end
17  
-
18  
-    assert_raise(ActiveRecord::ReadOnlyRecord)  { topic.save  }
19  
-    assert_raise(ActiveRecord::ReadOnlyRecord)  { topic.save! }
  20
+    assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save  }
  21
+    assert_raise(ActiveRecord::ReadOnlyRecord) { dev.save! }
20 22
   end
21 23
 
22 24
   def test_find_with_readonly_option
23  
-    Topic.find(:all).each { |t| assert !t.readonly? }
24  
-    Topic.find(:all, :readonly => false).each { |t| assert !t.readonly? }
25  
-    Topic.find(:all, :readonly => true).each { |t| assert t.readonly? }
  25
+    Developer.find(:all).each { |d| assert !d.readonly? }
  26
+    Developer.find(:all, :readonly => false).each { |d| assert !d.readonly? }
  27
+    Developer.find(:all, :readonly => true).each { |d| assert d.readonly? }
26 28
   end
27 29
 
28 30
   def test_find_with_joins_option_implies_readonly
29  
-    Topic.find(:all, :joins => '').each { |t| assert t.readonly? }
  31
+    Developer.find(:all, :joins => '').each { |d| assert d.readonly? }
  32
+    Developer.find(:all, :joins => '', :readonly => false).each { |d| assert !d.readonly? }
  33
+  end
  34
+
  35
+  def test_habtm_find_readonly
  36
+    dev = Developer.find(:first)
  37
+    dev.projects.each { |p| assert !p.readonly? }
  38
+    dev.projects.find(:all) { |p| assert !p.readonly? }
  39
+    dev.projects.find(:all, :readonly => true) { |p| assert p.readonly? }
30 40
   end
31 41
 end

0 notes on commit 7219e82

Please sign in to comment.
Something went wrong with that request. Please try again.