Skip to content

Commit

Permalink
Start using jsonapi-serializer gem instead of active_model_serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-hank committed Feb 9, 2022
1 parent 12bd083 commit d106230
Show file tree
Hide file tree
Showing 28 changed files with 143 additions and 137 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
source 'https://rubygems.org'

# Pinned due to development difficulties with AMS 0.9/0.10.
gem 'active_model_serializers', '~> 0.8.3'
gem 'activerecord-import'
gem 'acts-as-taggable-on'
gem 'addressable'
Expand All @@ -23,6 +21,7 @@ gem 'flutie'
gem 'jquery-placeholder-rails'
gem 'jquery-rails'
gem 'jquery-ui-rails'
gem 'jsonapi-serializer'
gem 'kaminari'
gem 'lograge'
gem 'mime-types'
Expand Down
6 changes: 3 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ GEM
active_link_to (1.0.5)
actionpack
addressable
active_model_serializers (0.8.4)
activemodel (>= 3.0)
activejob (6.1.4.4)
activesupport (= 6.1.4.4)
globalid (>= 0.3.6)
Expand Down Expand Up @@ -256,6 +254,8 @@ GEM
jquery-ui-rails (6.0.1)
railties (>= 3.2.16)
json (2.6.1)
jsonapi-serializer (2.2.0)
activesupport (>= 4.2)
kaminari (1.2.2)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.2.2)
Expand Down Expand Up @@ -533,7 +533,6 @@ PLATFORMS
ruby

DEPENDENCIES
active_model_serializers (~> 0.8.3)
activerecord-import
acts-as-taggable-on
addressable
Expand Down Expand Up @@ -562,6 +561,7 @@ DEPENDENCIES
jquery-placeholder-rails
jquery-rails
jquery-ui-rails
jsonapi-serializer
kaminari
lograge
memory_profiler
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/notices_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def create
Rails.logger.warn "Could not create notice with params: #{params}"
flash.alert = 'Notice creation failed. See errors below.'
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @notice.errors, status: :unprocessable_entity }
format.json { render json: { notices: @notice.errors }, status: :unprocessable_entity }
end
end
end
Expand All @@ -68,9 +68,7 @@ def show
respond_to do |format|
format.html { show_render_html }
format.json do
render json: @notice,
serializer: NoticeSerializerProxy,
root: json_root_for(@notice.class)
render json: { json_root_for(@notice.class) => NoticeSerializerProxy.new(@notice) }
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ def json_renderer
# subclass overrides to these constants, rather than pulling in the
# original definition.
render(
json: @wrapped_instances,
each_serializer: self.class::EACH_SERIALIZER,
serializer: ActiveModel::ArraySerializer,
root: self.class::URL_ROOT,
meta: meta_hash_for(@searchdata)
json: {
self.class::URL_ROOT => @wrapped_instances.map { |instance| self.class::EACH_SERIALIZER.new(instance) },
meta: meta_hash_for(@searchdata)
}
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class TopicsController < ApplicationController
def index
render json: Topic.all
render json: { topics: TopicSerializer.new(Topic.all) }
end

def show
Expand Down
4 changes: 2 additions & 2 deletions app/models/notice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ def language_enum
Language.all.inject( {} ) { |memo, l| memo[l.label] = l.code; memo }
end

def active_model_serializer
def model_serializer
if rescinded?
RescindedNoticeSerializer
else
super
"#{self.class.name}Serializer".safe_constantize
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/notice_serializer_proxy.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class NoticeSerializerProxy < SimpleDelegator
def initialize(instance, *args)
serializer = instance.active_model_serializer || NoticeSerializer
serializer = instance.model_serializer || NoticeSerializer
serialized = serializer.new(instance, *args)

super(serialized)
Expand Down
7 changes: 7 additions & 0 deletions app/serializers/base_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class BaseSerializer
include JSONAPI::Serializer

def as_json(options={})
serializable_hash[:data]
end
end
14 changes: 5 additions & 9 deletions app/serializers/court_order_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
class CourtOrderSerializer < NoticeSerializer
attribute :laws_referenced

def laws_referenced
attribute :laws_referenced do |object|
object.laws_referenced.map(&:name)
end

private
attributes_to_serialize.delete(:body)
attribute :explanation, &:body

def attributes
attributes = super
swap_keys(attributes, :body, :explanation)
attributes[:works].each do |work|
attribute :works do |object|
works(object).each do |work|
swap_keys(work, 'description', 'subject_of_court_order')
swap_keys(work, 'infringing_urls', 'targetted_urls')
work.delete('copyrighted_urls')
end
attributes
end
end
10 changes: 5 additions & 5 deletions app/serializers/data_protection_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class DataProtectionSerializer < NoticeSerializer
def attributes
attributes = super
swap_keys(attributes, :body, :legal_complaint)
attributes[:works].each do |work|
attributes_to_serialize.delete(:body)
attribute :legal_complaint, &:body

attribute :works do |object|
works(object).each do |work|
swap_keys(work, 'infringing_urls', 'urls_mentioned_in_request')
end
attributes
end
end
10 changes: 5 additions & 5 deletions app/serializers/defamation_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class DefamationSerializer < NoticeSerializer
def attributes
attributes = super
swap_keys(attributes, :body, :legal_complaint)
attributes[:works].each do |work|
attributes_to_serialize.delete(:body)
attribute :legal_complaint, &:body

attribute :works do |object|
works(object).each do |work|
swap_keys(work, 'infringing_urls', 'defamatory_urls')
work.delete('copyrighted_urls')
end
attributes
end
end
4 changes: 2 additions & 2 deletions app/serializers/entity_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class EntitySerializer < ActiveModel::Serializer
attributes :id, :parent_id, :name, :country_code, :url
class EntitySerializer < BaseSerializer
attributes :id, :parent_id, :name, :country_code, :url
end
12 changes: 5 additions & 7 deletions app/serializers/law_enforcement_request_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
class LawEnforcementRequestSerializer < NoticeSerializer
attributes :regulations, :request_type

def regulations
attribute :regulations do |object|
object.regulation_list.map(&:name)
end

private
attributes_to_serialize.delete(:body)
attribute :explanation, &:body

def attributes
attributes = super
swap_keys(attributes, :body, :explanation)
attributes[:works].each do |work|
attribute :works do |object|
works(object).each do |work|
swap_keys(work, 'description', 'subject_of_enforcement_request')
swap_keys(work, 'copyrighted_urls', 'original_work_urls')
swap_keys(work, 'infringing_urls', 'urls_in_request')
end
attributes
end
end
24 changes: 10 additions & 14 deletions app/serializers/limited_notice_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class LimitedNoticeSerializer < ActiveModel::Serializer
class LimitedNoticeSerializer < BaseSerializer
attributes :id, :type, :title, :body, :date_sent, :date_received,
:topics, :sender_name, :principal_name, :recipient_name, :works,
:tags, :jurisdictions, :action_taken, :language

# TODO: serialize additional entities

def topics
attribute :topics do |object|
object.topics.map(&:name)
end

def works
attribute :works do |object|
object.works.as_json(
only: [:description],
include: {
Expand All @@ -19,25 +19,21 @@ def works
)
end

def tags
attribute :tags do |object|
object.tag_list
end

def jurisdictions
attribute :jurisdictions do |object|
object.jurisdiction_list
end

private

def attributes
attributes = super || []

attributes[:score] = object._score if object.respond_to?(:_score)

attributes
attribute :score, if: proc { |record|
record.respond_to?(:_score)
} do |object|
object._score
end

def swap_keys(hash, original_key, new_key)
def self.swap_keys(hash, original_key, new_key)
original_value = hash.delete(original_key)
hash[new_key] = original_value
end
Expand Down
66 changes: 31 additions & 35 deletions app/serializers/notice_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class NoticeSerializer < ActiveModel::Serializer
class NoticeSerializer < BaseSerializer
attributes :id, :type, :title, :body, :date_sent, :date_received,
:topics, :sender_name, :principal_name, :recipient_name, :works,
:tags, :jurisdictions, :action_taken, :language
Expand All @@ -8,7 +8,7 @@ class NoticeSerializer < ActiveModel::Serializer

# TODO: serialize additional entities

def topics
attribute :topics do |object|
object.topics.map(&:name)
end

Expand All @@ -20,8 +20,35 @@ def topics
# accessible as a hash (rather than as an object to be serialized) so we can
# use hash operations on it within the hooks supported by
# active-model-serializer.
def works
if defined?(current_user) && current_user && Ability.new(scope).can?(:view_full_version_api, object)
attribute :works do |object|
works(object)
end

attribute :tags, &:tag_list

attribute :jurisdictions, &:jurisdiction_list

attribute :score, if: proc { |record|
record.respond_to?(:_score)
} do |object|
object._score
end

def self.swap_keys(hash, original_key, new_key)
original_value = hash.delete(original_key)
hash[new_key] = original_value
end

def self.cleaned_works(base_works)
base_works.each do |work|
work['infringing_urls'] = FALLBACK if work['infringing_urls'].empty?
work['copyrighted_urls'] = FALLBACK if work['copyrighted_urls'].empty?
end
base_works
end

def self.works(object)
if defined?(Current.user) && Current.user && Ability.new(Current.user).can?(:view_full_version_api, object)
base_works = object.works.as_json(
only: [:description],
include: {
Expand All @@ -40,35 +67,4 @@ def works
end
cleaned_works(base_works)
end

def cleaned_works(base_works)
base_works.each do |work|
work['infringing_urls'] = FALLBACK if work['infringing_urls'].empty?
work['copyrighted_urls'] = FALLBACK if work['copyrighted_urls'].empty?
end
base_works
end

def tags
object.tag_list
end

def jurisdictions
object.jurisdiction_list
end

private

def attributes
attributes = super || []

attributes[:score] = object._score if object.respond_to?(:_score)

attributes
end

def swap_keys(hash, original_key, new_key)
original_value = hash.delete(original_key)
hash[new_key] = original_value
end
end
10 changes: 4 additions & 6 deletions app/serializers/other_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class OtherSerializer < NoticeSerializer
private
attributes_to_serialize.delete(:body)
attribute :explanation, &:body

def attributes
attributes = super
swap_keys(attributes, :body, :explanation)
attributes[:works].each do |work|
attribute :works do |object|
works(object).each do |work|
swap_keys(work, 'description', 'complaint')
swap_keys(work, 'copyrighted_urls', 'original_work_urls')
swap_keys(work, 'infringing_urls', 'problematic_urls')
end
attributes
end
end
10 changes: 4 additions & 6 deletions app/serializers/private_information_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
class PrivateInformationSerializer < NoticeSerializer
private
attributes_to_serialize.delete(:body)
attribute :explanation, &:body

def attributes
attributes = super
swap_keys(attributes, :body, :explanation)
attributes[:works].each do |work|
attribute :works do |object|
works(object).each do |work|
swap_keys(work, 'description', 'complaint')
swap_keys(work, 'infringing_urls', 'urls_with_private_information')
end
attributes
end
end
Loading

0 comments on commit d106230

Please sign in to comment.