Skip to content

Tags: refactor + specs #605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/assets/javascripts/application/tags.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
26 changes: 10 additions & 16 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
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

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
12 changes: 5 additions & 7 deletions app/views/tags/alpha_grouped_index.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1>
<%= t '.maintitle' %>
<%= form_tag '/tags/alpha_grouped_index', method: :get do %>
<%= form_tag alpha_grouped_index_tags_path, method: :get do %>
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active switch_offer-js">
<%= radio_button_tag 'switch_offer', 'offer', true %>
Expand All @@ -13,10 +13,8 @@
</div>
<% end %>
</h1>
<div class="tag_management">
<div class="alpha_tag_list col-xs-12 col-md-12">
<%= render 'grouped_index',
alpha_tags: @alpha_tags,
post_type: params[:post_type] || 'offer' %>
</div>
<div class="alpha_tag_list col-xs-12 col-md-12">
<%= render 'grouped_index',
alpha_tags: @alpha_tags,
post_type: params[:post_type] || 'offer' %>
</div>
4 changes: 1 addition & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
45 changes: 36 additions & 9 deletions spec/controllers/tags_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions spec/support/controller_macros.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module ControllerMacros

def login(user = nil)
@request.env["devise.mapping"] = Devise.mappings[:user]

Expand All @@ -8,5 +7,4 @@ def login(user = nil)

sign_in @current_user
end

end
end