Skip to content

Commit

Permalink
Merge pull request #454 from commercekitchen/users_table
Browse files Browse the repository at this point in the history
Users table
  • Loading branch information
tmichaelreis authored Jun 1, 2020
2 parents 74f373b + be38699 commit 988d884
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
1 change: 1 addition & 0 deletions app/assets/stylesheets/components/_sortable_list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
display: table-cell;
width: 15%;
vertical-align: middle;

@media (max-width: $breakpoint-mobile-large) {
display: block;
}
Expand Down
13 changes: 13 additions & 0 deletions app/assets/stylesheets/components/_users_table.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.users-table {
width: 100%;
table-layout: fixed;

th,
td {
padding: 10px 10px;
}

td {
word-wrap: break-word;
}
}
3 changes: 2 additions & 1 deletion app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def export_user_info

def users_csv(users)
CSV.generate do |csv|
csv << ['User Name', 'User Last Name', 'User Email', 'User Role', 'Registration Date', 'Branch', 'Zip Code', 'Courses User has Started', 'Courses User has Completed']
csv << ['User Name', 'User Last Name', 'User Email', 'User Role', 'Preferred Language', 'Registration Date', 'Branch', 'Zip Code', 'Courses User has Started', 'Courses User has Completed']

users.each do |user|
row = []
Expand All @@ -60,6 +60,7 @@ def users_csv(users)
row << profile.try(:last_name)
row << user.email
row << user.roles.map(&:name).map(&:capitalize).join(', ')
row << user.preferred_language
row << user.created_at.in_time_zone('Central Time (US & Canada)').strftime('%m-%d-%Y')
row << profile.try(:library_location).try(:name)
row << profile.try(:zip_code)
Expand Down
10 changes: 10 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ def clean_up_paperclip_errors
errors.delete(:footer_logo_file_size)
end

def assignable_roles
default_roles = ['Admin', 'User', 'Trainer']

if student_programs?
default_roles + ['Student', 'Parent']
else
default_roles
end
end

def self.pla
find_by(subdomain: 'www')
end
Expand Down
2 changes: 0 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class User < ApplicationRecord
# Expose some information from profile
delegate :library_location_name, :library_location_zipcode, to: :profile, allow_nil: true

ROLES = %w[Admin Trainer User Parent Student].freeze

### Devise overrides to allow library card number login
# TODO: Pull this into a concern

Expand Down
12 changes: 7 additions & 5 deletions app/views/shared/_users_table.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<table class="users-table">
<colgroup>
<col span="1" style="width: 40%;">
<col span="1" style="width: 40%;">
<col span="1" style="width: 20%;">
</colgroup>

<tr>
<th>Name</th>
<th><%= t('login_signup.email') %></th>
<th>Language</th>
<th>User Role</th>
</tr>
<% users.each do |user| %>
Expand All @@ -13,12 +18,9 @@
<td>
<%= user.email %>
</td>
<td>
<%= user.preferred_language %>
</td>
<td>
<% if current_user.has_role?(:admin, current_organization) %>
<%= select_tag "user_#{user.id}", options_for_select(User::ROLES, user.current_roles.capitalize), data: { user_id: user.id, role: user.current_roles }, class: "user_role small narrow", method: :patch %>
<%= select_tag "user_#{user.id}", options_for_select(current_organization.assignable_roles, user.current_roles.capitalize), data: { user_id: user.id, role: user.current_roles }, class: "user_role small narrow", method: :patch %>
<% else %>
<%= user.current_roles.empty? ? "user" : user.current_roles %>
<% end %>
Expand Down
11 changes: 11 additions & 0 deletions spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@
end
end

describe '#assignable_roles' do
it 'returns correct options for typical organization' do
expect(org.assignable_roles).to contain_exactly('Admin', 'User', 'Trainer')
end

it 'returns correct options for program organization with student programs' do
FactoryBot.create(:program, :student_program, organization: org)
expect(org.assignable_roles).to contain_exactly('Admin', 'User', 'Trainer', 'Parent', 'Student')
end
end

describe '#pla' do
it 'returns PLA organization' do
expect(Organization.pla).to eq(pla)
Expand Down

0 comments on commit 988d884

Please sign in to comment.