Skip to content

Commit

Permalink
Validations on Map to check for resolved topic conflicts #238
Browse files Browse the repository at this point in the history
  • Loading branch information
simonreed committed Aug 29, 2019
1 parent 4a8c6ab commit 7c1d738
Show file tree
Hide file tree
Showing 16 changed files with 91 additions and 37 deletions.
17 changes: 17 additions & 0 deletions app/assets/javascripts/lib/data_manager.coffee
Expand Up @@ -360,6 +360,23 @@ data_manager.factory(
delete model.suggested_topic
model.$update_topic({topic_id: if Number.isInteger(topic_id) then topic_id else null })

DataManager.addSources = (model, new_sources, x, y)->
console.log(model)
model.$add_mapping {
sources:
id: new_sources
x: x
y: y
}

DataManager.addVariables = (model, variables)->
console.log(model)
model.$add_mapping {
variable_names: variables
x: null
y: null
}

DataManager.getInstrumentStats = (id, cb)->
DataManager.Data.InstrumentStats[id] = {$resolved: false}
DataManager.Data.InstrumentStats[id].$promise = InstrumentStats(id)
Expand Down
Expand Up @@ -39,4 +39,4 @@ questions.factory(
grid.clearCache() if grid?
}
]
)
)
2 changes: 1 addition & 1 deletion app/assets/javascripts/lib/resource.coffee
Expand Up @@ -82,4 +82,4 @@ resource.factory('GetResource', [
)
sub_promise.then(cb) if typeof cb is 'function'
rsrc
])
])
Expand Up @@ -73,12 +73,11 @@ show.controller(
$scope.detectKey = (event, variable, x = null, y = null)->
if event.keyCode == 13
new_sources = event.target.value.split ','
variable.$add_mapping {
sources:
id: new_sources
x: x
y: y
}
DataManager.addSources(variable, new_sources, x, y).then(->
$scope.model.orig_topic = $scope.model.topic
, (reason)->
variable.errors = reason.data.message
)
console.log variable

console.log $scope
Expand Down
14 changes: 6 additions & 8 deletions app/assets/javascripts/sections/mapping/index.coffee
Expand Up @@ -52,13 +52,11 @@ mapping.controller(
$scope.detectKey = (event, question, x = null, y = null)->
if event.keyCode == 13
variables = event.target.value.split ','
question.$add_mapping {
variable_names: variables
x: null
y: null
}
, ->
DataManager.resolveQuestions()
DataManager.addVariables(question, variables).then(->
$scope.model.orig_topic = $scope.model.topic
, (reason)->
question.errors = reason.data.message
)
console.log question

# $scope.detectKey = (event,question_id)->
Expand Down Expand Up @@ -137,7 +135,7 @@ mapping.directive(
$scope.model.orig_topic = $scope.model.topic
, (reason)->
$scope.model.topic = $scope.model.orig_topic
Flash.add('danger', reason.data.message)
$scope.model.errors = reason.data.message
).finally(->
bsLoadingOverlayService.stop()
)
Expand Down
8 changes: 7 additions & 1 deletion app/assets/javascripts/templates/partials/datasets/show.html
Expand Up @@ -34,8 +34,13 @@ <h3 class="panel-title">{{dataset.study}} - {{dataset.name}}</h3>
<td class="admin-min">
</td>
</tr>
<tr data-ng-repeat-end data-ng-class-odd="'odd'" data-ng-class="{danger: variable.strand && !variable.strand.good}">
<tr data-ng-repeat-end data-ng-class-odd="'odd'" data-ng-class="{danger: variable.errors}">
<td style="border-top:0" data-ng-attr-colspan="{{ is_admin() && 5 || 4 }}">
<div data-ng-if="variable.errors">
<div class="alert alert-danger" role="alert">
{{variable.errors}}
</div>
</div>
<form class="form-inline row">
<div class="form-group col-xs-3">
<label class="control-label">Used by</label>
Expand Down Expand Up @@ -85,6 +90,7 @@ <h3 class="panel-title">{{dataset.study}} - {{dataset.name}}</h3>
</div>
<div class="form-group col-xs-5">
<label class="col-xs-2">Topic</label>
<span>Resolved Topic - {{ variable.resolved_topic.name }}</span>
<div class="col-xs-10">
<a-topics
data-ng-model="variable"
Expand Down
@@ -1,5 +1,10 @@
<script type="text/ng-template" id="child_render.html">
<div class="tree-container row" data-ng-if="obj.type!='statement'">
<div data-ng-if="obj.errors">
<div class="alert alert-danger" role="alert">
{{obj.errors}}
</div>
</div>
<span class="a-label col-md-2">
{{obj.label}}
<div class="col-md-6 tq-mapping" data-ng-if="obj.type=='sequence'" style="float: right;">
Expand Down
25 changes: 16 additions & 9 deletions app/controllers/cc_questions_controller.rb
Expand Up @@ -24,17 +24,24 @@ def add_variables
variables = @object.instrument.variables.where(name: variable_names)
variables.to_a.compact!

variables.each do |variable|
unless @object.variables.find_by_id(variable.id)
if params.has_key?(:x) && params.has_key?(:y)
@object.map.create(variable: variable, x: params[:x].to_i, y: params[:y].to_i)
else
@object.variables << variable
begin
ActiveRecord::Base.transaction do
variables.each do |variable|
unless @object.variables.find_by_id(variable.id)
if params.has_key?(:x) && params.has_key?(:y)
@object.map.create!(variable: variable, x: params[:x].to_i, y: params[:y].to_i)
else
@object.variables << variable
end
end
end
@object.save
end
end
respond_to do |format|
format.json { render 'show' }
respond_to do |format|
format.json { render 'show' }
end
rescue => e
render json: {message: e.message}, status: :conflict
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/instruments_controller.rb
Expand Up @@ -78,7 +78,7 @@ def export
end

def variables
@collection = @object.variables
@collection = @object.variables.includes(:topic, :questions, :question_topics)
render 'variables/index'
end

Expand Down
14 changes: 10 additions & 4 deletions app/controllers/variables_controller.rb
Expand Up @@ -19,9 +19,15 @@ def add_sources

params[:sources] = JSON.parse(params[:sources])

@object.add_sources(params[:sources][:id], params[:sources][:x], params[:sources][:y])

render 'variables/show'
begin
ActiveRecord::Base.transaction do
@object.add_sources(params[:sources][:id], params[:sources][:x], params[:sources][:y])
@object.reload
end
render 'variables/show'
rescue => e
render json: {message: e.message}, status: :conflict
end
end

def remove_source
Expand All @@ -46,4 +52,4 @@ def collection
def set_dataset
@dataset = policy_scope(Dataset).includes(variables: [:questions, :src_variables, :der_variables, :topic] ).find(params[:dataset_id])
end
end
end
8 changes: 8 additions & 0 deletions app/models/map.rb
Expand Up @@ -12,4 +12,12 @@ class Map < ApplicationRecord
belongs_to :source, polymorphic: true
# Every mapping has a destination {Variable}
belongs_to :variable

# Validate to stop topic conflict between Variable(s) and CcQuestion
validate :resolved_topic_conflict

def resolved_topic_conflict
return if source.resolved_topic.nil? || variable.resolved_topic.nil?
errors.add(:topic, I18n.t('activerecord.errors.models.map.attributes.resolved_topic.variables_conflict', variable: variable, source: source, variable_topic: variable.resolved_topic, source_topic: source.resolved_topic))
end
end
4 changes: 1 addition & 3 deletions app/models/variable.rb
Expand Up @@ -72,14 +72,12 @@ def resolved_topic
def add_sources(source_labels, x = nil, y = nil)
sources = self.var_type == 'Normal' ? find_by_label_from_possible_questions(source_labels) : self.dataset.variables.find_by_name(source_labels)
[*sources].compact.each do |source|
if self.maps.create ({
self.maps.create! ({
variable: self,
source: source,
x: x,
y: y
})
self.strand + source.strand
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions app/views/variables/index.json.jbuilder
Expand Up @@ -7,4 +7,6 @@ json.array!(@collection) do |variable|
end
json.used_bys variable.der_variables, :id, :name, :label, :var_type
json.topic variable.topic, :id, :code, :name, :parent_id unless variable.topic.nil?
json.errors variable.errors.full_messages.to_sentence unless variable.valid?
json.resolved_topic variable.resolved_topic
end
2 changes: 2 additions & 0 deletions app/views/variables/show.json.jbuilder
Expand Up @@ -7,3 +7,5 @@ else
end
json.used_bys @object.der_variables, :id, :name, :label, :var_type
json.topic @object.topic, :id, :code, :name, :parent_id unless @object.topic.nil?
json.resolved_topic @object.resolved_topic
json.errors @object.errors.full_messages.to_sentence unless @object.valid?
4 changes: 4 additions & 0 deletions config/locales/en.yml
Expand Up @@ -34,3 +34,7 @@ en:
conflict: "conflict, already associated to topic %{topics} through variables %{variables}"
resolved_topic:
variables_conflict: "conflict, you cannot assign variable '%{new_variables}'' to this question because it has a topic of '%{new_topics}'' and your existing variables are using topic '%{existing_topic}'"
map:
attributes:
resolved_topic:
variables_conflict: "conflict, you cannot assign variable '%{variable}' to '%{source}' because '%{variable}' has a topic of '%{variable_topic}'' while '%{source}' is using topic '%{source_topic}'"
8 changes: 5 additions & 3 deletions lib/linkable.rb
Expand Up @@ -25,16 +25,18 @@ def set_topic
topic = Topic.find_by_id params[:topic_id]

begin
@object.topic = topic
@object.save!
ActiveRecord::Base.transaction do
@object.topic = topic
@object.save!
end

yield if block_given?

render :show
rescue Exceptions::TopicConflictError
render json: {message: 'Could not set topic as it would cause a conflict.'}, status: :conflict
rescue => e
render json: e, status: :bad_request
render json: {message: e.message}, status: :conflict
end
end

Expand Down

0 comments on commit 7c1d738

Please sign in to comment.