Skip to content

Commit

Permalink
adds filters to admin/organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kikito committed Aug 17, 2015
1 parent 264f631 commit 1c435ea
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 21 deletions.
17 changes: 15 additions & 2 deletions 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
5 changes: 5 additions & 0 deletions app/models/organization.rb
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions app/views/admin/organizations/index.html.erb
Expand Up @@ -2,6 +2,19 @@

<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>
<% @organizations.each do |organization| %>
<tr>
Expand All @@ -13,7 +26,7 @@
<% end %>
<% if can? :verify, organization %>
<td><%= link_to t('admin.organizations.index.verify'),
verify_admin_organization_path(organization),
verify_admin_organization_path(organization, filter: @filter),
method: :put
%>
</td>
Expand All @@ -23,12 +36,13 @@
<% end %>
<% if can? :reject, organization %>
<td><%= link_to t('admin.organizations.index.reject'),
reject_admin_organization_path(organization),
reject_admin_organization_path(organization, filter: @filter),
method: :put
%>
</td>
<% end %>
</tr>
<% end %>
</table>

</div>
1 change: 1 addition & 0 deletions config/i18n-tasks.yml
Expand Up @@ -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}.*'
Expand Down
21 changes: 14 additions & 7 deletions config/locales/admin.en.yml
Expand Up @@ -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'
Expand All @@ -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

8 changes: 5 additions & 3 deletions config/locales/admin.es.yml
Expand Up @@ -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'
Expand Down
71 changes: 64 additions & 7 deletions spec/features/admin/organizations_spec.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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

0 comments on commit 1c435ea

Please sign in to comment.