Add team_id field to assets and tables [SCI-1046]

This commit is contained in:
Oleksii Kriuchykhin 2017-03-22 16:24:48 +01:00
parent ee65e46fc7
commit 2edebb282a
9 changed files with 68 additions and 11 deletions

View file

@ -521,7 +521,9 @@ class ProtocolsController < ApplicationController
transaction_error = false
Protocol.transaction do
begin
import_into_existing(@protocol, @protocol_json, current_user)
import_into_existing(
@protocol, @protocol_json, current_user, current_team
)
rescue Exception
transaction_error = true
raise ActiveRecord:: Rollback

View file

@ -29,6 +29,7 @@ class ResultAssetsController < ApplicationController
@asset = Asset.new(result_params[:asset_attributes])
@asset.created_by = current_user
@asset.last_modified_by = current_user
@asset.team = current_team
@result = Result.new(
user: current_user,
my_module: @my_module,
@ -98,6 +99,7 @@ class ResultAssetsController < ApplicationController
asset = Asset.find_by_id(update_params[:asset_attributes][:id])
asset.created_by = current_user
asset.last_modified_by = current_user
asset.team = current_team
@result.asset = asset
end

View file

@ -31,6 +31,7 @@ class ResultTablesController < ApplicationController
def create
@table = Table.new(result_params[:table_attributes])
@table.created_by = current_user
@table.team = current_team
@table.last_modified_by = current_user
@result = Result.new(
user: current_user,
@ -92,6 +93,7 @@ class ResultTablesController < ApplicationController
update_params = result_params
@result.last_modified_by = current_user
@result.table.last_modified_by = current_user
@result.table.team = current_team
@result.assign_attributes(update_params)
flash_success = t("result_tables.update.success_flash",
module: @my_module.name)
@ -218,6 +220,4 @@ class ResultTablesController < ApplicationController
]
)
end
end

View file

@ -33,6 +33,14 @@ class StepsController < ApplicationController
@step.protocol = @protocol
@step.user = current_user
@step.last_modified_by = current_user
@step.assets.each do |asset|
asset.created_by = current_user
asset.team = current_team
end
@step.tables.each do |table|
table.created_by = current_user
table.team = current_team
end
# Update default checked state
@step.checklists.each do |checklist|
@ -133,6 +141,18 @@ class StepsController < ApplicationController
@step.assign_attributes(step_params_all)
@step.last_modified_by = current_user
@step.assets.each do |asset|
asset.created_by = current_user if asset.new_record?
asset.last_modified_by = current_user unless asset.new_record?
asset.team = current_team
end
@step.tables.each do |table|
table.created_by = current_user if table.new_record?
table.last_modified_by = current_user unless table.new_record?
table.team = current_team
end
if @step.save
@step.reload

View file

@ -48,6 +48,7 @@ class Asset < ActiveRecord::Base
belongs_to :last_modified_by,
foreign_key: 'last_modified_by_id',
class_name: 'User'
belongs_to :team
has_one :step_asset,
inverse_of: :asset,
dependent: :destroy

View file

@ -291,8 +291,6 @@ class Protocol < ActiveRecord::Base
)
item2.created_by = current_user
item2.last_modified_by = current_user
p item
p item2
item2.save
end
@ -306,6 +304,7 @@ class Protocol < ActiveRecord::Base
asset.file_file_size
)
asset2.created_by = current_user
asset2.team = dest.team
asset2.last_modified_by = current_user
asset2.file_processing = true if asset.is_image?
asset2.save
@ -323,6 +322,7 @@ class Protocol < ActiveRecord::Base
table2 = Table.new(name: table.name, contents: table.contents)
table2.created_by = current_user
table2.last_modified_by = current_user
table2.team = dest.team
step2.tables << table2
end
end

View file

@ -10,6 +10,7 @@ class Table < ActiveRecord::Base
belongs_to :created_by, foreign_key: 'created_by_id', class_name: 'User'
belongs_to :last_modified_by, foreign_key: 'last_modified_by_id', class_name: 'User'
belongs_to :team
has_one :step_table, inverse_of: :table
has_one :step, through: :step_table

View file

@ -22,18 +22,18 @@ module ProtocolsImporter
protocol.save!
# Protocol is saved, populate it
populate_protocol(protocol, protocol_json, user)
populate_protocol(protocol, protocol_json, user, team)
return protocol
end
def import_into_existing(protocol, protocol_json, user)
def import_into_existing(protocol, protocol_json, user, team)
# Firstly, destroy existing protocol's contents
protocol.destroy_contents(user)
protocol.reload
# Alright, now populate the protocol
populate_protocol(protocol, protocol_json, user)
populate_protocol(protocol, protocol_json, user, team)
protocol.reload
# Unlink the protocol
@ -43,7 +43,7 @@ module ProtocolsImporter
private
def populate_protocol(protocol, protocol_json, user)
def populate_protocol(protocol, protocol_json, user, team)
protocol.reload
asset_ids = []
@ -93,7 +93,8 @@ module ProtocolsImporter
name: table_json['name'],
contents: Base64.decode64(table_json['contents']),
created_by: user,
last_modified_by: user
last_modified_by: user,
team: team
)
StepTable.create!(
step: step,
@ -106,7 +107,8 @@ module ProtocolsImporter
step_json["assets"].values.each do |asset_json|
asset = Asset.new(
created_by: user,
last_modified_by: user
last_modified_by: user,
team: team
)
# Decode the file bytes

View file

@ -0,0 +1,29 @@
class AddTeamIdToAssetAndTables < ActiveRecord::Migration
def change
add_column :assets, :team_id, :integer
add_index :assets, :team_id
add_column :tables, :team_id, :integer
add_index :tables, :team_id
Asset.find_each do |asset|
if asset.result
asset.update_columns(
team_id: asset.result.my_module.experiment.project.team_id
)
elsif asset.step
asset.update_columns(team_id: asset.step.protocol.team_id)
end
end
Table.find_each do |table|
if table.result
table.update_columns(
team_id: table.result.my_module.experiment.project.team_id
)
elsif table.step
table.update_columns(team_id: table.step.protocol.team_id)
end
end
end
end