Refactor exports controller (#1567)
* Add basic coverage for settings/exports controller * Remove unused @account variable from settings/exports controller * Add coverage for download export actions * Remove deprecated `render :text` in favor of `send_data` for csv downloads * Add model to handle exports * Use Export class in settings/exports controller * Simplify settings/exports controller methods * Move settings/export to more restful routesgh/stable
parent
1921c5416b
commit
3ddd936b03
|
@ -0,0 +1,17 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Settings
|
||||||
|
module Exports
|
||||||
|
class BlockedAccountsController < ApplicationController
|
||||||
|
before_action :authenticate_user!
|
||||||
|
|
||||||
|
def index
|
||||||
|
export_data = Export.new(current_account.blocking).to_csv
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.csv { send_data export_data, filename: 'blocking.csv' }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,17 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Settings
|
||||||
|
module Exports
|
||||||
|
class FollowingAccountsController < ApplicationController
|
||||||
|
before_action :authenticate_user!
|
||||||
|
|
||||||
|
def index
|
||||||
|
export_data = Export.new(current_account.following).to_csv
|
||||||
|
|
||||||
|
respond_to do |format|
|
||||||
|
format.csv { send_data export_data, filename: 'following.csv' }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,46 +1,13 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'csv'
|
|
||||||
|
|
||||||
class Settings::ExportsController < ApplicationController
|
class Settings::ExportsController < ApplicationController
|
||||||
layout 'admin'
|
layout 'admin'
|
||||||
|
|
||||||
before_action :authenticate_user!
|
before_action :authenticate_user!
|
||||||
before_action :set_account
|
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@total_storage = current_account.media_attachments.sum(:file_file_size)
|
@total_storage = current_account.media_attachments.sum(:file_file_size)
|
||||||
@total_follows = current_account.following.count
|
@total_follows = current_account.following.count
|
||||||
@total_blocks = current_account.blocking.count
|
@total_blocks = current_account.blocking.count
|
||||||
end
|
end
|
||||||
|
|
||||||
def download_following_list
|
|
||||||
@accounts = current_account.following
|
|
||||||
|
|
||||||
respond_to do |format|
|
|
||||||
format.csv { render text: accounts_list_to_csv(@accounts) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def download_blocking_list
|
|
||||||
@accounts = current_account.blocking
|
|
||||||
|
|
||||||
respond_to do |format|
|
|
||||||
format.csv { render text: accounts_list_to_csv(@accounts) }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def set_account
|
|
||||||
@account = current_user.account
|
|
||||||
end
|
|
||||||
|
|
||||||
def accounts_list_to_csv(list)
|
|
||||||
CSV.generate do |csv|
|
|
||||||
list.each do |account|
|
|
||||||
csv << [(account.local? ? account.local_username_and_domain : account.acct)]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,18 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
require 'csv'
|
||||||
|
|
||||||
|
class Export
|
||||||
|
attr_reader :accounts
|
||||||
|
|
||||||
|
def initialize(accounts)
|
||||||
|
@accounts = accounts
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_csv
|
||||||
|
CSV.generate do |csv|
|
||||||
|
accounts.each do |account|
|
||||||
|
csv << [(account.local? ? account.local_username_and_domain : account.acct)]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,8 +10,8 @@
|
||||||
%tr
|
%tr
|
||||||
%th= t('exports.follows')
|
%th= t('exports.follows')
|
||||||
%td= @total_follows
|
%td= @total_follows
|
||||||
%td= table_link_to 'download', t('exports.csv'), follows_settings_export_path(format: :csv)
|
%td= table_link_to 'download', t('exports.csv'), settings_exports_follows_path(format: :csv)
|
||||||
%tr
|
%tr
|
||||||
%th= t('exports.blocks')
|
%th= t('exports.blocks')
|
||||||
%td= @total_blocks
|
%td= @total_blocks
|
||||||
%td= table_link_to 'download', t('exports.csv'), blocks_settings_export_path(format: :csv)
|
%td= table_link_to 'download', t('exports.csv'), settings_exports_blocks_path(format: :csv)
|
||||||
|
|
|
@ -53,11 +53,10 @@ Rails.application.routes.draw do
|
||||||
resource :preferences, only: [:show, :update]
|
resource :preferences, only: [:show, :update]
|
||||||
resource :import, only: [:show, :create]
|
resource :import, only: [:show, :create]
|
||||||
|
|
||||||
resource :export, only: [:show] do
|
resource :export, only: [:show]
|
||||||
collection do
|
namespace :exports, constraints: { format: :csv } do
|
||||||
get :follows, to: 'exports#download_following_list'
|
resources :follows, only: :index, controller: :following_accounts
|
||||||
get :blocks, to: 'exports#download_blocking_list'
|
resources :blocks, only: :index, controller: :blocked_accounts
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
resource :two_factor_auth, only: [:show, :new, :create] do
|
resource :two_factor_auth, only: [:show, :new, :create] do
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Settings::Exports::BlockedAccountsController do
|
||||||
|
before do
|
||||||
|
sign_in Fabricate(:user), scope: :user
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET #index' do
|
||||||
|
it 'returns a csv of the blocking accounts' do
|
||||||
|
get :index, format: :csv
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(response.content_type).to eq 'text/csv'
|
||||||
|
expect(response.headers['Content-Disposition']).to eq 'attachment; filename="blocking.csv"'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,17 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Settings::Exports::FollowingAccountsController do
|
||||||
|
before do
|
||||||
|
sign_in Fabricate(:user), scope: :user
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET #index' do
|
||||||
|
it 'returns a csv of the following accounts' do
|
||||||
|
get :index, format: :csv
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(response.content_type).to eq 'text/csv'
|
||||||
|
expect(response.headers['Content-Disposition']).to eq 'attachment; filename="following.csv"'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,14 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Settings::ExportsController do
|
||||||
|
before do
|
||||||
|
sign_in Fabricate(:user), scope: :user
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'GET #show' do
|
||||||
|
it 'returns http success' do
|
||||||
|
get :show
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -0,0 +1,17 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe Export do
|
||||||
|
describe 'to_csv' do
|
||||||
|
it 'returns a csv of the accounts' do
|
||||||
|
one = Account.new(username: 'one', domain: 'local.host')
|
||||||
|
two = Account.new(username: 'two', domain: 'local.host')
|
||||||
|
accounts = [one, two]
|
||||||
|
|
||||||
|
export = Export.new(accounts).to_csv
|
||||||
|
results = export.strip.split
|
||||||
|
|
||||||
|
expect(results.size).to eq 2
|
||||||
|
expect(results.first).to eq 'one@local.host'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Reference in New Issue