Class: KojacBaseSerializer

Inherits:
ActiveModel::Serializer
  • Object
show all
Defined in:
app/serializers/kojac_base_serializer.rb

Constant Summary collapse

SERIALIZABLE_TYPES =
[NilClass,FalseClass,TrueClass,Fixnum,Bignum,String,Symbol,Hash,Array]

Instance Method Summary collapse

Constructor Details

#initialize(object, options = {}) ⇒ KojacBaseSerializer

Returns a new instance of KojacBaseSerializer.



5
6
7
# File 'app/serializers/kojac_base_serializer.rb', line 5

def initialize(object, options={})
	super(object,options)
end

Instance Method Details

#attributesObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/serializers/kojac_base_serializer.rb', line 11

def attributes
	attrs = nil
	source = if policy = Kojac::policy(scope,object)
		attrs = policy.permitted_attributes(:read).map(&:to_s)
		object.attributes
	elsif object.respond_to? :attributes
		object.attributes
	elsif object.is_a? Hash
		object
	end
	result = {}
	source.each do |k,v|
		k_s = k.to_s
		next if attrs && !attrs.include?(k_s)
		if SERIALIZABLE_TYPES.include? v.class
			result[k_s] = v
		else
			result[k_s] = KojacUtils.to_jsono(v,scope: scope)
		end
	end
	result
end