mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-25 07:16:49 +00:00
Initialize things in the initialize method and define readers
This commit is contained in:
parent
ea6d712cc8
commit
eb5b27de69
@ -4,28 +4,25 @@ module ActiveModel
|
|||||||
class Base #:nodoc:
|
class Base #:nodoc:
|
||||||
def initialize(name, options={}, serializer_options={})
|
def initialize(name, options={}, serializer_options={})
|
||||||
@name = name
|
@name = name
|
||||||
|
@object = options[:value]
|
||||||
|
|
||||||
|
@embed = options[:embed]
|
||||||
|
@embed_key = options[:embed_key] || :id
|
||||||
|
@embed_in_root = options[:include]
|
||||||
|
|
||||||
@options = options
|
@options = options
|
||||||
@serializer_options = serializer_options
|
@serializer_options = serializer_options
|
||||||
end
|
end
|
||||||
|
|
||||||
def name
|
attr_reader :root, :name, :embed_in_root
|
||||||
options[:name] || @name
|
alias :embed_in_root? :embed_in_root
|
||||||
end
|
|
||||||
|
|
||||||
def root
|
|
||||||
options[:root] || @name
|
|
||||||
end
|
|
||||||
|
|
||||||
def embed_ids?
|
def embed_ids?
|
||||||
[:id, :ids].include? options[:embed]
|
[:id, :ids].include? embed
|
||||||
end
|
end
|
||||||
|
|
||||||
def embed_objects?
|
def embed_objects?
|
||||||
[:object, :objects].include? options[:embed]
|
[:object, :objects].include? embed
|
||||||
end
|
|
||||||
|
|
||||||
def embed_in_root?
|
|
||||||
options[:include]
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def embeddable?
|
def embeddable?
|
||||||
@ -34,17 +31,7 @@ module ActiveModel
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def object
|
attr_reader :object, :embed, :embed_key
|
||||||
options[:value]
|
|
||||||
end
|
|
||||||
|
|
||||||
def embed_key
|
|
||||||
if key = options[:embed_key]
|
|
||||||
key
|
|
||||||
else
|
|
||||||
:id
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def target_serializer
|
def target_serializer
|
||||||
serializer = options[:serializer]
|
serializer = options[:serializer]
|
||||||
@ -75,6 +62,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def root
|
||||||
|
options[:root] || name
|
||||||
|
end
|
||||||
|
|
||||||
def id_key
|
def id_key
|
||||||
"#{@name.to_s.singularize}_ids".to_sym
|
"#{@name.to_s.singularize}_ids".to_sym
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user