per-instance json serialization methods/excludes

refs CNVS-7597

allows streamlining some previously overridden to_json methods

test-plan: N/A

Change-Id: I401500d4fef301c8cdae455d4f71a130e2764108
Reviewed-on: https://gerrit.instructure.com/23645
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
Product-Review: Jacob Fugal <jacob@instructure.com>
QA-Review: Jacob Fugal <jacob@instructure.com>
This commit is contained in:
Jacob Fugal 2013-08-21 12:42:20 -06:00
parent 997e53df43
commit 34f5a5c1f1
6 changed files with 31 additions and 27 deletions

View File

@ -171,6 +171,10 @@ class RubricAssessment < ActiveRecord::Base
def methods_for_serialization(*methods)
@serialization_methods = methods
end
def serialization_methods
@serialization_methods || []
end
def assessor_name
self.assessor.name rescue t('unknown_user', "Unknown User")

View File

@ -1004,22 +1004,21 @@ class Submission < ActiveRecord::Base
end
end
# This little chunk makes it so that to_json will force it to always include the method :attachments
# it is especially needed in the extended gradebook so that when we grab all of the versions through simply_versioned
# that each of those versions include :attachments
alias_method :ar_to_json, :to_json
def to_json(options = {}, &block)
if simply_versioned_version_model
options[:methods] ||= []
options[:methods] = Array(options[:methods])
options[:methods] << :versioned_attachments
options[:methods].uniq!
end
self.ar_to_json(options, &block)
# default_options = { :methods => [ :attachments ]}
# options[:methods] = [options[:methods]] if options[:methods] && !options[:methods].is_a?(Array)
# default_options[:methods] += options[:methods] if options[:methods]
# self.ar_to_json(options.merge(default_options), &block)
# include the versioned_attachments in as_json if this was loaded from a
# specific version
def serialization_methods
!@without_versioned_attachments && simply_versioned_version_model ?
[:versioned_attachments] :
[]
end
# mechanism to turn off the above behavior for the duration of a
# block
def without_versioned_attachments
original, @without_versioned_attachments = @without_versioned_attachments, true
yield
ensure
@exclude_versioned_attachments = original
end
def self.json_serialization_full_parameters(additional_parameters={})

View File

@ -208,13 +208,8 @@ class SubmissionComment < ActiveRecord::Base
!hidden? && submission.possible_participants_ids.include?(author_id)
end
alias_method :ar_to_json, :to_json
def to_json(options = {}, &block)
if self.context.root_account.service_enabled?(:avatars)
options[:methods] ||= []
options[:methods] << :avatar_path
end
self.ar_to_json(options, &block)
def serialization_methods
context.root_account.service_enabled?(:avatars) ? [:avatar_path] : []
end
scope :visible, where(:hidden => false)

View File

@ -238,12 +238,13 @@ class ActiveRecord::Base
except = options.delete(:except) || []
except = Array(except)
except.concat(self.class.serialization_excludes) if self.class.respond_to?(:serialization_excludes)
except.concat(@serialization_excludes) if @serialization_excludes
except.concat(self.serialization_excludes) if self.respond_to?(:serialization_excludes)
except.uniq!
methods = options.delete(:methods) || []
methods = Array(methods)
methods.concat(self.class.serialization_methods) if self.class.respond_to?(:serialization_methods)
methods.concat(@serialization_methods) if @serialization_methods
methods.concat(self.serialization_methods) if self.respond_to?(:serialization_methods)
methods.uniq!
options[:except] = except unless except.empty?

View File

@ -30,7 +30,10 @@ module Api::V1
student = opts[:student] || submission.user
current_grader = submission.grader || default_grader
json = submission_attempt_json(version.model, assignment, api_context.user, api_context.session, nil, course).with_indifferent_access
model = version.model
json = model.without_versioned_attachments do
submission_attempt_json(model, assignment, api_context.user, api_context.session, nil, course).with_indifferent_access
end
grader = (json[:grader_id] && json[:grader_id] > 0 && user_cache[json[:grader_id]]) || default_grader
json = json.merge(

View File

@ -31,7 +31,9 @@ module Api::V1::Submission
if includes.include?("submission_history")
hash['submission_history'] = []
submission.submission_history.each_with_index do |ver, idx|
hash['submission_history'] << submission_attempt_json(ver, assignment, user, session, idx, context)
ver.without_versioned_attachments do
hash['submission_history'] << submission_attempt_json(ver, assignment, user, session, idx, context)
end
end
end