adds filters to admin/organizations

This commit is contained in:
kikito
2015-08-17 14:25:43 +02:00
parent 264f6318c3
commit 1c435eacbe
7 changed files with 120 additions and 21 deletions

View File

@@ -1,19 +1,32 @@
class Admin::OrganizationsController < Admin::BaseController class Admin::OrganizationsController < Admin::BaseController
before_filter :set_valid_filters
before_filter :parse_filter
load_and_authorize_resource load_and_authorize_resource
def index def index
@organizations = @organizations.send(@filter)
@organizations = @organizations.includes(:user).order(:name, 'users.email') @organizations = @organizations.includes(:user).order(:name, 'users.email')
end end
def verify def verify
@organization.verify @organization.verify
redirect_to action: :index redirect_to action: :index, filter: @filter
end end
def reject def reject
@organization.reject @organization.reject
redirect_to action: :index redirect_to action: :index, filter: @filter
end 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 end

View File

@@ -3,9 +3,14 @@ class Organization < ActiveRecord::Base
belongs_to :user belongs_to :user
validates :name, presence: true validates :name, presence: true
validates :user_id, presence: true
delegate :email, :phone_number, to: :user 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 def verify
update(verified_at: Time.now) update(verified_at: Time.now)
end end

View File

@@ -2,6 +2,19 @@
<h1><%= t('admin.organizations.index.title') %></h1> <h1><%= t('admin.organizations.index.title') %></h1>
<p>
<%= 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 %>
</p>
<table> <table>
<% @organizations.each do |organization| %> <% @organizations.each do |organization| %>
<tr> <tr>
@@ -13,7 +26,7 @@
<% end %> <% end %>
<% if can? :verify, organization %> <% if can? :verify, organization %>
<td><%= link_to t('admin.organizations.index.verify'), <td><%= link_to t('admin.organizations.index.verify'),
verify_admin_organization_path(organization), verify_admin_organization_path(organization, filter: @filter),
method: :put method: :put
%> %>
</td> </td>
@@ -23,7 +36,7 @@
<% end %> <% end %>
<% if can? :reject, organization %> <% if can? :reject, organization %>
<td><%= link_to t('admin.organizations.index.reject'), <td><%= link_to t('admin.organizations.index.reject'),
reject_admin_organization_path(organization), reject_admin_organization_path(organization, filter: @filter),
method: :put method: :put
%> %>
</td> </td>
@@ -31,4 +44,5 @@
</tr> </tr>
<% end %> <% end %>
</table> </table>
</div> </div>

View File

@@ -94,6 +94,7 @@ ignore_missing:
## Consider these keys used: ## Consider these keys used:
ignore_unused: ignore_unused:
- 'activerecord.*' - 'activerecord.*'
- 'admin.organizations.index.filter.*'
# - '{devise,kaminari,will_paginate}.*' # - '{devise,kaminari,will_paginate}.*'
# - 'simple_form.{yes,no}' # - 'simple_form.{yes,no}'
# - 'simple_form.{placeholders,hints,labels}.*' # - 'simple_form.{placeholders,hints,labels}.*'

View File

@@ -6,6 +6,19 @@ en:
dashboard: dashboard:
index: index:
title: Administration 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: tags:
index: index:
title: 'Debate topics' title: 'Debate topics'
@@ -14,10 +27,4 @@ en:
name: name:
placeholder: 'Write a topic' placeholder: 'Write a topic'
destroy: Delete Tag destroy: Delete Tag
organizations:
index:
title: Organizations
verify: Verify
reject: Reject
verified: Verified
rejected: Rejected

View File

@@ -14,9 +14,11 @@ es:
verified: Verificado verified: Verificado
rejected: Rechazado rejected: Rechazado
filter: Filtro filter: Filtro
filter_all: Todas filters:
filter_verified: Verificadas all: Todas
filter_rejected: Rechazadas pending: Pendientes
verified: Verificadas
rejected: Rechazadas
tags: tags:
index: index:
title: 'Temas de debate' title: 'Temas de debate'

View File

@@ -14,8 +14,8 @@ feature 'Moderations::Organizations' do
organization = create(:organization) organization = create(:organization)
visit admin_organizations_path visit admin_organizations_path
expect(page).to have_selector(:link_or_button, 'Verify') expect(page).to have_link('Verify')
expect(page).to have_selector(:link_or_button, 'Reject') expect(page).to have_link('Reject')
click_on 'Verify' click_on 'Verify'
expect(current_path).to eq(admin_organizations_path) expect(current_path).to eq(admin_organizations_path)
@@ -29,8 +29,8 @@ feature 'Moderations::Organizations' do
visit admin_organizations_path visit admin_organizations_path
expect(page).to have_content ('Verified') expect(page).to have_content ('Verified')
expect(page).to_not have_selector(:link_or_button, 'Verify') expect(page).to_not have_link('Verify')
expect(page).to have_selector(:link_or_button, 'Reject') expect(page).to have_link('Reject')
click_on 'Reject' click_on 'Reject'
expect(current_path).to eq(admin_organizations_path) expect(current_path).to eq(admin_organizations_path)
@@ -43,9 +43,8 @@ feature 'Moderations::Organizations' do
organization = create(:rejected_organization) organization = create(:rejected_organization)
visit admin_organizations_path visit admin_organizations_path
expect(page).to have_content ('Rejected') expect(page).to have_link('Verify')
expect(page).to have_selector(:link_or_button, 'Verify') expect(page).to_not have_link('Reject', exact: true)
expect(page).to_not have_selector(:link_or_button, 'Reject')
click_on 'Verify' click_on 'Verify'
expect(current_path).to eq(admin_organizations_path) expect(current_path).to eq(admin_organizations_path)
@@ -54,4 +53,62 @@ feature 'Moderations::Organizations' do
expect(organization.reload.verified?).to eq(true) expect(organization.reload.verified?).to eq(true)
end 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 end