Skip to content
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

Brands API endpoint #1387

Merged
merged 1 commit into from
Jan 24, 2023
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
2 changes: 1 addition & 1 deletion app/assets/javascripts/collected_inks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$(function () {
$("#collected_ink_brand_name").autocomplete({
source: function (request, response) {
fetch("/brands.json?term=" + encodeURIComponent(request.term))
fetch("/api/v1/brands?term=" + encodeURIComponent(request.term))
.then(function (r) {
return r.json();
})
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Api::V1::BaseController < ApplicationController
before_action :require_json
before_action :authenticate_user!

private

def require_json
respond_to :json
end
end
15 changes: 15 additions & 0 deletions app/controllers/api/v1/brands_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Api::V1::BrandsController < Api::V1::BaseController
def index
render json: serializer.serializable_hash.to_json
end

private

def clusters
BrandCluster.autocomplete_search(params[:term]).order(:name)
end

def serializer
BrandClusterSerializer.new(clusters)
end
end
9 changes: 1 addition & 8 deletions app/controllers/brands_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@ class BrandsController < ApplicationController
add_breadcrumb "Inks", "/brands"

def index
respond_to do |format|
format.json do
clusters = BrandCluster.autocomplete_search(params[:term])
serializer = BrandClusterSerializer.new(clusters)
render json: serializer.serializable_hash.to_json
end
format.html { @brands = BrandCluster.public.order(:name) }
end
@brands = BrandCluster.public.order(:name)
end

def show
Expand Down
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@
get "my_missing"
end
end

namespace :api do
namespace :v1 do
resources :brands, only: [:index]
end
end

namespace :admins do
resource :dashboard, only: [:show]
resources :users, only: %i[index show update] do
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
config.filter_rails_from_backtrace!
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :request
config.include ApiHelpers, type: :request

config.before(:each) { Sidekiq::Worker.clear_all }
end
32 changes: 32 additions & 0 deletions spec/requests/api/v1/brands_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
require "rails_helper"

RSpec.describe "Api::V1::Brands", type: :request do
describe "GET /index" do
it "requires authentication" do
get "/api/v1/brands", headers: { "ACCEPT" => "application/json" }
expect(response).to have_http_status(:unauthorized)
end

context "when signed in" do
let(:user) { create(:user) }
before(:each) { sign_in(user) }

it "returns all brands in alphabetical order" do
create(:brand_cluster, name: "Diamine")
create(:brand_cluster, name: "Robert Oster")
create(:brand_cluster, name: "Abraxas")

get "/api/v1/brands", headers: { "ACCEPT" => "application/json" }
expect(json).to match(
{
data: [
hash_including(attributes: { name: "Abraxas" }),
hash_including(attributes: { name: "Diamine" }),
hash_including(attributes: { name: "Robert Oster" })
]
}
)
end
end
end
end
5 changes: 5 additions & 0 deletions spec/support/api_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ApiHelpers
def json
JSON.parse(response.body, symbolize_names: true)
end
end