All the code in the `bin/` and the `config/` folders has been generated
running `rake app:update`. The only exception is the code in
`config/application.rb` where we've excluded the engines that Rails 6.0
has added, since we don't use them.
There are a few changes in Active Storage which aren't compatible with
the code we were using until now.
Since the method to assign an attachment in ActiveStorage has changed
and is incompatible with the hack we used to allow assigning `nil`
attachments, and since ActiveStorage now supports assigning `nil`
attachments, we're removing the mentioned hack. This makes the
HasAttachment module redundant, so we're removing it.
Another change in ActiveStorage is files are no longer saved before
saving the `ActiveStorage::Attachment` record. This means we need to
manually upload the file when using direct uploads. We also have to
change the width and height validations we used for images; however,
doing so results in very complex code, and we currently have to write
that code for both images and site customization images.
So, for now, we're just uploading the file before checking its
dimensions. Not ideal, though. We might use active_storage_validations
in the future to fix this issue (when they support a proc/lambda, as
mentioned in commit 600f5c35e).
We also need to update a couple of tests due to a small change in
response headers. Now the content disposition returns something like:
```
attachment; filename="budget_investments.csv"; filename*=UTF-8''budget_investments.csv
```
So we're updating regular expression we use to check the filename.
Finally, Rails 6.0.1 changed the way the host is set in integration
tests [1] and so both `Capybara.app_host` and `Capybara.default_host`
were ignored when generating URLs in the relationable examples. The only
way I've found to make it work is to explicitely assign the host to the
integration session. Rails 6.1 will change this setup again, so maybe
then we can remove this hack.
[1] https://github.com/rails/rails/pull/36283/commits/fe00711e9
191 lines
6.0 KiB
Ruby
191 lines
6.0 KiB
Ruby
shared_examples "relationable" do |relationable_model_name|
|
|
let(:relationable) { create(relationable_model_name.name.parameterize(separator: "_").to_sym) }
|
|
let(:related1) { create([:proposal, :debate, :budget_investment].sample) }
|
|
let(:related2) { create([:proposal, :debate, :budget_investment].sample) }
|
|
let(:user) { create(:user) }
|
|
|
|
before do
|
|
integration_session.host = Capybara.app_host # TODO: remove after upgrading to Rails 6.1
|
|
Setting["url"] = Capybara.app_host
|
|
end
|
|
|
|
scenario "related contents are listed" do
|
|
create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
|
|
|
visit polymorphic_path(relationable)
|
|
within("#related-content-list") do
|
|
expect(page).to have_content related1.title
|
|
end
|
|
|
|
visit polymorphic_path(related1)
|
|
within("#related-content-list") do
|
|
expect(page).to have_content relationable.title
|
|
end
|
|
end
|
|
|
|
scenario "related contents list is not rendered if there are no relations" do
|
|
visit polymorphic_path(relationable)
|
|
expect(page).not_to have_css "#related-content-list"
|
|
end
|
|
|
|
scenario "related contents can be added" do
|
|
login_as(user)
|
|
visit polymorphic_path(relationable)
|
|
|
|
expect(page).not_to have_css "#related_content"
|
|
expect(page).to have_css ".add-related-content[aria-expanded='false']"
|
|
|
|
click_button "Add related content"
|
|
|
|
expect(page).to have_css ".add-related-content[aria-expanded='true']"
|
|
|
|
within("#related_content") do
|
|
fill_in "Link to related content", with: polymorphic_url(related1)
|
|
click_button "Add"
|
|
end
|
|
|
|
within("#related-content-list") do
|
|
expect(page).to have_content related1.title
|
|
end
|
|
|
|
visit polymorphic_path(related1)
|
|
|
|
within("#related-content-list") do
|
|
expect(page).to have_content relationable.title
|
|
end
|
|
|
|
click_button "Add related content"
|
|
|
|
within("#related_content") do
|
|
fill_in "Link to related content", with: polymorphic_url(related2)
|
|
click_button "Add"
|
|
end
|
|
|
|
within("#related-content-list") do
|
|
expect(page).to have_content related2.title
|
|
end
|
|
end
|
|
|
|
scenario "if related content URL is invalid returns error" do
|
|
login_as(user)
|
|
visit polymorphic_path(relationable)
|
|
|
|
click_button "Add related content"
|
|
|
|
within("#related_content") do
|
|
fill_in "Link to related content", with: "http://invalidurl.com"
|
|
click_button "Add"
|
|
end
|
|
|
|
expect(page).to have_content "Link not valid. Remember to start with #{Capybara.app_host}."
|
|
end
|
|
|
|
scenario "returns error when relating content URL to itself" do
|
|
login_as(user)
|
|
visit polymorphic_path(relationable)
|
|
|
|
click_button "Add related content"
|
|
|
|
within("#related_content") do
|
|
fill_in "Link to related content", with: polymorphic_url(relationable)
|
|
click_button "Add"
|
|
end
|
|
|
|
expect(page).to have_content "Link not valid. You cannot relate a content to itself"
|
|
end
|
|
|
|
context "custom URLs" do
|
|
before do
|
|
custom_route = proc { get "/mypath/:id" => "debates#show" }
|
|
Rails.application.routes.send(:eval_block, custom_route)
|
|
end
|
|
|
|
after { Rails.application.reload_routes! }
|
|
|
|
scenario "finds relationable with custom URLs" do
|
|
related = create(:debate, title: "My path is the only one I've walked")
|
|
|
|
login_as(user)
|
|
visit polymorphic_path(relationable)
|
|
|
|
click_button "Add related content"
|
|
|
|
within("#related_content") do
|
|
fill_in "Link to related content", with: "#{Capybara.app_host}/mypath/#{related.id}"
|
|
click_button "Add"
|
|
end
|
|
|
|
within("#related-content-list") do
|
|
expect(page).to have_content "My path is the only one I've walked"
|
|
end
|
|
end
|
|
end
|
|
|
|
scenario "returns an error when the related content already exists" do
|
|
create(:related_content, parent_relationable: relationable, child_relationable: related1)
|
|
login_as(user)
|
|
visit polymorphic_path(relationable)
|
|
|
|
click_button "Add related content"
|
|
|
|
within("#related_content") do
|
|
fill_in "url", with: polymorphic_url(related1)
|
|
click_button "Add"
|
|
end
|
|
|
|
expect(page).to have_content "The related content you are adding already exists."
|
|
end
|
|
|
|
scenario "related content can be scored positively" do
|
|
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
|
|
|
login_as(user)
|
|
visit polymorphic_path(relationable)
|
|
|
|
within("#related-content-list") do
|
|
click_link "Yes"
|
|
|
|
expect(page).not_to have_link "Yes"
|
|
expect(page).not_to have_link "No"
|
|
end
|
|
|
|
expect(related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.id).value).to eq(1)
|
|
expect(related_content.opposite_related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.opposite_related_content.id).value).to eq(1)
|
|
end
|
|
|
|
scenario "related content can be scored negatively" do
|
|
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
|
|
|
login_as(user)
|
|
visit polymorphic_path(relationable)
|
|
|
|
within("#related-content-list") do
|
|
click_link "No"
|
|
|
|
expect(page).not_to have_link "Yes"
|
|
expect(page).not_to have_link "No"
|
|
end
|
|
|
|
expect(related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.id).value).to eq(-1)
|
|
expect(related_content.opposite_related_content.related_content_scores.find_by(user_id: user.id, related_content_id: related_content.opposite_related_content.id).value).to eq(-1)
|
|
end
|
|
|
|
scenario "if related content has negative score it will be hidden" do
|
|
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
|
|
|
2.times do
|
|
related_content.send("score_positive", build(:user))
|
|
end
|
|
|
|
6.times do
|
|
related_content.send("score_negative", build(:user))
|
|
end
|
|
|
|
login_as(user)
|
|
|
|
visit polymorphic_path(relationable)
|
|
|
|
expect(page).not_to have_css "#related-content-list"
|
|
end
|
|
end
|