mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Get rid of refine
This commit is contained in:
parent
1a8709d71c
commit
460a250984
@ -129,7 +129,7 @@ module ActiveModel
|
|||||||
|
|
||||||
define_include_method attr
|
define_include_method attr
|
||||||
|
|
||||||
self._associations[attr] = klass.refine(attr, options)
|
self._associations[attr] = [klass, options]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -217,8 +217,8 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
associations = {}
|
associations = {}
|
||||||
_associations.each do |attr, association_class|
|
_associations.each do |attr, (association_class, options)|
|
||||||
association = association_class.new(attr, self)
|
association = association_class.new(attr, self, options)
|
||||||
|
|
||||||
if model_association = klass.reflect_on_association(association.name)
|
if model_association = klass.reflect_on_association(association.name)
|
||||||
# Real association.
|
# Real association.
|
||||||
@ -381,8 +381,10 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
klass, opts = _associations[name]
|
||||||
association_class =
|
association_class =
|
||||||
if klass = _associations[name]
|
if klass
|
||||||
|
options = opts.merge options
|
||||||
klass
|
klass
|
||||||
elsif value.respond_to?(:to_ary)
|
elsif value.respond_to?(:to_ary)
|
||||||
Associations::HasMany
|
Associations::HasMany
|
||||||
|
|||||||
@ -2,34 +2,6 @@ module ActiveModel
|
|||||||
class Serializer
|
class Serializer
|
||||||
module Associations #:nodoc:
|
module Associations #:nodoc:
|
||||||
class Config #:nodoc:
|
class Config #:nodoc:
|
||||||
class_attribute :options
|
|
||||||
|
|
||||||
def self.refine(name, class_options)
|
|
||||||
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.options = class_options
|
|
||||||
|
|
||||||
# cache the root so we can reuse it without falling back on a per-instance basis
|
|
||||||
begin
|
|
||||||
self.options[:root] ||= self.new(name, nil).root
|
|
||||||
rescue
|
|
||||||
# this could fail if it needs a valid source, for example a polymorphic association
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
self.options = {}
|
|
||||||
|
|
||||||
def initialize(name, source, options={})
|
def initialize(name, source, options={})
|
||||||
@name = name
|
@name = name
|
||||||
@source = source
|
@source = source
|
||||||
@ -39,8 +11,6 @@ module ActiveModel
|
|||||||
def option(key, default=nil)
|
def option(key, default=nil)
|
||||||
if @options.key?(key)
|
if @options.key?(key)
|
||||||
@options[key]
|
@options[key]
|
||||||
elsif self.class.options.key?(key)
|
|
||||||
self.class.options[key]
|
|
||||||
else
|
else
|
||||||
default
|
default
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user