From ce0d3d71fa19fba1d40f6bcead08bdceaf260772 Mon Sep 17 00:00:00 2001 From: "Jesse C. Fisher" Date: Sat, 11 Nov 2023 17:32:29 +0000 Subject: [PATCH] Add follow / unfollow button --- app/assets/javascripts/relationships.coffee | 3 ++ app/assets/stylesheets/relationships.sass | 3 ++ app/controllers/relationships_controller.rb | 15 ++++++ app/controllers/tokens_controller.rb | 5 +- app/helpers/relationships_helper.rb | 2 + app/policies/relationship_policy.rb | 55 +++++++++++++++++++++ app/views/profiles/_follow.html.haml | 3 ++ app/views/profiles/_follow_form.html.haml | 5 ++ app/views/profiles/_unfollow.html.haml | 3 ++ app/views/profiles/show.html.haml | 1 + config/routes.rb | 7 ++- spec/helpers/relationships_helper_spec.rb | 15 ++++++ spec/requests/relationships_spec.rb | 7 +++ 13 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/relationships.coffee create mode 100644 app/assets/stylesheets/relationships.sass create mode 100644 app/controllers/relationships_controller.rb create mode 100644 app/helpers/relationships_helper.rb create mode 100644 app/policies/relationship_policy.rb create mode 100644 app/views/profiles/_follow.html.haml create mode 100644 app/views/profiles/_follow_form.html.haml create mode 100644 app/views/profiles/_unfollow.html.haml create mode 100644 spec/helpers/relationships_helper_spec.rb create mode 100644 spec/requests/relationships_spec.rb diff --git a/app/assets/javascripts/relationships.coffee b/app/assets/javascripts/relationships.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/relationships.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/relationships.sass b/app/assets/stylesheets/relationships.sass new file mode 100644 index 00000000..c5d17bca --- /dev/null +++ b/app/assets/stylesheets/relationships.sass @@ -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/ diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb new file mode 100644 index 00000000..e328cc73 --- /dev/null +++ b/app/controllers/relationships_controller.rb @@ -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 diff --git a/app/controllers/tokens_controller.rb b/app/controllers/tokens_controller.rb index fcf523f9..34709fb1 100644 --- a/app/controllers/tokens_controller.rb +++ b/app/controllers/tokens_controller.rb @@ -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) diff --git a/app/helpers/relationships_helper.rb b/app/helpers/relationships_helper.rb new file mode 100644 index 00000000..3b96a9c0 --- /dev/null +++ b/app/helpers/relationships_helper.rb @@ -0,0 +1,2 @@ +module RelationshipsHelper +end diff --git a/app/policies/relationship_policy.rb b/app/policies/relationship_policy.rb new file mode 100644 index 00000000..1c6fad29 --- /dev/null +++ b/app/policies/relationship_policy.rb @@ -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 diff --git a/app/views/profiles/_follow.html.haml b/app/views/profiles/_follow.html.haml new file mode 100644 index 00000000..69335a2c --- /dev/null +++ b/app/views/profiles/_follow.html.haml @@ -0,0 +1,3 @@ += form_for(current_user.active_relationships.build) do |f| + %div= hidden_field_tag :followed_id, @profile.id + = f.submit "Follow" diff --git a/app/views/profiles/_follow_form.html.haml b/app/views/profiles/_follow_form.html.haml new file mode 100644 index 00000000..a6d94448 --- /dev/null +++ b/app/views/profiles/_follow_form.html.haml @@ -0,0 +1,5 @@ +#follow_form + - if current_user.following?(@profile) + = render 'unfollow' + - else + = render 'follow' diff --git a/app/views/profiles/_unfollow.html.haml b/app/views/profiles/_unfollow.html.haml new file mode 100644 index 00000000..3a5fcf67 --- /dev/null +++ b/app/views/profiles/_unfollow.html.haml @@ -0,0 +1,3 @@ += form_for(current_user.active_relationships.find_by(followed_id: @profile.id), | + html: { method: :delete }) do |f| | + = f.submit "Unfollow" diff --git a/app/views/profiles/show.html.haml b/app/views/profiles/show.html.haml index ae936275..6cf0766a 100644 --- a/app/views/profiles/show.html.haml +++ b/app/views/profiles/show.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index d0c57335..e3022463 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/helpers/relationships_helper_spec.rb b/spec/helpers/relationships_helper_spec.rb new file mode 100644 index 00000000..ff213d76 --- /dev/null +++ b/spec/helpers/relationships_helper_spec.rb @@ -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 diff --git a/spec/requests/relationships_spec.rb b/spec/requests/relationships_spec.rb new file mode 100644 index 00000000..e98f5ea8 --- /dev/null +++ b/spec/requests/relationships_spec.rb @@ -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