Skip to content

Commit

Permalink
Refactor attributes.
Browse files Browse the repository at this point in the history
  • Loading branch information
beauby committed Oct 19, 2015
1 parent 65c0834 commit d00ee4d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 55 deletions.
47 changes: 5 additions & 42 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'thread_safe'
require 'active_model/serializer/array_serializer'
require 'active_model/serializer/include_tree'
require 'active_model/serializer/attributes'
require 'active_model/serializer/associations'
require 'active_model/serializer/configuration'
require 'active_model/serializer/fieldset'
Expand All @@ -9,7 +10,9 @@
module ActiveModel
class Serializer
include Configuration
include Attributes
include Associations

require 'active_model/serializer/adapter'

# Matches
Expand Down Expand Up @@ -43,10 +46,6 @@ def self.digest_caller_file(caller_line)

with_options instance_writer: false, instance_reader: false do |serializer|
class_attribute :_type, instance_reader: true
class_attribute :_attributes
self._attributes ||= []
class_attribute :_attributes_keys
self._attributes_keys ||= {}
serializer.class_attribute :_cache
serializer.class_attribute :_fragmented
serializer.class_attribute :_cache_key
Expand All @@ -57,9 +56,9 @@ def self.digest_caller_file(caller_line)
end

def self.inherited(base)
inherit_attributes(base)
inherit_associations(base)
caller_line = caller.first
base._attributes = _attributes.dup
base._attributes_keys = _attributes_keys.dup
base._cache_digest = digest_caller_file(caller_line)
super
end
Expand All @@ -68,30 +67,6 @@ def self.type(type)
self._type = type
end

def self.attributes(*attrs)
attrs = attrs.first if attrs.first.class == Array

attrs.each do |attr|
attribute(attr)
end
end

def self.attribute(attr, options = {}, &block)
key = options.fetch(:key, attr)
_attributes_keys[attr] = { key: key } if key != attr
_attributes << key unless _attributes.include?(key)

ActiveModelSerializers.silence_warnings do
define_method key do
if block_given?
instance_eval(&block)
else
object.read_attribute_for_serialization(attr)
end
end unless method_defined?(key) || _fragmented.respond_to?(attr)
end
end

def self.fragmented(serializer)
self._fragmented = serializer
end
Expand Down Expand Up @@ -172,18 +147,6 @@ def json_key
root || object.class.model_name.to_s.underscore
end

def attributes
attributes = self.class._attributes.dup

attributes.each_with_object({}) do |name, hash|
if self.class._fragmented
hash[name] = self.class._fragmented.public_send(name)
else
hash[name] = send(name)
end
end
end

protected

attr_accessor :instance_options
Expand Down
29 changes: 16 additions & 13 deletions lib/active_model/serializer/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ module Associations
DEFAULT_INCLUDE_TREE = ActiveModel::Serializer::IncludeTree.from_string('*')

included do |base|
class << base
attr_accessor :_reflections
end
base.class_attribute :_reflections
base._reflections ||= []

extend ActiveSupport::Autoload
autoload :Association
Expand All @@ -28,8 +27,8 @@ class << base
end

module ClassMethods
def inherited(base)
base._reflections = self._reflections.try(:dup) || []
def inherit_associations(base)
base._reflections = _reflections.dup
end

# @param [Symbol] name of the association
Expand All @@ -39,8 +38,8 @@ def inherited(base)
# @example
# has_many :comments, serializer: CommentSummarySerializer
#
def has_many(name, options = {})
associate HasManyReflection.new(name, options)
def has_many(name, options = {}, &block)
associate(HasManyReflection.new(name, options), block)
end

# @param [Symbol] name of the association
Expand All @@ -50,8 +49,8 @@ def has_many(name, options = {})
# @example
# belongs_to :author, serializer: AuthorSerializer
#
def belongs_to(name, options = {})
associate BelongsToReflection.new(name, options)
def belongs_to(name, options = {}, &block)
associate(BelongsToReflection.new(name, options), block)
end

# @param [Symbol] name of the association
Expand All @@ -61,8 +60,8 @@ def belongs_to(name, options = {})
# @example
# has_one :author, serializer: AuthorSerializer
#
def has_one(name, options = {})
associate HasOneReflection.new(name, options)
def has_one(name, options = {}, &block)
associate(HasOneReflection.new(name, options), block)
end

private
Expand All @@ -73,11 +72,15 @@ def has_one(name, options = {})
#
# @api private
#
def associate(reflection)
def associate(reflection, block)
self._reflections = _reflections.dup

define_method reflection.name do
object.send reflection.name
if block_given?
instance_eval(&block)
else
object.send reflection.name
end
end unless method_defined?(reflection.name)

self._reflections << reflection
Expand Down

0 comments on commit d00ee4d

Please sign in to comment.