blob: 2eca3ebc5e68cc3b8f052490b8d8de947773e316 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
require "active_support/core_ext/class/attribute"
require "active_support/core_ext/string/inflections"
require "set"
module ActiveModel
class Serializer
class_attribute :_attributes
self._attributes = Set.new
def self.attributes(*attrs)
self._attributes += attrs
end
attr_reader :object, :scope
def self.inherited(klass)
name = klass.name.demodulize.underscore.sub(/_serializer$/, '')
klass.class_eval do
alias_method name.to_sym, :object
end
end
def initialize(object, scope)
@object, @scope = object, scope
end
def as_json(*)
serializable_hash
end
def serializable_hash(*)
attributes
end
def attributes
hash = {}
_attributes.each do |name|
hash[name] = @object.read_attribute_for_serialization(name)
end
hash
end
end
end
|