Add follow / unfollow button

This commit is contained in:
Jesse C. Fisher 2023-11-11 17:32:29 +00:00 committed by TheNeedful-DO
parent 6ab972ed38
commit a155b83a1b
13 changed files with 122 additions and 2 deletions

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 Relationships 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,15 @@
class RelationshipsController < ApplicationController
def create
profile = Profile.find(params[:followed_id])
authorize Relationship
current_user.follow(profile)
redirect_to profile
end
def destroy
profile = Relationship.find(params[:id]).followed
authorize profile
current_user.unfollow(profile)
redirect_to profile
end
end

View File

@ -43,7 +43,10 @@ class TokensController < ApplicationController
@token.standard = :icrc1
@token.transfer_fee = metadata.lines[2].split[7].to_i
@token.decimals = metadata.lines[5].split[7].to_i
@token.logo_url = metadata.lines[9].split("\"")[1]
begin
@token.logo_url = metadata.lines[9].split("\"")[1]
rescue
end
@token.name = metadata.lines[3].split("\"")[3]
total_supply = `dfx canister --network ic call #{@token.canister_id} icrc1_total_supply 2>&1`
@token.total_supply = total_supply.split[0][1..-1].to_i/(10.0**@token.decimals)

View File

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

View File

@ -0,0 +1,55 @@
class RelationshipPolicy
attr_reader :user, :relationship
def initialize(user, model)
@user = user || User.new
@relationship = model
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? || @user.signed_in?
end
def new?
@user.admin? || @user.signed_in?
end
def edit?
@user.admin? || @user.id == @relationship.follower_id
end
def update?
@user.admin? || @user.id == @relationship.follower_id
end
def destroy?
@user.admin? || @user.id == @relationship.follower_id
end
end

View File

@ -0,0 +1,3 @@
= form_for(current_user.active_relationships.build) do |f|
%div= hidden_field_tag :followed_id, @profile.id
= f.submit "Follow"

View File

@ -0,0 +1,5 @@
#follow_form
- if current_user.following?(@profile)
= render 'unfollow'
- else
= render 'follow'

View File

@ -0,0 +1,3 @@
= form_for(current_user.active_relationships.find_by(followed_id: @profile.id), |
html: { method: :delete }) do |f| |
= f.submit "Unfollow"

View File

@ -3,6 +3,7 @@
= @profile.name
- if policy(@profile).edit?
= link_to 'Edit', edit_profile_path(@profile)
= render 'follow_form' if current_user.signed_in?
%p
= @profile.summary

View File

@ -1,6 +1,10 @@
Rails.application.routes.draw do
resources :social_links
resources :profiles
resources :profiles do
member do
get :followers
end
end
resources :tokens do
member do
get 'refresh'
@ -70,4 +74,5 @@ Rails.application.routes.draw do
get :following
end
end
resources :relationships, only: [:create, :destroy]
end

View File

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the RelationshipsHelper. For example:
#
# describe RelationshipsHelper 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 RelationshipsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -0,0 +1,7 @@
require 'rails_helper'
RSpec.describe "Relationships", type: :request do
describe "GET /index" do
pending "add some examples (or delete) #{__FILE__}"
end
end