Class: SupplejackApi::UserSetSerializer
- Inherits:
-
ActiveModel::Serializer
- Object
- ActiveModel::Serializer
- SupplejackApi::UserSetSerializer
- Defined in:
- app/serializers/supplejack_api/user_set_serializer.rb
Instance Method Summary collapse
-
#records(amount = nil) ⇒ Object
Returns a array of Hashes with the information from each record included in the Hash.
- #serializable_hash ⇒ Object
-
#simple_records ⇒ Object
Returns a array of Hashes with only the record_id and position.
-
#user ⇒ Object
Return the user information about the set, this is only displyed on the set show endpoint.
Instance Method Details
#records(amount = nil) ⇒ Object
Returns a array of Hashes with the information from each record included in the Hash
The values to be added from the record are stored in SetItem::ATTRIBUTES
44 45 46 47 48 49 |
# File 'app/serializers/supplejack_api/user_set_serializer.rb', line 44 def records(amount = nil) attributes = [:record_id, :position] + SetItem::ATTRIBUTES object.items_with_records(amount).map do |item| Hash[attributes.map { |attr| [attr, item.send(attr)] }] end end |
#serializable_hash ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/serializers/supplejack_api/user_set_serializer.rb', line 15 def serializable_hash hash = { id: object.id.to_s } hash.merge! attributes .reverse_merge!(items: true) include!(:record, node: hash) if [:items] hash[:description] = object.description hash[:privacy] = object.privacy hash[:tags] = object. hash[:records] = records elsif [:featured] hash[:records] = records(1) else hash[:records] = simple_records end hash[:user] = user if [:user] hash end |
#simple_records ⇒ Object
Returns a array of Hashes with only the record_id and position
53 54 55 56 57 |
# File 'app/serializers/supplejack_api/user_set_serializer.rb', line 53 def simple_records object.set_items.map do |item| { record_id: item.record_id, position: item.position } end end |
#user ⇒ Object
Return the user information about the set, this is only displyed on the set show endpoint.
When the user requesting the sets is a admin, also return the API Key for the owner of the set. This is required in order for applications to make requests on the user’s behalf.
66 67 68 69 70 71 72 73 74 75 |
# File 'app/serializers/supplejack_api/user_set_serializer.rb', line 66 def user hash = { name: object.user.try(:name) } admin = [:user] if admin && admin.respond_to?(:admin?) && admin.try(:admin?) hash[:api_key] = object.user.try(:api_key) end hash end |