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
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 41 def add_link(name, association, ) return if association.nil? @hash[:links][name] = case association.class when String, Integer association else association.to_param end # unless options[:embed] == :ids # plural_name = name.to_s.pluralize.to_sym # @hash[:linked][plural_name] ||= [] # @hash[:linked][plural_name].push association.attributes(options) # end end |
#add_links(name, association, options) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 24 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 # unless options[:embed] == :ids # @hash[:linked][name] ||= [] # @hash[:linked][name] += association.map { |item| item.attributes(options) } # end end |
#serializable_hash(options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# 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] ||= {} # unless options[:embed] == :ids # @hash[:linked] ||= {} # end if association.respond_to?(:each) add_links(name, association, ) else add_link(name, association, ) end end @hash end |
#to_json(options = {}) ⇒ Object
59 60 61 |
# File 'lib/jsonapi/consumer/resource/serializer_concern.rb', line 59 def to_json(={}) serializable_hash().to_json end |