-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The JsonCommenter will generate JSON comments instead of a simple comma seperated string.
- Loading branch information
1 parent
3abc911
commit ef9d118
Showing
6 changed files
with
210 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require "json" | ||
|
||
module ActiveRecord | ||
module Comments | ||
class JsonCommenter | ||
def comment(comment) | ||
return yield unless comment.is_a?(Hash) | ||
|
||
begin | ||
orig = current_comments.dup | ||
current_comments.merge!(comment) | ||
yield | ||
ensure | ||
current_comments.replace(orig) | ||
end | ||
end | ||
|
||
def with_comment_sql(sql) | ||
return sql unless comment = current_comment | ||
|
||
"#{sql} /* #{comment} */" | ||
end | ||
|
||
private | ||
|
||
def current_comments | ||
Thread.current[:ar_json_comment] ||= {} | ||
end | ||
|
||
def current_comment | ||
current_comments.to_json if current_comments.present? | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
require "spec_helper" | ||
|
||
describe ActiveRecord::Comments::JsonCommenter do | ||
subject(:commenter) { ActiveRecord::Comments::JsonCommenter.new } | ||
|
||
describe "#current_comment" do | ||
it "is empty when not a hash" do | ||
commenter.comment("xxx") {} | ||
expect(commenter.send(:current_comment)).to eq(nil) | ||
end | ||
|
||
it "is filled when called with hash" do | ||
result = nil | ||
commenter.comment(foo: "bar") do | ||
result = commenter.send(:current_comment) | ||
end | ||
expect(result).to eq('{"foo":"bar"}') | ||
end | ||
|
||
it "concatenates to json when multiple comments" do | ||
result = nil | ||
commenter.comment(foo: "bar") do | ||
commenter.comment(hello: "world") do | ||
result = commenter.send(:current_comment) | ||
end | ||
end | ||
expect(result).to eq('{"foo":"bar","hello":"world"}') | ||
end | ||
|
||
it "merges existing hash key" do | ||
result = nil | ||
commenter.comment(foo: "bar") do | ||
commenter.comment(foo: "world") do | ||
result = commenter.send(:current_comment) | ||
end | ||
end | ||
expect(result).to eq('{"foo":"world"}') | ||
end | ||
|
||
it "removes comment when its block ends" do | ||
result = nil | ||
commenter.comment(foo: "bar") do | ||
commenter.comment(hello: "world") {} | ||
result = commenter.send(:current_comment) | ||
end | ||
expect(result).to eq('{"foo":"bar"}') | ||
end | ||
|
||
it "does not removes comment when its block ends and the key already exist" do | ||
result = nil | ||
commenter.comment(foo: "bar") do | ||
commenter.comment(foo: "world") {} | ||
result = commenter.send(:current_comment) | ||
end | ||
expect(result).to eq('{"foo":"bar"}') | ||
end | ||
end | ||
|
||
describe "#comment" do | ||
it "returns results" do | ||
expect(commenter.comment(foo: "bar") { 1 }).to eq(1) | ||
end | ||
end | ||
|
||
describe "#with_comment_sql" do | ||
it "returns sql with single k/v pair comment" do | ||
result = nil | ||
commenter.comment(foo: "bar") do | ||
result = commenter.with_comment_sql("SELECT * FROM User") | ||
end | ||
expect(result).to eq('SELECT * FROM User /* {"foo":"bar"} */') | ||
end | ||
|
||
it "returns sql with multiple k/v pair comment" do | ||
result = nil | ||
commenter.comment({foo: "bar", hello: "world"}) do | ||
commenter.comment(hello: "foo") do | ||
result = commenter.with_comment_sql("SELECT * FROM User") | ||
end | ||
end | ||
expect(result).to eq('SELECT * FROM User /* {"foo":"bar","hello":"foo"} */') | ||
end | ||
|
||
it "returns sql without comments" do | ||
result = commenter.with_comment_sql("SELECT * FROM User") | ||
expect(result).to eq("SELECT * FROM User") | ||
end | ||
end | ||
end |
This file contains 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