Files
nairobi/spec/support/common_actions/notifications.rb
Javi Martín ff93f5a591 Use "resolve" for polymorphic hierarchy paths
In the past, we couldn't use `polymorphic_path` in many places. For
instance, `polymorphic_path(budget, investment)` would return
`budget_budget_investment_path`, while in our routes we had defined
`budget_investment_path`.

With the `resolve` method, introduced in Rails 5.1, we can use symbols
to define we want it to use `investment` instead of `budget_investment`.
It also works with nested resources, so now we can write
`polymorphic_path(investment)`.

This makes the code for `resource_hierarchy_for` almost impossible to
understand. I reached this result after having a look at the internals
of the `resolve` method in order to get its results and then remove the
symbols we include.

Note using this method will not make admin routes compatible with
`polymorphic_path`. Quoting from the Rails documentation:

> This custom behavior only applies to simple polymorphic URLs where a
> single model instance is passed and not more complicated forms, e.g:
> [example showing admin routes won't work]

Also note that now the `admin_polymorphic_path` method will not work for
every model due to inconsistencies in our admin routes. For instance, we
define `groups` and `budget_investments`; we should either use the
`budget_` prefix in all places or remove it everywhere. Right now the
code only works for items with the prefix; it isn't a big deal because
we never call it with an item without the prefix.

Finally, for unknown reasons some routing tests fail if we use
`polymorphic_path`, so we need to redefine that method in those tests
and force the `only_path: true` option.
2020-06-15 11:54:05 +02:00

69 lines
2.0 KiB
Ruby

module Notifications
def click_notifications_icon
find("#notifications a").click
end
def model_name(described_class)
return :proposal_notification if described_class == ProposalNotification
described_class.name.gsub("::", "_").downcase.to_sym
end
def comment_body(resource)
if resource.class.name == "Legislation::Question"
"Leave your answer"
else
"Leave your comment"
end
end
def submit_comment_text(resource)
if resource.class.name == "Legislation::Question"
"Publish answer"
else
"Publish comment"
end
end
def create_proposal_notification(proposal)
login_as(proposal.author)
visit root_path
click_link "My content"
within("#proposal_#{proposal.id}") do
click_link "Dashboard"
end
within("#side_menu") do
click_link "Message to users"
end
click_link "Send message to proposal followers"
fill_in "proposal_notification_title", with: "Thanks for supporting proposal: #{proposal.title}"
fill_in "proposal_notification_body", with: "Please share it with others! #{proposal.summary}"
click_button "Send message"
expect(page).to have_content "Your message has been sent correctly."
Notification.last
end
def path_for(resource)
polymorphic_path(resource)
end
def error_message(resource_model = nil)
resource_model ||= "(.*)"
field_check_message = "Please check the marked fields to know how to correct them:"
/\d errors? prevented this #{resource_model} from being saved. #{field_check_message}/
end
def fill_in_admin_notification_form(options = {})
select (options[:segment_recipient] || "All users"), from: :admin_notification_segment_recipient
fill_in "Title", with: (options[:title] || "This is the notification title")
fill_in "Text", with: (options[:body] || "This is the notification body")
fill_in :admin_notification_link, with: (options[:link] || "https://www.decide.madrid.es/vota")
end
end