Skip to content
Open
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/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ def object_invalid_or_unsaved?(object)
def object_for_request
ctl_name = controller_name.singularize
var = instance_variable_get("@#{ctl_name}")
ctl_name.include?('isa') ? var.send(ctl_name.sub('isa_', '')) : var
(["isa_assay", "isa_study"].any? { |isa| ctl_name.include?(isa) }) ? var.send(ctl_name.sub('isa_', '')) : var
end

def expire_activity_fragment_cache(controller, action)
Expand Down
39 changes: 39 additions & 0 deletions app/controllers/isa_tags_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class ISATagsController < ApplicationController
respond_to :json
api_actions :show, :index
before_action :ensure_json_request
before_action :isa_json_compliance_enabled?
before_action :login_required

def index
respond_to do |format|
format.json {
render json: ISATag.all,
each_serializer: ISATagSerializer,
meta: {
base_url: Seek::Config.site_base_host,
api_version: ActiveModel::Serializer.config.api_version
}
}
end
end

def show
respond_to do |format|
format.json { render json: ISATag.find(params[:id]) }
end
end


private

def ensure_json_request
# Accept header or URL format must request JSON
return if request.format.json?

render json: {
error: "Not Acceptable",
message: "This endpoint only serves application/json."
}, status: :not_acceptable # 406
end
end
5 changes: 5 additions & 0 deletions app/serializers/isa_tag_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ISATagSerializer < ActiveModel::Serializer
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this needs to be based off BaseSerializer or SimpleBaseSerializer to get the jsonapi and meta blocks

attributes :title
end
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -851,6 +851,10 @@
end
end

### ISA Tags ###

resources :isa_tags, only: [:index, :show]

### MISC MATCHES ###
get '/search/' => 'search#index', as: :search
get '/search/save' => 'search#save', as: :save_search
Expand Down
39 changes: 39 additions & 0 deletions test/functional/isa_tags_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true
require 'test_helper'

class ISATagsControllerTest < ActionController::TestCase
include AuthenticatedTestHelper
fixtures :isa_tags

def setup
@authenticated_user = FactoryBot.create(:person)
Seek::Config.isa_json_compliance_enabled = true
end

def teardown
Seek::Config.isa_json_compliance_enabled = false
end

test 'should return ISA tags if logged in' do
login_as @authenticated_user

get :index, as: :json
assert_response :success
response_body = JSON.parse(response.body)
assert_equal response_body["data"].count, 10
end

test 'should not return ISA tags if not logged in' do
get :index, as: :json
assert_response :unauthorized
assert_equal response.body, "HTTP Basic: Access denied.\n"
end

test 'should not respond to anything else than json requests' do
get :index, as: :xml
assert_response :not_acceptable
response_body = JSON.parse(response.body)
assert_equal response_body["error"], "Not Acceptable"
end

end
Loading