Browse Source

Download all users

pull/589/head
baarkerlounger 4 years ago
parent
commit
c123abf167
  1. 2
      app/controllers/users_controller.rb
  2. 6
      app/models/user.rb
  3. 2
      spec/requests/users_controller_spec.rb

2
app/controllers/users_controller.rb

@ -15,7 +15,7 @@ class UsersController < ApplicationController
format.html format.html
format.csv do format.csv do
if current_user.support? if current_user.support?
send_data @users.to_csv, filename: "users-#{Time.zone.now}.csv" send_data User.all.where(active: true).to_csv, filename: "users-#{Time.zone.now}.csv"
else else
head :unauthorized head :unauthorized
end end

6
app/models/user.rb

@ -97,9 +97,7 @@ class User < ApplicationRecord
end end
end end
def organisation_name delegate :name, to: :organisation, prefix: true
organisation.name
end
def self.download_attributes def self.download_attributes
%w[id email name organisation_name role old_user_id is_dpo is_key_contact active sign_in_count last_sign_in_at] %w[id email name organisation_name role old_user_id is_dpo is_key_contact active sign_in_count last_sign_in_at]
@ -110,7 +108,7 @@ class User < ApplicationRecord
csv << download_attributes csv << download_attributes
all.find_each do |record| all.find_each do |record|
csv << self.download_attributes.map { |attr| record.public_send(attr) } csv << download_attributes.map { |attr| record.public_send(attr) }
end end
end end
end end

2
spec/requests/users_controller_spec.rb

@ -782,7 +782,7 @@ RSpec.describe UsersController, type: :request do
it "downloads organisation names rather than ids" do it "downloads organisation names rather than ids" do
csv = CSV.parse(response.body) csv = CSV.parse(response.body)
expect(csv.second[3]).to eq("#{user.organisation.name}") expect(csv.second[3]).to eq(user.organisation.name.to_s)
end end
end end

Loading…
Cancel
Save