diff --git a/app/models/user.rb b/app/models/user.rb index 7240c8929..4bb09c4a0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -133,6 +133,16 @@ class User < ActiveRecord::Base update official_position: nil, official_level: 0 end + def has_official_email? + domain = Setting['email_domain_for_officials'] + !email.blank? && ( (email.end_with? "@#{domain}") || (email.end_with? ".#{domain}") ) + end + + def display_official_position_badge? + return true if official_level > 1 + official_position_badge? && official_level == 1 + end + def block debates_ids = Debate.where(author_id: id).pluck(:id) comments_ids = Comment.where(user_id: id).pluck(:id) @@ -197,11 +207,6 @@ class User < ActiveRecord::Base !erased? end - def has_official_email? - domain = Setting['email_domain_for_officials'] - !email.blank? && ( (email.end_with? "@#{domain}") || (email.end_with? ".#{domain}") ) - end - def locale self[:locale] ||= I18n.default_locale.to_s end diff --git a/app/views/comments/_comment.html.erb b/app/views/comments/_comment.html.erb index 0e88614c5..7b5ff4b7d 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -36,7 +36,7 @@ <%= t("comments.comment.user_deleted") %> <% else %> <%= link_to comment.user.name, user_path(comment.user) %> - <% if comment.user.official? %> + <% if comment.user.display_official_position_badge? %>  •  <%= comment.user.official_position %> diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index c9807041b..09d7d79c0 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -24,7 +24,7 @@ <%= debate.author.name %> - <% if debate.author.official? %> + <% if debate.author.display_official_position_badge? %>  •  <%= debate.author.official_position %> diff --git a/app/views/proposals/_proposal.html.erb b/app/views/proposals/_proposal.html.erb index e1e5d36a0..70c2b9c89 100644 --- a/app/views/proposals/_proposal.html.erb +++ b/app/views/proposals/_proposal.html.erb @@ -26,7 +26,7 @@ <%= proposal.author.name %> - <% if proposal.author.official? %> + <% if proposal.author.display_official_position_badge? %>  •  <%= proposal.author.official_position %> diff --git a/app/views/proposals/summary.html.erb b/app/views/proposals/summary.html.erb index 05ed7dac9..38c56c963 100644 --- a/app/views/proposals/summary.html.erb +++ b/app/views/proposals/summary.html.erb @@ -20,7 +20,7 @@ <%= t("proposals.show.author_deleted") %> <% else %> <%= proposal.author.name %> - <% if proposal.author.official? %> + <% if proposal.author.display_official_position_badge? %> <%= proposal.author.official_position %> diff --git a/app/views/shared/_author_info.html.erb b/app/views/shared/_author_info.html.erb index 1a0315341..d61317321 100644 --- a/app/views/shared/_author_info.html.erb +++ b/app/views/shared/_author_info.html.erb @@ -15,7 +15,7 @@ <% end %> - <% if resource.author.official? %> + <% if resource.author.display_official_position_badge? %>  •  <%= resource.author.official_position %> diff --git a/app/views/spending_proposals/_spending_proposal.html.erb b/app/views/spending_proposals/_spending_proposal.html.erb index af59def70..2854353bb 100644 --- a/app/views/spending_proposals/_spending_proposal.html.erb +++ b/app/views/spending_proposals/_spending_proposal.html.erb @@ -23,7 +23,7 @@ <%= spending_proposal.author.name %> - <% if spending_proposal.author.official? %> + <% if spending_proposal.author.display_official_position_badge? %>  •  <%= spending_proposal.author.official_position %> diff --git a/spec/features/official_positions_spec.rb b/spec/features/official_positions_spec.rb new file mode 100644 index 000000000..01f78370a --- /dev/null +++ b/spec/features/official_positions_spec.rb @@ -0,0 +1,97 @@ +require 'rails_helper' + +feature 'Official positions' do + + context "Badge" do + + background do + @user1 = create(:user, official_level: 1, official_position: "Employee", official_position_badge: true) + @user2 = create(:user, official_level: 0, official_position: "") + end + + scenario "Comments" do + proposal = create(:proposal) + comment1 = create(:comment, commentable: proposal, user: @user1) + comment2 = create(:comment, commentable: proposal, user: @user2) + + visit proposal_path(proposal) + + expect_badge_for("comment", comment1) + expect_no_badge_for("comment", comment2) + end + + context "Debates" do + + background do + @debate1 = create(:debate, author: @user1) + @debate2 = create(:debate, author: @user2) + end + + scenario "Index" do + visit debates_path + + expect_badge_for("debate", @debate1) + expect_no_badge_for("debate", @debate2) + end + + scenario "Show" do + visit debate_path(@debate1) + expect_badge_for("debate", @debate1) + + visit debate_path(@debate2) + expect_no_badge_for("debate", @debate2) + end + + end + + context "Proposals" do + + background do + @proposal1 = create(:proposal, author: @user1) + @proposal2 = create(:proposal, author: @user2) + + featured_proposals = 3.times { create(:proposal) } + end + + scenario "Index" do + visit proposals_path + + expect_badge_for("proposal", @proposal1) + expect_no_badge_for("proposal", @proposal2) + end + + scenario "Show" do + visit proposal_path(@proposal1) + expect_badge_for("proposal", @proposal1) + + visit proposal_path(@proposal2) + expect_no_badge_for("proposal", @proposal2) + end + + end + + context "Spending proposals" do + + background do + @spending_proposal1 = create(:spending_proposal, author: @user1) + @spending_proposal2 = create(:spending_proposal, author: @user2) + end + + scenario "Index" do + visit spending_proposals_path + + expect_badge_for("spending_proposal", @spending_proposal1) + expect_no_badge_for("spending_proposal", @spending_proposal2) + end + + scenario "Show" do + visit spending_proposal_path(@spending_proposal1) + expect_badge_for("spending_proposal", @spending_proposal1) + + visit spending_proposal_path(@spending_proposal2) + expect_no_badge_for("spending_proposal", @spending_proposal2) + end + + end + end +end \ No newline at end of file diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 41455eb1d..c56e7a115 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -289,6 +289,42 @@ describe User do end end + describe "official_position_badge" do + + describe "Users of level 1" do + + it "displays the badge if set in preferences" do + user = create(:user, official_level: 1, official_position_badge: true) + + expect(user.display_official_position_badge?).to eq true + end + + it "does not display the badge if set in preferences" do + user = create(:user, official_level: 1, official_position_badge: false) + + expect(user.display_official_position_badge?).to eq false + end + + end + + describe "Users higher than level 1" do + + it "displays the badge regardless of preferences" do + user1 = create(:user, official_level: 2, official_position_badge: false) + user2 = create(:user, official_level: 3, official_position_badge: false) + user3 = create(:user, official_level: 4, official_position_badge: false) + user4 = create(:user, official_level: 5, official_position_badge: false) + + expect(user1.display_official_position_badge?).to eq true + expect(user2.display_official_position_badge?).to eq true + expect(user3.display_official_position_badge?).to eq true + expect(user4.display_official_position_badge?).to eq true + end + + end + + end + describe "self.search" do it "find users by email" do user1 = create(:user, email: "larry@madrid.es") diff --git a/spec/support/common_actions.rb b/spec/support/common_actions.rb index 80bdde907..ca5c7b64a 100644 --- a/spec/support/common_actions.rb +++ b/spec/support/common_actions.rb @@ -231,4 +231,18 @@ module CommonActions DirectMessage.last end + def expect_badge_for(resource_name, resource) + within("##{resource_name}_#{resource.id}") do + expect(page).to have_css ".label.round" + expect(page).to have_content "Employee" + end + end + + def expect_no_badge_for(resource_name, resource) + within("##{resource_name}_#{resource.id}") do + expect(page).to_not have_css ".label.round" + expect(page).to_not have_content "Employee" + end + end + end