Add Canister scaffold

This commit is contained in:
icwizardmonke 2023-09-24 09:49:42 +00:00
parent 429e67aedd
commit ed1dab2429
20 changed files with 236 additions and 1 deletions

View File

@ -0,0 +1,73 @@
class CanistersController < ApplicationController
before_action :set_canister, only: %i[ show edit update destroy ]
# GET /canisters or /canisters.json
def index
@canisters = authorize Canister.all
end
# GET /canisters/1 or /canisters/1.json
def show
authorize @canister
end
# GET /canisters/new
def new
authorize @canister = current_user.canisters.new
end
# GET /canisters/1/edit
def edit
authorize @canister
end
# POST /canisters or /canisters.json
def create
authorize @canister = Canister.new(canister_params)
respond_to do |format|
if @canister.save
format.html { redirect_to @canister, notice: "Canister was successfully created." }
format.json { render :show, status: :created, location: @canister }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @canister.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /canisters/1 or /canisters/1.json
def update
authorize @canister
respond_to do |format|
if @canister.update(canister_params)
format.html { redirect_to @canister, notice: "Canister was successfully updated." }
format.json { render :show, status: :ok, location: @canister }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @canister.errors, status: :unprocessable_entity }
end
end
end
# DELETE /canisters/1 or /canisters/1.json
def destroy
authorize @canister
@canister.destroy
respond_to do |format|
format.html { redirect_to canisters_url, notice: "Canister was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_canister
@canister = Canister.find(params[:id])
end
# Only allow a list of trusted parameters through.
def canister_params
params.require(:canister).permit(:icid, :name, :user_id)
end
end

View File

@ -1,3 +1,7 @@
class VisitorsController < ApplicationController
before_action :skip_authorization
def index
@canisters = authorize Canster.all
end
end

View File

@ -0,0 +1,2 @@
module CanistersHelper
end

3
app/models/canister.rb Normal file
View File

@ -0,0 +1,3 @@
class Canister < ActiveRecord::Base
belongs_to :user
end

View File

@ -3,6 +3,7 @@ class User < ActiveRecord::Base
after_initialize :set_default_role, if: :new_record?
validates :email, presence: true, uniqueness: true
validates :soft_token, presence: true, uniqueness: true
has_many :canisters
def principal
stp = SoftTokenPrincipal.where(soft_token: self.soft_token).first_or_initialize

View File

@ -0,0 +1,37 @@
class CanisterPolicy
attr_reader :current_user, :model
def initialize(current_user, model)
@current_user = current_user || User.new
@model = model
end
def index?
@current_user.admin? || true
end
def show?
@current_user.admin? || @current_user == @model.user
end
def create?
@current_user.admin? || true
end
def new?
@current_user.admin?
end
def edit?
@current_user.admin? || @current_user == @model.user
end
def update?
@current_user.admin? || @current_user == @model.user
end
def destroy?
@current_user.admin? || @current_user == @model.user
end
end

View File

@ -0,0 +1,15 @@
%p#notice= notice
%p
%b Icid:
= canister.icid
%p
%b Name:
= canister.name
%p
%b User:
= canister.user
= link_to 'Edit', edit_canister_path(canister)
\|
= link_to 'Back', canisters_path

View File

@ -0,0 +1,2 @@
json.extract! canister, :id, :icid, :name, :user_id, :created_at, :updated_at
json.url canister_url(canister, format: :json)

View File

@ -0,0 +1,11 @@
= simple_form_for(@canister) do |f|
= f.error_notification
.form-inputs
= f.input :icid
= f.input :name
-# = f.association :user
= f.hidden_field :user_id
.form-actions
= f.button :submit

View File

@ -0,0 +1,7 @@
%h1 Editing canister
= render 'form'
= link_to 'Show', @canister
\|
= link_to 'Back', canisters_path

View File

@ -0,0 +1,24 @@
%h1 Listing canisters
%p= link_to '+ Add Existing Canister', new_canister_path
%table
%thead
%tr
%th ICid
%th Name
-# %th User
%th
%th
%th
%tbody
- @canisters.each do |canister|
%tr
%td= canister.icid
%td= canister.name
-# %td= canister.user
%td= link_to 'Show', canister
%td= link_to 'Edit', edit_canister_path(canister)
%td= link_to 'Destroy', canister, method: :delete, data: { confirm: 'Are you sure?' }

View File

@ -0,0 +1 @@
json.array! @canisters, partial: "canisters/canister", as: :canister

View File

@ -0,0 +1,5 @@
%h1 New canister
= render 'form'
= link_to 'Back', canisters_path

View File

@ -0,0 +1,15 @@
%p#notice= notice
%p
%b Icid:
= @canister.icid
%p
%b Name:
= @canister.name
%p
%b User:
= @canister.user
= link_to 'Edit', edit_canister_path(@canister)
\|
= link_to 'Back', canisters_path

View File

@ -0,0 +1 @@
json.partial! "canisters/canister", canister: @canister

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :canisters
resources :players
resources :tables do
member do

View File

@ -0,0 +1,11 @@
class CreateCanisters < ActiveRecord::Migration[5.2]
def change
create_table :canisters do |t|
t.string :icid
t.string :name
t.belongs_to :user, foreign_key: true
t.timestamps
end
end
end

View File

@ -10,11 +10,20 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2023_07_09_122324) do
ActiveRecord::Schema.define(version: 2023_09_21_143200) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "canisters", force: :cascade do |t|
t.string "icid"
t.string "name"
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_canisters_on_user_id"
end
create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
@ -142,4 +151,5 @@ ActiveRecord::Schema.define(version: 2023_07_09_122324) do
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "canisters", "users"
end

View File

@ -0,0 +1,7 @@
FactoryGirl.define do
factory :canister do
icid "MyString"
name "MyString"
user nil
end
end

View File

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Canister, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end