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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/encodable/active_record/instance_methods.rb', line 14
def serializable_hash(options = {})
options ||= {}
options[:as] ||= :default
original_except = if options[:except]
options[:except] = Array(options[:except]).map(&:to_sym)
else
options[:except] = []
end
if options && options[:only]
options[:except].push *self.class.default_attributes(options[:as]) - Array(options.delete(:only).map(&:to_sym))
end
inherited_except = original_except - self.class.default_attributes(options[:as])
case options[:include]
when Array, Symbol
includes = Array(options[:include])
options[:include] = Hash[*includes.map{|association| [association, {:except => inherited_except}]}.flatten]
else
options[:include] ||= {}
end
options[:except].push *self.class.unencodable_attributes(options[:as])
self.class.default_attributes(options[:as]).each do |attribute, as|
unless options[:except].include?(attribute)
if association = self.class.reflect_on_association(attribute)
options[:include][attribute] = {:except => inherited_except}
elsif respond_to?(attribute) && !self.class.column_names.include?(attribute.to_s)
options[:methods] ||= Array(options[:methods]).compact
options[:methods].push attribute
end
end
end
as_json = super(options)
self.class.renamed_encoded_attributes(options[:as]).each do |attribute, as|
if as_json.has_key?(attribute) || as_json.has_key?(attribute.to_s)
as_json[as.to_s] = as_json.delete(attribute) || as_json.delete(attribute.to_s)
end
end
as_json
end
|