Add Token scaffold

This commit is contained in:
Jesse C. Fisher 2023-10-27 04:47:40 +00:00 committed by TheNeedful-DO
parent bf0e558bdd
commit 0105134a7f
30 changed files with 622 additions and 5 deletions

View File

@ -82,5 +82,3 @@ gem 'inline_svg'
gem 'importmap-rails'
gem 'turbo-rails'
gem 'stimulus-rails'

View File

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the Tokens controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/

View File

@ -0,0 +1,73 @@
class TokensController < ApplicationController
before_action :set_token, only: %i[ show edit update destroy ]
# GET /tokens or /tokens.json
def index
authorize @tokens = Token.all
end
# GET /tokens/1 or /tokens/1.json
def show
authorize @token
end
# GET /tokens/new
def new
authorize @token = Token.new
end
# GET /tokens/1/edit
def edit
authorize @token
end
# POST /tokens or /tokens.json
def create
authorize @token = Token.new(token_params)
respond_to do |format|
if @token.save
format.html { redirect_to token_url(@token), notice: "Token was successfully created." }
format.json { render :show, status: :created, location: @token }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @token.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tokens/1 or /tokens/1.json
def update
authorize @token
respond_to do |format|
if @token.update(token_params)
format.html { redirect_to token_url(@token), notice: "Token was successfully updated." }
format.json { render :show, status: :ok, location: @token }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @token.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tokens/1 or /tokens/1.json
def destroy
@token.destroy
respond_to do |format|
format.html { redirect_to tokens_url, notice: "Token was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_token
@token = Token.find(params[:id])
end
# Only allow a list of trusted parameters through.
def token_params
params.require(:token).permit(:canister_id, :standard, :symbol, :name, :total_supply, :decimals, :transfer_fee, :logo_url)
end
end

View File

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

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

@ -0,0 +1,3 @@
class Token < ActiveRecord::Base
validates :canister_id, presence: true, uniqueness: true
end

View File

@ -0,0 +1,56 @@
class TokenPolicy
attr_reader :user, :token
def initialize(user, token)
@user = user || User.new
@token = token
end
class Scope
def initialize(user, scope)
@user = user
@scope = scope
end
def resolve
if user.admin?
scope.all
else
scope.all
end
end
private
attr_reader :user, :scope
end
def index?
true
end
def show?
true
end
def create?
@user.admin?
end
def new?
@user.admin?
end
def edit?
@user.admin?
end
def update?
@user.admin?
end
def destroy?
@user.admin?
end
end

View File

@ -1,8 +1,9 @@
<nav>
<ul>
<li><%= link_to 'Home', root_path, data: { turbo: false } %></li>
<li><%= link_to 'Tokens', tokens_path %></li>
<li><%= link_to 'Projects', projects_path %></li>
<li><%= link_to 'Doodads', doodads_path %></li>
<li><%= link_to 'Doodads', doodads_path, data: { turbo: false } %></li>
<% if !request.base_url.match(/pin|my|fireside/) %>
<% if policy(:table).index? %>
<li><%= link_to 'Tables', tables_path %></li>

View File

@ -0,0 +1,15 @@
= simple_form_for(@token) do |f|
= f.error_notification
.form-inputs
= f.input :canister_id
= f.input :standard
= f.input :symbol
= f.input :name
= f.input :total_supply
= f.input :decimals
= f.input :transfer_fee
= f.input :logo_url
.form-actions
= f.button :submit, "Add Token"

View File

@ -0,0 +1,2 @@
json.extract! token, :id, :canister_id, :standard, :symbol, :name, :total_supply, :decimals, :transfer_fee, :created_at, :updated_at
json.url token_url(token, format: :json)

View File

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

View File

@ -0,0 +1,37 @@
%h1 Listing tokens
- if policy(Token).new?
= link_to 'Add Token', new_token_path
%table
%thead
%tr
%th Logo
%th Canister
%th Standard
%th Symbol
%th Name
%th Total supply
%th Decimals
%th Transfer fee
- if policy(@tokens).update?
%th
%th
%th
%tbody
- @tokens.each do |token|
%tr
%td= token.logo_url
%td= token.canister_id
%td= token.standard
%td= token.symbol
%td= token.name
%td= token.total_supply
%td= token.decimals
%td= token.transfer_fee
- if policy(token).update?
%td= link_to 'Show', token
%td= link_to 'Edit', edit_token_path(token)
%td= link_to 'Destroy', token, method: :delete, data: { confirm: 'Are you sure?' }

View File

@ -0,0 +1 @@
json.array! @tokens, partial: "tokens/token", as: :token

View File

@ -0,0 +1,5 @@
%h1 Add token
= render 'form'
= link_to 'Back', tokens_path

View File

@ -0,0 +1,30 @@
%p#notice= notice
%p
%b Canister:
= @token.logo_url
%p
%b Canister:
= @token.canister_id
%p
%b Standard:
= @token.standard
%p
%b Symbol:
= @token.symbol
%p
%b Name:
= @token.name
%p
%b Total supply:
= @token.total_supply
%p
%b Decimals:
= @token.decimals
%p
%b Transfer fee:
= @token.transfer_fee
= link_to 'Edit', edit_token_path(@token)
\|
= link_to 'Back', tokens_path

View File

@ -0,0 +1 @@
json.partial! "tokens/token", token: @token

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do
resources :tokens
resources :projects
resources :doodads
resources :canisters

View File

@ -0,0 +1,16 @@
class CreateTokens < ActiveRecord::Migration[6.1]
def change
create_table :tokens do |t|
t.string :canister_id
t.integer :standard
t.string :symbol
t.string :name
t.bigint :total_supply
t.integer :decimals
t.bigint :transfer_fee
t.timestamps
end
add_index :tokens, :canister_id, unique: true
end
end

View File

@ -0,0 +1,5 @@
class AddLogoUrlToToken < ActiveRecord::Migration[6.1]
def change
add_column :tokens, :logo_url, :string
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2023_10_26_005659) do
ActiveRecord::Schema.define(version: 2023_10_27_042335) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -133,6 +133,20 @@ ActiveRecord::Schema.define(version: 2023_10_26_005659) do
t.index ["slug"], name: "index_tables_on_slug", unique: true
end
create_table "tokens", force: :cascade do |t|
t.string "canister_id"
t.integer "standard"
t.string "symbol"
t.string "name"
t.bigint "total_supply"
t.integer "decimals"
t.bigint "transfer_fee"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "logo_url"
t.index ["canister_id"], name: "index_tokens_on_canister_id", unique: true
end
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: ""

11
spec/factories/tokens.rb Normal file
View File

@ -0,0 +1,11 @@
FactoryGirl.define do
factory :token do
canister_id "MyString"
standard 1
symbol "MyString"
name "MyString"
total_supply ""
decimals 1
transfer_fee ""
end
end

View File

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the TokensHelper. For example:
#
# describe TokensHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe TokensHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

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

View File

@ -4,7 +4,7 @@ require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
`trash tmp/capybara/*`
# `trash tmp/capybara/*`
require 'capybara-screenshot/rspec'
# Requires supporting ruby files with custom matchers and macros, etc, in

View File

@ -0,0 +1,135 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to test the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
RSpec.describe "/tokens", type: :request do
# This should return the minimal set of attributes required to create a valid
# Token. As you add validations to Token, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}
describe "GET /index" do
it "renders a successful response" do
Token.create! valid_attributes
get tokens_url
expect(response).to be_successful
end
end
describe "GET /show" do
it "renders a successful response" do
token = Token.create! valid_attributes
get token_url(token)
expect(response).to be_successful
end
end
describe "GET /new" do
it "renders a successful response" do
get new_token_url
expect(response).to be_successful
end
end
describe "GET /edit" do
it "renders a successful response" do
token = Token.create! valid_attributes
get edit_token_url(token)
expect(response).to be_successful
end
end
describe "POST /create" do
context "with valid parameters" do
it "creates a new Token" do
expect {
post tokens_url, params: { token: valid_attributes }
}.to change(Token, :count).by(1)
end
it "redirects to the created token" do
post tokens_url, params: { token: valid_attributes }
expect(response).to redirect_to(token_url(Token.last))
end
end
context "with invalid parameters" do
it "does not create a new Token" do
expect {
post tokens_url, params: { token: invalid_attributes }
}.to change(Token, :count).by(0)
end
it "renders a successful response (i.e. to display the 'new' template)" do
post tokens_url, params: { token: invalid_attributes }
expect(response).to be_successful
end
end
end
describe "PATCH /update" do
context "with valid parameters" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
it "updates the requested token" do
token = Token.create! valid_attributes
patch token_url(token), params: { token: new_attributes }
token.reload
skip("Add assertions for updated state")
end
it "redirects to the token" do
token = Token.create! valid_attributes
patch token_url(token), params: { token: new_attributes }
token.reload
expect(response).to redirect_to(token_url(token))
end
end
context "with invalid parameters" do
it "renders a successful response (i.e. to display the 'edit' template)" do
token = Token.create! valid_attributes
patch token_url(token), params: { token: invalid_attributes }
expect(response).to be_successful
end
end
end
describe "DELETE /destroy" do
it "destroys the requested token" do
token = Token.create! valid_attributes
expect {
delete token_url(token)
}.to change(Token, :count).by(-1)
end
it "redirects to the tokens list" do
token = Token.create! valid_attributes
delete token_url(token)
expect(response).to redirect_to(tokens_url)
end
end
end

View File

@ -0,0 +1,38 @@
require "rails_helper"
RSpec.describe TokensController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "/tokens").to route_to("tokens#index")
end
it "routes to #new" do
expect(get: "/tokens/new").to route_to("tokens#new")
end
it "routes to #show" do
expect(get: "/tokens/1").to route_to("tokens#show", id: "1")
end
it "routes to #edit" do
expect(get: "/tokens/1/edit").to route_to("tokens#edit", id: "1")
end
it "routes to #create" do
expect(post: "/tokens").to route_to("tokens#create")
end
it "routes to #update via PUT" do
expect(put: "/tokens/1").to route_to("tokens#update", id: "1")
end
it "routes to #update via PATCH" do
expect(patch: "/tokens/1").to route_to("tokens#update", id: "1")
end
it "routes to #destroy" do
expect(delete: "/tokens/1").to route_to("tokens#destroy", id: "1")
end
end
end

View File

@ -0,0 +1,40 @@
require 'rails_helper'
RSpec.describe "tokens/edit", type: :view do
let(:token) {
Token.create!(
canister_id: "MyString",
standard: 1,
symbol: "MyString",
name: "MyString",
total_supply: "",
decimals: 1,
transfer_fee: ""
)
}
before(:each) do
assign(:token, token)
end
it "renders the edit token form" do
render
assert_select "form[action=?][method=?]", token_path(token), "post" do
assert_select "input[name=?]", "token[canister_id]"
assert_select "input[name=?]", "token[standard]"
assert_select "input[name=?]", "token[symbol]"
assert_select "input[name=?]", "token[name]"
assert_select "input[name=?]", "token[total_supply]"
assert_select "input[name=?]", "token[decimals]"
assert_select "input[name=?]", "token[transfer_fee]"
end
end
end

View File

@ -0,0 +1,38 @@
require 'rails_helper'
RSpec.describe "tokens/index", type: :view do
before(:each) do
assign(:tokens, [
Token.create!(
canister_id: "Canister",
standard: 2,
symbol: "Symbol",
name: "Name",
total_supply: "",
decimals: 3,
transfer_fee: ""
),
Token.create!(
canister_id: "Canister",
standard: 2,
symbol: "Symbol",
name: "Name",
total_supply: "",
decimals: 3,
transfer_fee: ""
)
])
end
it "renders a list of tokens" do
render
cell_selector = Rails::VERSION::STRING >= '7' ? 'div>p' : 'tr>td'
assert_select cell_selector, text: Regexp.new("Canister".to_s), count: 2
assert_select cell_selector, text: Regexp.new(2.to_s), count: 2
assert_select cell_selector, text: Regexp.new("Symbol".to_s), count: 2
assert_select cell_selector, text: Regexp.new("Name".to_s), count: 2
assert_select cell_selector, text: Regexp.new("".to_s), count: 2
assert_select cell_selector, text: Regexp.new(3.to_s), count: 2
assert_select cell_selector, text: Regexp.new("".to_s), count: 2
end
end

View File

@ -0,0 +1,36 @@
require 'rails_helper'
RSpec.describe "tokens/new", type: :view do
before(:each) do
assign(:token, Token.new(
canister_id: "MyString",
standard: 1,
symbol: "MyString",
name: "MyString",
total_supply: "",
decimals: 1,
transfer_fee: ""
))
end
it "renders new token form" do
render
assert_select "form[action=?][method=?]", tokens_path, "post" do
assert_select "input[name=?]", "token[canister_id]"
assert_select "input[name=?]", "token[standard]"
assert_select "input[name=?]", "token[symbol]"
assert_select "input[name=?]", "token[name]"
assert_select "input[name=?]", "token[total_supply]"
assert_select "input[name=?]", "token[decimals]"
assert_select "input[name=?]", "token[transfer_fee]"
end
end
end

View File

@ -0,0 +1,26 @@
require 'rails_helper'
RSpec.describe "tokens/show", type: :view do
before(:each) do
assign(:token, Token.create!(
canister_id: "Canister",
standard: 2,
symbol: "Symbol",
name: "Name",
total_supply: "",
decimals: 3,
transfer_fee: ""
))
end
it "renders attributes in <p>" do
render
expect(rendered).to match(/Canister/)
expect(rendered).to match(/2/)
expect(rendered).to match(/Symbol/)
expect(rendered).to match(/Name/)
expect(rendered).to match(//)
expect(rendered).to match(/3/)
expect(rendered).to match(//)
end
end