Fix Hound errors

This commit is contained in:
Jure Grabnar 2016-08-23 11:08:10 +02:00
parent 0d56cea3e4
commit 67768c8865
3 changed files with 21 additions and 27 deletions

View file

@ -154,7 +154,7 @@ class CanvasController < ApplicationController
end
# Make sure move parameter is valid
to_move = Hash.new
to_move = {}
if can_move_modules(@experiment) && update_params[:move].present?
begin
to_move = JSON.parse(update_params[:move])
@ -173,17 +173,15 @@ class CanvasController < ApplicationController
end
# Distinguish between moving modules/module_groups
to_move_groups = Hash.new
to_move_groups = {}
to_move.each do |key, value|
if key.match(/.*,.*/)
if key =~ /.*,.*/
to_move_groups[key.split(',')] = value
to_move.delete(key)
end
end
if error then
render_403 and return
end
render_403 and return if error
# Make sure that to_clone is an array of pairs,
# as well as that all IDs exist

View file

@ -476,7 +476,6 @@ class Experiment < ActiveRecord::Base
end
end
# Move modules; this method accepts a map where keys
# represent IDs of modules, and values represent experiment
# IDs of new names to which the given modules should be moved.
@ -486,20 +485,20 @@ class Experiment < ActiveRecord::Base
to_move.each do |id, experiment_id|
my_module = my_modules.find_by_id(id)
experiment = project.experiments.find_by_id(experiment_id)
if my_module.present? && experiment.present?
my_module.experiment = experiment
next unless my_module.present? && experiment.present?
# Calculate new module position
new_pos = my_module.get_new_position
my_module.x = new_pos[:x]
my_module.y = new_pos[:y]
my_module.experiment = experiment
unless my_module.outputs.destroy_all && my_module.inputs.destroy_all
raise ActiveRecord::ActiveRecordError
end
# Calculate new module position
new_pos = my_module.get_new_position
my_module.x = new_pos[:x]
my_module.y = new_pos[:y]
my_module.save!
unless my_module.outputs.destroy_all && my_module.inputs.destroy_all
raise ActiveRecord::ActiveRecordError
end
my_module.save!
end
end

View file

@ -324,25 +324,23 @@ class MyModule < ActiveRecord::Base
# Find an empty position for the restored module. It's
# basically a first empty row with empty space inside x=[0, 32).
def get_new_position
if experiment.blank?
return { x: 0, y: 0 }
end
return { x: 0, y: 0 } if experiment.blank?
# Get all modules position that overlap with first column, [0, WIDTH) and
# sort them by y coordinate.
positions = experiment.active_modules.collect{ |m| [m.x, m.y] }
.select{ |x, y| x >= 0 && x < WIDTH }
.sort_by{ |x, y| y }
positions = experiment.active_modules.collect { |m| [m.x, m.y] }
.select { |x, _| x >= 0 && x < WIDTH }
.sort_by { |_, y| y }
return { x: 0, y: 0 } if positions.empty? || positions.first[1] >= HEIGHT
# It looks we'll have to find a gap between the modules if it exists (at
# least 2*HEIGHT wide
ind = positions.each_cons(2).map{ |f, s| s[1]-f[1] }
.index { |y| y >= 2*HEIGHT }
ind = positions.each_cons(2).map { |f, s| s[1] - f[1] }
.index { |y| y >= 2 * HEIGHT }
return { x: 0, y: positions[ind][1] + HEIGHT } if ind
# We lucked out, no gaps, therefore we need to add it after the last element
return { x: 0, y: positions.last[1] + HEIGHT }
{ x: 0, y: positions.last[1] + HEIGHT }
end
private
@ -350,5 +348,4 @@ class MyModule < ActiveRecord::Base
def create_blank_protocol
protocols << Protocol.new_blank_for_module(self)
end
end