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
47
48
49
50
51
52
53
|
# File 'lib/jsonapi-serializer-formats.rb', line 9
def self.included(base)
base.class_eval do
class << self
def scoped_formats
@@scoped_formats ||= []
end
alias_method :jsonapi_attributes, :attributes
def attributes(*attributes_list, &block)
formats = [*scoped_formats]
if formats.length.positive?
opts = attributes_list.last
unless opts.is_a?(Hash)
opts = {}
attributes_list << opts
end
cond = opts[:if]
opts[:if] = Proc.new do |_, params = {}|
next false if cond.present? && !cond.call(_, params)
render_formats = [params[:format]].compact.flatten
formats.all? { |f| render_formats.include?(f) }
end
end
jsonapi_attributes(*attributes_list, &block)
end
def format(fmt)
scoped_formats.push(fmt)
yield
ensure
scoped_formats.pop
end
alias_method :attribute, :attributes
end
end
end
|