Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update compatability with newest dependencies and new JSON...

  • Loading branch information...
commit 3709d4171582fb2bcf6fd2f777d971607847f208 1 parent 7675771
@jweiss authored
View
28 lib/rocking_chair/helper.rb
@@ -2,16 +2,36 @@
module RockingChair
module Helper
-
+
def self.jsonfy_options(options, *keys)
keys.each do |key|
- options[key] = ActiveSupport::JSON.decode(options[key]) if options[key]
+ options[key] = json_content(options[key]) if options[key]
end
end
-
+
def self.access(attr_name, doc)
doc.respond_to?(:_document) ? doc._document[attr_name] : doc[attr_name]
end
-
+
+ def self.json_content(string)
+ if string && string.size < 2 # just one character --> only hashes and arrays are valid root objects
+ fixed_json_content(string)
+ elsif string == 'null'
+ nil
+ elsif string
+ ActiveSupport::JSON.decode(string)
+ else
+ nil
+ end
+ rescue JSON::ParserError, MultiJson::DecodeError
+ # probably: only hashes and arrays are valid root objects --> try to fix
+ fixed_json_content(string)
+ end
+
+ def self.fixed_json_content(string)
+ # wrap content in array, then parse and return first value
+ ActiveSupport::JSON.decode("[#{string}]")[0]
+ end
+
end
end
View
14 test/database_test.rb
@@ -238,7 +238,7 @@ class DatabaseTest < Test::Unit::TestCase
{"id" => "B", "key" => "B", "value" => {"rev" => "rev"}},
{"id" => "C", "key" => "C", "value" => {"rev" => "rev"}}
]
- }.to_json, @db.all_documents('startkey' => 'B'))
+ }.to_json, @db.all_documents('startkey' => 'B'.to_json ))
end
should "start by the given key and ignore quotes" do
@@ -266,7 +266,7 @@ class DatabaseTest < Test::Unit::TestCase
"total_rows" => 4, "offset" => 0, "rows" => [
{"id" => "B", "key" => "B", "value" => {"rev" => "rev"}}
]
- }.to_json, @db.all_documents('startkey' => 'B', 'limit' => '1'))
+ }.to_json, @db.all_documents('startkey' => 'B'.to_json, 'limit' => '1'))
end
should "combine start and descending" do
@@ -281,7 +281,7 @@ class DatabaseTest < Test::Unit::TestCase
{"id" => "B", "key" => "B", "value" => {"rev" => "rev"}},
{"id" => "A", "key" => "A", "value" => {"rev" => "rev"}}
]
- }.to_json, @db.all_documents('startkey' => "B\u9999", 'endkey' => "A", 'descending' => 'true'))
+ }.to_json, @db.all_documents('startkey' => "B\u9999".to_json, 'endkey' => "A".to_json, 'descending' => 'true'))
end
should "combine start, limit, and descending" do
@@ -295,7 +295,7 @@ class DatabaseTest < Test::Unit::TestCase
"total_rows" => 4, "offset" => 0, "rows" => [
{"id" => "B", "key" => "B", "value" => {"rev" => "rev"}}
]
- }.to_json, @db.all_documents('startkey' => "B\u9999", 'endkey' => "B", 'descending' => 'true', 'limit' => '1'))
+ }.to_json, @db.all_documents('startkey' => "B\u9999".to_json, 'endkey' => "B".to_json, 'descending' => 'true', 'limit' => '1'))
end
should "end by the given key" do
@@ -309,7 +309,7 @@ class DatabaseTest < Test::Unit::TestCase
{"id" => "A", "key" => "A", "value" => {"rev" => "rev"}},
{"id" => "B", "key" => "B", "value" => {"rev" => "rev"}}
]
- }.to_json, @db.all_documents('endkey' => 'B', 'startkey' => 'A'))
+ }.to_json, @db.all_documents('endkey' => 'B'.to_json, 'startkey' => 'A'.to_json))
end
should "combine start and end key" do
@@ -324,7 +324,7 @@ class DatabaseTest < Test::Unit::TestCase
{"id" => "B", "key" => "B", "value" => {"rev" => "rev"}},
{"id" => "C", "key" => "C", "value" => {"rev" => "rev"}}
]
- }.to_json, @db.all_documents('startkey' => 'B', 'endkey' => 'C'))
+ }.to_json, @db.all_documents('startkey' => 'B'.to_json, 'endkey' => 'C'.to_json))
end
should "combine start, end key, and include_docs" do
@@ -339,7 +339,7 @@ class DatabaseTest < Test::Unit::TestCase
{"id" => "B", "key" => "B", "value" => {"rev" => "rev", '_rev' => 'rev', 'data' => 'Z', '_id' => 'B'}},
{"id" => "C", "key" => "C", "value" => {"rev" => "rev", '_rev' => 'rev', 'data' => 'Z', '_id' => 'C'}}
]
- }, parse_json(@db.all_documents('startkey' => 'B', 'endkey' => 'C', 'include_docs' => 'true')))
+ }, parse_json(@db.all_documents('startkey' => 'B'.to_json, 'endkey' => 'C'.to_json, 'include_docs' => 'true')))
end
end
View
3  test/simply_stored_test.rb
@@ -195,10 +195,11 @@ class SimplyStoredTest < Test::Unit::TestCase
end
should "count_by with nil attributes" do
- Project.create(:title => nil)
+ p = Project.create(:title => nil)
assert_equal 1, Project.count
assert_equal 1, Project.count_by_title(nil)
+ assert_equal p, Project.find_by_title(nil)
Project.create(:title => nil, :manager_id => 12)
View
2  test/view_test.rb
@@ -69,7 +69,7 @@ class ViewTest < Test::Unit::TestCase
should "respond to defined views" do
assert_nothing_raised do
- @db.view('user', 'by_firstname', 'key' => 'abc')
+ @db.view('user', 'by_firstname', 'key' => 'abc'.to_json)
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.