diff --git a/Gemfile b/Gemfile index 3ce020b5..950e3895 100644 --- a/Gemfile +++ b/Gemfile @@ -82,5 +82,3 @@ gem 'inline_svg' gem 'importmap-rails' gem 'turbo-rails' gem 'stimulus-rails' - - diff --git a/app/assets/javascripts/tokens.coffee b/app/assets/javascripts/tokens.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/tokens.coffee @@ -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/ diff --git a/app/assets/stylesheets/tokens.scss b/app/assets/stylesheets/tokens.scss new file mode 100644 index 00000000..47b8ce4a --- /dev/null +++ b/app/assets/stylesheets/tokens.scss @@ -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/ diff --git a/app/controllers/tokens_controller.rb b/app/controllers/tokens_controller.rb new file mode 100644 index 00000000..abd422c8 --- /dev/null +++ b/app/controllers/tokens_controller.rb @@ -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 diff --git a/app/helpers/tokens_helper.rb b/app/helpers/tokens_helper.rb new file mode 100644 index 00000000..a16c59dd --- /dev/null +++ b/app/helpers/tokens_helper.rb @@ -0,0 +1,2 @@ +module TokensHelper +end diff --git a/app/models/token.rb b/app/models/token.rb new file mode 100644 index 00000000..db919465 --- /dev/null +++ b/app/models/token.rb @@ -0,0 +1,3 @@ +class Token < ActiveRecord::Base + validates :canister_id, presence: true, uniqueness: true +end diff --git a/app/policies/token_policy.rb b/app/policies/token_policy.rb new file mode 100644 index 00000000..eb3e10c8 --- /dev/null +++ b/app/policies/token_policy.rb @@ -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 diff --git a/app/views/layouts/_navigation_links.html.erb b/app/views/layouts/_navigation_links.html.erb index 0d9912be..d9a9b7b8 100644 --- a/app/views/layouts/_navigation_links.html.erb +++ b/app/views/layouts/_navigation_links.html.erb @@ -1,8 +1,9 @@