Skip to content

Commit

Permalink
rubocop linting #681
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Nov 22, 2020
1 parent a544073 commit 1e83869
Show file tree
Hide file tree
Showing 426 changed files with 19,219 additions and 8,352 deletions.
840 changes: 229 additions & 611 deletions .rubocop.yml

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ group :development, :test do
gem "binding_of_caller"
gem "bullet", "~> 6.1"
gem "byebug", platforms: %i[mri mingw x64_mingw]
gem "prettier", "~> 0.13.0"
gem "rspec-benchmark", "~> 0.4.0"
gem "rspec-graphql_matchers", "~> 1.1"
gem "rspec-rails", "~> 3.8", ">= 3.8.2"
gem "rubocop", "~> 0.77.0"
gem "rubocop", "~> 1.3", ">= 1.3.1"
gem "rubocop-performance", "~> 1.5", ">= 1.5.1"
gem "rubocop-rails", "~> 2.4"
gem "rubocop-rails", "~> 2.8", ">= 2.8.1"
gem "rubocop-packaging", "~> 0.5.1"
gem "rubocop-rspec", "~> 2.0", require: false
end

group :development do
Expand Down
50 changes: 30 additions & 20 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ GEM
audited (4.9.0)
activerecord (>= 4.2, < 6.1)
aws-eventstream (1.1.0)
aws-partitions (1.393.0)
aws-sdk-core (3.109.2)
aws-partitions (1.397.0)
aws-sdk-core (3.109.3)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.239.0)
aws-sigv4 (~> 1.1)
jmespath (~> 1.0)
aws-sdk-kms (1.39.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.84.1)
aws-sdk-s3 (1.85.0)
aws-sdk-core (~> 3, >= 3.109.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.1)
Expand Down Expand Up @@ -272,11 +272,10 @@ GEM
i18n_data (0.8.0)
iso-639 (0.3.5)
iso8601 (0.9.1)
jaro_winkler (1.5.4)
jmespath (1.4.0)
json (2.3.1)
json-canonicalization (0.2.0)
json-ld (3.1.4)
json-ld (3.1.5)
htmlentities (~> 4.3)
json-canonicalization (~> 0.2)
link_header (~> 0.0, >= 0.0.8)
Expand Down Expand Up @@ -382,7 +381,6 @@ GEM
addressable
css_parser (>= 1.6.0)
htmlentities (>= 4.0.0)
prettier (0.13.0)
promise.rb (0.7.4)
public_suffix (2.0.5)
pwqgen.rb (0.1.0)
Expand Down Expand Up @@ -428,7 +426,7 @@ GEM
rb-inotify (0.10.1)
ffi (~> 1.0)
rchardet (1.8.0)
rdf (3.1.6)
rdf (3.1.7)
hamster (~> 3.0)
link_header (~> 0.0, >= 0.0.8)
rdf-aggregate-repo (3.1.0)
Expand Down Expand Up @@ -459,6 +457,7 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 4.0)
netrc (~> 0.8)
rexml (3.2.4)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
Expand Down Expand Up @@ -486,19 +485,29 @@ GEM
rspec-mocks (~> 3.9.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.4)
rubocop (0.77.0)
jaro_winkler (~> 1.5.1)
rubocop (1.3.1)
parallel (~> 1.10)
parser (>= 2.6)
parser (>= 2.7.1.5)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8)
rexml
rubocop-ast (>= 1.1.1)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 1.7)
rubocop-performance (1.6.1)
rubocop (>= 0.71.0)
rubocop-rails (2.5.2)
activesupport
unicode-display_width (>= 1.4.0, < 2.0)
rubocop-ast (1.1.1)
parser (>= 2.7.1.5)
rubocop-packaging (0.5.1)
rubocop (>= 0.89, < 2.0)
rubocop-performance (1.9.0)
rubocop (>= 0.90.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.8.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 0.72.0)
rubocop (>= 0.87.0)
rubocop-rspec (2.0.0)
rubocop (~> 1.0)
rubocop-ast (>= 1.1.0)
ruby-enum (0.8.0)
i18n
ruby-progressbar (1.10.1)
Expand Down Expand Up @@ -574,7 +583,7 @@ GEM
unf (0.1.4)
unf_ext
unf_ext (0.0.7.7)
unicode-display_width (1.6.1)
unicode-display_width (1.7.0)
unicode_utils (1.4.0)
uniform_notifier (1.13.0)
uuid (2.3.9)
Expand Down Expand Up @@ -662,7 +671,6 @@ DEPENDENCIES
oj (>= 2.8.3)
oj_mimic_json (~> 1.0, >= 1.0.1)
premailer (~> 1.11, >= 1.11.1)
prettier (~> 0.13.0)
pwqgen.rb (~> 0.1.0)
rack-cors (~> 1.0)
rack-utf8_sanitizer (~> 1.6)
Expand All @@ -671,9 +679,11 @@ DEPENDENCIES
rspec-benchmark (~> 0.4.0)
rspec-graphql_matchers (~> 1.1)
rspec-rails (~> 3.8, >= 3.8.2)
rubocop (~> 0.77.0)
rubocop (~> 1.3, >= 1.3.1)
rubocop-packaging (~> 0.5.1)
rubocop-performance (~> 1.5, >= 1.5.1)
rubocop-rails (~> 2.4)
rubocop-rails (~> 2.8, >= 2.8.1)
rubocop-rspec (~> 2.0)
seedbank
sentry-raven (~> 2.9)
shoryuken (~> 4.0)
Expand Down
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

Expand Down
123 changes: 81 additions & 42 deletions app/controllers/activities_controller.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
# frozen_string_literal: true

class ActivitiesController < ApplicationController
include Countable

before_action :set_activity, only: [:show]
before_action :set_activity, only: %i[show]

def index
sort = case params[:sort]
when "relevance" then { "_score" => { order: "desc" } }
when "created" then { created: { order: "asc" } }
when "-created" then { created: { order: "desc" } }
else { created: { order: "desc" } }
end
sort =
case params[:sort]
when "relevance"
{ "_score" => { order: "desc" } }
when "created"
{ created: { order: "asc" } }
when "-created"
{ created: { order: "desc" } }
else
{ created: { order: "desc" } }
end

page = page_from_params(params)

if params[:id].present?
response = Activity.find_by(id: params[:id])
response = if params[:id].present?
Activity.find_by(id: params[:id])
elsif params[:ids].present?
response = Activity.find_by_id(params[:ids], page: page, sort: sort)
Activity.find_by_id(params[:ids], page: page, sort: sort)
else
response = Activity.query(params[:query],
uid: params[:datacite_doi_id] || params[:provider_id] || params[:client_id] || params[:repository_id],
page: page,
sort: sort,
scroll_id: params[:scroll_id])
Activity.query(
params[:query],
uid:
params[:datacite_doi_id] || params[:provider_id] ||
params[:client_id] ||
params[:repository_id],
page: page,
sort: sort,
scroll_id: params[:scroll_id],
)
end

begin
Expand All @@ -31,58 +43,85 @@ def index
total = response.total
else
total = response.results.total
total_for_pages = page[:cursor].nil? ? total.to_f : [total.to_f, 10000].min
total_for_pages =
page[:cursor].nil? ? total.to_f : [total.to_f, 10_000].min
total_pages = page[:size] > 0 ? (total_for_pages / page[:size]).ceil : 0
end

if page[:scroll].present?
options = {}
options[:meta] = {
total: total,
"scroll-id" => response.scroll_id,
total: total, "scroll-id" => response.scroll_id
}.compact
options[:links] = {
self: request.original_url,
next: results.size < page[:size] || page[:size] == 0 ? nil : request.base_url + "/activities?" + {
"scroll-id" => response.scroll_id,
"page[scroll]" => page[:scroll],
"page[size]" => page[:size],
}.compact.to_query,
next:
if results.size < page[:size] || page[:size] == 0
nil
else
request.base_url + "/activities?" +
{
"scroll-id" => response.scroll_id,
"page[scroll]" => page[:scroll],
"page[size]" => page[:size],
}.compact.
to_query
end,
}.compact
options[:is_collection] = true

render json: ActivitySerializer.new(results, options).serialized_json, status: :ok
render json: ActivitySerializer.new(results, options).serialized_json,
status: :ok
else
results = response.results

options = {}
options[:meta] = {
total: total,
"totalPages" => total_pages,
page: page[:cursor].nil? && page[:number].present? ? page[:number] : nil,
page:
page[:cursor].nil? && page[:number].present? ? page[:number] : nil,
}.compact

options[:links] = {
self: request.original_url,
next: response.results.size < page[:size] ? nil : request.base_url + "/activities?" + {
query: params[:query],
"page[cursor]" => page[:cursor] ? make_cursor(results) : nil,
"page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil,
"page[size]" => page[:size],
sort: params[:sort],
}.compact.to_query,
next:
if response.results.size < page[:size]
nil
else
request.base_url + "/activities?" +
{
query: params[:query],
"page[cursor]" => page[:cursor] ? make_cursor(results) : nil,
"page[number]" =>
if page[:cursor].nil? && page[:number].present?
page[:number] + 1
end,
"page[size]" => page[:size],
sort: params[:sort],
}.compact.
to_query
end,
}.compact
options[:include] = @include
options[:is_collection] = true

render json: ActivitySerializer.new(results, options).serialized_json, status: :ok
render json: ActivitySerializer.new(results, options).serialized_json,
status: :ok
end
rescue Elasticsearch::Transport::Transport::Errors::BadRequest => e
Raven.capture_exception(e)

message = JSON.parse(e.message[6..-1]).to_h.dig("error", "root_cause", 0, "reason")
message =
JSON.parse(e.message[6..-1]).to_h.dig(
"error",
"root_cause",
0,
"reason",
)

render json: { "errors" => { "title" => message } }.to_json, status: :bad_request
render json: { "errors" => { "title" => message } }.to_json,
status: :bad_request
end
end

Expand All @@ -91,14 +130,14 @@ def show
options[:include] = @include
options[:is_collection] = false

render json: ActivitySerializer.new(@activity, options).serialized_json, status: :ok
render json: ActivitySerializer.new(@activity, options).serialized_json,
status: :ok
end

protected

def set_activity
response = Activity.find_by(id: params[:id])
@activity = response.results.first
fail ActiveRecord::RecordNotFound if @activity.blank?
end
def set_activity
response = Activity.find_by(id: params[:id])
@activity = response.results.first
fail ActiveRecord::RecordNotFound if @activity.blank?
end
end
Loading

0 comments on commit 1e83869

Please sign in to comment.