Folders#create action
parent
3030344426
commit
dc86f82795
|
@ -34,6 +34,7 @@ class Device < ApplicationRecord
|
|||
has_many :in_use_tools
|
||||
has_many :in_use_points
|
||||
has_many :users
|
||||
has_many :folders
|
||||
|
||||
validates_presence_of :name
|
||||
validates :timezone, inclusion: {
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
class Folder < ApplicationRecord
|
||||
belongs_to :device
|
||||
belongs_to :device, dependent: :destroy
|
||||
|
||||
has_many :sub_folders, class_name: "Folder",
|
||||
foreign_key: "parent_id"
|
||||
|
||||
belongs_to :parent, class_name: "Folder", optional: true
|
||||
end
|
|
@ -0,0 +1,37 @@
|
|||
module Folders
|
||||
class Create < Mutations::Command
|
||||
required do
|
||||
model :device
|
||||
string :color
|
||||
string :name
|
||||
end
|
||||
|
||||
optional do
|
||||
integer :parent_id
|
||||
end
|
||||
|
||||
def validate
|
||||
validate_parent_id
|
||||
end
|
||||
|
||||
def execute
|
||||
Folder.create!(update_params)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_params
|
||||
{ parent: parent }.merge(inputs.except(:parent_id))
|
||||
end
|
||||
|
||||
def parent
|
||||
@parent ||= device.folders.find(parent_id)
|
||||
end
|
||||
|
||||
def validate_parent_id
|
||||
unless parent
|
||||
add_error :folder_id, :folder_id_invalid, "Folder ID is not valid"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,14 +1,20 @@
|
|||
class AddFolderColumns < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
create_table :folders do |t|
|
||||
t.references :device
|
||||
t.references :device, null: false
|
||||
t.timestamps
|
||||
# https://twitter.com/wesbos/status/719678818831757313?lang=en
|
||||
t.string :color, limit: 20
|
||||
t.string :name, limit: 40
|
||||
t.string :color, limit: 20, null: false
|
||||
t.string :name, limit: 40, null: false
|
||||
end
|
||||
add_column :folders, :parent_id, :integer, null: true, index: true
|
||||
add_foreign_key :folders, :folders, column: :parent_id
|
||||
add_column :folders,
|
||||
:parent_id,
|
||||
:integer,
|
||||
null: true,
|
||||
index: true
|
||||
add_foreign_key :folders,
|
||||
:folders,
|
||||
column: :parent_id
|
||||
|
||||
add_reference :sequences, :folder, index: true
|
||||
end
|
||||
|
|
|
@ -669,11 +669,11 @@ ALTER SEQUENCE public.firmware_configs_id_seq OWNED BY public.firmware_configs.i
|
|||
|
||||
CREATE TABLE public.folders (
|
||||
id bigint NOT NULL,
|
||||
device_id bigint,
|
||||
device_id bigint NOT NULL,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
updated_at timestamp(6) without time zone NOT NULL,
|
||||
color character varying(20),
|
||||
name character varying(40),
|
||||
color character varying(20) NOT NULL,
|
||||
name character varying(40) NOT NULL,
|
||||
parent_id integer
|
||||
);
|
||||
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
require "spec_helper"
|
||||
|
||||
describe Api::FoldersController do
|
||||
let(:user) { FactoryBot.create(:user) }
|
||||
include Devise::Test::ControllerHelpers
|
||||
|
||||
it "creates a webcam feed" do
|
||||
sign_in user
|
||||
parent = Folder.create!(name: "parent", color: "red", device: user.device)
|
||||
input = {
|
||||
parent_id: parent.id,
|
||||
color: "blue",
|
||||
name: "child"
|
||||
}
|
||||
b4 = Folder.count
|
||||
expect(user.device.folders.count).to eq(1)
|
||||
post :create, body: input.to_json
|
||||
expect(response.status).to eq(200)
|
||||
expect(Folder.count).to be > b4
|
||||
expect(user.device.folders.count).to eq(2)
|
||||
input.keys.map do |key|
|
||||
binding.pry if json[key] != input[key]
|
||||
expect(input[key]).to eq(json[key])
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue