From 98040e191ef04721eea038f838596c5f342bee2a Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jun 2016 19:40:30 +0200 Subject: [PATCH 1/5] fixes conflicts with fork --- app/controllers/account_controller.rb | 2 +- app/views/account/show.html.erb | 11 +++++++++++ config/locales/en.yml | 1 + config/locales/es.yml | 1 + db/migrate/20160617172616_add_badge_to_users.rb | 5 +++++ db/schema.rb | 3 ++- spec/features/account_spec.rb | 2 ++ spec/models/user_spec.rb | 6 ++++++ 8 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20160617172616_add_badge_to_users.rb diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index 9212eb4fa..c3d814b38 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -25,7 +25,7 @@ class AccountController < ApplicationController if @account.organization? params.require(:account).permit(:phone_number, :email_on_comment, :email_on_comment_reply, :newsletter, organization_attributes: [:name, :responsible_name]) else - params.require(:account).permit(:username, :public_activity, :email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter) + params.require(:account).permit(:username, :public_activity, :email_on_comment, :email_on_comment_reply, :email_on_direct_message, :email_digest, :newsletter, :official_position_badge) end end diff --git a/app/views/account/show.html.erb b/app/views/account/show.html.erb index 98987626d..deaa4c889 100644 --- a/app/views/account/show.html.erb +++ b/app/views/account/show.html.erb @@ -87,6 +87,17 @@ <% end %> +
+ <%= f.label :official_position_badge do %> + <%= f.check_box :official_position_badge, + title: t('account.show.official_position_badge_label'), + label: false %> + + <%= t("account.show.official_position_badge_label") %> + + <% end %> +
+ <%= f.submit t("account.show.save_changes_submit"), class: "button" %> diff --git a/config/locales/en.yml b/config/locales/en.yml index b16f1092a..608e0efab 100755 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -17,6 +17,7 @@ en: subscription_to_website_newsletter_label: Receive by email website relevant information email_on_direct_message_label: Receive emails about direct messages email_digest_label: Receive a summary of proposal notifications + official_position_badge_label: Show official position badge title: My account user_permission_debates: Participate on debates user_permission_info: With your account you can... diff --git a/config/locales/es.yml b/config/locales/es.yml index 620499ace..9678ed5c1 100755 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -17,6 +17,7 @@ es: subscription_to_website_newsletter_label: Recibir emails con información interesante sobre la web email_on_direct_message_label: Recibir emails con mensajes privados email_digest_label: Recibir resumen de notificaciones sobre propuestas + official_position_badge_label: Mostrar etiqueta de tipo de usuario title: Mi cuenta user_permission_debates: Participar en debates user_permission_info: Con tu cuenta ya puedes... diff --git a/db/migrate/20160617172616_add_badge_to_users.rb b/db/migrate/20160617172616_add_badge_to_users.rb new file mode 100644 index 000000000..2f634aa49 --- /dev/null +++ b/db/migrate/20160617172616_add_badge_to_users.rb @@ -0,0 +1,5 @@ +class AddBadgeToUsers < ActiveRecord::Migration + def change + add_column :users, :official_position_badge, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index f5671b3a3..6932611e4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160614160949) do +ActiveRecord::Schema.define(version: 20160617172616) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -462,6 +462,7 @@ ActiveRecord::Schema.define(version: 20160614160949) do t.boolean "email_on_proposal_notification", default: true t.boolean "email_digest", default: true t.boolean "email_on_direct_message", default: true + t.boolean "official_position_badge", default: false end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree diff --git a/spec/features/account_spec.rb b/spec/features/account_spec.rb index 0d5da38b2..604872a58 100644 --- a/spec/features/account_spec.rb +++ b/spec/features/account_spec.rb @@ -37,6 +37,7 @@ feature 'Account' do check 'account_email_on_comment_reply' uncheck 'account_email_digest' uncheck 'account_email_on_direct_message' + check 'account_official_position_badge' click_button 'Save changes' expect(page).to have_content "Changes saved" @@ -48,6 +49,7 @@ feature 'Account' do expect(find("#account_email_on_comment_reply")).to be_checked expect(find("#account_email_digest")).to_not be_checked expect(find("#account_email_on_direct_message")).to_not be_checked + expect(find("#account_official_position_badge")).to be_checked end scenario 'Edit Organization' do diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 143053c77..41455eb1d 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -95,6 +95,12 @@ describe User do expect(subject.email_on_direct_message).to eq(true) end end + + describe 'official_position_badge' do + it 'should be false by default' do + expect(subject.official_position_badge).to eq(false) + end + end end describe "administrator?" do From 51d688897aba29b5993317c465a714723db7bf27 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 20 Jun 2016 21:07:32 +0200 Subject: [PATCH 2/5] displays badge option if official position is level 1 --- app/views/account/show.html.erb | 22 +++++++++++---------- spec/features/account_spec.rb | 35 +++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/app/views/account/show.html.erb b/app/views/account/show.html.erb index deaa4c889..247aa6024 100644 --- a/app/views/account/show.html.erb +++ b/app/views/account/show.html.erb @@ -87,16 +87,18 @@ <% end %> -
- <%= f.label :official_position_badge do %> - <%= f.check_box :official_position_badge, - title: t('account.show.official_position_badge_label'), - label: false %> - - <%= t("account.show.official_position_badge_label") %> - - <% end %> -
+ <% if @account.official_level == 1 %> +
+ <%= f.label :official_position_badge do %> + <%= f.check_box :official_position_badge, + title: t('account.show.official_position_badge_label'), + label: false %> + + <%= t("account.show.official_position_badge_label") %> + + <% end %> +
+ <% end %> <%= f.submit t("account.show.save_changes_submit"), class: "button" %> diff --git a/spec/features/account_spec.rb b/spec/features/account_spec.rb index 604872a58..d1c547185 100644 --- a/spec/features/account_spec.rb +++ b/spec/features/account_spec.rb @@ -37,7 +37,6 @@ feature 'Account' do check 'account_email_on_comment_reply' uncheck 'account_email_digest' uncheck 'account_email_on_direct_message' - check 'account_official_position_badge' click_button 'Save changes' expect(page).to have_content "Changes saved" @@ -49,7 +48,6 @@ feature 'Account' do expect(find("#account_email_on_comment_reply")).to be_checked expect(find("#account_email_digest")).to_not be_checked expect(find("#account_email_on_direct_message")).to_not be_checked - expect(find("#account_official_position_badge")).to be_checked end scenario 'Edit Organization' do @@ -71,6 +69,39 @@ feature 'Account' do expect(find("#account_email_on_comment_reply")).to be_checked end + context "Option to display badge for official position" do + + scenario "Users with official position of level 1" do + official_user = create(:user, official_level: 1) + + login_as(official_user) + visit account_path + + check 'account_official_position_badge' + click_button 'Save changes' + expect(page).to have_content "Changes saved" + + visit account_path + expect(find("#account_official_position_badge")).to be_checked + end + + scenario "Users with official position of level 2 and above" do + official_user2 = create(:user, official_level: 2) + official_user3 = create(:user, official_level: 3) + + login_as(official_user2) + visit account_path + + expect(page).to_not have_css '#account_official_position_badge' + + login_as(official_user3) + visit account_path + + expect(page).to_not have_css '#account_official_position_badge' + end + + end + scenario "Errors on edit" do visit account_path From 9e910a834baeddb31da012f78dde85dad542a655 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 20 Jun 2016 21:08:49 +0200 Subject: [PATCH 3/5] Displays badge for official positions --- app/models/user.rb | 15 ++- app/views/comments/_comment.html.erb | 2 +- app/views/debates/_debate.html.erb | 2 +- app/views/proposals/_proposal.html.erb | 2 +- app/views/proposals/summary.html.erb | 2 +- app/views/shared/_author_info.html.erb | 2 +- .../_spending_proposal.html.erb | 2 +- spec/features/official_positions_spec.rb | 97 +++++++++++++++++++ spec/models/user_spec.rb | 36 +++++++ spec/support/common_actions.rb | 14 +++ 10 files changed, 163 insertions(+), 11 deletions(-) create mode 100644 spec/features/official_positions_spec.rb 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 From 00dbbd0689f90e97ab65823ddb4783efe8ceebd2 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 20 Jun 2016 21:54:45 +0200 Subject: [PATCH 4/5] fixes specs (unauthenticated users can view spending proposal show) --- app/controllers/spending_proposals_controller.rb | 2 +- spec/features/spending_proposals_spec.rb | 3 --- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/app/controllers/spending_proposals_controller.rb b/app/controllers/spending_proposals_controller.rb index 3f590f414..5f8b04717 100644 --- a/app/controllers/spending_proposals_controller.rb +++ b/app/controllers/spending_proposals_controller.rb @@ -1,7 +1,7 @@ class SpendingProposalsController < ApplicationController include FeatureFlags - before_action :authenticate_user!, except: [:index] + before_action :authenticate_user!, except: [:index, :show] before_action -> { flash.now[:notice] = flash[:notice].html_safe if flash[:html_safe] && flash[:notice] } load_and_authorize_resource diff --git a/spec/features/spending_proposals_spec.rb b/spec/features/spending_proposals_spec.rb index 8b52bd014..01220ab27 100644 --- a/spec/features/spending_proposals_spec.rb +++ b/spec/features/spending_proposals_spec.rb @@ -178,9 +178,6 @@ feature 'Spending proposals' do end scenario "Show" do - user = create(:user) - login_as(user) - spending_proposal = create(:spending_proposal, geozone: create(:geozone), association_name: 'People of the neighbourhood') From 1f7e86e206736125403dcfdb84ca3d110dbc4e81 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 20 Jun 2016 22:06:49 +0200 Subject: [PATCH 5/5] fixes specs --- app/views/admin/stats/proposal_notifications.html.erb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/views/admin/stats/proposal_notifications.html.erb b/app/views/admin/stats/proposal_notifications.html.erb index 79e19a247..51130424d 100644 --- a/app/views/admin/stats/proposal_notifications.html.erb +++ b/app/views/admin/stats/proposal_notifications.html.erb @@ -7,14 +7,18 @@

<%= t("admin.stats.proposal_notifications.proposals_with_notifications") %>
- <%= @proposals_with_notifications %> + + <%= @proposals_with_notifications %> +