For the following example we’re gonna use RSpec for the concise syntax for test doubles.
The remaining code is about permissions related logic: the edit link must be rendered only if the current user is an admin. This is tested by looking at the output of the template.
Notice that exposures
includes an unused params
key. While this is not strictly required, we recommend providing it since it’s expected by some standard view helpers (e.g. form helpers).
module Web
module Views
module Books
class Show
include Web::View
def formatted_price
end
def edit_link
if can_edit_book?
link_to "Edit", routes.edit_book_path(id: book.id)
end
private
def can_edit_book?
current_user.admin?
end
end
end
end