Flip tool <--> tool_slot relations
parent
15261c9ae9
commit
7463001c78
|
@ -25,8 +25,7 @@ private
|
|||
|
||||
def update_params
|
||||
output = {tool: tool}
|
||||
output[:name] = params[:name] if params[:name]
|
||||
output[:tool_slot] = params[:tool_slot_id] if params[:tool_slot_id]
|
||||
output[:name] = params[:name] if params[:name]
|
||||
output
|
||||
end
|
||||
|
||||
|
@ -36,8 +35,6 @@ private
|
|||
else
|
||||
@create_params = { name: params[:name],
|
||||
device: current_device }
|
||||
tsid = params[:tool_slot_id]
|
||||
@create_params[:tool_slot_id] = tsid if tsid
|
||||
@create_params
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
class Tool < ApplicationRecord
|
||||
belongs_to :tool_slot
|
||||
belongs_to :device
|
||||
has_one :tool_slot
|
||||
validates :device, presence: true
|
||||
validates :name, uniqueness: { scope: :device }
|
||||
validates :name, uniqueness: { scope: :device }
|
||||
end
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
class ToolSlot < ApplicationRecord
|
||||
has_many :tools
|
||||
belongs_to :tool
|
||||
belongs_to :tool_bay
|
||||
end
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
module ToolSlots
|
||||
class Base < Mutations::Command
|
||||
def has_tool_id
|
||||
!!tool_id
|
||||
end
|
||||
|
||||
def has_tool_bay
|
||||
# Verifies that the tool bay EXISTS and is owned by user.
|
||||
device.tool_bays.where(id: tool_bay_id).any?
|
||||
end
|
||||
|
||||
def owns_tool
|
||||
devices.tools.where(id: tool_id).any?
|
||||
end
|
||||
|
||||
def validate_bay
|
||||
add_error :tool_bay_id,
|
||||
:not_found,
|
||||
"Can't find tool bay with id #{tool_bay_id}" unless has_tool_bay
|
||||
end
|
||||
|
||||
def validate_tool
|
||||
if has_tool_id && !owns_tool
|
||||
add_error :tool_id,
|
||||
:not_found,
|
||||
"Can't find tool with id #{tool_id}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
module ToolSlots
|
||||
class Create < Mutations::Command
|
||||
class Create < ToolSlots::Base
|
||||
required do
|
||||
model :device, class: Device
|
||||
integer :tool_bay_id
|
||||
|
@ -14,11 +14,8 @@ module ToolSlots
|
|||
end
|
||||
|
||||
def validate
|
||||
if !device.tool_bays.where(id: tool_bay_id).any?
|
||||
add_error :tool_bay_id,
|
||||
:not_found,
|
||||
"Can't find tool bay with id #{tool_bay_id}"
|
||||
end
|
||||
validate_tool
|
||||
validate_bay
|
||||
end
|
||||
|
||||
def execute
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
module ToolSlots
|
||||
class Update < Mutations::Command
|
||||
class Update < ToolSlots::Base
|
||||
required do
|
||||
model :device, class: Device
|
||||
model :tool_slot, class: ToolSlot
|
||||
end
|
||||
|
||||
optional do
|
||||
integer :tool_id
|
||||
string :name
|
||||
integer :x
|
||||
integer :y
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
module Tools
|
||||
class Base < Mutations::Command
|
||||
def it_is_your_tool_slot
|
||||
(tool_slot.tool_bay.device_id == device.id)
|
||||
end
|
||||
|
||||
def tool_slot
|
||||
@tool_slot ||= ToolSlot.find_by(id: tool_slot_id)
|
||||
end
|
||||
|
||||
def forbidden!
|
||||
raise Errors::Forbidden
|
||||
end
|
||||
|
||||
def bad_tool_slot_id!
|
||||
add_error :tool_slot, :bad, "Bad tool slot ID"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,19 +1,10 @@
|
|||
module Tools
|
||||
class Create < Tools::Base
|
||||
class Create < Mutations::Command
|
||||
required do
|
||||
string :name
|
||||
model :device, class: Device
|
||||
end
|
||||
|
||||
optional do
|
||||
integer :tool_slot_id
|
||||
end
|
||||
|
||||
def validate
|
||||
bad_tool_slot_id! if tool_slot_id && !tool_slot
|
||||
forbidden! if tool_slot_id && !it_is_your_tool_slot
|
||||
end
|
||||
|
||||
def execute
|
||||
Tool.create!(inputs)
|
||||
end
|
||||
|
|
|
@ -1,17 +1,11 @@
|
|||
module Tools
|
||||
class Update < Tools::Base
|
||||
class Update < Mutations::Command
|
||||
required do
|
||||
model :tool, class: Tool
|
||||
end
|
||||
|
||||
optional do
|
||||
string :name
|
||||
integer :tool_slot_id
|
||||
end
|
||||
|
||||
def validate
|
||||
bad_tool_slot_id! if tool_slot_id && !tool_slot
|
||||
forbidden! if tool_slot_id && !it_is_your_tool_slot
|
||||
end
|
||||
|
||||
def execute
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
class ToolSerializer < ActiveModel::Serializer
|
||||
attributes :id, :tool_slot_id, :name
|
||||
attributes :id, :name
|
||||
end
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
class ToolSlotSerializer < ActiveModel::Serializer
|
||||
attributes :id, :tool_bay_id, :name, :x, :y, :z
|
||||
attributes :id, :tool_bay_id, :tool_id, :name, :x, :y, :z
|
||||
end
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
class FlipFlopToolsRelation < ActiveRecord::Migration[5.0]
|
||||
def change
|
||||
remove_column :tools, :tool_slot_id, :integer
|
||||
add_column :tool_slots, :tool_id, :integer
|
||||
add_index :tool_slots, :tool_id
|
||||
end
|
||||
end
|
10
db/schema.rb
10
db/schema.rb
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20161206162700) do
|
||||
ActiveRecord::Schema.define(version: 20161206210809) do
|
||||
|
||||
create_table "delayed_jobs", force: :cascade do |t|
|
||||
t.integer "priority", default: 0, null: false
|
||||
|
@ -136,17 +136,17 @@ ActiveRecord::Schema.define(version: 20161206162700) do
|
|||
t.integer "z"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "tool_id"
|
||||
t.index ["tool_bay_id"], name: "index_tool_slots_on_tool_bay_id"
|
||||
t.index ["tool_id"], name: "index_tool_slots_on_tool_id"
|
||||
end
|
||||
|
||||
create_table "tools", force: :cascade do |t|
|
||||
t.integer "tool_slot_id"
|
||||
t.string "name"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
t.integer "device_id"
|
||||
t.index ["device_id"], name: "index_tools_on_device_id"
|
||||
t.index ["tool_slot_id"], name: "index_tools_on_tool_slot_id"
|
||||
end
|
||||
|
||||
create_table "users", force: :cascade do |t|
|
||||
|
|
Loading…
Reference in New Issue