-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
339 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# frozen_string_literal: true | ||
|
||
module Api | ||
module V1 | ||
module Import | ||
class BooksController < Api::ApiController | ||
# before_action :find_book | ||
# before_action :check_case | ||
|
||
# rubocop:disable Metrics/MethodLength | ||
# rubocop:disable Metrics/AbcSize | ||
# rubocop:disable Metrics/CyclomaticComplexity | ||
# rubocop:disable Metrics/PerceivedComplexity | ||
# rubocop:disable Layout/LineLength | ||
def create | ||
team_id = params.require(:team_id) | ||
params_to_use = book_params.to_h.deep_symbolize_keys | ||
|
||
@book = Book.new | ||
|
||
@book.team = Team.find(team_id) | ||
|
||
scorer_name = params_to_use[:scorer][:name] | ||
unless Scorer.exists?(name: scorer_name) | ||
@book.errors.add(:base, "Scorer with name '#{scorer_name}' needs to be migrated over first.") | ||
end | ||
|
||
selection_strategy_name = params_to_use[:selection_strategy][:name] | ||
unless SelectionStrategy.exists?(name: selection_strategy_name) | ||
@book.errors.add(:selection_strategy, | ||
"Selection strategy with name '#{selection_strategy_name}' needs to be migrated over first.") | ||
end | ||
|
||
if params_to_use[:query_doc_pairs] | ||
list_of_emails_of_users = [] | ||
params_to_use[:query_doc_pairs].each do |query_doc_pair| | ||
next unless query_doc_pair[:judgements] | ||
|
||
query_doc_pair[:judgements].each do |judgement| | ||
list_of_emails_of_users << judgement[:user_email] | ||
end | ||
end | ||
list_of_emails_of_users.uniq! | ||
list_of_emails_of_users.each do |email| | ||
unless User.exists?(email: email) | ||
@book.errors.add(:base, "User with email '#{email}' needs to be migrated over first.") | ||
end | ||
end | ||
end | ||
|
||
unless @book.errors.empty? | ||
render json: @book.errors, status: :bad_request | ||
return | ||
end | ||
|
||
# passed first set of validations. | ||
@book.name = params_to_use[:name] | ||
@book.show_rank = params_to_use[:show_rank] | ||
@book.support_implicit_judgements = params_to_use[:support_implicit_judgements] | ||
|
||
@book.scorer = Scorer.find_by(name: scorer_name) | ||
@book.selection_strategy = SelectionStrategy.find_by(name: selection_strategy_name) | ||
|
||
params_to_use[:query_doc_pairs]&.each do |query_doc_pair| | ||
qdp = @book.query_doc_pairs.build(query_doc_pair.except(:judgements)) | ||
next unless query_doc_pair[:judgements] | ||
|
||
query_doc_pair[:judgements].each do |judgement| | ||
judgement[:user] = User.find_by(email: judgement[:user_email]) | ||
qdp.judgements.build(judgement.except(:user_email)) | ||
end | ||
end | ||
|
||
if @book.save | ||
respond_with @book | ||
else | ||
render json: @book.errors, status: :bad_request | ||
end | ||
end | ||
# rubocop:enable Metrics/MethodLength | ||
# rubocop:enable Metrics/AbcSize | ||
# rubocop:enable Metrics/CyclomaticComplexity | ||
# rubocop:enable Metrics/PerceivedComplexity | ||
# rubocop:enable Layout/LineLength | ||
|
||
private | ||
|
||
def book_params | ||
params.require(:book).permit! | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
export = @export ||= false | ||
|
||
json.name book.name | ||
json.book_id book.id unless export | ||
json.show_rank book.show_rank | ||
json.support_implicit_judgements book.support_implicit_judgements | ||
|
||
if export | ||
if book.scorer.present? | ||
json.scorer do | ||
json.partial! 'api/v1/scorers/scorer', scorer: book.scorer, export: export | ||
end | ||
end | ||
|
||
if book.selection_strategy.present? | ||
json.selection_strategy do | ||
json.partial! 'selection_strategy', selection_strategy: book.selection_strategy | ||
end | ||
end | ||
end | ||
|
||
json.query_doc_pairs do | ||
json.array! book.query_doc_pairs, | ||
partial: 'api/v1/books/query_doc_pairs', as: :query_doc_pair, | ||
export: export | ||
end | ||
json.book_id book.id |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! 'book', book: @book, export: false | ||
json.partial! 'book', book: @book |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! 'book', book: @book, export: false | ||
json.partial! 'book', book: @book |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! 'book', book: @book, export: false | ||
json.partial! 'book', book: @book |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
json.name book.name | ||
json.show_rank book.show_rank | ||
json.support_implicit_judgements book.support_implicit_judgements | ||
|
||
if book.scorer.present? | ||
json.scorer do | ||
json.partial! 'api/v1/scorers/scorer', scorer: book.scorer, export: true | ||
end | ||
end | ||
|
||
if book.selection_strategy.present? | ||
json.selection_strategy do | ||
json.partial! 'selection_strategy', selection_strategy: book.selection_strategy | ||
end | ||
end | ||
|
||
json.query_doc_pairs do | ||
json.array! book.query_doc_pairs, | ||
partial: 'query_doc_pair', as: :query_doc_pair | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
json.rating judgement.rating | ||
json.unrateable judgement.unrateable | ||
json.user_email judgement.user.email if judgement.user |
10 changes: 10 additions & 0 deletions
10
app/views/api/v1/export/books/_query_doc_pair.json.jbuilder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
json.query_text query_doc_pair.query_text | ||
json.doc_id query_doc_pair.doc_id | ||
json.position query_doc_pair.position | ||
json.document_fields query_doc_pair.document_fields | ||
|
||
json.judgements do | ||
json.array! query_doc_pair.judgements, partial: 'judgements', as: :judgement | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
json.partial! 'book', book: @book |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
require 'csv' | ||
module Api | ||
module V1 | ||
module Export | ||
class BooksControllerTest < ActionController::TestCase | ||
let(:doug) { users(:doug) } | ||
|
||
before do | ||
@controller = Api::V1::Export::BooksController.new | ||
|
||
login_user doug | ||
end | ||
|
||
describe 'Exporting a book in json' do | ||
let(:book) { books(:james_bond_movies) } | ||
let(:doug) { users(:doug) } | ||
|
||
test 'the AR object ids are replaced with names' do | ||
get :show, params: { book_id: book.id } | ||
assert_response :ok | ||
body = response.parsed_body | ||
require 'json' | ||
puts JSON.pretty_generate(body) | ||
assert_nil body['book_id'] | ||
assert_not_nil body['name'] | ||
|
||
assert_nil body['scorer_id'] | ||
assert_not_nil body['scorer'] | ||
assert_nil body['scorer']['scorer_id'] | ||
assert_empty body['scorer']['teams'] | ||
|
||
assert_nil body['selection_strategy_id'] | ||
assert_not_nil body['selection_strategy'] | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.