Skip to content

Commit

Permalink
Add ability to download user lists as CSV
Browse files Browse the repository at this point in the history
* Global and chapter lists can be downloaded as CSV
* Still need to add to the interface
  • Loading branch information
jcn committed Jun 8, 2024
1 parent 128e73b commit e0d3cfa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
10 changes: 9 additions & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ class UsersController < ApplicationController
before_action :ensure_current_user_or_admin, :only => [:update, :edit]

def index
@users = User.all_with_chapter(params[:chapter_id]).paginate(page: params[:page], per_page: 250)
@users = User.all_with_chapter(params[:chapter_id])

respond_to do |format|
format.html do
@users = @users.paginate(page: params[:page], per_page: 250)
end
format.csv do
end
end
end

def update
Expand Down
4 changes: 4 additions & 0 deletions app/views/users/index.csv.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<%= CSV.generate_line %w[id first_name last_name email url chapter_name role created_at updated_at] -%>
<%- @users.each do |user| -%>
<%= CSV.generate_line [user.id, user.first_name, user.last_name, user.email, user.url, user.chapter_name, user.role_name, user.created_at.to_s(:iso8601), user.updated_at.to_s(:iso8601)] -%>
<%- end -%>
18 changes: 15 additions & 3 deletions spec/controllers/users_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
require 'spec_helper'

describe UsersController do
describe UsersController, type: :controller do
context "signed in as admin user" do
render_views

let(:user) { FactoryGirl.create(:user, :admin => true) }
before do
sign_in_as user
get :index
end
it { is_expected.to respond_with(:success) }

context "fetching an HTML file" do
before { get :index }
it { is_expected.to respond_with(:success) }
it { should render_template("index") }
end

context "fetching a CSV file" do
before { get :index, format: "csv" }
it { is_expected.to respond_with(:success) }
it { should render_template("index") }
end
end
context "signed in as non-admin user" do
let(:role) { FactoryGirl.create(:role) }
Expand Down

0 comments on commit e0d3cfa

Please sign in to comment.