DEV: Use core method to preload category custom fields for site serializer
This commit is contained in:
parent
8beaa6c1d9
commit
8b8512e73e
26
plugin.rb
26
plugin.rb
|
@ -324,31 +324,6 @@ SQL
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require_dependency 'basic_category_serializer'
|
|
||||||
class ::BasicCategorySerializer
|
|
||||||
attributes :custom_fields
|
|
||||||
|
|
||||||
def custom_fields
|
|
||||||
object.custom_fields
|
|
||||||
end
|
|
||||||
|
|
||||||
def include_custom_fields?
|
|
||||||
object.custom_field_preloaded?("enable_accepted_answers") && SiteSetting.show_filter_by_solved_status && custom_fields.present?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
require_dependency 'site'
|
|
||||||
class ::Site
|
|
||||||
alias_method :discourse_categories, :categories
|
|
||||||
|
|
||||||
def categories
|
|
||||||
@categories ||= begin
|
|
||||||
Category.preload_custom_fields(discourse_categories, ["enable_accepted_answers"]) if SiteSetting.show_filter_by_solved_status
|
|
||||||
discourse_categories
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
require_dependency 'topic_view_serializer'
|
require_dependency 'topic_view_serializer'
|
||||||
class ::TopicViewSerializer
|
class ::TopicViewSerializer
|
||||||
attributes :accepted_answer
|
attributes :accepted_answer
|
||||||
|
@ -529,6 +504,7 @@ SQL
|
||||||
end
|
end
|
||||||
|
|
||||||
TopicList.preloaded_custom_fields << "accepted_answer_post_id" if TopicList.respond_to? :preloaded_custom_fields
|
TopicList.preloaded_custom_fields << "accepted_answer_post_id" if TopicList.respond_to? :preloaded_custom_fields
|
||||||
|
Site.preloaded_category_custom_fields << "enable_accepted_answers" if Site.respond_to? :preloaded_category_custom_fields
|
||||||
|
|
||||||
if CategoryList.respond_to?(:preloaded_topic_custom_fields)
|
if CategoryList.respond_to?(:preloaded_topic_custom_fields)
|
||||||
CategoryList.preloaded_topic_custom_fields << "accepted_answer_post_id"
|
CategoryList.preloaded_topic_custom_fields << "accepted_answer_post_id"
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
require 'rails_helper'
|
|
||||||
|
|
||||||
describe BasicCategorySerializer do
|
|
||||||
let(:category) { Fabricate(:category) }
|
|
||||||
let(:guardian) { Guardian.new }
|
|
||||||
|
|
||||||
before do
|
|
||||||
SiteSetting.show_filter_by_solved_status = true
|
|
||||||
category.custom_fields["enable_accepted_answers"] = true
|
|
||||||
category.save_custom_fields
|
|
||||||
end
|
|
||||||
|
|
||||||
it "should include custom fields only if its preloaded" do
|
|
||||||
json = described_class.new(category, scope: guardian, root: false).as_json
|
|
||||||
expect(json.to_s).not_to include("custom_fields")
|
|
||||||
|
|
||||||
category.expects(:custom_field_preloaded?).returns(true)
|
|
||||||
json = described_class.new(category, scope: guardian, root: false).as_json
|
|
||||||
expect(json.to_s).to include("custom_fields")
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in New Issue