Module: JSONAPI::Consumer::Resource::SerializerConcern
- Extended by:
- ActiveSupport::Concern
- Included in:
- JSONAPI::Consumer::Resource
- Defined in:
- lib/jsonapi/consumer/resource/serializer_concern.rb
Instance Method Summary collapse
- #add_link(name, association, options) ⇒ Object
- #add_links(name, association, options) ⇒ Object
- #serializable_hash(options = {}) ⇒ Object
- #to_json(options = {}) ⇒ Object
Instance Method Details
#add_link(name, association, options) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 34 def add_link(name, association, ) return if association.nil? @hash[:links][name] = case association.class when String, Integer association else association.to_param end end |
#add_links(name, association, options) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 22 def add_links(name, association, ) @hash[:links][name] ||= [] @hash[:links][name] += (association || []).map do |obj| case obj.class when String, Integer obj else obj.to_param end end end |
#serializable_hash(options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 5 def serializable_hash(={}) @hash = persisted? ? attributes : attributes.except(self.class.primary_key) self.each_association do |name, association, | @hash[:links] ||= {} if association.respond_to?(:each) or _association_type(name) == :has_many add_links(name, association, ) else add_link(name, association, ) end @hash.delete(:links) if remove_links? end @hash end |
#to_json(options = {}) ⇒ Object
45 46 47 |
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 45 def to_json(={}) serializable_hash().to_json end |