Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] New users table layout #1842

Merged
merged 6 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions backend/app/controllers/spree/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class UsersController < ResourceController

after_action :sign_in_if_change_own_password, only: :update

before_action :load_roles, :load_stock_locations, only: [:edit, :new]
before_action :load_roles, only: [:index, :edit, :new]
before_action :load_stock_locations, only: [:edit, :new]

def index
respond_with(@collection) do |format|
Expand Down Expand Up @@ -108,7 +109,9 @@ def collection
.limit(params[:limit] || 100)
else
@search = Spree.user_class.ransack(params[:q])
@collection = @search.result.page(params[:page]).per(Spree::Config[:admin_products_per_page])
@collection = @search.result.includes(:spree_roles)
@collection = @collection.includes(:spree_orders)
@collection = @collection.page(params[:page]).per(Spree::Config[:admin_products_per_page])
end
end

Expand Down Expand Up @@ -140,7 +143,9 @@ def sign_in_if_change_own_password

def load_roles
@roles = Spree::Role.all
@user_roles = @user.spree_roles
if @user
@user_roles = @user.spree_roles
end
end

def load_stock_locations
Expand Down
51 changes: 46 additions & 5 deletions backend/app/views/spree/admin/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,36 @@
<% content_for :table_filter do %>
<div data-hook="admin_users_index_search">
<%= search_form_for [:admin, @search], url: admin_users_url do |f| %>
<div class="form-group">
<%= f.label :email_cont, Spree.t(:email) %>
<%= f.text_field :email_cont, class: "form-control" %>
<div class="row">
<div class="col-xs-12 col-md-5">
<div class="form-group">
<%= f.label :email_cont, Spree.t(:email) %>
<%= f.text_field :email_cont, class: "form-control" %>
</div>
</div>
<div class="col-xs-12 col-md-3">
<div class="form-group">
<%= f.label :spree_roles_id_in, Spree.user_class.human_attribute_name(:spree_roles) %>
<%= f.collection_select :spree_roles_id_in, @roles, :id, :name, {},
multiple: true, class: 'select2 fullwidth' %>
</div>
</div>
<div class="col-xs-12 col-md-4">
<div class="date-range-filter form-group">
<%= label_tag :q_created_at_gt, Spree.t(:member_since) %>
<div class="date-range-fields input-group">
<%= f.text_field :created_at_gt, class: 'datepicker form-control datepicker-from',
value: params[:q].try!("[]", :created_at_gt), placeholder: Spree.t(:start) %>

<span class="range-divider input-group-addon">
<i class="fa fa-arrow-right"></i>
</span>

<%= f.text_field :created_at_lt, class: 'datepicker form-control datepicker-to',
value: params[:q].try!("[]", :created_at_lt), placeholder: Spree.t(:stop) %>
</div>
</div>
</div>
</div>

<div class="actions filter-actions" data-hook="admin_users_index_search_buttons">
Expand All @@ -27,21 +54,35 @@
</div>
<% end %>

<%= paginate @users, theme: "solidus_admin" %>

<table class="index" id="listing_users" data-hook>
<colgroup>
<col style="width: 90%">
<col style="width: 40%">
<col style="width: 20%">
<col style="width: 10%">
<col style="width: 10%">
<col style="width: 10%">
<col style="width: 10%">
</colgroup>
<thead>
<tr data-hook="admin_users_index_headers">
<th><%= sort_link @search,:email, Spree::LegacyUser.model_name.human, {}, {title: 'users_email_title'} %></th>
<th><%= sort_link @search, :email, Spree.user_class.human_attribute_name(:email), {}, {title: 'users_email_title'} %></th>
<th><%= Spree.user_class.human_attribute_name(:spree_roles) %></th>
<th class="align-center"><%= Spree.t(:num_orders) %></th>
<th class="align-center"><%= Spree.user_class.human_attribute_name(:lifetime_value) %></th>
<th class="align-center"><%= sort_link @search, :created_at, Spree.t(:member_since) %></th>
<th data-hook="admin_users_index_header_actions" class="actions"></th>
</tr>
</thead>
<tbody>
<% @users.each do |user|%>
<tr id="<%= spree_dom_id user %>" data-hook="admin_users_index_rows">
<td class='user_email'><%=link_to user.email, edit_admin_user_url(user) %></td>
<td><%= user.spree_roles.map(&:name).to_sentence %></td>
<td class="align-center"><%= link_to user.orders.count, spree.orders_admin_user_path(user) %></td>
<td class="align-center"><%= link_to user.display_lifetime_value, spree.items_admin_user_path(user) %></td>
<td class="align-center"><%= l user.created_at.to_date %></td>
<td data-hook="admin_users_index_row_actions" class="actions">
<% if can?(:edit, user) %>
<%= link_to_edit user, no_text: true %>
Expand Down
43 changes: 40 additions & 3 deletions backend/spec/features/admin/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
let!(:country) { create(:country) }
let!(:user_a) { create(:user_with_addresses, email: '[email protected]') }
let!(:user_b) { create(:user_with_addresses, email: '[email protected]') }
let!(:admin_role) { create(:role, name: 'admin') }
let!(:user_role) { create(:role, name: 'user') }

let(:order) { create(:completed_order_with_totals, user: user_a, number: "R123") }

Expand Down Expand Up @@ -62,7 +64,8 @@

it "can sort desc" do
within_table(table_id) do
click_link sort_link
# Ransack adds a ▲ to the sort link. With exact match Capybara is not able to find that link
click_link sort_link, exact: false

expect(page).to have_text text_match_1
expect(page).to have_text text_match_2
Expand All @@ -86,14 +89,49 @@
end
end

it 'displays the correct results for a user search' do
it 'displays the correct results for a user search by email' do
fill_in 'q_email_cont', with: user_a.email
click_button 'Search'
within_table('listing_users') do
expect(page).to have_text user_a.email
expect(page).not_to have_text user_b.email
end
end

context "member since" do
it_behaves_like "a sortable attribute" do
let(:text_match_1) { user_a.email }
let(:text_match_2) { user_b.email }
let(:table_id) { "listing_users" }
let(:sort_link) { Spree.t(:member_since) }
end

it 'displays the correct results for a user search by creation date' do
user_a.update_column(:created_at, 2.weeks.ago)
fill_in 'q_created_at_lt', with: 1.week.ago
click_button 'Search'
within_table('listing_users') do
expect(page).to have_text user_a.email
expect(page).not_to have_text user_b.email
end
end
end

context 'with users having roles' do
before do
user_a.spree_roles << admin_role
user_b.spree_roles << user_role
end

it 'displays the correct results for a user search by role' do
select 'admin', from: Spree.user_class.human_attribute_name(:spree_roles)
click_button 'Search'
within_table('listing_users') do
expect(page).to have_text user_a.email
expect(page).not_to have_text user_b.email
end
end
end
end

context 'editing users' do
Expand All @@ -119,7 +157,6 @@
end

it 'can edit user roles' do
Spree::Role.create name: "admin"
click_link 'Account'

check 'user_spree_role_admin'
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/concerns/spree/user_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ module UserMethods

include Spree::RansackableAttributes unless included_modules.include?(Spree::RansackableAttributes)

self.whitelisted_ransackable_associations = %w[addresses]
self.whitelisted_ransackable_attributes = %w[id email]
self.whitelisted_ransackable_associations = %w[addresses spree_roles]
self.whitelisted_ransackable_attributes = %w[id email created_at]
end

def wallet
Expand Down
3 changes: 3 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ en:
email: Email
password: Password
password_confirmation: Password Confirmation
spree_roles: Roles
spree/line_item:
description: Item Description
name: Name
Expand Down Expand Up @@ -380,6 +381,8 @@ en:
email: Email
password: Password
password_confirmation: Password Confirmation
spree_roles: Roles
lifetime_value: Total spent
spree/variant:
cost_currency: Cost Currency
cost_price: Cost Price
Expand Down