|
| 1 | +module Canvas |
| 2 | + class APISerializer < ActiveModel::Serializer |
| 3 | + extend Forwardable |
| 4 | + include Canvas::APISerialization |
| 5 | + |
| 6 | + attr_reader :controller, :session |
| 7 | + alias_method :user, :scope |
| 8 | + alias_method :current_user, :user |
| 9 | + def_delegators :@controller, :stringify_json_ids?, :polymorphic_url, |
| 10 | + :accepts_jsonapi?, :session, :context |
| 11 | + |
| 12 | + # See ActiveModel::Serializer's documentation for options. |
| 13 | + # |
| 14 | + # object - thing to serialize, e.g. quiz, assignment |
| 15 | + # options - see AMS documentation, however, you must pass a :controller |
| 16 | + # key with a controller. |
| 17 | + # |
| 18 | + # methods available on your instance: |
| 19 | + # |
| 20 | + # session - controller's session |
| 21 | + # controller - controller |
| 22 | + # accepts_jsonapi? - if jsonapi header is present |
| 23 | + # polymorphic_url - for good ole' rails URL helpers |
| 24 | + # context - if your controller has a @context, it'll be that. Otherwise, |
| 25 | + # nil. |
| 26 | + # stringify_json_ids? - whether the stringify_json_ids? header is present |
| 27 | + # user - whatever you passed as options[:scope] |
| 28 | + # current_user - alias for user |
| 29 | + def initialize(object, options={}) |
| 30 | + super(object, options) |
| 31 | + @controller = options[:controller] |
| 32 | + unless controller |
| 33 | + raise ArgumentError.new("You must pass a controller to APISerializer!") |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + # Overriding to allow for "links" hash. |
| 38 | + # You should probably NOT override this method in your own serializer. |
| 39 | + # This will be going away once ActiveModel::Serializer has support for |
| 40 | + # the "links" style. |
| 41 | + def associations |
| 42 | + associations = self.class._associations |
| 43 | + included_associations = filter(associations.keys) |
| 44 | + associations.each_with_object({}) do |(name, association), hash| |
| 45 | + if included_associations.include? name |
| 46 | + if association.embed_ids? |
| 47 | + hash['links'] ||= {} |
| 48 | + hash['links'][association.name] = serialize_ids association |
| 49 | + elsif association.embed_objects? |
| 50 | + hash['links'] ||= {} |
| 51 | + hash['links'][association.embedded_key] = serialize association |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + # Overriding *ONLY* to add our own stringify logic in here. |
| 58 | + # You should not override as_json in your own subclass. |
| 59 | + # Override `ActiveModel::Serializer`s serializable_object to stringify_ids |
| 60 | + # and ids in relationships if necessary. |
| 61 | + # |
| 62 | + # You can override when to stringify by implementing the "stringify_ids?" |
| 63 | + # method. |
| 64 | + def as_json(options={}) |
| 65 | + root = options[:root] |
| 66 | + hash = super(options) |
| 67 | + response = root ? (hash[root] || hash) : hash |
| 68 | + response = response[self.root] || response |
| 69 | + stringify!(response) |
| 70 | + hash |
| 71 | + end |
| 72 | + # Creates a method alias for the "object" method based on the name of your |
| 73 | + # serializer. For example, if your class is `QuizSerializer`, you will |
| 74 | + # have a method named "quiz" available to your class, so you don't have to |
| 75 | + # use object if you don't want to. |
| 76 | + def self.inherited(klass) |
| 77 | + super(klass) |
| 78 | + resource_name = klass.name.underscore.downcase.split('_serializer').first |
| 79 | + klass.send(:alias_method, resource_name.to_sym, :object) |
| 80 | + end |
| 81 | + |
| 82 | + private |
| 83 | + |
| 84 | + # Overwrite AMS's serialize_id's function until it has support |
| 85 | + # for serializing a url for "links". |
| 86 | + # You can opt into this behavior by using `embed: :ids` |
| 87 | + # |
| 88 | + # ```ruby |
| 89 | + # has_one :assignment_group, embed: :ids |
| 90 | + # ``` |
| 91 | + # |
| 92 | + # Will give you a response like: |
| 93 | + # |
| 94 | + # ```json |
| 95 | + # { |
| 96 | + # "quizzes": [ |
| 97 | + # { |
| 98 | + # "id": 1, |
| 99 | + # "links": { |
| 100 | + # "assignment_group": "http://canvas.example.com/api/v1/path/to/assignment_group" |
| 101 | + # } |
| 102 | + # } |
| 103 | + # ] |
| 104 | + # } |
| 105 | + # ``` |
| 106 | + # |
| 107 | + # Note that using `embed_in_root: true` will default to serializing ids, e.g.: |
| 108 | + # |
| 109 | + # ```ruby |
| 110 | + # has_one :assignment_group, embed: ids, embed_in_root: true |
| 111 | + # ``` |
| 112 | + # |
| 113 | + # ```json |
| 114 | + # { |
| 115 | + # "quizzes": [ |
| 116 | + # { |
| 117 | + # "id": "1", |
| 118 | + # "links": { |
| 119 | + # "assignment_group": "1" |
| 120 | + # } |
| 121 | + # } |
| 122 | + # ], |
| 123 | + # "assignment_groups": [ |
| 124 | + # { |
| 125 | + # "id": "1" |
| 126 | + # } |
| 127 | + # ] |
| 128 | + # } |
| 129 | + # ``` |
| 130 | + def serialize_ids(association) |
| 131 | + if association.embed_ids? && !association.embed_in_root |
| 132 | + name = association.name |
| 133 | + send("#{name}_url".to_sym) if send(name).present? |
| 134 | + else |
| 135 | + super |
| 136 | + end |
| 137 | + end |
| 138 | + end |
| 139 | +end |
0 commit comments