Class: SupplejackApi::UserSetSerializer

Inherits:
ActiveModel::Serializer
  • Object
show all
Defined in:
app/serializers/supplejack_api/user_set_serializer.rb

Instance Method Summary collapse

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_hashObject



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

  options.reverse_merge!(items: true)

  include!(:record, node: hash)

  if options[:items]
    hash[:description] = object.description
    hash[:privacy] = object.privacy
    hash[:tags] = object.tags
    hash[:records] = records
  elsif options[:featured]
    hash[:records] = records(1)
  else
    hash[:records] = simple_records
  end

  hash[:user] = user if options[:user]

  hash
end

#simple_recordsObject

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

#userObject

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 = options[:user]
  if admin && admin.respond_to?(:admin?) && admin.try(:admin?)
    hash[:api_key] = object.user.try(:api_key)
  end

  hash
end