Created
May 14, 2011 02:17
-
-
Save AquaGeek/971633 to your computer and use it in GitHub Desktop.
Rails Lighthouse ticket #2662
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From 19e70500bbc31827f9db350e0118a611f8f7f79d Mon Sep 17 00:00:00 2001 | |
From: Alexander Simonov <[email protected]> | |
Date: Sun, 17 May 2009 19:26:56 +0300 | |
Subject: [PATCH] Add support for nested SQL queries | |
--- | |
activerecord/lib/active_record/base.rb | 7 ++++++- | |
activerecord/test/cases/base_test.rb | 27 +++++++++++++++++++++++++++ | |
activerecord/test/models/topic.rb | 2 ++ | |
3 files changed, 35 insertions(+), 1 deletions(-) | |
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb | |
index ca4f4fa..f77b285 100755 | |
--- a/activerecord/lib/active_record/base.rb | |
+++ b/activerecord/lib/active_record/base.rb | |
@@ -2991,7 +2991,12 @@ module ActiveRecord #:nodoc: | |
value = value.to_yaml | |
end | |
- quoted[name] = connection.quote(value, column) | |
+ # Add checks for nested sql variables | |
+ if self.instance_variable_defined?("@" + name + "_sql") | |
+ quoted[name] = self.instance_variable_get("@" + name + "_sql") | |
+ else | |
+ quoted[name] = connection.quote(value, column) | |
+ end | |
end | |
end | |
include_readonly_attributes ? quoted : remove_readonly_attributes(quoted) | |
diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb | |
index 59aa695..fe810ce 100755 | |
--- a/activerecord/test/cases/base_test.rb | |
+++ b/activerecord/test/cases/base_test.rb | |
@@ -277,6 +277,16 @@ class BasicsTest < ActiveRecord::TestCase | |
assert_equal "David", topic2.author_name | |
end | |
+ def test_create_nested_sql | |
+ topic_with_parent_id = Topic.find(2) | |
+ topic = Topic.new | |
+ topic.title = "Nested SQL Topic" | |
+ topic.parent_id_sql = "(SELECT parent_id FROM topics WHERE id=#{topic_with_parent_id.id})" | |
+ topic.save | |
+ topicReloaded = Topic.find(topic.id) | |
+ assert_equal topic_with_parent_id.parent_id, topicReloaded.parent_id | |
+ end | |
+ | |
def test_update | |
topic = Topic.new | |
topic.title = "Another New Topic" | |
@@ -308,6 +318,23 @@ class BasicsTest < ActiveRecord::TestCase | |
minimalistic = minimalistics(:first) | |
assert_nothing_raised { minimalistic.save } | |
end | |
+ | |
+ def test_update_nested_sql | |
+ topic_with_parent_id = Topic.find(2) | |
+ topic = Topic.new | |
+ topic.title = "Nested SQL Topic" | |
+ topic.parent_id = 4 | |
+ topic.save | |
+ topicReloaded = Topic.find(topic.id) | |
+ assert_equal 4,topicReloaded.parent_id | |
+ | |
+ topicReloaded.parent_id = nil | |
+ topicReloaded.parent_id_sql = "(SELECT parent_id FROM topics WHERE id=#{topic_with_parent_id.id})" | |
+ topicReloaded.save | |
+ | |
+ topicReloadedAgain = Topic.find(topic.id) | |
+ assert_equal topic_with_parent_id.parent_id,topicReloadedAgain.parent_id | |
+ end | |
def test_write_attribute | |
topic = Topic.new | |
diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb | |
index 51012d2..69be9ae 100644 | |
--- a/activerecord/test/models/topic.rb | |
+++ b/activerecord/test/models/topic.rb | |
@@ -43,6 +43,8 @@ class Topic < ActiveRecord::Base | |
before_create :default_written_on | |
before_destroy :destroy_children | |
+ | |
+ attr_accessor :parent_id_sql | |
def parent | |
Topic.find(parent_id) | |
-- | |
1.6.1.2 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment