I am getting a parameter e.g: member_id in Grape::API like
desc 'Return Events'
params do
requires :member_id, type: Integer, desc: 'Member'
end
get 'all' do
#some code
end
end
and I want to pass it to ActiveModel::Serializer
so that I can perform some functionality.
Is there any way that I can pass it to ActiveModel::Serializer
?
When you serialize an object using ActiveModel::Serializers
, you can pass along options which are available inside the serializer as options
(or instance_options
, or context
, depending on which version of AMS you're using).
For instance, in Rails you would pass a foo
option like so:
# 0.8.x or 0.10.x
render @my_model, foo: true
MyModelSerializer.new(@my_model, foo: true).as_json
# 0.9.x
render @my_model, context: { foo: true }
MyModelSerializer.new(@my_model, context: { foo: true }).as_json
In your serializer you access options
(or instance_options
) to get the value:
class MyModelSerializer < ActiveModel::Serializer
attributes :my_attribute
def my_attribute
# 0.8.x: options
# 0.9.x: context
# 0.10.x: instance_options
if options[:foo] == true
"foo was set"
end
end
def