active-model-serializers

Ember.js current_user - accessing global variable from controller

夙愿已清 提交于 2019-11-29 04:05:30
问题 I am baffled by a seemingly simple ember question. I am using active_model_serializers meta data serialization to serialize my rails current_user method from a rails controller, then extracting and setting that current_user meta json to a global ember variable using this guy's temporary solution. App.CustomRESTSerializer = DS.RESTSerializer.extend({ extractMeta: function(loader, type, json) { var meta; meta = json[this.configOption(type, 'meta')]; if (!meta) { return; } Ember.set('App

Correct way to implement API versioning with active_model_serializers

一曲冷凌霜 提交于 2019-11-29 03:56:21
问题 I know there are already some questions and also this is a open issue regarding AMS not handling namespaces too efficiently (which is used by this versioning approach) but I wanted to be sure I am in the right track within the current constraints. Right now I am using Rails 5 and AMS 0.10.1, so I did the following: # config/initializers/active_model_serializer.rb ActiveModelSerializers.config.serializer_lookup_enabled = false to disable default serializer lookup (which didn't work anyway);

Serialize permissions (e.g. CanCan) with active_model_serializers

╄→尐↘猪︶ㄣ 提交于 2019-11-29 01:33:30
问题 How do I serialize permissions with active_model_serializers? I don't have access to current_user or the can? method in models and serializers. 回答1: First, to get access to the current_user in the serializer context, use the new scope feature: class ApplicationController < ActionController::Base ... serialization_scope :current_user end In case you are instantiating serializers manually, be sure to pass the scope: model.active_model_serializer.new(model, scope: serialization_scope) Then

Conditional attributes in Active Model Serializers

亡梦爱人 提交于 2019-11-28 17:49:07
How do I render an attribute only if some condition is true? For example, I want to render User's token attribute on create action. Abdelhakim AKODADI You can also do it this way: class EntitySerializer < ActiveModel::Serializer attributes :id, :created_at, :updated_at attribute :conditional_attr, if: :condition? def condition? #condition code goes here end end For example: class UserSerializer < ActiveModel::Serializer attributes :id, :username, :name, :email, :created_at, :updated_at attribute :auth_token, if: :auth_token? def created_at object.created_at.to_i end def updated_at object

How do you initialize an ActiveModel::Serializer class with an ActiveRecord::Relation array?

五迷三道 提交于 2019-11-28 15:43:26
I have a serializer class FundingSerializer < ActiveModel::Serializer attributes :id, has_one :user has_one :tournament embed :ids, include: true end That initializes with the proper associations FundingSerializer.new(Funding.first).to_json yields "{\"users\":[{\"id\":2,\"first_name\":\"Nick\"}],\"tournaments\":[{\"id\":1,\"end_date\":\"2013-07-21T23:18:54.981Z\",\"start_date\":\"2013-07-14T23:18:54.980Z\"}],\"funding\":{\"id\":1}}" but, FundingSerializer.new(Funding.all).to_json gets this error. undefined method `read_attribute_for_serialization' for #<ActiveRecord::Relation::ActiveRecord

How do you initialize an ActiveModel::Serializer class with an ActiveRecord::Relation array?

主宰稳场 提交于 2019-11-27 09:21:56
问题 I have a serializer class FundingSerializer < ActiveModel::Serializer attributes :id, has_one :user has_one :tournament embed :ids, include: true end That initializes with the proper associations FundingSerializer.new(Funding.first).to_json yields "{\"users\":[{\"id\":2,\"first_name\":\"Nick\"}],\"tournaments\":[{\"id\":1,\"end_date\":\"2013-07-21T23:18:54.981Z\",\"start_date\":\"2013-07-14T23:18:54.980Z\"}],\"funding\":{\"id\":1}}" but, FundingSerializer.new(Funding.all).to_json gets this

How to pass parameters to ActiveModel serializer

我的未来我决定 提交于 2019-11-26 10:27:44
问题 I\'m using active model serializer. I have a model event which has_many activities. I want to return the event with the first n activities. I think I should pass the params n to the event serializer. 回答1: Options passed in are available through the @options hash. So if you do: respond_with @event, activity_count: 5 You can use @options[:activity_count] within the serializer. 回答2: In version ~> 0.10.0 you need to use @instance_options . Using @Jon Gold example from above: # controller def