From 1327b7276d4d24413df10f9c23330538bd333165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Fri, 23 Jun 2017 15:01:03 +0200 Subject: [PATCH] Create investments helper. Fix indentation error and some specs names and structure. --- app/helpers/investments_helper.rb | 15 ++++++++++ .../budgets/investments/_image_form.html.erb | 5 ++-- .../investments/_investment_show.html.erb | 29 +++++++++---------- spec/features/budgets/investments_spec.rb | 22 ++++++++++++-- 4 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 app/helpers/investments_helper.rb diff --git a/app/helpers/investments_helper.rb b/app/helpers/investments_helper.rb new file mode 100644 index 000000000..e6767c4c5 --- /dev/null +++ b/app/helpers/investments_helper.rb @@ -0,0 +1,15 @@ +module InvestmentsHelper + + def investment_image_file_name(investment) + investment.image.url.split('/').last.split("?")[0] if investment.image.present? + end + + def investment_image_advice_note_key(investment) + investment.image.exists? ? "edit_note" : "add_note" + end + + def investment_image_button_text(investment) + investment.image.exists? ? "edit_image" : "add_image" + end + +end \ No newline at end of file diff --git a/app/views/budgets/investments/_image_form.html.erb b/app/views/budgets/investments/_image_form.html.erb index db73cfff0..89b2a5bcd 100644 --- a/app/views/budgets/investments/_image_form.html.erb +++ b/app/views/budgets/investments/_image_form.html.erb @@ -3,8 +3,7 @@
- <% note_key = @investment.image.exists? ? "edit_note" : "add_note" %> - <%= t("budgets.investments.edit_image.#{note_key}", title: @investment.title) %>

+ <%= t("budgets.investments.edit_image.#{investment_image_advice_note_key(@investment)}", title: @investment.title) %>

<% if @investment.image.exists? %> @@ -17,7 +16,7 @@ <%= f.file_field :image, accept: 'image/jpeg', label: false, class:'show-for-sr' %>
<%= f.label :image, t("images.upload_image"), class:'button' %> -

<%= @investment.image.url.split('/').last.split("?")[0] if @investment.image.exists?%>

+

<%= investment_image_file_name(@investment) %>

diff --git a/app/views/budgets/investments/_investment_show.html.erb b/app/views/budgets/investments/_investment_show.html.erb index 5e353f80a..f2658cc94 100644 --- a/app/views/budgets/investments/_investment_show.html.erb +++ b/app/views/budgets/investments/_investment_show.html.erb @@ -9,9 +9,8 @@ class: 'button hollow float-right' %> <% end %> - <% if author_of?(investment, current_user) || (current_user && current_user.administrator?) %> - <% button_text = investment.image.exists? ? t("budgets.investments.show.edit_image") : t("budgets.investments.show.add_image") %> - <%= link_to button_text, + <% if can?(:edit_image, @investment) %> + <%= link_to t("budgets.investments.show.#{investment_image_button_text(@investment)}"), edit_image_budget_investment_path(investment.budget, investment), class: 'button hollow float-right' %> <% end %> @@ -26,19 +25,19 @@

<% if investment.image.exists? %> -
-
-
- <%= image_tag investment.image.url(:large), - title: investment.image_title, - alt: investment.image_title %> -
- <%= investment.image_title %> -
-
-
+
+
+
+ <%= image_tag investment.image.url(:large), + title: investment.image_title, + alt: investment.image_title %> +
+ <%= investment.image_title %> +
+
+
+
-
<% end %>

<%= t("budgets.investments.show.code_html", code: investment.id) %> diff --git a/spec/features/budgets/investments_spec.rb b/spec/features/budgets/investments_spec.rb index 7a125909c..fb3061733 100644 --- a/spec/features/budgets/investments_spec.rb +++ b/spec/features/budgets/investments_spec.rb @@ -392,6 +392,7 @@ feature 'Budget Investments' do scenario "Should show add text when investment has not image" do investment = create(:budget_investment, heading: heading, author: author) login_as(author) + visit budget_investment_path(budget, investment) expect(page).to have_link "Add image" @@ -400,6 +401,7 @@ feature 'Budget Investments' do scenario "Should show edit text when investment has already an image" do investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author) login_as(author) + visit budget_investment_path(budget, investment) expect(page).to have_link "Edit image" @@ -407,6 +409,7 @@ feature 'Budget Investments' do scenario "Should not be shown for anonymous users" do investment = create(:budget_investment, heading: heading) + visit budget_investment_path(budget, investment) expect(page).not_to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment) @@ -414,6 +417,7 @@ feature 'Budget Investments' do scenario "Should not be shown when current user is not investment author" do investment = create(:budget_investment, heading: heading) + visit budget_investment_path(budget, investment) expect(page).not_to have_link "Edit image", href: edit_image_budget_investment_path(budget, investment) @@ -422,6 +426,7 @@ feature 'Budget Investments' do scenario "Should be shown when current user is investment author" do investment = create(:budget_investment, heading: heading, author: author) login_as(author) + visit budget_investment_path(budget, investment) expect(page).to have_link "Add image", href: edit_image_budget_investment_path(budget, investment) @@ -431,6 +436,7 @@ feature 'Budget Investments' do administrator = create(:administrator).user investment = create(:budget_investment, heading: heading, author: author) login_as(administrator) + visit budget_investment_path(budget, investment) expect(page).to have_link "Add image", href: edit_image_budget_investment_path(budget, investment) @@ -439,6 +445,7 @@ feature 'Budget Investments' do scenario "Edit image page should not be accesible when there is no logged user" do investment = create(:budget_investment, heading: heading, author: author) + visit edit_image_budget_investment_path(budget, investment) expect(page).to have_content "You must sign in or register to continue" @@ -448,6 +455,7 @@ feature 'Budget Investments' do other_author = create(:user, :level_two, username: 'Manuel') investment = create(:budget_investment, heading: heading, author: author) login_as(other_author) + visit edit_image_budget_investment_path(budget, investment) expect(page).to have_content "You do not have permission to carry out the action 'edit_image' on budget/investment." @@ -456,6 +464,7 @@ feature 'Budget Investments' do scenario "Edit image page should be accesible when author is currently logged" do investment = create(:budget_investment, heading: heading, author: author) login_as(author) + visit edit_image_budget_investment_path(budget, investment) expect(page).to have_content "Change your project image" @@ -465,6 +474,7 @@ feature 'Budget Investments' do administrator = create(:administrator).user investment = create(:budget_investment, heading: heading, author: author) login_as(administrator) + visit edit_image_budget_investment_path(budget, investment) expect(page).to have_content "Change your project image" @@ -473,6 +483,7 @@ feature 'Budget Investments' do scenario "Remove image button should not be present when investment image does not exists" do investment = create(:budget_investment, heading: heading, author: author) login_as(author) + visit edit_image_budget_investment_path(budget, investment) expect(page).not_to have_link "Remove image" @@ -481,6 +492,7 @@ feature 'Budget Investments' do scenario "Remove image button should be present when investment has an image defined" do investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author) login_as(author) + visit edit_image_budget_investment_path(budget, investment) expect(page).to have_link "Remove image" @@ -490,8 +502,8 @@ feature 'Budget Investments' do administrator = create(:administrator).user investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author) login_as(administrator) - visit edit_image_budget_investment_path(budget, investment) + visit edit_image_budget_investment_path(budget, investment) click_link "Remove image" expect(page).to have_content "Investment project image removed succesfully." @@ -500,8 +512,8 @@ feature 'Budget Investments' do scenario "Remove image should be possible for investment author" do investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author) login_as(author) - visit edit_image_budget_investment_path(budget, investment) + visit edit_image_budget_investment_path(budget, investment) click_link "Remove image" expect(page).to have_content "Investment project image removed succesfully." @@ -510,6 +522,7 @@ feature 'Budget Investments' do scenario "Remove image should not be possible for any other logged users (except administrators and author)" do investment = create(:budget_investment, :with_descriptive_image, heading: heading, author: author) login_as(create(:user)) + visit edit_image_budget_investment_path(budget, investment) expect(page).to have_content "You do not have permission to carry out the action 'edit_image' on budget/investment." @@ -521,6 +534,7 @@ feature 'Budget Investments' do login_as(other_author) visit edit_image_budget_investment_path(investment.budget, investment) + expect(current_path).not_to eq(edit_image_budget_investment_path(investment.budget, investment)) expect(page).to have_content 'You do not have permission' end @@ -533,13 +547,14 @@ feature 'Budget Investments' do fill_in :budget_investment_image_title, with: "New image title" attach_file :budget_investment_image, "spec/fixtures/files/logo_header.jpg" click_on "Save image" + within ".budget-investment-show" do expect(page).to have_css("img[src*='logo_header.jpg']") end expect(page).to have_content 'Investment project image updated succesfully. ' end - scenario "Update image should be possible for authors" do + scenario "Update image should be possible for administrators" do administrator = create(:administrator).user investment = create(:budget_investment, heading: heading, author: author) login_as(administrator) @@ -548,6 +563,7 @@ feature 'Budget Investments' do fill_in :budget_investment_image_title, with: "New image title" attach_file :budget_investment_image, "spec/fixtures/files/logo_header.jpg" click_on "Save image" + within ".budget-investment-show" do expect(page).to have_css("img[src*='logo_header.jpg']") end