From 1a47601bc3fd6d1bb9c732a5b673f41fb976977f Mon Sep 17 00:00:00 2001 From: Marc Anguera Insa Date: Tue, 16 Mar 2021 23:57:05 +0100 Subject: [PATCH] Tags: refactor to make it DRY + specs --- app/assets/javascripts/application/tags.js | 8 ++-- app/controllers/tags_controller.rb | 26 +++++------- app/views/tags/alpha_grouped_index.html.erb | 12 +++--- config/routes.rb | 4 +- spec/controllers/tags_controller_spec.rb | 45 ++++++++++++++++----- spec/support/controller_macros.rb | 4 +- 6 files changed, 57 insertions(+), 42 deletions(-) diff --git a/app/assets/javascripts/application/tags.js b/app/assets/javascripts/application/tags.js index 3fc98952f..d7341a03d 100644 --- a/app/assets/javascripts/application/tags.js +++ b/app/assets/javascripts/application/tags.js @@ -1,15 +1,15 @@ $(function() { $(".switch_offer-js").on("click", function() { - loadTags('/tags/offers'); + loadTags('offer'); }); $(".switch_inquiry-js").on("click", function() { - loadTags('/tags/inquiries'); + loadTags('inquiry'); }); - function loadTags(url){ + function loadTags(type){ $.get({ - url: url, + url: `/tags/alpha_grouped_index.js?post_type=${type}`, dataType: 'html', error: function(jqXHR, textStatus, errorThrown) { $('.alpha_tag_list').html('AJAX Error: ' + textStatus); diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index aa7ddf02b..f44583d78 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -1,7 +1,7 @@ class TagsController < ApplicationController def index - @posts = Post.by_organization(current_organization) - @all_tags = @posts.find_like_tag(params[:term]) + posts = Post.by_organization(current_organization) + @all_tags = posts.find_like_tag(params[:term]) render json: @all_tags end @@ -9,25 +9,19 @@ def index def alpha_grouped_index redirect_to users_path && return unless current_organization - @alpha_tags = case params[:post_type] || "offer" + post_type = params[:post_type] || "offer" + @alpha_tags = case post_type when "offer" then Offer when "inquiry" then Inquiry end.by_organization(current_organization). active.of_active_members. alphabetical_grouped_tags - end - - def inquiries - @alpha_tags = Inquiry.by_organization(current_organization).active.of_active_members. - alphabetical_grouped_tags - - render partial: "grouped_index", locals: { alpha_tags: @alpha_tags, post_type: "inquiries" } - end - - def offers - @alpha_tags = Offer.by_organization(current_organization).active.of_active_members. - alphabetical_grouped_tags - render partial: "grouped_index", locals: { alpha_tags: @alpha_tags, post_type: "offers" } + respond_to do |format| + format.html + format.js do + render partial: "grouped_index", locals: { alpha_tags: @alpha_tags, post_type: post_type } + end + end end end diff --git a/app/views/tags/alpha_grouped_index.html.erb b/app/views/tags/alpha_grouped_index.html.erb index a9dc41df7..8c7cf58e7 100644 --- a/app/views/tags/alpha_grouped_index.html.erb +++ b/app/views/tags/alpha_grouped_index.html.erb @@ -1,6 +1,6 @@

<%= t '.maintitle' %> - <%= form_tag '/tags/alpha_grouped_index', method: :get do %> + <%= form_tag alpha_grouped_index_tags_path, method: :get do %>
<% end %>

-
-
- <%= render 'grouped_index', - alpha_tags: @alpha_tags, - post_type: params[:post_type] || 'offer' %> -
+
+ <%= render 'grouped_index', + alpha_tags: @alpha_tags, + post_type: params[:post_type] || 'offer' %>
diff --git a/config/routes.rb b/config/routes.rb index ff9aaf87c..c49eb00d9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -85,9 +85,7 @@ resources :tags, only: [:index] do collection do - get "alpha_grouped_index" - get "inquiries" - get "offers" + get :alpha_grouped_index end end diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index 18463048e..0b5701d94 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -1,31 +1,58 @@ RSpec.describe TagsController do let (:tags) { %w(foo bar baz) } + let (:more_tags) { %w(ruby rails js) } let (:organization) { Fabricate(:organization) } let (:member) { Fabricate(:member, organization: organization) } - let! (:post) { Fabricate(:offer, - user: member.user, - organization: organization, - tags: tags) } + let! (:offer) { Fabricate(:offer, user: member.user, organization: organization, tags: tags) } + let! (:inquiry) { Fabricate(:inquiry, user: member.user, organization: organization, tags: more_tags) } - describe "GET 'index'" do + describe "GET index" do before(:each) do login(member.user) end it "returns http success" do - get 'index' + get :index expect(response).to have_http_status(:ok) expect(response.content_type).to match("application/json") end it "with no search term, returns all tags" do - get 'index' - expect(assigns(:all_tags)).to eq(tags) + get :index + expect(assigns(:all_tags)).to eq(more_tags + tags) end it "with search term, returns filtered tags" do - get 'index', params: { term: "foo" } + get :index, params: { term: "foo" } expect(assigns(:all_tags)).to eq(["foo"]) end end + + describe "GET alpha_grouped_index" do + before { session[:current_organization_id] = organization.id } + + it "load offers tags by default if no type is passed" do + get :alpha_grouped_index + + expect(assigns(:alpha_tags)).to eq({ + "B" => [["bar", 1], ["baz", 1]], + "F" => [["foo", 1]] + }) + end + + it "load tags by type" do + get :alpha_grouped_index, params: { post_type: "inquiry" } + + expect(assigns(:alpha_tags)).to eq({ + "J" => [["js", 1]], + "R" => [["rails", 1], ["ruby", 1]] + }) + end + + it "renders a partial with format js" do + get :alpha_grouped_index, xhr: true + + expect(response).to render_template(partial: "_grouped_index") + end + end end diff --git a/spec/support/controller_macros.rb b/spec/support/controller_macros.rb index bdef8a696..b1104fdd7 100644 --- a/spec/support/controller_macros.rb +++ b/spec/support/controller_macros.rb @@ -1,5 +1,4 @@ module ControllerMacros - def login(user = nil) @request.env["devise.mapping"] = Devise.mappings[:user] @@ -8,5 +7,4 @@ def login(user = nil) sign_in @current_user end - -end \ No newline at end of file +end