diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ac9e2d6c..4d1caead 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/views/users/index.csv.erb b/app/views/users/index.csv.erb new file mode 100644 index 00000000..3159443a --- /dev/null +++ b/app/views/users/index.csv.erb @@ -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 -%> diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 12a5af69..5f653c3a 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -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) }