Prior to this commit, we were somewhat naively rendering Hash style data attributes in our ERB templates. By rendering each hash attribute separately, we were rendering characters that could break the javascript (e.g. double hack or backslash `"` or `\`). By moving to this view_object rendering, we leverage Rails's `to_json` behavior to ensure properly escaped values. As part of this exercise, I generalized the method to allow for other places to benefit from this behavior. This generalization also helps ensure that we have a more conformant rendering (e.g. we should always have an :id, :className, and :name value in our data-info hash). _Note: I've updated the user's names for Cypress tests as they are more likely to catch the particular issue than anything else. I assume that I'm going to break some cypress tests and will need some help fixing them._ Closes #15916, #14704 Supersedes #15983 How to test locally: Assuming you have seeded database (e.g. `rails db:seed`), checkout the "main" branch. Then in `rails console` find a user that's written articles: ```ruby user = Article.last.user user.update(name: "\\: #{user.name}") user.articles.each(&:save) ``` Now, again on the "main" branch, start your application (e.g., `bin/startup`). Then get a logged in and a logged out browser session going. Open your web inspector and open console. Then go to the local instances homepage (e.g., http://localhost:3000) and look for JS errors. On the main branch, you should see an exception around `JSON.parse(button.data.info)` (assuming that the `user`'s article is rendered on the homepage). Then go to the user's page (e.g. https://localhost:3000/:user-slug) and look for JS parse errors. On this PR's branch (e.g., `jeremyf/take-two-at-resolving-gh-15916`) you shouldn't see those console errors. More importantly, the Follow buttons should work.
41 lines
1.3 KiB
Ruby
41 lines
1.3 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe DataInfo, type: :view_object do
|
|
describe "#to_json" do
|
|
subject(:results) { described_class.to_json(**parameters) }
|
|
|
|
# Including two characters of special purpose, the double hack
|
|
# (e.g. '"') and the backslash (e.g. '\'). These were causing
|
|
# upstream problems.
|
|
let(:user) { build(:user, id: 123, name: "Duane \"The Rock\" Johnson \\\\.//") }
|
|
|
|
context "when given a User" do
|
|
let(:parameters) { { object: user } }
|
|
|
|
it "parses to valid JSON" do
|
|
expect(JSON.parse(results))
|
|
.to eq({ "className" => "User", "id" => 123, "name" => "Duane \"The Rock\" Johnson \\\\.//" })
|
|
end
|
|
end
|
|
|
|
context "when given a User with additional attributes" do
|
|
let(:parameters) { { object: user, id: 8_675_309, style: "full" } }
|
|
|
|
it "parses to valid JSON" do
|
|
expect(JSON.parse(results))
|
|
.to eq({ "className" => "User", "id" => 8_675_309, "name" => "Duane \"The Rock\" Johnson \\\\.//",
|
|
"style" => "full" })
|
|
end
|
|
end
|
|
|
|
context "when given a Tag" do
|
|
let(:tag) { build(:tag, id: 1234) }
|
|
let(:parameters) { { object: tag } }
|
|
|
|
it "parses to valid JSON" do
|
|
expect(JSON.parse(results))
|
|
.to eq({ "className" => "Tag", "id" => tag.id, "name" => tag.name })
|
|
end
|
|
end
|
|
end
|
|
end
|