Fix smart annotations in demo project

Closes SCI-3097, SCI-3102.
This commit is contained in:
Jure Grabnar 2019-02-26 14:36:48 +01:00
parent 2464a45f83
commit 30bc536d07

View file

@ -1,4 +1,7 @@
module FirstTimeDataGenerator module FirstTimeDataGenerator
# Default inventory repository
REPO_SAMPLES_NAME = 'Samples'.freeze
# Create data for demo for new users # Create data for demo for new users
def seed_demo_data(user, team, asset_queue = :demo) def seed_demo_data(user, team, asset_queue = :demo)
@user = user @user = user
@ -8,28 +11,49 @@ module FirstTimeDataGenerator
# Do nothing # Do nothing
return unless team return unless team
# create custom repository samples # check if samples repo already exist, then create custom repository samples
repository = Repository.create( repository = Repository.where(team: team).where(name: REPO_SAMPLES_NAME)
name: 'Samples', repository =
team: team, if repository.blank?
created_by: user if team.repositories.count < Rails.configuration.x.repositories_limit
) Repository.create(
name: REPO_SAMPLES_NAME,
team: team,
created_by: user
)
else
# User first repo just as a placeholder, this call will fail anyhow
Repository.create(
name: team.repositories.first.name,
team: team,
created_by: user
)
end
else
repository.first
end
# create list value column for sample types # create list value column for sample types
repository_column_sample_types = RepositoryColumn.create( repo_columns = []
repository: repository, ['Sample Types', 'Sample Groups'].each do |repo_name|
created_by: user, repo_column = repository.repository_columns.where(name: repo_name)
data_type: :RepositoryListValue,
name: 'Sample Types'
)
# create list value column for sample groups repo_columns <<
repository_column_sample_groups = RepositoryColumn.create( if repo_column.blank?
repository: repository, RepositoryColumn.create(
created_by: user, repository: repository,
data_type: :RepositoryListValue, created_by: user,
name: 'Sample Groups' data_type: :RepositoryListValue,
) name: repo_name
)
else
repo_column.first
end
end
# Maintain old names
repository_column_sample_types, repository_column_sample_groups =
repo_columns
# create few list items for sample types # create few list items for sample types
repository_items_sample_types = [] repository_items_sample_types = []
@ -41,7 +65,15 @@ module FirstTimeDataGenerator
repository_column: repository_column_sample_types, repository_column: repository_column_sample_types,
repository: repository repository: repository
) )
repository_items_sample_types << item
# Check if it already exists
if item.persisted?
repository_items_sample_types << item
else
repository_items_sample_types << repository_column_sample_types
.repository_list_items
.where(data: name).first
end
end end
# create few list items for sample groups # create few list items for sample groups
@ -54,7 +86,15 @@ module FirstTimeDataGenerator
repository_column: repository_column_sample_groups, repository_column: repository_column_sample_groups,
repository: repository repository: repository
) )
repository_items_sample_groups << item
# Check if it already exists
if item.persisted?
repository_items_sample_groups << item
else
repository_items_sample_groups << repository_column_sample_groups
.repository_list_items
.where(data: name).first
end
end end
repository_rows_to_assign = [] repository_rows_to_assign = []
@ -585,19 +625,22 @@ module FirstTimeDataGenerator
'Collection of potatoes' 'Collection of potatoes'
] ]
second_rep_item = smart_annotate_rep_item(repository_rows_to_assign.second)
third_rep_item = smart_annotate_rep_item(repository_rows_to_assign.third)
fifth_rep_item = smart_annotate_rep_item(repository_rows_to_assign.fifth)
module_step_descriptions = [ module_step_descriptions = [
'<html> '<html>
<body> <body>
<p>50% of samples should be mock inoculated <p>50% of samples should be mock inoculated
<span class=\"atwho-inserted\"contenteditable=\"false\" <span class=\"atwho-inserted\"contenteditable=\"false\"
data-atwho-at-query=\"#\">[#' + sample_name + '3~rep_item~3]</span> data-atwho-at-query=\"#\">[#' + third_rep_item + ']</span>
<span class=\"atwho-inserted\" contenteditable=\"false\" <span class=\"atwho-inserted\" contenteditable=\"false\"
data-atwho-at-query=\"#\">[#' + sample_name + '5~rep_item~5]</span> data-atwho-at-query=\"#\">[#' + fifth_rep_item + ']</span>
while other 50% with PVY NTN virus while other 50% with PVY NTN virus
<span class=\"atwho-inserted\" contenteditable=\"false\" <span class=\"atwho-inserted\" contenteditable=\"false\"
data-atwho-at-query=\"#\">[#' + sample_name + '3~rep_item~3]</span> data-atwho-at-query=\"#\">[#' + third_rep_item + ']</span>
<span class=\"atwho-inserted\" contenteditable=\"false\" <span class=\"atwho-inserted\" contenteditable=\"false\"
data-atwho-at-query=\"#\">[#' + sample_name + '5~rep_item~5]</span>. data-atwho-at-query=\"#\">[#' + fifth_rep_item + ']</span>.
</p> </p>
</body> </body>
</html>', </html>',
@ -611,6 +654,9 @@ module FirstTimeDataGenerator
module_step_names, module_step_names,
module_step_descriptions) module_step_descriptions)
# Delete repository items, if we went over the limit
repository_rows_to_assign.map(&:destroy) unless repository.id
# Add table to existig step # Add table to existig step
step = my_modules[1].protocol.steps.where('position = 0').take step = my_modules[1].protocol.steps.where('position = 0').take
Table.create( Table.create(
@ -627,12 +673,11 @@ module FirstTimeDataGenerator
file_name: 'PVY-inoculated_plant_symptoms.JPG' file_name: 'PVY-inoculated_plant_symptoms.JPG'
) )
# Add comment to step 1 # Add comment to step 1
user_annotation = '[@' + user.name + '~' + user.id.to_s + ']' user_annotation = user.name
generate_step_comment( generate_step_comment(
step, step,
user, user,
user_annotation + ' I have used different sample [#' + sample_name + "#{user_annotation} I have used different sample [##{second_rep_item}]"
'2~rep_item~2]'
) )
# Add comment to step 3 # Add comment to step 3
step = my_modules[1].protocol.steps.where('position = 2').take step = my_modules[1].protocol.steps.where('position = 2').take
@ -1666,4 +1711,8 @@ module FirstTimeDataGenerator
step_name: step.name) step_name: step.name)
).sneaky_save ).sneaky_save
end end
def smart_annotate_rep_item(item)
"#{item.name}~rep_item~#{Base62.encode(item.id)}"
end
end end