Add Canister scaffold
This commit is contained in:
parent
9db776bfab
commit
d809465af1
73
app/controllers/canisters_controller.rb
Normal file
73
app/controllers/canisters_controller.rb
Normal 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
|
||||
@ -1,3 +1,7 @@
|
||||
class VisitorsController < ApplicationController
|
||||
before_action :skip_authorization
|
||||
|
||||
def index
|
||||
@canisters = authorize Canster.all
|
||||
end
|
||||
end
|
||||
|
||||
2
app/helpers/canisters_helper.rb
Normal file
2
app/helpers/canisters_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module CanistersHelper
|
||||
end
|
||||
3
app/models/canister.rb
Normal file
3
app/models/canister.rb
Normal file
@ -0,0 +1,3 @@
|
||||
class Canister < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
end
|
||||
@ -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
|
||||
|
||||
37
app/policies/canister_policy.rb
Normal file
37
app/policies/canister_policy.rb
Normal 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
|
||||
15
app/views/canisters/_canister.html.haml
Normal file
15
app/views/canisters/_canister.html.haml
Normal 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
|
||||
2
app/views/canisters/_canister.json.jbuilder
Normal file
2
app/views/canisters/_canister.json.jbuilder
Normal file
@ -0,0 +1,2 @@
|
||||
json.extract! canister, :id, :icid, :name, :user_id, :created_at, :updated_at
|
||||
json.url canister_url(canister, format: :json)
|
||||
11
app/views/canisters/_form.html.haml
Normal file
11
app/views/canisters/_form.html.haml
Normal 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
|
||||
7
app/views/canisters/edit.html.haml
Normal file
7
app/views/canisters/edit.html.haml
Normal file
@ -0,0 +1,7 @@
|
||||
%h1 Editing canister
|
||||
|
||||
= render 'form'
|
||||
|
||||
= link_to 'Show', @canister
|
||||
\|
|
||||
= link_to 'Back', canisters_path
|
||||
24
app/views/canisters/index.html.haml
Normal file
24
app/views/canisters/index.html.haml
Normal 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?' }
|
||||
|
||||
1
app/views/canisters/index.json.jbuilder
Normal file
1
app/views/canisters/index.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.array! @canisters, partial: "canisters/canister", as: :canister
|
||||
5
app/views/canisters/new.html.haml
Normal file
5
app/views/canisters/new.html.haml
Normal file
@ -0,0 +1,5 @@
|
||||
%h1 New canister
|
||||
|
||||
= render 'form'
|
||||
|
||||
= link_to 'Back', canisters_path
|
||||
15
app/views/canisters/show.html.haml
Normal file
15
app/views/canisters/show.html.haml
Normal 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
|
||||
1
app/views/canisters/show.json.jbuilder
Normal file
1
app/views/canisters/show.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.partial! "canisters/canister", canister: @canister
|
||||
@ -1,4 +1,5 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :canisters
|
||||
resources :players
|
||||
resources :tables do
|
||||
member do
|
||||
|
||||
11
db/migrate/20230921143200_create_canisters.rb
Normal file
11
db/migrate/20230921143200_create_canisters.rb
Normal 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
|
||||
12
db/schema.rb
12
db/schema.rb
@ -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
|
||||
|
||||
7
spec/factories/canisters.rb
Normal file
7
spec/factories/canisters.rb
Normal file
@ -0,0 +1,7 @@
|
||||
FactoryGirl.define do
|
||||
factory :canister do
|
||||
icid "MyString"
|
||||
name "MyString"
|
||||
user nil
|
||||
end
|
||||
end
|
||||
5
spec/models/canister_spec.rb
Normal file
5
spec/models/canister_spec.rb
Normal file
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Canister, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user