Class: UserImportFile
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- UserImportFile
- Includes:
- ImportFile, Statesman::Adapters::ActiveRecordQueries
- Defined in:
- app/models/user_import_file.rb
Instance Attribute Summary collapse
-
#mode ⇒ Object
Returns the value of attribute mode.
Instance Method Summary collapse
-
#import ⇒ Object
利用者情報をTSVファイルを用いて作成します。.
-
#modify ⇒ Object
利用者情報をTSVファイルを用いて更新します。.
-
#remove ⇒ Object
利用者情報をTSVファイルを用いて削除します。.
- #state_machine ⇒ Object
Methods included from ImportFile
Instance Attribute Details
#mode ⇒ Object
Returns the value of attribute mode.
37 38 39 |
# File 'app/models/user_import_file.rb', line 37 def mode @mode end |
Instance Method Details
#import ⇒ Object
利用者情報をTSVファイルを用いて作成します。
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/models/user_import_file.rb', line 47 def import transition_to!(:started) num = { user_imported: 0, user_found: 0, failed: 0, error: 0 } rows = open_import_file(create_import_temp_file(user_import)) row_num = 1 field = rows.first if [field['username']].reject{ |f| f.to_s.strip == "" }.empty? raise "username column is not found" end rows.each do |row| row_num += 1 import_result = UserImportResult.create!( user_import_file_id: id, body: row.fields.join("\t") ) next if row['dummy'].to_s.strip.present? username = row['username'] new_user = User.where(username: username).first if new_user import_result.user = new_user import_result.save num[:user_found] += 1 else new_user = User.new new_user.role = Role.where(name: row['role']).first if new_user.role unless user.has_role?(new_user.role.name) num[:failed] += 1 next end else new_user.role = Role.find(2) # User end new_user.username = username new_user.assign_attributes(set_user_params(row)) profile = Profile.new profile.assign_attributes(set_profile_params(row)) Profile.transaction do if new_user.valid? and profile.valid? new_user.profile = profile import_result.user = new_user import_result.save! num[:user_imported] += 1 else = "line #{row_num}: " += new_user.errors..join(" ") += profile.errors..join(" ") import_result. = import_result.save num[:error] += 1 end end end end Sunspot.commit rows.close = user_import_results.order(:id).pluck(:error_message).compact unless .empty? self. = '' if .nil? self. += "\n" self. += .join("\n") end save if num[:error] >= 1 transition_to!(:failed) else transition_to!(:completed) end num rescue => e transition_to!(:failed) raise e end |
#modify ⇒ Object
利用者情報をTSVファイルを用いて更新します。
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'app/models/user_import_file.rb', line 127 def modify transition_to!(:started) num = { user_updated: 0, user_not_found: 0, failed: 0 } rows = open_import_file(create_import_temp_file(user_import)) row_num = 1 field = rows.first if [field['username']].reject{|f| f.to_s.strip == ""}.empty? raise "username column is not found" end rows.each do |row| row_num += 1 next if row['dummy'].to_s.strip.present? import_result = UserImportResult.create!( user_import_file_id: id, body: row.fields.join("\t") ) username = row['username'] new_user = User.where(username: username).first if new_user.try(:profile) new_user.assign_attributes(set_user_params(row)) new_user.profile.assign_attributes(set_profile_params(row)) Profile.transaction do if new_user.save and new_user.profile.save num[:user_updated] += 1 import_result.user = new_user import_result.save! else num[:failed] += 1 end end else num[:user_not_found] += 1 end end rows.close transition_to!(:completed) Sunspot.commit num rescue => e self. = "line #{row_num}: #{e.}" save transition_to!(:failed) raise e end |
#remove ⇒ Object
利用者情報をTSVファイルを用いて削除します。
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'app/models/user_import_file.rb', line 177 def remove transition_to!(:started) row_num = 1 rows = open_import_file(create_import_temp_file(user_import)) field = rows.first if [field['username']].reject{ |f| f.to_s.strip == "" }.empty? raise "username column is not found" end rows.each do |row| row_num += 1 username = row['username'].to_s.strip remove_user = User.where(username: username).first if remove_user.try(:deletable_by?, user) UserImportFile.transaction do remove_user.destroy remove_user.profile.destroy end end end transition_to!(:completed) rescue => e self. = "line #{row_num}: #{e.}" save transition_to!(:failed) raise e end |
#state_machine ⇒ Object
39 40 41 |
# File 'app/models/user_import_file.rb', line 39 def state_machine UserImportFileStateMachine.new(self, transition_class: UserImportFileTransition) end |