Class: ActiveModel::Serializer::Associations::Config
- Inherits:
-
Object
- Object
- ActiveModel::Serializer::Associations::Config
- Defined in:
- lib/active_model/serializer/associations.rb
Overview
:nodoc:
Class Method Summary collapse
Instance Method Summary collapse
- #associated_object ⇒ Object
- #embed_ids? ⇒ Boolean
- #embed_in_root? ⇒ Boolean
- #embed_objects? ⇒ Boolean
- #embeddable? ⇒ Boolean
-
#initialize(name, source, options = {}) ⇒ Config
constructor
A new instance of Config.
- #key ⇒ Object
- #name ⇒ Object
- #option(key, default = nil) ⇒ Object
- #root ⇒ Object
- #source_serializer ⇒ Object
- #target_serializer ⇒ Object
Constructor Details
#initialize(name, source, options = {}) ⇒ Config
Returns a new instance of Config.
33 34 35 36 37 |
# File 'lib/active_model/serializer/associations.rb', line 33 def initialize(name, source, ={}) @name = name @source = source @options = end |
Class Method Details
.refine(name, class_options) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/active_model/serializer/associations.rb', line 7 def self.refine(name, ) current_class = self Class.new(self) do singleton_class.class_eval do define_method(:to_s) do "(subclass of #{current_class.name})" end alias inspect to_s end self. = # cache the root so we can reuse it without falling back on a per-instance basis begin self.[:root] ||= self.new(name, nil).root rescue # this could fail if it needs a valid source, for example a polymorphic association end end end |
Instance Method Details
#associated_object ⇒ Object
70 71 72 |
# File 'lib/active_model/serializer/associations.rb', line 70 def associated_object option(:value) || source_serializer.send(name) end |
#embed_ids? ⇒ Boolean
74 75 76 |
# File 'lib/active_model/serializer/associations.rb', line 74 def [:id, :ids].include? option(:embed, source_serializer.) end |
#embed_in_root? ⇒ Boolean
82 83 84 |
# File 'lib/active_model/serializer/associations.rb', line 82 def option(:include, source_serializer.) end |
#embed_objects? ⇒ Boolean
78 79 80 |
# File 'lib/active_model/serializer/associations.rb', line 78 def [:object, :objects].include? option(:embed, source_serializer.) end |
#embeddable? ⇒ Boolean
86 87 88 |
# File 'lib/active_model/serializer/associations.rb', line 86 def !associated_object.nil? end |
#key ⇒ Object
58 59 60 |
# File 'lib/active_model/serializer/associations.rb', line 58 def key option(:key) || @name end |
#name ⇒ Object
66 67 68 |
# File 'lib/active_model/serializer/associations.rb', line 66 def name option(:name) || @name end |
#option(key, default = nil) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/active_model/serializer/associations.rb', line 39 def option(key, default=nil) if @options.key?(key) @options[key] elsif self.class..key?(key) self.class.[key] else default end end |
#root ⇒ Object
62 63 64 |
# File 'lib/active_model/serializer/associations.rb', line 62 def root option(:root) || @name end |
#source_serializer ⇒ Object
54 55 56 |
# File 'lib/active_model/serializer/associations.rb', line 54 def source_serializer @source end |
#target_serializer ⇒ Object
49 50 51 52 |
# File 'lib/active_model/serializer/associations.rb', line 49 def target_serializer serializer = option(:serializer) serializer.is_a?(String) ? serializer.constantize : serializer end |