Skip to content

Commit

Permalink
[FEAT] Allow admin users to download report of all registered users
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Reis committed Jun 30, 2017
1 parent 5622594 commit c1fe04d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 10 deletions.
44 changes: 44 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require "csv"

module Admin
class UsersController < BaseController
def change_user_roles
Expand All @@ -14,5 +16,47 @@ def change_user_roles
render status: :unprocessable_entity, json: "roles failed to update"
end
end

def export_user_info
@users = User.where(organization_id: current_organization.id)

respond_to do |format|
format.csv { send_data users_csv(@users), filename: "#{subdomain_name}_users_#{current_date_string}.csv" }
end
end

private

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

users.each do |user|
row = []
profile = user.profile

row << profile.try(:first_name)
row << profile.try(:last_name)
row << user.email
row << user.roles.map(&:name).map(&:capitalize).join(", ")
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)
row << user.course_progresses.where(completed_at: nil).map{ |cp| cp.course.title }.join(", ")
row << user.course_progresses.where.not(completed_at: nil).map{ |cp| cp.course.title }.join(", ")

csv << row
end
end
end

def subdomain_name
current_organization.try(:subdomain)
end

def current_date_string
Date.today.strftime("%m-%d-%Y")
end
end
end
1 change: 0 additions & 1 deletion app/views/admin/courses/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
</div>
</div>


<% (@category_ids || []).each do |category_id| %>
<%= render partial: "admin/courses/sortable_list", locals: { courses: @courses.with_category(category_id), category: Category.find_by_id(category_id) } %>
<% end %>
Expand Down
20 changes: 13 additions & 7 deletions app/views/admin/users/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

<h1>Users</h1>

<div class="users-search-box">
<%= form_tag admin_users_index_path, method: :get do %>
<%= search_field_tag :search, params[:search], placeholder: "Search users by keyword" %>
<i class="icon-search grey"></i>
<%= submit_tag "Search", id: "search_btn" %>
<% end %>
<div class="row">
<div class="sixcol">
<div class="users-search-box">
<%= form_tag admin_users_index_path, method: :get do %>
<%= search_field_tag :search, params[:search], placeholder: "Search users by keyword" %>
<i class="icon-search grey"></i>
<%= submit_tag "Search", id: "search_btn" %>
<% end %>
</div>
</div>
<div class="sixcol last left-or-right">
<%= link_to "Export User Information", admin_export_user_info_path(format: :csv), class: "btn button-color" %>
</div>
</div>

<div class="row twelvecol underlined">
Expand All @@ -28,7 +35,6 @@
</div>
</div>


<% @users.each do |user| %>
<div class="row twelvecol underlined">
<div class="two-of-twelve">
Expand Down
2 changes: 1 addition & 1 deletion config/deploy/staging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

set :bundle_without, %w{integration production test}.join(" ")

set :branch, "release-2.3.2.1"
set :branch, "release-2.4.0"

server "dl-stageapp-01.do.lark-it.com",
user: fetch(:application),
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
patch 'update_pub_status'
end

patch 'users/:id/change_user_roles', to: 'users#change_user_roles', as: :change_user_roles
patch 'users/:id/change_user_roles', to: 'users#change_user_roles', as: :change_user_roles

get 'users/export_user_info', to: 'users#export_user_info', as: :export_user_info

resources :courses do
put :sort, on: :collection
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/admin/users_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,18 @@
expect(@user.current_roles).to eq("admin")
end
end

describe "#export_user_info" do
before do
4.times do
create(:user, organization: @user.organization)
end

get :export_user_info, format: :csv
end

it "assigns correct number of users" do
expect(assigns(:users).count).to eq(6)
end
end
end

0 comments on commit c1fe04d

Please sign in to comment.