diff --git a/app/assets/javascripts/initializers/initScrolling.js.erb b/app/assets/javascripts/initializers/initScrolling.js.erb index 388ae30fb..29ef4e51f 100644 --- a/app/assets/javascripts/initializers/initScrolling.js.erb +++ b/app/assets/javascripts/initializers/initScrolling.js.erb @@ -94,13 +94,13 @@ function fetchNextFollowingPage(el) { var indexParams = JSON.parse(el.dataset.params); var action = indexParams.action; if (action.includes("users")) { - fetchNext(el, "/api/followings/users", insertNext({ elId: "follows" }, buildFollowsHTML)); + fetchNext(el, "/followings/users", insertNext({ elId: "follows" }, buildFollowsHTML)); } else if (action.includes("podcasts")) { - fetchNext(el, "/api/followings/podcasts", insertNext({ elId: "follows" }, buildFollowsHTML)); + fetchNext(el, "/followings/podcasts", insertNext({ elId: "follows" }, buildFollowsHTML)); } else if (action.includes("organizations")) { - fetchNext(el, "/api/followings/organizations", insertNext({ elId: "follows" }, buildFollowsHTML)); + fetchNext(el, "/followings/organizations", insertNext({ elId: "follows" }, buildFollowsHTML)); } else { - fetchNext(el, "/api/followings/tags", insertNext({ elId: "follows" }, buildTagsHTML)); + fetchNext(el, "/followings/tags", insertNext({ elId: "follows" }, buildTagsHTML)); } } diff --git a/app/controllers/api/v0/followings_controller.rb b/app/controllers/api/v0/followings_controller.rb deleted file mode 100644 index 2793155ea..000000000 --- a/app/controllers/api/v0/followings_controller.rb +++ /dev/null @@ -1,53 +0,0 @@ -module Api - module V0 - class FollowingsController < ApiController - before_action :authenticate_user! - before_action -> { limit_per_page(default: 80, max: 1000) } - - def users - relation = current_user.follows_by_type("User"). - select(ATTRIBUTES_FOR_SERIALIZATION). - order(created_at: :desc) - @follows = load_follows_and_paginate(relation) - end - - def tags - relation = current_user.follows_by_type("ActsAsTaggableOn::Tag"). - select(TAGS_ATTRIBUTES_FOR_SERIALIZATION). - order(points: :desc) - @followed_tags = load_follows_and_paginate(relation) - end - - def organizations - relation = current_user.follows_by_type("Organization"). - select(ATTRIBUTES_FOR_SERIALIZATION). - order(created_at: :desc) - @followed_organizations = load_follows_and_paginate(relation) - end - - def podcasts - relation = current_user.follows_by_type("Podcast"). - select(ATTRIBUTES_FOR_SERIALIZATION). - order(created_at: :desc) - @followed_podcasts = load_follows_and_paginate(relation) - end - - ATTRIBUTES_FOR_SERIALIZATION = %i[id followable_id followable_type].freeze - private_constant :ATTRIBUTES_FOR_SERIALIZATION - - TAGS_ATTRIBUTES_FOR_SERIALIZATION = [*ATTRIBUTES_FOR_SERIALIZATION, :points].freeze - private_constant :TAGS_ATTRIBUTES_FOR_SERIALIZATION - - private - - def limit_per_page(default:, max:) - per_page = (params[:per_page] || default).to_i - @follows_limit = [per_page, max].min - end - - def load_follows_and_paginate(relation) - relation.includes(:followable).page(params[:page]).per(@follows_limit) - end - end - end -end diff --git a/app/controllers/followings_controller.rb b/app/controllers/followings_controller.rb new file mode 100644 index 000000000..736975d08 --- /dev/null +++ b/app/controllers/followings_controller.rb @@ -0,0 +1,49 @@ +class FollowingsController < ApplicationController + before_action :authenticate_user! + before_action -> { limit_per_page(default: 80, max: 1000) } + + def users + relation = current_user.follows_by_type("User"). + select(ATTRIBUTES_FOR_SERIALIZATION). + order(created_at: :desc) + @follows = load_follows_and_paginate(relation) + end + + def tags + relation = current_user.follows_by_type("ActsAsTaggableOn::Tag"). + select(TAGS_ATTRIBUTES_FOR_SERIALIZATION). + order(points: :desc) + @followed_tags = load_follows_and_paginate(relation) + end + + def organizations + relation = current_user.follows_by_type("Organization"). + select(ATTRIBUTES_FOR_SERIALIZATION). + order(created_at: :desc) + @followed_organizations = load_follows_and_paginate(relation) + end + + def podcasts + relation = current_user.follows_by_type("Podcast"). + select(ATTRIBUTES_FOR_SERIALIZATION). + order(created_at: :desc) + @followed_podcasts = load_follows_and_paginate(relation) + end + + ATTRIBUTES_FOR_SERIALIZATION = %i[id followable_id followable_type].freeze + private_constant :ATTRIBUTES_FOR_SERIALIZATION + + TAGS_ATTRIBUTES_FOR_SERIALIZATION = [*ATTRIBUTES_FOR_SERIALIZATION, :points].freeze + private_constant :TAGS_ATTRIBUTES_FOR_SERIALIZATION + + private + + def limit_per_page(default:, max:) + per_page = (params[:per_page] || default).to_i + @follows_limit = [per_page, max].min + end + + def load_follows_and_paginate(relation) + relation.includes(:followable).page(params[:page]).per(@follows_limit) + end +end diff --git a/app/views/api/v0/followings/organizations.json.jbuilder b/app/views/followings/organizations.json.jbuilder similarity index 100% rename from app/views/api/v0/followings/organizations.json.jbuilder rename to app/views/followings/organizations.json.jbuilder diff --git a/app/views/api/v0/followings/podcasts.json.jbuilder b/app/views/followings/podcasts.json.jbuilder similarity index 100% rename from app/views/api/v0/followings/podcasts.json.jbuilder rename to app/views/followings/podcasts.json.jbuilder diff --git a/app/views/api/v0/followings/tags.json.jbuilder b/app/views/followings/tags.json.jbuilder similarity index 100% rename from app/views/api/v0/followings/tags.json.jbuilder rename to app/views/followings/tags.json.jbuilder diff --git a/app/views/api/v0/followings/users.json.jbuilder b/app/views/followings/users.json.jbuilder similarity index 100% rename from app/views/api/v0/followings/users.json.jbuilder rename to app/views/followings/users.json.jbuilder diff --git a/config/routes.rb b/config/routes.rb index fa51af04f..444b0c288 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -133,12 +133,6 @@ Rails.application.routes.draw do get :users get :organizations end - namespace :followings do - get :users - get :tags - get :organizations - get :podcasts - end resources :webhooks, only: %i[index create show destroy] resources :classified_listings, path: :listings, only: %i[index show create update] @@ -219,6 +213,12 @@ Rails.application.routes.draw do resources :user_blocks, param: :blocked_id, only: %i[show create destroy] resources :podcasts, only: %i[new create] resolve("ProMembership") { [:pro_membership] } # see https://guides.rubyonrails.org/routing.html#using-resolve + namespace :followings, defaults: { format: :json } do + get :users + get :tags + get :organizations + get :podcasts + end get "/search/tags" => "search#tags" get "/search/chat_channels" => "search#chat_channels" diff --git a/spec/requests/api/v0/followings_spec.rb b/spec/requests/followings_spec.rb similarity index 87% rename from spec/requests/api/v0/followings_spec.rb rename to spec/requests/followings_spec.rb index 1f13e9165..74baddba1 100644 --- a/spec/requests/api/v0/followings_spec.rb +++ b/spec/requests/followings_spec.rb @@ -1,9 +1,9 @@ require "rails_helper" -RSpec.describe "Api::V0::FollowingsController", type: :request do +RSpec.describe "FollowingsController", type: :request do let(:user) { create(:user) } - describe "GET /api/followings/users" do + describe "GET /followings/users" do let(:followed) { create(:user) } before do @@ -14,7 +14,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do context "when user is unauthorized" do it "returns unauthorized" do - get api_followings_users_path + get followings_users_path expect(response).to have_http_status(:unauthorized) end @@ -26,7 +26,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do end it "returns user's followings list with the correct format" do - get api_followings_users_path + get followings_users_path expect(response).to have_http_status(:ok) response_following = response.parsed_body.first @@ -40,7 +40,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do end end - describe "GET /api/followings/tags" do + describe "GET /followings/tags" do let(:followed) { create(:tag) } before do @@ -51,7 +51,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do context "when user is unauthorized" do it "returns unauthorized" do - get api_followings_tags_path + get followings_tags_path expect(response).to have_http_status(:unauthorized) end @@ -63,7 +63,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do end it "returns user's followings list with the correct format" do - get api_followings_tags_path + get followings_tags_path expect(response).to have_http_status(:ok) follow = user.follows.last @@ -79,7 +79,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do end end - describe "GET /api/followings/organizations" do + describe "GET /followings/organizations" do let(:followed) { create(:organization) } before do @@ -90,7 +90,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do context "when user is unauthorized" do it "returns unauthorized" do - get api_followings_organizations_path + get followings_organizations_path expect(response).to have_http_status(:unauthorized) end @@ -102,7 +102,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do end it "returns user's followings list with the correct format" do - get api_followings_organizations_path + get followings_organizations_path expect(response).to have_http_status(:ok) response_following = response.parsed_body.first @@ -116,7 +116,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do end end - describe "GET /api/followings/podcasts" do + describe "GET /followings/podcasts" do let(:followed) { create(:podcast) } before do @@ -127,7 +127,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do context "when user is unauthorized" do it "returns unauthorized" do - get api_followings_podcasts_path + get followings_podcasts_path expect(response).to have_http_status(:unauthorized) end @@ -139,7 +139,7 @@ RSpec.describe "Api::V0::FollowingsController", type: :request do end it "returns user's followings list with the correct format" do - get api_followings_podcasts_path + get followings_podcasts_path expect(response).to have_http_status(:ok) response_following = response.parsed_body.first