1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

fix import users from csv

This commit is contained in:
Sylvain 2020-05-19 11:45:51 +02:00
parent c9670c9d1d
commit 18e2b032ae
4 changed files with 32 additions and 30 deletions

View File

@ -9,7 +9,7 @@ class User < ApplicationRecord
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable,
:confirmable
rolify after_add: :update_statistic_profile, after_remove: :update_statistic_profile
rolify
# enable OmniAuth authentication only if needed
devise :omniauthable, omniauth_providers: [AuthProvider.active.strategy_name.to_sym] unless
@ -319,6 +319,16 @@ class User < ApplicationRecord
.delete_if { |col| blacklist.include?(col[0]) }
end
# will update the statistic_profile after a group switch or a role update
def update_statistic_profile
raise NoProfileError if statistic_profile.nil? || statistic_profile.id.nil?
statistic_profile.update_attributes(
group_id: group_id,
role_id: roles.first.id
)
end
protected
# remove projects drafts that are not linked to another user
@ -430,15 +440,4 @@ class User < ApplicationRecord
email: email
)
end
# will update the statistic_profile after a group switch
# Rolify callbacks will call this function with an argument unused here
def update_statistic_profile(_param = nil)
raise NoProfileError if statistic_profile.nil?
statistic_profile.update_attributes(
group_id: group_id,
role_id: roles.first.id
)
end
end

View File

@ -7,8 +7,7 @@ class Members::ImportService
require 'csv'
log = []
begin
CSV.foreach(import.attachment.url, headers: true, col_sep: ';') do |row|
begin
CSV.foreach(import.attachment.url, headers: true, col_sep: ';') do |row|
password = hide_password(row)
log << { row: row.to_hash }
@ -31,7 +30,6 @@ class Members::ImportService
puts e
puts e.backtrace
end
end
rescue ArgumentError => e
log << e.to_s
puts e

View File

@ -41,13 +41,16 @@ class Members::MembersService
@member.statistic_profile.group_id = params[:group_id]
@member.statistic_profile.role_id = Role.find_or_create_by!(name: 'member').id
if @member.save
@member.generate_subscription_invoice(current_user.id)
@member.send_confirmation_instructions
UsersMailer.delay.notify_user_account_created(@member, @member.password)
true
else
false
ActiveRecord::Base.transaction do
if @member.save
@member.update_statistic_profile
@member.generate_subscription_invoice(current_user.id)
@member.send_confirmation_instructions
UsersMailer.delay.notify_user_account_created(@member, @member.password)
true
else
false
end
end
end

View File

@ -1,24 +1,26 @@
# frozen_string_literal: true
ActiveRecord::Base.class_eval do
def dump_fixture
fixture_file = "#{Rails.root}/test/fixtures/#{self.class.table_name}.yml"
File.open(fixture_file, "a") do |f|
File.open(fixture_file, 'a') do |f|
f.puts({ "#{self.class.table_name.singularize}_#{id}" => attributes }.
to_yaml.sub!(/---\s?/, "\n"))
end
end
def self.dump_fixtures
fixture_file = "#{Rails.root}/test/fixtures/#{self.table_name}.yml"
mode = (File.exists?(fixture_file) ? 'a' : 'w')
fixture_file = "#{Rails.root}/test/fixtures/#{table_name}.yml"
mode = (File.exist?(fixture_file) ? 'a' : 'w')
File.open(fixture_file, mode) do |f|
if self.attribute_names.include?("id")
self.all.each do |instance|
f.puts({ "#{self.table_name.singularize}_#{instance.id}" => instance.attributes }.to_yaml.sub!(/---\s?/, "\n"))
if attribute_names.include?('id')
all.each do |instance|
f.puts({ "#{table_name.singularize}_#{instance.id}" => instance.attributes }.to_yaml.sub!(/---\s?/, "\n"))
end
else
self.all.each_with_index do |instance, i|
f.puts({ "#{self.table_name.singularize}_#{i}" => instance.attributes }.to_yaml.sub!(/---\s?/, "\n"))
all.each_with_index do |instance, i|
f.puts({ "#{table_name.singularize}_#{i}" => instance.attributes }.to_yaml.sub!(/---\s?/, "\n"))
end
end
end