1
0

inline_renderer.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # frozen_string_literal: true
  2. class InlineRenderer
  3. def initialize(object, current_account, template)
  4. @object = object
  5. @current_account = current_account
  6. @template = template
  7. end
  8. def render
  9. case @template
  10. when :status
  11. serializer = REST::StatusSerializer
  12. preload_associations_for_status
  13. when :notification
  14. serializer = REST::NotificationSerializer
  15. when :conversation
  16. serializer = REST::ConversationSerializer
  17. when :announcement
  18. serializer = REST::AnnouncementSerializer
  19. when :reaction
  20. serializer = REST::ReactionSerializer
  21. else
  22. return
  23. end
  24. serializable_resource = ActiveModelSerializers::SerializableResource.new(@object, serializer: serializer, scope: current_user, scope_name: :current_user)
  25. serializable_resource.as_json
  26. end
  27. def self.render(object, current_account, template)
  28. new(object, current_account, template).render
  29. end
  30. private
  31. def preload_associations_for_status
  32. ActiveRecord::Associations::Preloader.new(records: [@object], associations: {
  33. active_mentions: :account,
  34. reblog: {
  35. active_mentions: :account,
  36. },
  37. }).call
  38. end
  39. def current_user
  40. @current_account&.user
  41. end
  42. end