diff --git a/app/controllers/protocols_controller.rb b/app/controllers/protocols_controller.rb index 302ddcc6c..8b7c5dd93 100644 --- a/app/controllers/protocols_controller.rb +++ b/app/controllers/protocols_controller.rb @@ -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 diff --git a/app/controllers/result_assets_controller.rb b/app/controllers/result_assets_controller.rb index 6cdea4b86..3ce0b8745 100644 --- a/app/controllers/result_assets_controller.rb +++ b/app/controllers/result_assets_controller.rb @@ -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 diff --git a/app/controllers/result_tables_controller.rb b/app/controllers/result_tables_controller.rb index c2e640474..7c3820625 100644 --- a/app/controllers/result_tables_controller.rb +++ b/app/controllers/result_tables_controller.rb @@ -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 - diff --git a/app/controllers/steps_controller.rb b/app/controllers/steps_controller.rb index e057c2270..c781102c6 100644 --- a/app/controllers/steps_controller.rb +++ b/app/controllers/steps_controller.rb @@ -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 diff --git a/app/models/asset.rb b/app/models/asset.rb index dcce691a1..f31358cef 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -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 diff --git a/app/models/protocol.rb b/app/models/protocol.rb index 17d658fb4..79efff5ad 100644 --- a/app/models/protocol.rb +++ b/app/models/protocol.rb @@ -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 diff --git a/app/models/table.rb b/app/models/table.rb index cf9874389..45ed3252e 100644 --- a/app/models/table.rb +++ b/app/models/table.rb @@ -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 diff --git a/app/utilities/protocols_importer.rb b/app/utilities/protocols_importer.rb index a44e8c897..0e0edddf1 100644 --- a/app/utilities/protocols_importer.rb +++ b/app/utilities/protocols_importer.rb @@ -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 diff --git a/db/migrate/20170322095856_add_team_id_to_asset_and_tables.rb b/db/migrate/20170322095856_add_team_id_to_asset_and_tables.rb new file mode 100644 index 000000000..fa6a8b2ae --- /dev/null +++ b/db/migrate/20170322095856_add_team_id_to_asset_and_tables.rb @@ -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