From 3551b55bdddb9ab6b966d63e25fe96918a3d2811 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 11 Aug 2015 18:57:16 +0200 Subject: [PATCH 01/33] add migration for User organization fields --- ...0150810155304_add_organization_to_users.rb | 7 +++++++ db/schema.rb | 21 +++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20150810155304_add_organization_to_users.rb diff --git a/db/migrate/20150810155304_add_organization_to_users.rb b/db/migrate/20150810155304_add_organization_to_users.rb new file mode 100644 index 000000000..b5c38400c --- /dev/null +++ b/db/migrate/20150810155304_add_organization_to_users.rb @@ -0,0 +1,7 @@ +class AddOrganizationToUsers < ActiveRecord::Migration + def change + add_column :users, :organization_name, :string, limit: 80 + add_column :users, :organization_verified_at, :datetime + add_column :users, :phone_number, :string, limit: 30 + end +end diff --git a/db/schema.rb b/db/schema.rb index 2d97d8a60..343294168 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: 20150807140346) do +ActiveRecord::Schema.define(version: 20150810155304) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -74,18 +74,18 @@ ActiveRecord::Schema.define(version: 20150807140346) do add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree create_table "users", force: :cascade do |t| - t.string "email", default: "", null: false - t.string "encrypted_password", default: "", null: false + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false + t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "first_name" t.string "last_name" t.string "confirmation_token" @@ -93,9 +93,12 @@ ActiveRecord::Schema.define(version: 20150807140346) do t.datetime "confirmation_sent_at" t.string "unconfirmed_email" t.string "nickname" - t.boolean "use_nickname", default: false, null: false - t.boolean "email_on_debate_comment", default: false - t.boolean "email_on_comment_reply", default: false + t.boolean "use_nickname", default: false, null: false + t.boolean "email_on_debate_comment", default: false + t.boolean "email_on_comment_reply", default: false + t.string "organization_name", limit: 80 + t.datetime "organization_verified_at" + t.string "phone_number", limit: 30 end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree From 83e3dd1c6fa738618294388cc6e8c9ec48c3541c Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 11 Aug 2015 19:15:00 +0200 Subject: [PATCH 02/33] Implements User#organization? and User#verified_organization? --- app/models/user.rb | 8 ++++++++ spec/models/user_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 4eb832546..c6e477815 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,4 +27,12 @@ class User < ActiveRecord::Base def moderator? @is_moderator ||= Moderator.where(user_id: id).exists? end + + def organization? + organization_name.present? + end + + def verified_organization? + organization_verified_at.present? + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 4a8fe8fd7..b57595660 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -111,4 +111,24 @@ describe User do end end + describe "organization?" do + it "is false when organization_name is blank" do + expect(subject.organization?).to be false + end + it "is true when organization_name exists" do + subject.organization_name = "Anonymous" + expect(subject.organization?).to be true + end + end + + describe "verified_organization?" do + it "is false when organization_verified_at? is blank" do + expect(subject.verified_organization?).to be false + end + it "is true when organization_verified_at? exists" do + subject.organization_verified_at = Time.now + expect(subject.verified_organization?).to be true + end + end + end From 9ee1e5dbc54fe3e56073104da529ae6b78472297 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 11 Aug 2015 19:34:18 +0200 Subject: [PATCH 03/33] Adds Abilities for Organizations --- app/models/ability.rb | 14 +++++++++++--- spec/models/ability_spec.rb | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index c1fc24ae3..3314bc321 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -8,14 +8,22 @@ class Ability if user # logged-in users can [:read, :update], User, id: user.id - can [:read, :create, :vote], Debate + can :read, Debate can :update, Debate do |debate| debate.editable_by?(user) end - can [:create, :vote], Comment + unless user.organization? + can :vote, Debate + can :vote, Comment + end - if user.moderator? or user.administrator? + if !user.organization? || user.verified_organization? + can :create, Comment + can :create, Debate + end + + if user.moderator? || user.administrator? elsif user.administrator? diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 3954ec600..352f8085c 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -45,6 +45,31 @@ describe Ability do end end + describe "Organization" do + let(:user) { create(:user, organization_name: "Organization") } + + it { should be_able_to(:show, user) } + it { should be_able_to(:edit, user) } + + it { should be_able_to(:index, Debate) } + it { should be_able_to(:show, debate) } + + it { should_not be_able_to(:vote, debate) } + it { should_not be_able_to(:vote, Comment) } + + describe "Not verified" do + it { should_not be_able_to(:create, Comment) } + it { should_not be_able_to(:create, Debate) } + end + + describe "Verified" do + before(:each) { user.organization_verified_at = Time.now } + + it { should be_able_to(:create, Comment) } + it { should be_able_to(:create, Debate) } + end + end + describe "Moderator" do let(:user) { create(:user) } before { create(:moderator, user: user) } From 523e69fb02b4ee614478cdeeda945a248226df63 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 11 Aug 2015 19:48:11 +0200 Subject: [PATCH 04/33] Adds validations for organizations --- app/models/user.rb | 20 ++++++++++++++++---- spec/models/user_spec.rb | 12 ++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index c6e477815..6ced65533 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,12 +4,14 @@ class User < ActiveRecord::Base acts_as_voter - validates :first_name, presence: true, unless: :use_nickname? - validates :last_name, presence: true, unless: :use_nickname? - validates :nickname, presence: true, if: :use_nickname? + validates :first_name, presence: true, if: :use_first_name? + validates :last_name, presence: true, if: :use_last_name? + validates :nickname, presence: true, if: :use_nickname? def name - use_nickname? ? nickname : "#{first_name} #{last_name}" + return nickname if use_nickname? + return organization_name if organization? + "#{first_name} #{last_name}" end def votes_on_debates(debates_ids = []) @@ -35,4 +37,14 @@ class User < ActiveRecord::Base def verified_organization? organization_verified_at.present? end + + private + def use_first_name? + !use_nickname? && !organization? + end + + def use_last_name? + use_first_name? + end + end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index b57595660..58c2c3cba 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -119,6 +119,18 @@ describe User do subject.organization_name = "Anonymous" expect(subject.organization?).to be true end + + it "deactivates the validation of first_name and last_name " do + subject.first_name = nil + subject.last_name = nil + subject.organization_name = "The A Team" + expect(subject).to be_valid + end + + it "calculates the name using the organization name" do + subject.organization_name = "The A Team" + expect(subject.name).to eq("The A Team") + end end describe "verified_organization?" do From 5cef152c37fa6991e513fafbdb0be7f736045b94 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 12 Aug 2015 13:47:47 +0200 Subject: [PATCH 05/33] Adds organization rejection & tests --- app/models/user.rb | 8 ++++- ...2_add_organization_rejected_at_to_users.rb | 5 ++++ db/schema.rb | 12 +++++++- spec/models/user_spec.rb | 30 +++++++++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20150812104712_add_organization_rejected_at_to_users.rb diff --git a/app/models/user.rb b/app/models/user.rb index 6ced65533..0bee83446 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -35,7 +35,13 @@ class User < ActiveRecord::Base end def verified_organization? - organization_verified_at.present? + organization_verified_at.present? && + (organization_rejected_at.blank? || organization_rejected_at < organization_verified_at) + end + + def rejected_organization? + organization_rejected_at.present? && + (organization_verified_at.blank? || organization_verified_at < organization_rejected_at) end private diff --git a/db/migrate/20150812104712_add_organization_rejected_at_to_users.rb b/db/migrate/20150812104712_add_organization_rejected_at_to_users.rb new file mode 100644 index 000000000..a097c7b34 --- /dev/null +++ b/db/migrate/20150812104712_add_organization_rejected_at_to_users.rb @@ -0,0 +1,5 @@ +class AddOrganizationRejectedAtToUsers < ActiveRecord::Migration + def change + add_column :users, :organization_rejected_at, :datetime + end +end diff --git a/db/schema.rb b/db/schema.rb index 343294168..79860f41f 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: 20150810155304) do +ActiveRecord::Schema.define(version: 20150812104712) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -53,6 +53,15 @@ ActiveRecord::Schema.define(version: 20150810155304) do add_index "moderators", ["user_id"], name: "index_moderators_on_user_id", using: :btree + create_table "simple_captcha_data", force: :cascade do |t| + t.string "key", limit: 40 + t.string "value", limit: 6 + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "simple_captcha_data", ["key"], name: "idx_key", using: :btree + create_table "taggings", force: :cascade do |t| t.integer "tag_id" t.integer "taggable_id" @@ -99,6 +108,7 @@ ActiveRecord::Schema.define(version: 20150810155304) do t.string "organization_name", limit: 80 t.datetime "organization_verified_at" t.string "phone_number", limit: 30 + t.datetime "organization_rejected_at" end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 58c2c3cba..c258e2bd1 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -141,6 +141,36 @@ describe User do subject.organization_verified_at = Time.now expect(subject.verified_organization?).to be true end + it "is false when the organization was verified and then rejected" do + subject.organization_verified_at = Time.now + subject.organization_rejected_at = Time.now + 1 + expect(subject.verified_organization?).to be false + end + it "is true when the organization was rejected and then verified" do + subject.organization_rejected_at = Time.now + subject.organization_verified_at = Time.now + 1 + expect(subject.verified_organization?).to be true + end + end + + describe "rejected_organization?" do + it "is false when organization_rejected_at? is blank" do + expect(subject.rejected_organization?).to be false + end + it "is true when organization_rejected_at? exists" do + subject.organization_rejected_at = Time.now + expect(subject.rejected_organization?).to be true + end + it "is true when the organization was verified and then rejected" do + subject.organization_verified_at = Time.now + subject.organization_rejected_at = Time.now + 1 + expect(subject.rejected_organization?).to be true + end + it "is false when the organization was rejected and then verified" do + subject.organization_rejected_at = Time.now + subject.organization_verified_at = Time.now + 1 + expect(subject.rejected_organization?).to be false + end end end From 450b0098c6efc9f8b311677ae6f5153c18f2a48b Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 12 Aug 2015 17:48:25 +0200 Subject: [PATCH 06/33] Adds abilities for verifying/rejecting organizations --- app/models/ability.rb | 7 +++++++ spec/models/ability_spec.rb | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/app/models/ability.rb b/app/models/ability.rb index 3314bc321..d1e47095f 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -24,6 +24,13 @@ class Ability end if user.moderator? || user.administrator? + can :verify_organization, User do |u| + !u.verified_organization? + end + + can :reject_organization, User do |u| + !u.rejected_organization? + end elsif user.administrator? diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 352f8085c..896794310 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -78,6 +78,20 @@ describe Ability do it { should be_able_to(:show, debate) } it { should be_able_to(:vote, debate) } + describe "organizations" do + let(:pending_organization) { create(:user, organization_name: 'org') } + let(:rejected_organization) { create(:user, organization_name: 'org', organization_rejected_at: Time.now)} + let(:verified_organization) { create(:user, organization_name: 'org', organization_verified_at: Time.now)} + + it { should be_able_to( :verify_organization, pending_organization) } + it { should be_able_to( :reject_organization, pending_organization) } + + it { should_not be_able_to(:verify_organization, verified_organization) } + it { should be_able_to( :reject_organization, verified_organization) } + + it { should_not be_able_to(:reject_organization, rejected_organization) } + it { should be_able_to( :verify_organization, rejected_organization) } + end end describe "Administrator" do From 674e2c5b8cf52bebbfd4115f794db96982cfd86c Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 12 Aug 2015 17:49:01 +0200 Subject: [PATCH 07/33] Adds User.organizations scope --- app/models/user.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 0bee83446..04acd124c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,6 +8,8 @@ class User < ActiveRecord::Base validates :last_name, presence: true, if: :use_last_name? validates :nickname, presence: true, if: :use_nickname? + scope :organizations, -> { where("users.organization_name IS NOT NULL AND users.organization_name <> ''") } + def name return nickname if use_nickname? return organization_name if organization? From cb301c44266cfdde3caf8e5241478a10aff99bd3 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 12 Aug 2015 18:24:57 +0200 Subject: [PATCH 08/33] adds organization factory and refactors ability_spec --- spec/factories.rb | 9 +++++++++ spec/models/ability_spec.rb | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/spec/factories.rb b/spec/factories.rb index fc76685d0..582c3e0a9 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -8,6 +8,13 @@ FactoryGirl.define do confirmed_at { Time.now } end + factory :organization, class: User do + organization_name 'org' + sequence(:email) { |n| "org#{n}@madrid.es" } + password 'pleaseverifyme' + confirmed_at { Time.now } + end + factory :debate do sequence(:title) {|n| "Debate #{n} title"} description 'Debate description' @@ -39,4 +46,6 @@ FactoryGirl.define do user end + + end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 896794310..952f1cf1f 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -46,7 +46,7 @@ describe Ability do end describe "Organization" do - let(:user) { create(:user, organization_name: "Organization") } + let(:user) { create(:organization) } it { should be_able_to(:show, user) } it { should be_able_to(:edit, user) } From a59ef10e446b2a247ad8aa50e0c86e756ec5ad92 Mon Sep 17 00:00:00 2001 From: kikito Date: Wed, 12 Aug 2015 18:26:12 +0200 Subject: [PATCH 09/33] adds organization controller, with views and tests --- .../moderation/organizations_controller.rb | 25 ++++++++ .../moderation/organizations/index.html.erb | 31 ++++++++++ config/locales/moderation.en.yml | 9 ++- config/locales/moderation.es.yml | 10 +++- config/routes.rb | 6 ++ .../features/moderation/organizations_spec.rb | 57 +++++++++++++++++++ 6 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 app/controllers/moderation/organizations_controller.rb create mode 100644 app/views/moderation/organizations/index.html.erb create mode 100644 spec/features/moderation/organizations_spec.rb diff --git a/app/controllers/moderation/organizations_controller.rb b/app/controllers/moderation/organizations_controller.rb new file mode 100644 index 000000000..4788d3aa2 --- /dev/null +++ b/app/controllers/moderation/organizations_controller.rb @@ -0,0 +1,25 @@ +class Moderation::OrganizationsController < Moderation::BaseController + + before_filter :load_organizations, only: :index + load_and_authorize_resource class: 'User' + + def index + end + + def verify_organization + @organization.update(organization_verified_at: Time.now) + redirect_to action: :index + end + + def reject_organization + @organization.update(organization_rejected_at: Time.now) + redirect_to action: :index + end + + private + + def load_organizations + @organizations = User.organizations.order(:organization_name, :email) + end + +end diff --git a/app/views/moderation/organizations/index.html.erb b/app/views/moderation/organizations/index.html.erb new file mode 100644 index 000000000..3f1d833b6 --- /dev/null +++ b/app/views/moderation/organizations/index.html.erb @@ -0,0 +1,31 @@ +

<%= t('moderation.organizations.index.title') %>

+ + +<% @organizations.each do |organization| %> + + + + + <% if organization.verified_organization? %> + + <% end %> + <% if can? :verify_organization, organization %> + + <% end %> + <% if organization.rejected_organization? %> + + <% end %> + <% if can? :reject_organization, organization %> + + <% end %> + +<% end %> +
<%= organization.organization_name %><%= organization.email %><%= organization.phone_number %><%= t('moderation.organizations.index.verified') %><%= link_to t('moderation.organizations.index.verify'), + verify_organization_moderation_organization_path(organization), + method: :put + %> + <%= t('moderation.organizations.index.rejected') %><%= link_to t('moderation.organizations.index.reject'), + reject_organization_moderation_organization_path(organization), + method: :put + %> +
diff --git a/config/locales/moderation.en.yml b/config/locales/moderation.en.yml index f6cf170e6..c3a765104 100644 --- a/config/locales/moderation.en.yml +++ b/config/locales/moderation.en.yml @@ -2,4 +2,11 @@ en: moderation: dashboard: index: - title: Moderation \ No newline at end of file + title: Moderation + organizations: + index: + title: Organizations + verify: Verify + reject: Reject + verified: Verified + rejected: Rejected diff --git a/config/locales/moderation.es.yml b/config/locales/moderation.es.yml index d54710977..958bd7c75 100644 --- a/config/locales/moderation.es.yml +++ b/config/locales/moderation.es.yml @@ -2,4 +2,12 @@ es: moderation: dashboard: index: - title: Moderación \ No newline at end of file + title: Moderación + organizations: + index: + title: Organizaciones + verify: Verificar + reject: Rechazar + verified: Verificado + rejected: Rechazado + diff --git a/config/routes.rb b/config/routes.rb index 74179ba2a..87eceda55 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,6 +27,12 @@ Rails.application.routes.draw do namespace :moderation do root to: "dashboard#index" + resources :organizations, only: :index do + member do + put :verify_organization + put :reject_organization + end + end end # Example of regular route: diff --git a/spec/features/moderation/organizations_spec.rb b/spec/features/moderation/organizations_spec.rb new file mode 100644 index 000000000..9c07a2b9c --- /dev/null +++ b/spec/features/moderation/organizations_spec.rb @@ -0,0 +1,57 @@ +require 'rails_helper' + +feature 'Moderations::Organizations' do + + + background do + moderator = create(:user) + create(:moderator, user: moderator) + + login_as(moderator) + end + + scenario "pending organizations have links to verify and reject" do + organization = create(:organization) + + visit moderation_organizations_path + expect(page).to have_selector(:link_or_button, 'Verify') + expect(page).to have_selector(:link_or_button, 'Reject') + + click_on 'Verify' + expect(current_path).to eq(moderation_organizations_path) + expect(page).to have_content ('Verified') + + expect(organization.reload.verified_organization?).to eq(true) + end + + scenario "verified organizations have link to reject" do + organization = create(:organization, organization_verified_at: Time.now) + + visit moderation_organizations_path + expect(page).to have_content ('Verified') + expect(page).to_not have_selector(:link_or_button, 'Verify') + expect(page).to have_selector(:link_or_button, 'Reject') + + click_on 'Reject' + expect(current_path).to eq(moderation_organizations_path) + expect(page).to have_content ('Rejected') + + expect(organization.reload.rejected_organization?).to eq(true) + end + + scenario "rejected organizations have link to verify" do + organization = create(:organization, organization_rejected_at: Time.now) + + visit moderation_organizations_path + expect(page).to have_content ('Rejected') + expect(page).to have_selector(:link_or_button, 'Verify') + expect(page).to_not have_selector(:link_or_button, 'Reject') + + click_on 'Verify' + expect(current_path).to eq(moderation_organizations_path) + expect(page).to have_content ('Verified') + + expect(organization.reload.verified_organization?).to eq(true) + end + +end From 91804b5f7c4e191f84847f330740b4748370fb0a Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 13 Aug 2015 18:18:49 +0200 Subject: [PATCH 10/33] replaces organization migrations with a new table --- ...0150810155304_add_organization_to_users.rb | 7 ---- ...2_add_organization_rejected_at_to_users.rb | 5 --- .../20150813142213_create_organizations.rb | 10 ++++++ ...0150813161654_add_phone_number_to_users.rb | 5 +++ db/schema.rb | 36 +++++++++++-------- 5 files changed, 37 insertions(+), 26 deletions(-) delete mode 100644 db/migrate/20150810155304_add_organization_to_users.rb delete mode 100644 db/migrate/20150812104712_add_organization_rejected_at_to_users.rb create mode 100644 db/migrate/20150813142213_create_organizations.rb create mode 100644 db/migrate/20150813161654_add_phone_number_to_users.rb diff --git a/db/migrate/20150810155304_add_organization_to_users.rb b/db/migrate/20150810155304_add_organization_to_users.rb deleted file mode 100644 index b5c38400c..000000000 --- a/db/migrate/20150810155304_add_organization_to_users.rb +++ /dev/null @@ -1,7 +0,0 @@ -class AddOrganizationToUsers < ActiveRecord::Migration - def change - add_column :users, :organization_name, :string, limit: 80 - add_column :users, :organization_verified_at, :datetime - add_column :users, :phone_number, :string, limit: 30 - end -end diff --git a/db/migrate/20150812104712_add_organization_rejected_at_to_users.rb b/db/migrate/20150812104712_add_organization_rejected_at_to_users.rb deleted file mode 100644 index a097c7b34..000000000 --- a/db/migrate/20150812104712_add_organization_rejected_at_to_users.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddOrganizationRejectedAtToUsers < ActiveRecord::Migration - def change - add_column :users, :organization_rejected_at, :datetime - end -end diff --git a/db/migrate/20150813142213_create_organizations.rb b/db/migrate/20150813142213_create_organizations.rb new file mode 100644 index 000000000..a0974df7f --- /dev/null +++ b/db/migrate/20150813142213_create_organizations.rb @@ -0,0 +1,10 @@ +class CreateOrganizations < ActiveRecord::Migration + def change + create_table :organizations do |t| + t.belongs_to :user, index: true, foreign_key: true + t.string :name, limit: 80 + t.datetime :verified_at + t.datetime :rejected_at + end + end +end diff --git a/db/migrate/20150813161654_add_phone_number_to_users.rb b/db/migrate/20150813161654_add_phone_number_to_users.rb new file mode 100644 index 000000000..070b99586 --- /dev/null +++ b/db/migrate/20150813161654_add_phone_number_to_users.rb @@ -0,0 +1,5 @@ +class AddPhoneNumberToUsers < ActiveRecord::Migration + def change + add_column :users, :phone_number, :string, limit: 30 + end +end diff --git a/db/schema.rb b/db/schema.rb index 79860f41f..2c4e7e639 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: 20150812104712) do +ActiveRecord::Schema.define(version: 20150813161654) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -28,12 +28,13 @@ ActiveRecord::Schema.define(version: 20150812104712) do t.string "title" t.text "body" t.string "subject" - t.integer "user_id", null: false + t.integer "user_id", null: false t.integer "parent_id" t.integer "lft" t.integer "rgt" t.datetime "created_at" t.datetime "updated_at" + t.integer "children_count", default: 0 end add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type", using: :btree @@ -53,6 +54,15 @@ ActiveRecord::Schema.define(version: 20150812104712) do add_index "moderators", ["user_id"], name: "index_moderators_on_user_id", using: :btree + create_table "organizations", force: :cascade do |t| + t.integer "user_id" + t.string "name", limit: 80 + t.datetime "verified_at" + t.datetime "rejected_at" + end + + add_index "organizations", ["user_id"], name: "index_organizations_on_user_id", using: :btree + create_table "simple_captcha_data", force: :cascade do |t| t.string "key", limit: 40 t.string "value", limit: 6 @@ -83,18 +93,18 @@ ActiveRecord::Schema.define(version: 20150812104712) do add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree create_table "users", force: :cascade do |t| - t.string "email", default: "", null: false - t.string "encrypted_password", default: "", null: false + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false t.string "reset_password_token" t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false + t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" t.string "current_sign_in_ip" t.string "last_sign_in_ip" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.string "first_name" t.string "last_name" t.string "confirmation_token" @@ -102,13 +112,10 @@ ActiveRecord::Schema.define(version: 20150812104712) do t.datetime "confirmation_sent_at" t.string "unconfirmed_email" t.string "nickname" - t.boolean "use_nickname", default: false, null: false - t.boolean "email_on_debate_comment", default: false - t.boolean "email_on_comment_reply", default: false - t.string "organization_name", limit: 80 - t.datetime "organization_verified_at" - t.string "phone_number", limit: 30 - t.datetime "organization_rejected_at" + t.boolean "use_nickname", default: false, null: false + t.boolean "email_on_debate_comment", default: false + t.boolean "email_on_comment_reply", default: false + t.string "phone_number", limit: 30 end add_index "users", ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true, using: :btree @@ -132,4 +139,5 @@ ActiveRecord::Schema.define(version: 20150812104712) do add_foreign_key "administrators", "users" add_foreign_key "moderators", "users" + add_foreign_key "organizations", "users" end From 19db7f9ddaaac066a524e61114825375c893f285 Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 13 Aug 2015 20:02:29 +0200 Subject: [PATCH 11/33] Updates factories to use the new organization table structure --- spec/factories.rb | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 582c3e0a9..6e7c25413 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -8,13 +8,6 @@ FactoryGirl.define do confirmed_at { Time.now } end - factory :organization, class: User do - organization_name 'org' - sequence(:email) { |n| "org#{n}@madrid.es" } - password 'pleaseverifyme' - confirmed_at { Time.now } - end - factory :debate do sequence(:title) {|n| "Debate #{n} title"} description 'Debate description' @@ -46,6 +39,17 @@ FactoryGirl.define do user end + factory :organization do + user + sequence(:name) { |n| "org#{n}" } + end + factory :verified_organization, parent: :organization do + verified_at { Time.now} + end + + factory :rejected_organization, parent: :organization do + rejected_at { Time.now} + end end From b4b69d89e7445dcada46faee327949446692afb7 Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 13 Aug 2015 20:03:38 +0200 Subject: [PATCH 12/33] Updates abilities for the new organisations Note that unverified organisations can now create debates and comments - that is a change over the initial request --- app/models/ability.rb | 17 ++++++----------- spec/models/ability_spec.rb | 38 +++++++++++++++---------------------- 2 files changed, 21 insertions(+), 34 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index d1e47095f..0d1a518bf 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -13,24 +13,19 @@ class Ability debate.editable_by?(user) end + can :create, Comment + can :create, Debate + unless user.organization? can :vote, Debate can :vote, Comment end - if !user.organization? || user.verified_organization? - can :create, Comment - can :create, Debate - end - if user.moderator? || user.administrator? - can :verify_organization, User do |u| - !u.verified_organization? - end - can :reject_organization, User do |u| - !u.rejected_organization? - end + can :read, Organization + can(:verify, Organization){ |o| !o.verified? } + can(:reject, Organization){ |o| !o.rejected? } elsif user.administrator? diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 952f1cf1f..0cc6edcc8 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -46,28 +46,18 @@ describe Ability do end describe "Organization" do - let(:user) { create(:organization) } + let(:user) { create(:user) } + before(:each) { create(:organization, user: user) } it { should be_able_to(:show, user) } it { should be_able_to(:edit, user) } it { should be_able_to(:index, Debate) } it { should be_able_to(:show, debate) } - it { should_not be_able_to(:vote, debate) } + + it { should be_able_to(:create, Comment) } it { should_not be_able_to(:vote, Comment) } - - describe "Not verified" do - it { should_not be_able_to(:create, Comment) } - it { should_not be_able_to(:create, Debate) } - end - - describe "Verified" do - before(:each) { user.organization_verified_at = Time.now } - - it { should be_able_to(:create, Comment) } - it { should be_able_to(:create, Debate) } - end end describe "Moderator" do @@ -78,19 +68,21 @@ describe Ability do it { should be_able_to(:show, debate) } it { should be_able_to(:vote, debate) } + it { should be_able_to(:read, Organization) } + describe "organizations" do - let(:pending_organization) { create(:user, organization_name: 'org') } - let(:rejected_organization) { create(:user, organization_name: 'org', organization_rejected_at: Time.now)} - let(:verified_organization) { create(:user, organization_name: 'org', organization_verified_at: Time.now)} + let(:pending_organization) { create(:organization) } + let(:rejected_organization) { create(:rejected_organization) } + let(:verified_organization) { create(:verified_organization) } - it { should be_able_to( :verify_organization, pending_organization) } - it { should be_able_to( :reject_organization, pending_organization) } + it { should be_able_to( :verify, pending_organization) } + it { should be_able_to( :reject, pending_organization) } - it { should_not be_able_to(:verify_organization, verified_organization) } - it { should be_able_to( :reject_organization, verified_organization) } + it { should_not be_able_to(:verify, verified_organization) } + it { should be_able_to( :reject, verified_organization) } - it { should_not be_able_to(:reject_organization, rejected_organization) } - it { should be_able_to( :verify_organization, rejected_organization) } + it { should_not be_able_to(:reject, rejected_organization) } + it { should be_able_to( :verify, rejected_organization) } end end From bdf462c16b35f6543682fbee564d66956e382e4e Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 13 Aug 2015 20:04:21 +0200 Subject: [PATCH 13/33] Adds new Organization model --- app/models/organization.rb | 26 ++++++++++++++++++ spec/models/organization_spec.rb | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 app/models/organization.rb create mode 100644 spec/models/organization_spec.rb diff --git a/app/models/organization.rb b/app/models/organization.rb new file mode 100644 index 000000000..8bcb377b6 --- /dev/null +++ b/app/models/organization.rb @@ -0,0 +1,26 @@ +class Organization < ActiveRecord::Base + + belongs_to :user + + delegate :email, :phone_number, to: :user + + def verify + update(verified_at: Time.now) + end + + def reject + update(rejected_at: Time.now) + end + + def verified? + verified_at.present? && + (rejected_at.blank? || rejected_at < verified_at) + end + + def rejected? + rejected_at.present? && + (verified_at.blank? || verified_at < rejected_at) + end + + +end diff --git a/spec/models/organization_spec.rb b/spec/models/organization_spec.rb new file mode 100644 index 000000000..6fad93d5b --- /dev/null +++ b/spec/models/organization_spec.rb @@ -0,0 +1,46 @@ +require 'rails_helper' + +describe Organization do + + subject { create(:organization) } + + describe "verified?" do + it "is false when verified_at? is blank" do + expect(subject.verified?).to be false + end + it "is true when verified_at? exists" do + subject.verified_at = Time.now + expect(subject.verified?).to be true + end + it "is false when the organization was verified and then rejected" do + subject.verified_at = Time.now + subject.rejected_at = Time.now + 1 + expect(subject.verified?).to be false + end + it "is true when the organization was rejected and then verified" do + subject.rejected_at = Time.now + subject.verified_at = Time.now + 1 + expect(subject.verified?).to be true + end + end + + describe "rejected?" do + it "is false when rejected_at? is blank" do + expect(subject.rejected?).to be false + end + it "is true when rejected_at? exists" do + subject.rejected_at = Time.now + expect(subject.rejected?).to be true + end + it "is true when the organization was verified and then rejected" do + subject.verified_at = Time.now + subject.rejected_at = Time.now + 1 + expect(subject.rejected?).to be true + end + it "is false when the organization was rejected and then verified" do + subject.rejected_at = Time.now + subject.verified_at = Time.now + 1 + expect(subject.rejected?).to be false + end + end +end From 976c88177da4ef6fc25151c0608705e2e7aa4712 Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 13 Aug 2015 20:04:57 +0200 Subject: [PATCH 14/33] Updates User model, moving org stuff to Organisation model --- app/models/user.rb | 26 +++++++--------- spec/models/user_spec.rb | 66 +++++++++------------------------------- 2 files changed, 25 insertions(+), 67 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 04acd124c..9fb1f65c4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,11 +8,17 @@ class User < ActiveRecord::Base validates :last_name, presence: true, if: :use_last_name? validates :nickname, presence: true, if: :use_nickname? - scope :organizations, -> { where("users.organization_name IS NOT NULL AND users.organization_name <> ''") } + has_one :administrator + has_one :moderator + has_one :organization + + scope :administrators, -> { joins(:administrators) } + scope :moderators, -> { joins(:moderator) } + scope :organizations, -> { joins(:organization) } def name return nickname if use_nickname? - return organization_name if organization? + return organization.name if organization? "#{first_name} #{last_name}" end @@ -25,25 +31,15 @@ class User < ActiveRecord::Base end def administrator? - @is_administrator ||= Administrator.where(user_id: id).exists? + administrator.present? end def moderator? - @is_moderator ||= Moderator.where(user_id: id).exists? + moderator.present? end def organization? - organization_name.present? - end - - def verified_organization? - organization_verified_at.present? && - (organization_rejected_at.blank? || organization_rejected_at < organization_verified_at) - end - - def rejected_organization? - organization_rejected_at.present? && - (organization_verified_at.blank? || organization_verified_at < organization_rejected_at) + organization.present? end private diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index c258e2bd1..b357d2818 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -112,64 +112,26 @@ describe User do end describe "organization?" do - it "is false when organization_name is blank" do + it "is false when the user is not an organization" do expect(subject.organization?).to be false end - it "is true when organization_name exists" do - subject.organization_name = "Anonymous" - expect(subject.organization?).to be true - end - it "deactivates the validation of first_name and last_name " do - subject.first_name = nil - subject.last_name = nil - subject.organization_name = "The A Team" - expect(subject).to be_valid - end + describe 'when it is an organization' do + before(:each) { create(:organization, user: subject) } - it "calculates the name using the organization name" do - subject.organization_name = "The A Team" - expect(subject.name).to eq("The A Team") - end - end + it "is true when the user is an organization" do + expect(subject.organization?).to be true + end - describe "verified_organization?" do - it "is false when organization_verified_at? is blank" do - expect(subject.verified_organization?).to be false - end - it "is true when organization_verified_at? exists" do - subject.organization_verified_at = Time.now - expect(subject.verified_organization?).to be true - end - it "is false when the organization was verified and then rejected" do - subject.organization_verified_at = Time.now - subject.organization_rejected_at = Time.now + 1 - expect(subject.verified_organization?).to be false - end - it "is true when the organization was rejected and then verified" do - subject.organization_rejected_at = Time.now - subject.organization_verified_at = Time.now + 1 - expect(subject.verified_organization?).to be true - end - end + it "deactivates the validation of first_name and last_name" do + subject.first_name = nil + subject.last_name = nil + expect(subject).to be_valid + end - describe "rejected_organization?" do - it "is false when organization_rejected_at? is blank" do - expect(subject.rejected_organization?).to be false - end - it "is true when organization_rejected_at? exists" do - subject.organization_rejected_at = Time.now - expect(subject.rejected_organization?).to be true - end - it "is true when the organization was verified and then rejected" do - subject.organization_verified_at = Time.now - subject.organization_rejected_at = Time.now + 1 - expect(subject.rejected_organization?).to be true - end - it "is false when the organization was rejected and then verified" do - subject.organization_rejected_at = Time.now - subject.organization_verified_at = Time.now + 1 - expect(subject.rejected_organization?).to be false + it "calculates the name using the organization name" do + expect(subject.name).to eq(subject.organization.name) + end end end From 4e5d53f2f17271d86ae6946cd0284683233868dc Mon Sep 17 00:00:00 2001 From: kikito Date: Thu, 13 Aug 2015 20:05:32 +0200 Subject: [PATCH 15/33] Updates the Moderation Organisation controller --- .../moderation/organizations_controller.rb | 18 ++++++------------ .../moderation/organizations/index.html.erb | 16 ++++++++-------- config/routes.rb | 4 ++-- spec/features/moderation/organizations_spec.rb | 10 +++++----- 4 files changed, 21 insertions(+), 27 deletions(-) diff --git a/app/controllers/moderation/organizations_controller.rb b/app/controllers/moderation/organizations_controller.rb index 4788d3aa2..1528d0ea2 100644 --- a/app/controllers/moderation/organizations_controller.rb +++ b/app/controllers/moderation/organizations_controller.rb @@ -1,25 +1,19 @@ class Moderation::OrganizationsController < Moderation::BaseController - before_filter :load_organizations, only: :index - load_and_authorize_resource class: 'User' + load_and_authorize_resource def index + @organizations = @organizations.includes(:user).order(:name, 'users.email') end - def verify_organization - @organization.update(organization_verified_at: Time.now) + def verify + @organization.verify redirect_to action: :index end - def reject_organization - @organization.update(organization_rejected_at: Time.now) + def reject + @organization.reject redirect_to action: :index end - private - - def load_organizations - @organizations = User.organizations.order(:organization_name, :email) - end - end diff --git a/app/views/moderation/organizations/index.html.erb b/app/views/moderation/organizations/index.html.erb index 3f1d833b6..06549a9b3 100644 --- a/app/views/moderation/organizations/index.html.erb +++ b/app/views/moderation/organizations/index.html.erb @@ -1,27 +1,27 @@

<%= t('moderation.organizations.index.title') %>

-<% @organizations.each do |organization| %> + <% @organizations.each do |organization| %> - + - <% if organization.verified_organization? %> + <% if organization.verified? %> <% end %> - <% if can? :verify_organization, organization %> + <% if can? :verify, organization %> <% end %> - <% if organization.rejected_organization? %> + <% if organization.rejected? %> <% end %> - <% if can? :reject_organization, organization %> + <% if can? :reject, organization %> diff --git a/config/routes.rb b/config/routes.rb index 87eceda55..fa1f6624b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,8 +29,8 @@ Rails.application.routes.draw do root to: "dashboard#index" resources :organizations, only: :index do member do - put :verify_organization - put :reject_organization + put :verify + put :reject end end end diff --git a/spec/features/moderation/organizations_spec.rb b/spec/features/moderation/organizations_spec.rb index 9c07a2b9c..22cf8d2db 100644 --- a/spec/features/moderation/organizations_spec.rb +++ b/spec/features/moderation/organizations_spec.rb @@ -21,11 +21,11 @@ feature 'Moderations::Organizations' do expect(current_path).to eq(moderation_organizations_path) expect(page).to have_content ('Verified') - expect(organization.reload.verified_organization?).to eq(true) + expect(organization.reload.verified?).to eq(true) end scenario "verified organizations have link to reject" do - organization = create(:organization, organization_verified_at: Time.now) + organization = create(:verified_organization) visit moderation_organizations_path expect(page).to have_content ('Verified') @@ -36,11 +36,11 @@ feature 'Moderations::Organizations' do expect(current_path).to eq(moderation_organizations_path) expect(page).to have_content ('Rejected') - expect(organization.reload.rejected_organization?).to eq(true) + expect(organization.reload.rejected?).to eq(true) end scenario "rejected organizations have link to verify" do - organization = create(:organization, organization_rejected_at: Time.now) + organization = create(:rejected_organization) visit moderation_organizations_path expect(page).to have_content ('Rejected') @@ -51,7 +51,7 @@ feature 'Moderations::Organizations' do expect(current_path).to eq(moderation_organizations_path) expect(page).to have_content ('Verified') - expect(organization.reload.verified_organization?).to eq(true) + expect(organization.reload.verified?).to eq(true) end end From 60a1cc22676d3c4dd981f8ab876f5d219684a5d0 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 14 Aug 2015 20:24:19 +0200 Subject: [PATCH 16/33] Adds User.is_organization and User.organization_name --- app/models/user.rb | 18 ++++++++++++++---- spec/models/user_spec.rb | 30 ++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 9fb1f65c4..3b14879ca 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,9 +4,10 @@ class User < ActiveRecord::Base acts_as_voter - validates :first_name, presence: true, if: :use_first_name? - validates :last_name, presence: true, if: :use_last_name? - validates :nickname, presence: true, if: :use_nickname? + validates :first_name, presence: true, if: :use_first_name? + validates :last_name, presence: true, if: :use_last_name? + validates :nickname, presence: true, if: :use_nickname? + validates :organization_name, presence: true, if: :is_organization has_one :administrator has_one :moderator @@ -16,6 +17,11 @@ class User < ActiveRecord::Base scope :moderators, -> { joins(:moderator) } scope :organizations, -> { joins(:organization) } + attr_accessor :organization_name + attr_accessor :is_organization + + after_save :create_associated_organization + def name return nickname if use_nickname? return organization.name if organization? @@ -44,11 +50,15 @@ class User < ActiveRecord::Base private def use_first_name? - !use_nickname? && !organization? + !is_organization && !use_nickname? end def use_last_name? use_first_name? end + def create_associated_organization + create_organization(name: organization_name) if is_organization + end + end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index b357d2818..ce9fb5989 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -123,16 +123,34 @@ describe User do expect(subject.organization?).to be true end - it "deactivates the validation of first_name and last_name" do - subject.first_name = nil - subject.last_name = nil - expect(subject).to be_valid - end - it "calculates the name using the organization name" do expect(subject.name).to eq(subject.organization.name) end end end + describe "is_organization" do + before(:each) { subject.is_organization = true } + + it "deactivates the validation of first_name and last_name, and activates the validation of organization_name" do + subject.first_name = nil + subject.last_name = nil + subject.organization_name = nil + expect(subject).to_not be_valid + + subject.organization_name = 'org' + expect(subject).to be_valid + end + + it "triggers the creation of an associated organization using organization_name" do + expect(subject.organization).to_not be + + subject.is_organization = true + subject.organization_name = 'org' + subject.save + + expect(subject.organization).to be + end + end + end From 9343952d524f1ccbc87656052cae10c5d0f6f3de Mon Sep 17 00:00:00 2001 From: kikito Date: Sun, 16 Aug 2015 23:35:56 +0200 Subject: [PATCH 17/33] Adds User.organization_attributes --- app/models/organization.rb | 3 ++- app/models/user.rb | 20 +++++++++----------- spec/models/user_spec.rb | 28 +++++++++++----------------- 3 files changed, 22 insertions(+), 29 deletions(-) diff --git a/app/models/organization.rb b/app/models/organization.rb index 8bcb377b6..29bf01c2c 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -2,6 +2,8 @@ class Organization < ActiveRecord::Base belongs_to :user + validates :name, presence: true + delegate :email, :phone_number, to: :user def verify @@ -22,5 +24,4 @@ class Organization < ActiveRecord::Base (verified_at.blank? || verified_at < rejected_at) end - end diff --git a/app/models/user.rb b/app/models/user.rb index 3b14879ca..ef1afc5ee 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,15 +4,18 @@ class User < ActiveRecord::Base acts_as_voter - validates :first_name, presence: true, if: :use_first_name? - validates :last_name, presence: true, if: :use_last_name? - validates :nickname, presence: true, if: :use_nickname? - validates :organization_name, presence: true, if: :is_organization - has_one :administrator has_one :moderator has_one :organization + validates :first_name, presence: true, if: :use_first_name? + validates :last_name, presence: true, if: :use_last_name? + validates :nickname, presence: true, if: :use_nickname? + + validates_associated :organization, message: false + + accepts_nested_attributes_for :organization + scope :administrators, -> { joins(:administrators) } scope :moderators, -> { joins(:moderator) } scope :organizations, -> { joins(:organization) } @@ -50,15 +53,10 @@ class User < ActiveRecord::Base private def use_first_name? - !is_organization && !use_nickname? + !organization? && !use_nickname? end def use_last_name? use_first_name? end - - def create_associated_organization - create_organization(name: organization_name) if is_organization - end - end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index ce9fb5989..7d79898c0 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -129,27 +129,21 @@ describe User do end end - describe "is_organization" do - before(:each) { subject.is_organization = true } + describe "organization_attributes" do + before(:each) { subject.organization_attributes = {name: 'org'} } - it "deactivates the validation of first_name and last_name, and activates the validation of organization_name" do - subject.first_name = nil - subject.last_name = nil - subject.organization_name = nil - expect(subject).to_not be_valid - - subject.organization_name = 'org' - expect(subject).to be_valid + it "triggers the creation of an associated organization" do + expect(subject.organization).to be + expect(subject.organization.name).to eq('org') end - it "triggers the creation of an associated organization using organization_name" do - expect(subject.organization).to_not be + it "deactivates the validation of first_name and last_name, and activates the validation of organization" do + subject.first_name = nil + subject.last_name = nil + expect(subject).to be_valid - subject.is_organization = true - subject.organization_name = 'org' - subject.save - - expect(subject.organization).to be + subject.organization.name= nil + expect(subject).to_not be_valid end end From 1f2f318e83b1dee529b2ab14331b0e7e7dfe3892 Mon Sep 17 00:00:00 2001 From: kikito Date: Sun, 16 Aug 2015 23:36:33 +0200 Subject: [PATCH 18/33] Turns on devise scoped views That way we can have users/registrations & organizations/registrations --- config/initializers/devise.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 667c5f1b8..3be902f18 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -211,7 +211,7 @@ Devise.setup do |config| # Turn scoped views on. Before rendering "sessions/new", it will first check for # "users/sessions/new". It's turned off by default because it's slower if you # are using only default views. - # config.scoped_views = false + config.scoped_views = true # Configure the default scope given to Warden. By default it's the first # devise role declared in your routes (usually :user). From de73eac2d0e821158e7954676efdde95a0ecfb37 Mon Sep 17 00:00:00 2001 From: kikito Date: Sun, 16 Aug 2015 23:41:05 +0200 Subject: [PATCH 19/33] Moves the registrations controller to users/registrations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is necessary because rails gets confused when it has a a “root” registration_controller and another non-root one. The non-root one uses the views from the root. --- .../{ => users}/registrations_controller.rb | 9 ++-- app/views/devise/registrations/edit.html.erb | 45 ------------------ app/views/users/registrations/edit.html.erb | 45 ++++++++++++++++++ .../registrations/new.html.erb | 32 ++++++------- config/locales/devise_views.en.yml | 47 ++++++++++--------- config/locales/devise_views.es.yml | 47 ++++++++++--------- config/routes.rb | 2 +- 7 files changed, 114 insertions(+), 113 deletions(-) rename app/controllers/{ => users}/registrations_controller.rb (51%) delete mode 100644 app/views/devise/registrations/edit.html.erb create mode 100644 app/views/users/registrations/edit.html.erb rename app/views/{devise => users}/registrations/new.html.erb (68%) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/users/registrations_controller.rb similarity index 51% rename from app/controllers/registrations_controller.rb rename to app/controllers/users/registrations_controller.rb index 4991cdf70..174b12aa2 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -1,4 +1,4 @@ -class RegistrationsController < Devise::RegistrationsController +class Users::RegistrationsController < Devise::RegistrationsController include RecaptchaHelper def create @@ -11,11 +11,10 @@ class RegistrationsController < Devise::RegistrationsController end end - private - def sign_up_params - params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :use_nickname, :nickname) - end + def sign_up_params + params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :use_nickname, :nickname) + end end diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb deleted file mode 100644 index b670b6776..000000000 --- a/app/views/devise/registrations/edit.html.erb +++ /dev/null @@ -1,45 +0,0 @@ -
- -
\ No newline at end of file diff --git a/app/views/users/registrations/edit.html.erb b/app/views/users/registrations/edit.html.erb new file mode 100644 index 000000000..b17b8f2e1 --- /dev/null +++ b/app/views/users/registrations/edit.html.erb @@ -0,0 +1,45 @@ +
+ +
diff --git a/app/views/devise/registrations/new.html.erb b/app/views/users/registrations/new.html.erb similarity index 68% rename from app/views/devise/registrations/new.html.erb rename to app/views/users/registrations/new.html.erb index c7cd839a2..893054f8e 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/users/registrations/new.html.erb @@ -2,60 +2,60 @@
-

<%= t("devise_views.registrations.new.title") %>

+

<%= t("devise_views.users.registrations.new.title") %>

<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %>
- <%= f.label :first_name, t("devise_views.registrations.new.first_name_label") %> - <%= f.text_field :first_name, autofocus: true, placeholder: t("devise_views.registrations.new.first_name_label") %> + <%= f.label :first_name, t("devise_views.users.registrations.new.first_name_label") %> + <%= f.text_field :first_name, autofocus: true, placeholder: t("devise_views.users.registrations.new.first_name_label") %>
- <%= f.label :last_name, t("devise_views.registrations.new.last_name_label") %> - <%= f.text_field :last_name, placeholder: t("devise_views.registrations.new.last_name_label") %> + <%= f.label :last_name, t("devise_views.users.registrations.new.last_name_label") %> + <%= f.text_field :last_name, placeholder: t("devise_views.users.registrations.new.last_name_label") %>
<%= f.check_box :use_nickname %> - <%= t("devise_views.registrations.new.use_nickname_label") %> + <%= t("devise_views.users.registrations.new.use_nickname_label") %>
- <%= f.label :nickname, t("devise_views.registrations.new.nickname_label") %> - <%= f.text_field :nickname, placeholder: t("devise_views.registrations.new.nickname_label") %> + <%= f.label :nickname, t("devise_views.users.registrations.new.nickname_label") %> + <%= f.text_field :nickname, placeholder: t("devise_views.users.registrations.new.nickname_label") %>
- <%= f.label :email, t("devise_views.registrations.new.email_label") %> - <%= f.email_field :email, placeholder: t("devise_views.registrations.new.email_label") %> + <%= f.label :email, t("devise_views.users.registrations.new.email_label") %> + <%= f.email_field :email, placeholder: t("devise_views.users.registrations.new.email_label") %>
- <%= f.label :password, t("devise_views.registrations.new.password_label"), class: "inline-block" %> + <%= f.label :password, t("devise_views.users.registrations.new.password_label"), class: "inline-block" %> <% if @minimum_password_length %> - <%= t("devise_views.registrations.new.min_length", min: @minimum_password_length) %> + <%= t("devise_views.users.registrations.new.min_length", min: @minimum_password_length) %> <% end %> - <%= f.password_field :password, autocomplete: "off", placeholder: t("devise_views.registrations.new.password_label") %> + <%= f.password_field :password, autocomplete: "off", placeholder: t("devise_views.users.registrations.new.password_label") %>
- <%= f.label :password_confirmation, t("devise_views.registrations.new.password_confirmation_label") %> - <%= f.password_field :password_confirmation, autocomplete: "off", placeholder: t("devise_views.registrations.new.password_confirmation_label") %> + <%= f.label :password_confirmation, t("devise_views.users.registrations.new.password_confirmation_label") %> + <%= f.password_field :password_confirmation, autocomplete: "off", placeholder: t("devise_views.users.registrations.new.password_confirmation_label") %>
@@ -63,7 +63,7 @@
- <%= f.submit t("devise_views.registrations.new.submit"), class: "button radius expand" %> + <%= f.submit t("devise_views.users.registrations.new.submit"), class: "button radius expand" %>
<% end %> diff --git a/config/locales/devise_views.en.yml b/config/locales/devise_views.en.yml index ae778d00f..3560ecdc5 100644 --- a/config/locales/devise_views.en.yml +++ b/config/locales/devise_views.en.yml @@ -36,29 +36,30 @@ en: title: "Forgot your password?" email_label: "Email" send_submit: "Send me reset password instructions" - registrations: - edit: - edit: "Edit" - email_label: "Email" - waiting_for: "Currently waiting confirmation for:" - leave_blank: "Leave blank if you don't want to change it" - password_label: "New password" - password_confirmation_label: "Confirm new password" - current_password_label: "Current password" - need_current: "We need your current password to confirm your changes" - update_submit: "Update" - back_link: "Back" - new: - title: "Sign up" - first_name_label: "First name" - last_name_label: "Last name" - nickname_label: "Nickname" - use_nickname_label: "Use nickname" - email_label: "Email" - password_label: "Password" - min_length: "(%{min} characters minimum)" - password_confirmation_label: "Confirm password" - submit: "Sign up" + users: + registrations: + edit: + edit: "Edit" + email_label: "Email" + waiting_for: "Currently waiting confirmation for:" + leave_blank: "Leave blank if you don't want to change it" + password_label: "New password" + password_confirmation_label: "Confirm new password" + current_password_label: "Current password" + need_current: "We need your current password to confirm your changes" + update_submit: "Update" + back_link: "Back" + new: + title: "Sign up" + first_name_label: "First name" + last_name_label: "Last name" + nickname_label: "Nickname" + use_nickname_label: "Use nickname" + email_label: "Email" + password_label: "Password" + min_length: "(%{min} characters minimum)" + password_confirmation_label: "Confirm password" + submit: "Sign up" sessions: new: title: "Log in" diff --git a/config/locales/devise_views.es.yml b/config/locales/devise_views.es.yml index 647dd3237..50da3f345 100644 --- a/config/locales/devise_views.es.yml +++ b/config/locales/devise_views.es.yml @@ -36,29 +36,30 @@ es: title: "¿Has olvidado tu contraseña?" email_label: "Email" send_submit: "Recibir instrucciones para recuperar mi contraseña" - registrations: - edit: - edit: "Editar" - email_label: "Email" - waiting_for: "Esperando confirmación de:" - leave_blank: "Dejar en blanco si no deseas cambiarla" - password_label: "Contraseña nueva" - password_confirmation_label: "Confirmar contraseña nueva" - current_password_label: "Contraseña actual" - need_current: "Necesitamos tu contraseña actual para confirmar los cambios" - update_submit: "Actualizar" - back_link: "Atrás" - new: - title: "Registrarse" - first_name_label: "Nombre" - last_name_label: "Apellidos" - nickname_label: "Pseudónimo" - use_nickname_label: "Usar pseudónimo" - email_label: "Email" - password_label: "Contraseña" - min_length: "(mínimo %{min} caracteres)" - password_confirmation_label: "Confirmar contraseña" - submit: "Registrarse" + users: + registrations: + edit: + edit: "Editar" + email_label: "Email" + waiting_for: "Esperando confirmación de:" + leave_blank: "Dejar en blanco si no deseas cambiarla" + password_label: "Contraseña nueva" + password_confirmation_label: "Confirmar contraseña nueva" + current_password_label: "Contraseña actual" + need_current: "Necesitamos tu contraseña actual para confirmar los cambios" + update_submit: "Actualizar" + back_link: "Atrás" + new: + title: "Registrarse" + first_name_label: "Nombre" + last_name_label: "Apellidos" + nickname_label: "Pseudónimo" + use_nickname_label: "Usar pseudónimo" + email_label: "Email" + password_label: "Contraseña" + min_length: "(mínimo %{min} caracteres)" + password_confirmation_label: "Confirmar contraseña" + submit: "Registrarse" sessions: new: title: "Entrar" diff --git a/config/routes.rb b/config/routes.rb index fa1f6624b..286b8237a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - devise_for :users, controllers: { registrations: 'registrations' } + devise_for :users, controllers: { registrations: 'users/registrations' } # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". From 9af1f2f4a9b4acdb8bd17d72ef3385e6bc978859 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 01:00:46 +0200 Subject: [PATCH 20/33] Adds registration controller for organizations --- .../organizations/registrations_controller.rb | 29 +++++++++ .../organizations/registrations/new.html.erb | 64 +++++++++++++++++++ config/locales/activerecord.en.yml | 5 +- config/locales/activerecord.es.yml | 5 +- config/locales/devise_views.en.yml | 12 ++++ config/locales/devise_views.es.yml | 12 ++++ config/routes.rb | 5 ++ spec/features/organizations_spec.rb | 32 ++++++++++ 8 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 app/controllers/organizations/registrations_controller.rb create mode 100644 app/views/organizations/registrations/new.html.erb create mode 100644 spec/features/organizations_spec.rb diff --git a/app/controllers/organizations/registrations_controller.rb b/app/controllers/organizations/registrations_controller.rb new file mode 100644 index 000000000..2751bc988 --- /dev/null +++ b/app/controllers/organizations/registrations_controller.rb @@ -0,0 +1,29 @@ +class Organizations::RegistrationsController < Devise::RegistrationsController + include RecaptchaHelper + + def new + super do |user| + user.build_organization + end + end + + def create + if verify_captcha?(resource) + super do |user| + # Removes unuseful "organization is invalid" error message + user.errors.messages.delete(:organization) + end + else + build_resource(sign_up_params) + flash.now[:alert] = t('recaptcha.errors.verification_failed') + respond_with resource + end + end + + private + + def sign_up_params + params.require(:user).permit(:email, :password, :phone_number, :password_confirmation, organization_attributes: [:name]) + end + +end diff --git a/app/views/organizations/registrations/new.html.erb b/app/views/organizations/registrations/new.html.erb new file mode 100644 index 000000000..f8ca107be --- /dev/null +++ b/app/views/organizations/registrations/new.html.erb @@ -0,0 +1,64 @@ +
+
+
+
+

<%= t("devise_views.organizations.registrations.new.title") %>

+ + <%= form_for(resource, as: :user, url: organization_registration_path) do |f| %> + + <%= devise_error_messages! %> + + <%= f.fields_for :organization do |fo| %> +
+
+ <%= fo.label :organization_name, t("devise_views.organizations.registrations.new.organization_name_label") %> + <%= fo.text_field :name, autofocus: true, placeholder: t("devise_views.organizations.registrations.new.organization_name_label") %> +
+
+ <% end %> + +
+
+ <%= f.label :email, t("devise_views.organizations.registrations.new.email_label") %> + <%= f.email_field :email, placeholder: t("devise_views.organizations.registrations.new.email_label") %> +
+
+ +
+
+ <%= f.label :phone_number, t("devise_views.organizations.registrations.new.phone_number_label") %> + <%= f.text_field :phone_number, placeholder: t("devise_views.organizations.registrations.new.phone_number_label") %> +
+
+ +
+
+ <%= f.label :password, t("devise_views.organizations.registrations.new.password_label"), class: "inline-block" %> + <% if @minimum_password_length %> + <%= t("devise_views.organizations.registrations.new.min_length", min: @minimum_password_length) %> + <% end %> + <%= f.password_field :password, autocomplete: "off", placeholder: t("devise_views.organizations.registrations.new.password_label") %> +
+
+ +
+
+ <%= f.label :password_confirmation, t("devise_views.organizations.registrations.new.password_confirmation_label") %> + <%= f.password_field :password_confirmation, autocomplete: "off", placeholder: t("devise_views.organizations.registrations.new.password_confirmation_label") %> +
+
+ + <%= render 'shared/captcha', resource: resource %> + +
+
+ <%= f.submit t("devise_views.organizations.registrations.new.submit"), class: "button radius expand" %> +
+
+ <% end %> + + <%= render "devise/shared/links" %> +
+
+
+
diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 3edd65527..e62df2a65 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -5,6 +5,7 @@ en: debate: Debate user: User vote: Vote + organization: Organization attributes: comment: body: Comment @@ -19,4 +20,6 @@ en: first_name: "First name" last_name: "Last name" nickname: Nickname - password: Password \ No newline at end of file + password: Password + organization: + name: Organization name diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 6bab3edb6..36af7e1b3 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -5,6 +5,7 @@ es: debate: Debate user: Usuario vote: Voto + organization: Organización attributes: comment: body: Comentario @@ -19,4 +20,6 @@ es: first_name: Nombre last_name: Apellidos nickname: Pseudónimo - password: Contraseña \ No newline at end of file + password: Contraseña + organization: + name: Nombre de organización diff --git a/config/locales/devise_views.en.yml b/config/locales/devise_views.en.yml index 3560ecdc5..a2f2fe1e1 100644 --- a/config/locales/devise_views.en.yml +++ b/config/locales/devise_views.en.yml @@ -60,6 +60,17 @@ en: min_length: "(%{min} characters minimum)" password_confirmation_label: "Confirm password" submit: "Sign up" + organizations: + registrations: + new: + title: "Sign up as organization" + organization_name_label: "Organization name" + email_label: "Email" + password_label: "Password" + phone_number_label: "Phone number" + min_length: "(%{min} characters minimum)" + password_confirmation_label: "Confirm password" + submit: "Sign up" sessions: new: title: "Log in" @@ -77,6 +88,7 @@ en: login: "Log in" signup: "Sign up" signin_with_provider: "Sign in with %{provider}" + organization_signup: "Sign up as an organization" new_password: "Forgot your password?" new_confirmation: "Didn't receive confirmation instructions?" new_unlock: "Didn't receive unlock instructions?" diff --git a/config/locales/devise_views.es.yml b/config/locales/devise_views.es.yml index 50da3f345..0f920ecf2 100644 --- a/config/locales/devise_views.es.yml +++ b/config/locales/devise_views.es.yml @@ -60,6 +60,17 @@ es: min_length: "(mínimo %{min} caracteres)" password_confirmation_label: "Confirmar contraseña" submit: "Registrarse" + organizations: + registrations: + new: + title: "Registrarse como organización" + organization_name_label: "Nombre de la organización" + email_label: "Email" + password_label: "Contraseña" + phone_number_label: "Teléfono" + min_length: "(mínimo %{min} caracteres)" + password_confirmation_label: "Confirmar contraseña" + submit: "Registrarse" sessions: new: title: "Entrar" @@ -76,6 +87,7 @@ es: links: login: "Entrar" signup: "Registrarse" + organization_signup: "Registro para organizaciones" signin_with_provider: "Entrar con %{provider}" new_password: "¿Olvidaste tu contraseña?" new_confirmation: "¿No has recibido instrucciones para confirmar tu cuenta?" diff --git a/config/routes.rb b/config/routes.rb index 286b8237a..ba2fc77c4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,10 @@ Rails.application.routes.draw do devise_for :users, controllers: { registrations: 'users/registrations' } + devise_for :organizations, class_name: 'User', + controllers: { + registrations: 'organizations/registrations', + sessions: 'devise/sessions' + } # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/spec/features/organizations_spec.rb b/spec/features/organizations_spec.rb new file mode 100644 index 000000000..4bee2cef5 --- /dev/null +++ b/spec/features/organizations_spec.rb @@ -0,0 +1,32 @@ +require 'rails_helper' + +feature 'Organizations' do + + scenario 'Organizations can be created' do + user = User.organizations.where(email: 'green@peace.com').first + expect(user).to_not be + + visit new_organization_registration_path + + fill_in 'user_organization_attributes_name', with: 'Greenpeace' + fill_in 'user_email', with: 'green@peace.com' + fill_in 'user_password', with: 'greenpeace' + fill_in 'user_password_confirmation', with: 'greenpeace' + + click_button 'Sign up' + + user = User.organizations.where(email: 'green@peace.com').first + expect(user).to be + expect(user).to be_organization + expect(user.organization).to_not be_verified + end + + scenario "Organization fields are validated" do + visit new_organization_registration_path + click_button 'Sign up' + + expect(page).to have_content "Email can't be blank" + expect(page).to have_content "Password can't be blank" + expect(page).to have_content "Organization name can't be blank" + end +end From e84873d875c9e63f2ce60ca38e03c4eda56a360e Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 01:01:16 +0200 Subject: [PATCH 21/33] Modifies devise shared link to include organisations sometimes --- app/views/devise/shared/_links.html.erb | 13 ++++++++++--- spec/features/organizations_spec.rb | 13 +++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/views/devise/shared/_links.html.erb b/app/views/devise/shared/_links.html.erb index d7a77614e..0fd80f429 100644 --- a/app/views/devise/shared/_links.html.erb +++ b/app/views/devise/shared/_links.html.erb @@ -3,10 +3,16 @@ <%= link_to t("devise_views.shared.links.login"), new_session_path(resource_name) %>
<% end -%> - <%- if devise_mapping.registerable? && controller_name != 'registrations' %> - <%= link_to t("devise_views.shared.links.signup"), new_registration_path(resource_name) %>
+ <%- if devise_mapping.registerable? && + controller_name != 'registrations' || + controller_path != 'users/registrations' %> + <%= link_to t("devise_views.shared.links.signup"), new_user_registration_path %>
<% end -%> + <%- if devise_mapping.registerable? && controller_name == 'registrations' && controller_path != 'organizations/registrations' %> + <%= link_to t("devise_views.shared.links.organization_signup"), new_organization_registration_path %>
+ <% end -%> + <%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> <%= link_to t("devise_views.shared.links.new_password"), new_password_path(resource_name) %>
<% end -%> @@ -24,4 +30,5 @@ <%= link_to t("devise_views.shared.links.signin_with_provider", provider: provider.to_s.titleize), omniauth_authorize_path(resource_name, provider) %>
<% end -%> <% end -%> -
\ No newline at end of file + +
diff --git a/spec/features/organizations_spec.rb b/spec/features/organizations_spec.rb index 4bee2cef5..d0144ecb5 100644 --- a/spec/features/organizations_spec.rb +++ b/spec/features/organizations_spec.rb @@ -29,4 +29,17 @@ feature 'Organizations' do expect(page).to have_content "Password can't be blank" expect(page).to have_content "Organization name can't be blank" end + + scenario 'Shared links' do + visit new_user_registration_path + expect(page).to have_link "Sign up as an organization" + + visit new_organization_registration_path + expect(page).to have_link "Sign up" + + visit new_user_session_path + + expect(page).to have_link "Sign up" + expect(page).to_not have_link "Sign up as an organization" + end end From 543f71889126e96fc99564affc10808bbbf280c7 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 01:12:14 +0200 Subject: [PATCH 22/33] Adds phone_number to the regular user registration / account views --- app/controllers/account_controller.rb | 2 +- app/controllers/users/registrations_controller.rb | 2 +- app/views/account/show.html.erb | 5 +++++ app/views/users/registrations/new.html.erb | 7 +++++++ config/locales/devise_views.en.yml | 1 + config/locales/devise_views.es.yml | 1 + config/locales/en.yml | 1 + config/locales/es.yml | 1 + 8 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb index eb0837fee..3c81da291 100644 --- a/app/controllers/account_controller.rb +++ b/app/controllers/account_controller.rb @@ -21,7 +21,7 @@ class AccountController < ApplicationController end def account_params - params.require(:account).permit(:first_name, :last_name, :nickname, :use_nickname, :email_on_debate_comment, :email_on_comment_reply) + params.require(:account).permit(:first_name, :last_name, :phone_number, :nickname, :use_nickname, :email_on_debate_comment, :email_on_comment_reply) end end diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 174b12aa2..916b5128b 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -14,7 +14,7 @@ class Users::RegistrationsController < Devise::RegistrationsController private def sign_up_params - params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation, :use_nickname, :nickname) + params.require(:user).permit(:first_name, :last_name, :email, :phone_number, :password, :password_confirmation, :use_nickname, :nickname) end end diff --git a/app/views/account/show.html.erb b/app/views/account/show.html.erb index 185af445b..aae6ae28a 100644 --- a/app/views/account/show.html.erb +++ b/app/views/account/show.html.erb @@ -35,6 +35,11 @@ <% end %>
+
+ <%= f.label :last_name, t("account.show.phone_number_label") %> + <%= f.text_field :phone_number, placeholder: t("account.show.phone_number_label") %> +
+
<%= f.label :email_on_debate_comment do %> <%= f.check_box :email_on_debate_comment %> diff --git a/app/views/users/registrations/new.html.erb b/app/views/users/registrations/new.html.erb index 893054f8e..c41028fe6 100644 --- a/app/views/users/registrations/new.html.erb +++ b/app/views/users/registrations/new.html.erb @@ -42,6 +42,13 @@
+
+
+ <%= f.label :phone_number, t("devise_views.users.registrations.new.phone_number_label") %> + <%= f.text_field :phone_number, placeholder: t("devise_views.users.registrations.new.phone_number_label") %> +
+
+
<%= f.label :password, t("devise_views.users.registrations.new.password_label"), class: "inline-block" %> diff --git a/config/locales/devise_views.en.yml b/config/locales/devise_views.en.yml index a2f2fe1e1..6895790cb 100644 --- a/config/locales/devise_views.en.yml +++ b/config/locales/devise_views.en.yml @@ -56,6 +56,7 @@ en: nickname_label: "Nickname" use_nickname_label: "Use nickname" email_label: "Email" + phone_number_label: "Phone number" password_label: "Password" min_length: "(%{min} characters minimum)" password_confirmation_label: "Confirm password" diff --git a/config/locales/devise_views.es.yml b/config/locales/devise_views.es.yml index 0f920ecf2..700040e12 100644 --- a/config/locales/devise_views.es.yml +++ b/config/locales/devise_views.es.yml @@ -56,6 +56,7 @@ es: nickname_label: "Pseudónimo" use_nickname_label: "Usar pseudónimo" email_label: "Email" + phone_number_label: "Teléfono" password_label: "Contraseña" min_length: "(mínimo %{min} caracteres)" password_confirmation_label: "Confirmar contraseña" diff --git a/config/locales/en.yml b/config/locales/en.yml index 29bc4df24..480a50a56 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -70,6 +70,7 @@ en: change_credentials_link: "Change my credentials" first_name_label: "First Name" last_name_label: "Last Name" + phone_number_label: "Phone number" use_nickname_label: "Use nickname" nickname_label: "Nickname" recaptcha: diff --git a/config/locales/es.yml b/config/locales/es.yml index 16af9d324..2ee0d07b6 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -70,6 +70,7 @@ es: email_on_comment_reply_label: "Recibir un email cuando alguien contesta a mis comentarios" first_name_label: "Nombre" last_name_label: "Apellidos" + phone_number_label: "Teléfono" use_nickname_label: "Usar pseudónimo" nickname_label: "Pseudónimo" recaptcha: From 42b441a866833f23636eb12852cfaaf1a48c3a69 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 12:29:42 +0200 Subject: [PATCH 23/33] fixes specs by using the correct capthca helper instead of deactivating it --- config/initializers/simple_captcha.rb | 2 +- spec/features/organizations_spec.rb | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/config/initializers/simple_captcha.rb b/config/initializers/simple_captcha.rb index 25d3a1342..2ac5826c3 100644 --- a/config/initializers/simple_captcha.rb +++ b/config/initializers/simple_captcha.rb @@ -1,4 +1,4 @@ -SimpleCaptcha.always_pass = Rails.env.test? +SimpleCaptcha.always_pass = false SimpleCaptcha.setup do |sc| # default: 100x28 diff --git a/spec/features/organizations_spec.rb b/spec/features/organizations_spec.rb index a79264d65..00ec6c6f9 100644 --- a/spec/features/organizations_spec.rb +++ b/spec/features/organizations_spec.rb @@ -12,6 +12,7 @@ feature 'Organizations' do fill_in 'user_email', with: 'green@peace.com' fill_in 'user_password', with: 'greenpeace' fill_in 'user_password_confirmation', with: 'greenpeace' + fill_in 'user_captcha', with: correct_captcha_text click_button 'Sign up' From ae21209920fc5b20566e0b0a6260e906f65ac6b4 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 13:20:52 +0200 Subject: [PATCH 24/33] Moves the layout to admin::BaseController and moderation::BaseController --- app/controllers/admin/base_controller.rb | 4 +++- app/controllers/admin/tags_controller.rb | 1 - app/controllers/moderation/base_controller.rb | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index f4322efe7..81ec67996 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,4 +1,6 @@ class Admin::BaseController < ApplicationController + layout 'admin' + before_action :authenticate_user! skip_authorization_check @@ -10,4 +12,4 @@ class Admin::BaseController < ApplicationController raise CanCan::AccessDenied unless current_user.try(:administrator?) end -end \ No newline at end of file +end diff --git a/app/controllers/admin/tags_controller.rb b/app/controllers/admin/tags_controller.rb index ecacdbdb2..03cfeb64f 100644 --- a/app/controllers/admin/tags_controller.rb +++ b/app/controllers/admin/tags_controller.rb @@ -1,5 +1,4 @@ class Admin::TagsController < Admin::BaseController - layout 'admin' before_action :find_tag, only: [:update, :destroy] respond_to :html, :js diff --git a/app/controllers/moderation/base_controller.rb b/app/controllers/moderation/base_controller.rb index 0ee155e03..f2a794526 100644 --- a/app/controllers/moderation/base_controller.rb +++ b/app/controllers/moderation/base_controller.rb @@ -1,4 +1,6 @@ class Moderation::BaseController < ApplicationController + layout 'admin' + before_action :authenticate_user! skip_authorization_check From 3096a738922d99db635e9b102bb23a6ec83b3fdc Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 13:22:38 +0200 Subject: [PATCH 25/33] Moves organization verification to Admin from Moderation --- .../organizations_controller.rb | 2 +- app/views/admin/organizations/index.html.erb | 34 +++++++++++++++++++ .../moderation/organizations/index.html.erb | 31 ----------------- config/locales/admin.en.yml | 7 ++++ config/locales/admin.es.yml | 11 ++++++ config/locales/moderation.en.yml | 8 +---- config/locales/moderation.es.yml | 8 +---- config/routes.rb | 12 +++---- .../organizations_spec.rb | 18 +++++----- 9 files changed, 70 insertions(+), 61 deletions(-) rename app/controllers/{moderation => admin}/organizations_controller.rb (80%) create mode 100644 app/views/admin/organizations/index.html.erb delete mode 100644 app/views/moderation/organizations/index.html.erb rename spec/features/{moderation => admin}/organizations_spec.rb (76%) diff --git a/app/controllers/moderation/organizations_controller.rb b/app/controllers/admin/organizations_controller.rb similarity index 80% rename from app/controllers/moderation/organizations_controller.rb rename to app/controllers/admin/organizations_controller.rb index 1528d0ea2..6f88f8d90 100644 --- a/app/controllers/moderation/organizations_controller.rb +++ b/app/controllers/admin/organizations_controller.rb @@ -1,4 +1,4 @@ -class Moderation::OrganizationsController < Moderation::BaseController +class Admin::OrganizationsController < Admin::BaseController load_and_authorize_resource diff --git a/app/views/admin/organizations/index.html.erb b/app/views/admin/organizations/index.html.erb new file mode 100644 index 000000000..d09f810d5 --- /dev/null +++ b/app/views/admin/organizations/index.html.erb @@ -0,0 +1,34 @@ +
+ +

<%= t('admin.organizations.index.title') %>

+ +
<%= organization.organization_name %><%= organization.name %> <%= organization.email %> <%= organization.phone_number %><%= t('moderation.organizations.index.verified') %><%= link_to t('moderation.organizations.index.verify'), - verify_organization_moderation_organization_path(organization), + verify_moderation_organization_path(organization), method: :put %> <%= t('moderation.organizations.index.rejected') %><%= link_to t('moderation.organizations.index.reject'), - reject_organization_moderation_organization_path(organization), + reject_moderation_organization_path(organization), method: :put %>
+ <% @organizations.each do |organization| %> + + + + + <% if organization.verified? %> + + <% end %> + <% if can? :verify, organization %> + + <% end %> + <% if organization.rejected? %> + + <% end %> + <% if can? :reject, organization %> + + <% end %> + + <% end %> +
<%= organization.name %><%= organization.email %><%= organization.phone_number %><%= t('admin.organizations.index.verified') %><%= link_to t('admin.organizations.index.verify'), + verify_admin_organization_path(organization), + method: :put + %> + <%= t('admin.organizations.index.rejected') %><%= link_to t('admin.organizations.index.reject'), + reject_admin_organization_path(organization), + method: :put + %> +
+ diff --git a/app/views/moderation/organizations/index.html.erb b/app/views/moderation/organizations/index.html.erb deleted file mode 100644 index 06549a9b3..000000000 --- a/app/views/moderation/organizations/index.html.erb +++ /dev/null @@ -1,31 +0,0 @@ -

<%= t('moderation.organizations.index.title') %>

- - - <% @organizations.each do |organization| %> - - - - - <% if organization.verified? %> - - <% end %> - <% if can? :verify, organization %> - - <% end %> - <% if organization.rejected? %> - - <% end %> - <% if can? :reject, organization %> - - <% end %> - -<% end %> -
<%= organization.name %><%= organization.email %><%= organization.phone_number %><%= t('moderation.organizations.index.verified') %><%= link_to t('moderation.organizations.index.verify'), - verify_moderation_organization_path(organization), - method: :put - %> - <%= t('moderation.organizations.index.rejected') %><%= link_to t('moderation.organizations.index.reject'), - reject_moderation_organization_path(organization), - method: :put - %> -
diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index a5b469e1b..50f59cb81 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -11,3 +11,10 @@ en: name: placeholder: 'Write a topic' destroy: Delete Tag + organizations: + index: + title: Organizations + verify: Verify + reject: Reject + verified: Verified + rejected: Rejected diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index fecb7c5f4..74290ce6c 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -3,6 +3,17 @@ es: dashboard: index: title: Administración + organizations: + index: + title: Organizaciones + verify: Verificar + reject: Rechazar + verified: Verificado + rejected: Rechazado + filter: Filtro + filter_all: Todas + filter_verified: Verificadas + filter_rejected: Rechazadas tags: index: title: 'Temas de debate' diff --git a/config/locales/moderation.en.yml b/config/locales/moderation.en.yml index c3a765104..cf53051ed 100644 --- a/config/locales/moderation.en.yml +++ b/config/locales/moderation.en.yml @@ -3,10 +3,4 @@ en: dashboard: index: title: Moderation - organizations: - index: - title: Organizations - verify: Verify - reject: Reject - verified: Verified - rejected: Rejected + diff --git a/config/locales/moderation.es.yml b/config/locales/moderation.es.yml index 958bd7c75..a921f0827 100644 --- a/config/locales/moderation.es.yml +++ b/config/locales/moderation.es.yml @@ -3,11 +3,5 @@ es: dashboard: index: title: Moderación - organizations: - index: - title: Organizaciones - verify: Verificar - reject: Rechazar - verified: Verificado - rejected: Rechazado + diff --git a/config/routes.rb b/config/routes.rb index 7ccbe2bb0..34fa752e1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,18 +28,18 @@ Rails.application.routes.draw do namespace :admin do root to: "dashboard#index" - - resources :tags, only: [:index, :create, :update, :destroy] - end - - namespace :moderation do - root to: "dashboard#index" resources :organizations, only: :index do member do put :verify put :reject end end + + resources :tags, only: [:index, :create, :update, :destroy] + end + + namespace :moderation do + root to: "dashboard#index" end # Example of regular route: diff --git a/spec/features/moderation/organizations_spec.rb b/spec/features/admin/organizations_spec.rb similarity index 76% rename from spec/features/moderation/organizations_spec.rb rename to spec/features/admin/organizations_spec.rb index 22cf8d2db..626eb9f9e 100644 --- a/spec/features/moderation/organizations_spec.rb +++ b/spec/features/admin/organizations_spec.rb @@ -4,21 +4,21 @@ feature 'Moderations::Organizations' do background do - moderator = create(:user) - create(:moderator, user: moderator) + administrator = create(:user) + create(:administrator, user: administrator) - login_as(moderator) + login_as(administrator) end scenario "pending organizations have links to verify and reject" do organization = create(:organization) - visit moderation_organizations_path + visit admin_organizations_path expect(page).to have_selector(:link_or_button, 'Verify') expect(page).to have_selector(:link_or_button, 'Reject') click_on 'Verify' - expect(current_path).to eq(moderation_organizations_path) + expect(current_path).to eq(admin_organizations_path) expect(page).to have_content ('Verified') expect(organization.reload.verified?).to eq(true) @@ -27,13 +27,13 @@ feature 'Moderations::Organizations' do scenario "verified organizations have link to reject" do organization = create(:verified_organization) - visit moderation_organizations_path + visit admin_organizations_path expect(page).to have_content ('Verified') expect(page).to_not have_selector(:link_or_button, 'Verify') expect(page).to have_selector(:link_or_button, 'Reject') click_on 'Reject' - expect(current_path).to eq(moderation_organizations_path) + expect(current_path).to eq(admin_organizations_path) expect(page).to have_content ('Rejected') expect(organization.reload.rejected?).to eq(true) @@ -42,13 +42,13 @@ feature 'Moderations::Organizations' do scenario "rejected organizations have link to verify" do organization = create(:rejected_organization) - visit moderation_organizations_path + visit admin_organizations_path expect(page).to have_content ('Rejected') expect(page).to have_selector(:link_or_button, 'Verify') expect(page).to_not have_selector(:link_or_button, 'Reject') click_on 'Verify' - expect(current_path).to eq(moderation_organizations_path) + expect(current_path).to eq(admin_organizations_path) expect(page).to have_content ('Verified') expect(organization.reload.verified?).to eq(true) From 264f6318c336d135deee8f8f83d256fcb036b74c Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 13:23:18 +0200 Subject: [PATCH 26/33] Adds i18n & ul for admin menus --- app/views/admin/_menu.html.erb | 5 ++++- config/locales/admin.en.yml | 3 +++ config/locales/admin.es.yml | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 79540a5d0..007dd966f 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -1,3 +1,6 @@
- <%= link_to t('admin.tags.index.title'), admin_tags_path %> +
    +
  • <%= link_to t('admin.menu.tags'), admin_tags_path %>
  • +
  • <%= link_to t('admin.menu.organizations'), admin_organizations_path %>
  • +
diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 50f59cb81..d9a40ff41 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -1,5 +1,8 @@ en: admin: + menu: + tags: Tags + organizations: Organizations dashboard: index: title: Administration diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 74290ce6c..bb1576045 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -1,5 +1,8 @@ es: admin: + menu: + tags: Temas de debate + organizations: Organizaciones dashboard: index: title: Administración From 1c435eacbea6cf7d7494d9cd20eb66b9a1e2eb6d Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 14:25:43 +0200 Subject: [PATCH 27/33] adds filters to admin/organizations --- .../admin/organizations_controller.rb | 17 ++++- app/models/organization.rb | 5 ++ app/views/admin/organizations/index.html.erb | 18 ++++- config/i18n-tasks.yml | 1 + config/locales/admin.en.yml | 21 ++++-- config/locales/admin.es.yml | 8 ++- spec/features/admin/organizations_spec.rb | 71 +++++++++++++++++-- 7 files changed, 120 insertions(+), 21 deletions(-) diff --git a/app/controllers/admin/organizations_controller.rb b/app/controllers/admin/organizations_controller.rb index 6f88f8d90..c7e3c159c 100644 --- a/app/controllers/admin/organizations_controller.rb +++ b/app/controllers/admin/organizations_controller.rb @@ -1,19 +1,32 @@ class Admin::OrganizationsController < Admin::BaseController + before_filter :set_valid_filters + before_filter :parse_filter load_and_authorize_resource def index + @organizations = @organizations.send(@filter) @organizations = @organizations.includes(:user).order(:name, 'users.email') end def verify @organization.verify - redirect_to action: :index + redirect_to action: :index, filter: @filter end def reject @organization.reject - redirect_to action: :index + redirect_to action: :index, filter: @filter end + private + def set_valid_filters + @valid_filters = %w{all pending verified rejected} + end + + def parse_filter + @filter = params[:filter] + @filter = 'all' unless @valid_filters.include?(@filter) + end + end diff --git a/app/models/organization.rb b/app/models/organization.rb index 29bf01c2c..80e1a559a 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -3,9 +3,14 @@ class Organization < ActiveRecord::Base belongs_to :user validates :name, presence: true + validates :user_id, presence: true delegate :email, :phone_number, to: :user + scope :pending, -> { where(verified_at: nil, rejected_at: nil) } + scope :verified, -> { where("verified_at is not null and (rejected_at is null or rejected_at < verified_at)") } + scope :rejected, -> { where("rejected_at is not null and (verified_at is null or verified_at < rejected_at)") } + def verify update(verified_at: Time.now) end diff --git a/app/views/admin/organizations/index.html.erb b/app/views/admin/organizations/index.html.erb index d09f810d5..b22dfdbc9 100644 --- a/app/views/admin/organizations/index.html.erb +++ b/app/views/admin/organizations/index.html.erb @@ -2,6 +2,19 @@

<%= t('admin.organizations.index.title') %>

+

+ <%= t('admin.organizations.index.filter') %>: + + <% @valid_filters.each do |filter| %> + <% if @filter == filter %> + <%= t("admin.organizations.index.filters.#{filter}") %> + <% else %> + <%= link_to t("admin.organizations.index.filters.#{filter}"), + admin_organizations_path(filter: filter) %> + <% end %> + <% end %> +

+ <% @organizations.each do |organization| %> @@ -13,7 +26,7 @@ <% end %> <% if can? :verify, organization %> @@ -23,7 +36,7 @@ <% end %> <% if can? :reject, organization %> @@ -31,4 +44,5 @@ <% end %>
<%= link_to t('admin.organizations.index.verify'), - verify_admin_organization_path(organization), + verify_admin_organization_path(organization, filter: @filter), method: :put %> <%= link_to t('admin.organizations.index.reject'), - reject_admin_organization_path(organization), + reject_admin_organization_path(organization, filter: @filter), method: :put %>
+ diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml index 5e6eb2899..e53b82334 100644 --- a/config/i18n-tasks.yml +++ b/config/i18n-tasks.yml @@ -94,6 +94,7 @@ ignore_missing: ## Consider these keys used: ignore_unused: - 'activerecord.*' + - 'admin.organizations.index.filter.*' # - '{devise,kaminari,will_paginate}.*' # - 'simple_form.{yes,no}' # - 'simple_form.{placeholders,hints,labels}.*' diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index d9a40ff41..34a25deaf 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -6,6 +6,19 @@ en: dashboard: index: title: Administration + organizations: + index: + title: Organizations + verify: Verify + reject: Reject + verified: Verified + rejected: Rejected + filter: Filtro + filters: + all: All + pending: Pending + verified: Verified + rejected: Rejected tags: index: title: 'Debate topics' @@ -14,10 +27,4 @@ en: name: placeholder: 'Write a topic' destroy: Delete Tag - organizations: - index: - title: Organizations - verify: Verify - reject: Reject - verified: Verified - rejected: Rejected + diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index bb1576045..86761f02c 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -14,9 +14,11 @@ es: verified: Verificado rejected: Rechazado filter: Filtro - filter_all: Todas - filter_verified: Verificadas - filter_rejected: Rechazadas + filters: + all: Todas + pending: Pendientes + verified: Verificadas + rejected: Rechazadas tags: index: title: 'Temas de debate' diff --git a/spec/features/admin/organizations_spec.rb b/spec/features/admin/organizations_spec.rb index 626eb9f9e..c3ac46d3d 100644 --- a/spec/features/admin/organizations_spec.rb +++ b/spec/features/admin/organizations_spec.rb @@ -14,8 +14,8 @@ feature 'Moderations::Organizations' do organization = create(:organization) visit admin_organizations_path - expect(page).to have_selector(:link_or_button, 'Verify') - expect(page).to have_selector(:link_or_button, 'Reject') + expect(page).to have_link('Verify') + expect(page).to have_link('Reject') click_on 'Verify' expect(current_path).to eq(admin_organizations_path) @@ -29,8 +29,8 @@ feature 'Moderations::Organizations' do visit admin_organizations_path expect(page).to have_content ('Verified') - expect(page).to_not have_selector(:link_or_button, 'Verify') - expect(page).to have_selector(:link_or_button, 'Reject') + expect(page).to_not have_link('Verify') + expect(page).to have_link('Reject') click_on 'Reject' expect(current_path).to eq(admin_organizations_path) @@ -43,9 +43,8 @@ feature 'Moderations::Organizations' do organization = create(:rejected_organization) visit admin_organizations_path - expect(page).to have_content ('Rejected') - expect(page).to have_selector(:link_or_button, 'Verify') - expect(page).to_not have_selector(:link_or_button, 'Reject') + expect(page).to have_link('Verify') + expect(page).to_not have_link('Reject', exact: true) click_on 'Verify' expect(current_path).to eq(admin_organizations_path) @@ -54,4 +53,62 @@ feature 'Moderations::Organizations' do expect(organization.reload.verified?).to eq(true) end + scenario "Current filter is properly highlighted" do + visit admin_organizations_path + expect(page).to_not have_link('All') + expect(page).to have_link('Pending') + expect(page).to have_link('Verified') + expect(page).to have_link('Rejected') + + visit admin_organizations_path(filter: 'all') + expect(page).to_not have_link('All') + expect(page).to have_link('Pending') + expect(page).to have_link('Verified') + expect(page).to have_link('Rejected') + + visit admin_organizations_path(filter: 'pending') + expect(page).to have_link('All') + expect(page).to_not have_link('Pending') + expect(page).to have_link('Verified') + expect(page).to have_link('Rejected') + + visit admin_organizations_path(filter: 'verified') + expect(page).to have_link('All') + expect(page).to have_link('Pending') + expect(page).to_not have_link('Verified') + expect(page).to have_link('Rejected') + + visit admin_organizations_path(filter: 'rejected') + expect(page).to have_link('All') + expect(page).to have_link('Pending') + expect(page).to have_link('Verified') + expect(page).to_not have_link('Rejected') + end + + scenario "Filtering organizations" do + create(:organization, name: "Pending Organization") + create(:rejected_organization, name: "Rejected Organization") + create(:verified_organization, name: "Verified Organization") + + visit admin_organizations_path(filter: 'all') + expect(page).to have_content('Pending Organization') + expect(page).to have_content('Rejected Organization') + expect(page).to have_content('Verified Organization') + + visit admin_organizations_path(filter: 'pending') + expect(page).to have_content('Pending Organization') + expect(page).to_not have_content('Rejected Organization') + expect(page).to_not have_content('Verified Organization') + + visit admin_organizations_path(filter: 'verified') + expect(page).to_not have_content('Pending Organization') + expect(page).to_not have_content('Rejected Organization') + expect(page).to have_content('Verified Organization') + + visit admin_organizations_path(filter: 'rejected') + expect(page).to_not have_content('Pending Organization') + expect(page).to have_content('Rejected Organization') + expect(page).to_not have_content('Verified Organization') + end + end From c1a6a37651033c9bf14d4077867f782e7c465a5e Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 17:11:17 +0200 Subject: [PATCH 28/33] Removes user_id validation from Organisations --- app/models/organization.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/models/organization.rb b/app/models/organization.rb index 80e1a559a..de120de9e 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -3,7 +3,6 @@ class Organization < ActiveRecord::Base belongs_to :user validates :name, presence: true - validates :user_id, presence: true delegate :email, :phone_number, to: :user From 8311ab6c2b468c9d0824c0757aa761f049666897 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 17:19:10 +0200 Subject: [PATCH 29/33] fixes typo in admin.en.yml --- config/locales/admin.en.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 34a25deaf..e455f417a 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -13,7 +13,7 @@ en: reject: Reject verified: Verified rejected: Rejected - filter: Filtro + filter: Filter filters: all: All pending: Pending From 1ffc342079d00339285d2071d53df72eb7bad06d Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 17:19:55 +0200 Subject: [PATCH 30/33] fixes typo in admin.es.yml --- config/locales/admin.es.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index 86761f02c..0813a1931 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -11,8 +11,8 @@ es: title: Organizaciones verify: Verificar reject: Rechazar - verified: Verificado - rejected: Rechazado + verified: Verificada + rejected: Rechazada filter: Filtro filters: all: Todas From c3bee45eda88e4c28562fb381632a5d968f6c63d Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 17:59:23 +0200 Subject: [PATCH 31/33] fixes error in abilities --- app/models/ability.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index 8dcb319e3..9cce129a2 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -22,15 +22,15 @@ class Ability end if user.moderator? || user.administrator? - can :read, Organization can(:verify, Organization){ |o| !o.verified? } can(:reject, Organization){ |o| !o.rejected? } can :hide, Comment can :hide, Debate + end - elsif user.administrator? + if user.administrator? can :restore, Comment can :restore, Debate end From 66c8931ae99e288bc588002ff1b383f44da4aaa3 Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 17 Aug 2015 18:31:04 +0200 Subject: [PATCH 32/33] adds account tests to increase test coverage --- spec/features/account_spec.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/spec/features/account_spec.rb b/spec/features/account_spec.rb index 1294d59d2..28785ed08 100644 --- a/spec/features/account_spec.rb +++ b/spec/features/account_spec.rb @@ -19,6 +19,18 @@ feature 'Account' do expect(page).to have_selector(avatar('Manuela Colau'), count: 1) end + scenario 'Show organization' do + create(:organization, user: @user, name: "Manuela Corp") + + visit account_path + + expect(page).to have_selector("input[value='Manuela Corp']") + expect(page).to_not have_selector("input[value='Manuela']") + expect(page).to_not have_selector("input[value='Colau']") + + expect(page).to have_selector(avatar('Manuela Corp'), count: 1) + end + scenario 'Edit' do visit account_path @@ -38,6 +50,24 @@ feature 'Account' do expect(page).to have_selector("input[id='account_email_on_comment_reply'][value='1']") end + scenario 'Edit Organization' do + create(:organization, user: @user, name: "Manuela Corp") + visit account_path + + fill_in 'account_organization_attributes_name', with: 'Google' + check 'account_email_on_debate_comment' + check 'account_email_on_comment_reply' + click_button 'Save changes' + + expect(page).to have_content "Saved" + + visit account_path + + expect(page).to have_selector("input[value='Google']") + expect(page).to have_selector("input[id='account_email_on_debate_comment'][value='1']") + expect(page).to have_selector("input[id='account_email_on_comment_reply'][value='1']") + end + scenario "Errors on edit" do visit account_path From cf4495e17cf6434dd77dd1a73389a4f66163697a Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 17 Aug 2015 19:25:40 +0200 Subject: [PATCH 33/33] adds spec for error messages on registration signup [#99] --- spec/features/organizations_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/spec/features/organizations_spec.rb b/spec/features/organizations_spec.rb index 00ec6c6f9..dcbaaab51 100644 --- a/spec/features/organizations_spec.rb +++ b/spec/features/organizations_spec.rb @@ -22,6 +22,14 @@ feature 'Organizations' do expect(user.organization).to_not be_verified end + scenario 'Errors on create' do + visit new_organization_registration_path + + click_button 'Sign up' + + expect(page).to have_content error_message + end + scenario 'Shared links' do visit new_user_registration_path expect(page).to have_link "Sign up as an organization"