Post serializer

This commit is contained in:
Kira 2019-11-26 15:37:27 -08:00
parent d1750f938a
commit 0c69db869c
5 changed files with 89 additions and 22 deletions

View File

@ -7,6 +7,7 @@ gem "pg"
gem "dalli", :platforms => :ruby
gem "simple_form"
gem "mechanize"
gem 'active_model_serializers', '~> 0.10.0'
gem "whenever", :require => false
gem "sanitize"
gem 'ruby-vips'
@ -33,7 +34,6 @@ gem 'addressable'
gem 'httparty'
gem 'rakismet'
gem 'recaptcha', require: "recaptcha/rails"
gem 'activemodel-serializers-xml'
gem 'ptools'
gem 'jquery-rails'
gem 'webpacker', '>= 4.0.x'

View File

@ -165,18 +165,6 @@ class ApplicationRecord < ActiveRecord::Base
super(options)
end
def to_xml(options = {}, &block)
options ||= {}
options[:except] ||= []
options[:except] += hidden_attributes
options[:methods] ||= []
options[:methods] += method_attributes
super(options, &block)
end
def serializable_hash(*args)
hash = super(*args)
hash.transform_keys { |key| key.delete("?") }

View File

@ -1601,16 +1601,16 @@ class Post < ApplicationRecord
list
end
def associated_attributes
[:pixiv_ugoira_frame_data]
end
# def associated_attributes
# [:pixiv_ugoira_frame_data]
# end
def as_json(options = {})
options ||= {}
options[:include] ||= []
options[:include] += associated_attributes
super(options)
end
# def as_json(options = {})
# options ||= {}
# options[:include] ||= []
# options[:include] += associated_attributes
# super(options)
# end
def minimal_attributes
preview_dims = preview_dimensions

View File

@ -0,0 +1,78 @@
class PostSerializer < ActiveModel::Serializer
def tags
tags = {}
TagCategory.categories.each do |category|
tags[category] = object.typed_tags(category)
end
tags
end
def file
file_attributes = {
width: object.image_width,
height: object.image_height,
ext: object.file_ext,
size: object.file_size,
md5: object.md5,
url: nil
}
if object.visible?
file_attributes[:url] = object.file_url
end
file_attributes
end
def preview
dims = object.preview_dimensions
preview_attributes = {
width: dims[1],
height: dims[0],
url: nil
}
if object.visible?
preview_attributes[:url] = object.preview_file_url
end
preview_attributes
end
def score
{
up: object.up_score,
down: object.down_score,
total: object.score
}
end
def flags
{
pending: object.is_pending,
flagged: object.is_flagged,
note_locked: object.is_note_locked,
status_locked: object.is_status_locked,
rating_locked: object.is_rating_locked,
deleted: object.is_deleted
}
end
def sources
object.source.split("\n")
end
def pools
object.pool_ids
end
def relationships
{
parent_id: object.parent_id,
has_children: object.has_children,
has_active_children: object.has_active_children
}
end
def locked_tags
object.locked_tags&.split(' ') || []
end
attributes :id, :created_at, :updated_at, :file, :preview, :score, :tags, :locked_tags, :change_seq, :flags, :rating, :fav_count, :sources, :pools, :approver_id, :uploader_id, :description, :comment_count
end

View File

@ -0,0 +1 @@
ActiveModelSerializers.config.adapter = :json