From d809465af154d6c0294db4887781d897998c4260 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Sun, 24 Sep 2023 09:49:42 +0000 Subject: [PATCH] Add Canister scaffold --- app/controllers/canisters_controller.rb | 73 +++++++++++++++++++ app/controllers/visitors_controller.rb | 4 + app/helpers/canisters_helper.rb | 2 + app/models/canister.rb | 3 + app/models/user.rb | 1 + app/policies/canister_policy.rb | 37 ++++++++++ app/views/canisters/_canister.html.haml | 15 ++++ app/views/canisters/_canister.json.jbuilder | 2 + app/views/canisters/_form.html.haml | 11 +++ app/views/canisters/edit.html.haml | 7 ++ app/views/canisters/index.html.haml | 24 ++++++ app/views/canisters/index.json.jbuilder | 1 + app/views/canisters/new.html.haml | 5 ++ app/views/canisters/show.html.haml | 15 ++++ app/views/canisters/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20230921143200_create_canisters.rb | 11 +++ db/schema.rb | 12 ++- spec/factories/canisters.rb | 7 ++ spec/models/canister_spec.rb | 5 ++ 20 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 app/controllers/canisters_controller.rb create mode 100644 app/helpers/canisters_helper.rb create mode 100644 app/models/canister.rb create mode 100644 app/policies/canister_policy.rb create mode 100644 app/views/canisters/_canister.html.haml create mode 100644 app/views/canisters/_canister.json.jbuilder create mode 100644 app/views/canisters/_form.html.haml create mode 100644 app/views/canisters/edit.html.haml create mode 100644 app/views/canisters/index.html.haml create mode 100644 app/views/canisters/index.json.jbuilder create mode 100644 app/views/canisters/new.html.haml create mode 100644 app/views/canisters/show.html.haml create mode 100644 app/views/canisters/show.json.jbuilder create mode 100644 db/migrate/20230921143200_create_canisters.rb create mode 100644 spec/factories/canisters.rb create mode 100644 spec/models/canister_spec.rb diff --git a/app/controllers/canisters_controller.rb b/app/controllers/canisters_controller.rb new file mode 100644 index 00000000..fe0e3313 --- /dev/null +++ b/app/controllers/canisters_controller.rb @@ -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 diff --git a/app/controllers/visitors_controller.rb b/app/controllers/visitors_controller.rb index 5c8d7136..e735a802 100644 --- a/app/controllers/visitors_controller.rb +++ b/app/controllers/visitors_controller.rb @@ -1,3 +1,7 @@ class VisitorsController < ApplicationController before_action :skip_authorization + + def index + @canisters = authorize Canster.all + end end diff --git a/app/helpers/canisters_helper.rb b/app/helpers/canisters_helper.rb new file mode 100644 index 00000000..102a42c5 --- /dev/null +++ b/app/helpers/canisters_helper.rb @@ -0,0 +1,2 @@ +module CanistersHelper +end diff --git a/app/models/canister.rb b/app/models/canister.rb new file mode 100644 index 00000000..8eaf3bb6 --- /dev/null +++ b/app/models/canister.rb @@ -0,0 +1,3 @@ +class Canister < ActiveRecord::Base + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index b22b66da..eff04513 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/app/policies/canister_policy.rb b/app/policies/canister_policy.rb new file mode 100644 index 00000000..17e7cad8 --- /dev/null +++ b/app/policies/canister_policy.rb @@ -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 diff --git a/app/views/canisters/_canister.html.haml b/app/views/canisters/_canister.html.haml new file mode 100644 index 00000000..26314dd0 --- /dev/null +++ b/app/views/canisters/_canister.html.haml @@ -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 diff --git a/app/views/canisters/_canister.json.jbuilder b/app/views/canisters/_canister.json.jbuilder new file mode 100644 index 00000000..0888ed70 --- /dev/null +++ b/app/views/canisters/_canister.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! canister, :id, :icid, :name, :user_id, :created_at, :updated_at +json.url canister_url(canister, format: :json) diff --git a/app/views/canisters/_form.html.haml b/app/views/canisters/_form.html.haml new file mode 100644 index 00000000..7ba183fe --- /dev/null +++ b/app/views/canisters/_form.html.haml @@ -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 diff --git a/app/views/canisters/edit.html.haml b/app/views/canisters/edit.html.haml new file mode 100644 index 00000000..13d481c3 --- /dev/null +++ b/app/views/canisters/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing canister + += render 'form' + += link_to 'Show', @canister +\| += link_to 'Back', canisters_path diff --git a/app/views/canisters/index.html.haml b/app/views/canisters/index.html.haml new file mode 100644 index 00000000..99d3560d --- /dev/null +++ b/app/views/canisters/index.html.haml @@ -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?' } + diff --git a/app/views/canisters/index.json.jbuilder b/app/views/canisters/index.json.jbuilder new file mode 100644 index 00000000..2d201997 --- /dev/null +++ b/app/views/canisters/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @canisters, partial: "canisters/canister", as: :canister diff --git a/app/views/canisters/new.html.haml b/app/views/canisters/new.html.haml new file mode 100644 index 00000000..acbcc9ad --- /dev/null +++ b/app/views/canisters/new.html.haml @@ -0,0 +1,5 @@ +%h1 New canister + += render 'form' + += link_to 'Back', canisters_path diff --git a/app/views/canisters/show.html.haml b/app/views/canisters/show.html.haml new file mode 100644 index 00000000..dfc7080a --- /dev/null +++ b/app/views/canisters/show.html.haml @@ -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 diff --git a/app/views/canisters/show.json.jbuilder b/app/views/canisters/show.json.jbuilder new file mode 100644 index 00000000..7648e0a6 --- /dev/null +++ b/app/views/canisters/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "canisters/canister", canister: @canister diff --git a/config/routes.rb b/config/routes.rb index 3037b5d4..070c20b0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :canisters resources :players resources :tables do member do diff --git a/db/migrate/20230921143200_create_canisters.rb b/db/migrate/20230921143200_create_canisters.rb new file mode 100644 index 00000000..1cd98b71 --- /dev/null +++ b/db/migrate/20230921143200_create_canisters.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index f380fff9..22b94e1f 100644 --- a/db/schema.rb +++ b/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 diff --git a/spec/factories/canisters.rb b/spec/factories/canisters.rb new file mode 100644 index 00000000..fe9bd06e --- /dev/null +++ b/spec/factories/canisters.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :canister do + icid "MyString" + name "MyString" + user nil + end +end diff --git a/spec/models/canister_spec.rb b/spec/models/canister_spec.rb new file mode 100644 index 00000000..37aefc4b --- /dev/null +++ b/spec/models/canister_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Canister, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end