diff --git a/app/controllers/admin/organizations_controller.rb b/app/controllers/admin/organizations_controller.rb
index 6f88f8d90ea..c7e3c159cf1 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 29bf01c2ce1..80e1a559ab6 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 d09f810d555..b22dfdbc967 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 %>
<%= link_to t('admin.organizations.index.verify'),
- verify_admin_organization_path(organization),
+ verify_admin_organization_path(organization, filter: @filter),
method: :put
%>
|
@@ -23,7 +36,7 @@
<% end %>
<% if can? :reject, organization %>
<%= link_to t('admin.organizations.index.reject'),
- reject_admin_organization_path(organization),
+ reject_admin_organization_path(organization, filter: @filter),
method: :put
%>
|
@@ -31,4 +44,5 @@
<% end %>
+
diff --git a/config/i18n-tasks.yml b/config/i18n-tasks.yml
index 5e6eb28995d..e53b823348e 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 d9a40ff4152..34a25deaf1a 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 bb1576045a1..86761f02cbd 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 626eb9f9e80..c3ac46d3d8c 100644
--- a/spec/features/admin/organizations_spec.rb
+++ b/spec/features/admin/organizations_spec.rb
@@ -14,8 +14,8 @@
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 @@
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 @@
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 @@
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