Skip to content

Commit

Permalink
Merge branch 'master' into master-legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
thatandromeda committed May 20, 2019
2 parents 8f5c5ff + 9304c75 commit 04b8309
Show file tree
Hide file tree
Showing 25 changed files with 68 additions and 23 deletions.
7 changes: 5 additions & 2 deletions app/controllers/notices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ def new
def create
respond_to do |format|
format.json do
return unless authorized_to_create?
unless authorized_to_create?
self.status = :unauthorized
self.response_body = { documentation_link: Rails.configuration.x.api_documentation_link }.to_json
return
end
create_respond_json
end

Expand Down Expand Up @@ -176,7 +180,6 @@ def build_works(notice)

def authorized_to_create?
if cannot?(:submit, Notice)
head :unauthorized
false
else
true
Expand Down
23 changes: 13 additions & 10 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,11 @@ def json_renderer
)
end

# Find the instance in the database corresponding to the given elasticsearch
# result and enrich it with search-related metadata for display. Return the
# enriched instance (or nil, if no Notice was found).
def augment_instance(result)
instance = self.class::SEARCHED_MODEL.where(id: result._source[:id])
# Enrich the activerecord object with search-related metadata for display.
# Return the enriched instance (or nil, if none was found).
def augment_instance(instance)
return unless instance.present?

instance = instance.first
result = @ids_to_results[instance.id]

class << instance
attr_accessor :_score, :highlight
Expand All @@ -72,9 +69,15 @@ def sort_by(sort_by_param)
end

def wrap_instances
@searchdata.results
.map { |r| augment_instance(r) }
.compact
@ids_to_results = @searchdata.results.map { |r| [r._source[:id], r] }.to_h
instances = self.class::SEARCHED_MODEL.where(id: @ids_to_results.keys)
# Because the instances are in db order, we'll need to re-sort them to
# preserve the elasticsearch order. This is preferable to iterating through
# the elasticsearch results and fetching one db item each time, however;
# we used to do that and all those roundtrips were costing us in
# performance.
instances.map { |r| augment_instance(r) }
.sort_by { |a| @ids_to_results.keys.index(a[:id]) }
end

# Elasticsearch cannot return more than 20_000 results in production (2000
Expand Down
2 changes: 2 additions & 0 deletions app/models/counternotice.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Counternotice < Notice
define_elasticsearch_mapping

Expand Down
2 changes: 2 additions & 0 deletions app/models/court_order.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class CourtOrder < Notice
DEFAULT_ENTITY_NOTICE_ROLES = (BASE_ENTITY_NOTICE_ROLES |
%w[recipient sender principal issuing_court
Expand Down
2 changes: 2 additions & 0 deletions app/models/data_protection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class DataProtection < Notice
DEFAULT_ENTITY_NOTICE_ROLES = (BASE_ENTITY_NOTICE_ROLES |
%w[recipient]).freeze
Expand Down
2 changes: 2 additions & 0 deletions app/models/date_range_filter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class DateRangeFilter

attr_reader :title, :parameter
Expand Down
4 changes: 3 additions & 1 deletion app/models/defamation.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class Defamation < Notice
MASK = 'REDACTED'.freeze
MASK = 'REDACTED'
REDACTION_REGEX = /google/i

define_elasticsearch_mapping(works: [:description])
Expand Down
2 changes: 2 additions & 0 deletions app/models/dmca.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class DMCA < Notice
define_elasticsearch_mapping

Expand Down
2 changes: 2 additions & 0 deletions app/models/entity.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'validates_automatically'
require 'hierarchical_relationships'
require 'entity_index_queuer'
Expand Down
4 changes: 2 additions & 2 deletions app/models/entity_notice_role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class EntityNoticeRole < ActiveRecord::Base
include ValidatesAutomatically

validates :name, length: { maximum: 255 }

belongs_to :entity
belongs_to :notice, touch: true

Expand All @@ -21,7 +21,7 @@ class EntityNoticeRole < ActiveRecord::Base
issuing_court
plaintiff
defendant
)
).freeze

accepts_nested_attributes_for :entity

Expand Down
2 changes: 2 additions & 0 deletions app/models/file_upload.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'validates_automatically'

class FileUpload < ActiveRecord::Base
Expand Down
2 changes: 2 additions & 0 deletions app/models/government_request.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class GovernmentRequest < Notice
DEFAULT_ENTITY_NOTICE_ROLES = (BASE_ENTITY_NOTICE_ROLES |
%w[recipient sender principal submitter]).freeze
Expand Down
2 changes: 2 additions & 0 deletions app/models/law_enforcement_request.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class LawEnforcementRequest < Notice
DEFAULT_ENTITY_NOTICE_ROLES = (BASE_ENTITY_NOTICE_ROLES |
%w[recipient sender principal]).freeze
Expand Down
2 changes: 2 additions & 0 deletions app/models/notice.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'language'
require 'recent_scope'
require 'validates_automatically'
Expand Down
4 changes: 3 additions & 1 deletion app/models/other.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class Other < Notice
MASK = 'REDACTED'.freeze
MASK = 'REDACTED'
REDACTION_REGEX = /google/i

define_elasticsearch_mapping(works: [:description])
Expand Down
2 changes: 2 additions & 0 deletions app/models/private_information.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class PrivateInformation < Notice
define_elasticsearch_mapping

Expand Down
2 changes: 2 additions & 0 deletions app/models/searchability.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Searchability
def self.included(base)
base.extend ClassMethods
Expand Down
4 changes: 3 additions & 1 deletion app/models/searches_models.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class SearchesModels
attr_accessor :sort_by
attr_reader :instances, :page
Expand Down Expand Up @@ -175,7 +177,7 @@ def search_config
end

def search_definition
@search_definition ||= {}
@search_definition ||= {'_source': ['score', 'id', 'title']}
end

def setup_aggregations
Expand Down
2 changes: 2 additions & 0 deletions app/models/term_filter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class TermFilter

attr_reader :title, :parameter
Expand Down
2 changes: 2 additions & 0 deletions app/models/term_search.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class TermSearch

attr_reader :parameter, :title, :field
Expand Down
2 changes: 2 additions & 0 deletions app/models/trademark.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Trademark < Notice
define_elasticsearch_mapping

Expand Down
2 changes: 2 additions & 0 deletions app/models/work.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'validates_automatically'

class Work < ActiveRecord::Base
Expand Down
7 changes: 3 additions & 4 deletions app/views/shared/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Topics</a>
<div id="dropdown-topics" class="dropdown-menu" role="menu">
<ol>
<% topic_roots.each do |root| %>
<% cache(root) do %>
<%= render "shared/topic_dropdown", topic: root %>
<% end %>
<% cache('dropdown-topics') do %>
<%= render partial: 'shared/topic_dropdown', collection: topic_roots,
as: :topic %>
<% end %>
</ol>
</div>
Expand Down
3 changes: 2 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class Application < Rails::Application
timestamp = datetime.strftime '%Y-%m-%d %H:%M:%S (%Z)'
"#{timestamp} #{severity}: #{progname} #{msg}\n"
end

# Configuration settings
config.x.api_documentation_link = "https://github.com/berkmancenter/lumendatabase/wiki/Lumen-API-Documentation"
# Mailer settings
config.default_sender = ENV['DEFAULT_SENDER'] || 'no-reply@example.com'
config.return_path = ENV['RETURN_PATH'] || 'user@example.com'
Expand Down
3 changes: 2 additions & 1 deletion spec/controllers/notices_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,11 @@ def stub_find_notice(notice = nil)
it 'returns unauthorized if one cannot submit' do
stub_submit_notice
@ability.cannot(:submit, Notice)

response_body = { documentation_link: Rails.configuration.x.api_documentation_link }.to_json
post_create :json

expect(response.status).to eq 401
expect(response.body).to eq response_body
end

it 'returns a proper Location header when saved successfully' do
Expand Down

0 comments on commit 04b8309

Please sign in to comment.