active-model-serializers

Rails 5 - Object Relation Impedence and how to structure multiple inherited classes/tables

只愿长相守 提交于 2019-12-01 16:14:11
问题 EDIT I have edited this from the original to make it easier to understand. I understand the Object Relationship Impedance problem. I understand Rails STI and Polymorphism (the Rails way and that it's not true OO Polymorphism). I've read a TON of blogs and questions on this, and still cannot find the answer to this problem. class Person < ApplicationRecord (ie what was ActiveRecord::Base) end class Employee < Person end class Customer < Person end ... multiple other types of Person Now lets

Changing Active Model Serializers Default Adapter

心已入冬 提交于 2019-11-30 08:38:16
If I want to switch from the default attributes adapter to the json api adapter, where would I do this? The getting started states this: Generally speaking, you as a user of AMS will write (or generate) these serializer classes. If you want to use a different adapter, such as a JsonApi, you can change this in an initializer: ActiveModel::Serializer.config.adapter = :json_api What initializer are they referring to? Do I create a new one? Sorry for the noob question In general, initilizers are put under the app/config/initializers directory in a Rails app. So, in your case, you can create a new

Conditional attributes in Active Model Serializers

╄→尐↘猪︶ㄣ 提交于 2019-11-30 06:22: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. 回答1: 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

Ember.js current_user - accessing global variable from controller

点点圈 提交于 2019-11-30 05:49:43
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.metaData', meta); this._super(loader, type, json); } }); All good up until this point. Now I can access the

Correct way to implement API versioning with active_model_serializers

橙三吉。 提交于 2019-11-30 05:48:28
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); and # app/controllers/application_controller.rb class ApplicationController < ActionController::API def

Test ActiveModel::Serializer classes with Rspec

穿精又带淫゛_ 提交于 2019-11-30 04:21:47
Given the following ActiveModel::Serializer class: class SampleSerializer < ActiveModel::Serializer attributes :id, :name end How can this be tested with RSpec ? gnerkus Assumptions This answer assumes you have the rspec-rails , active_model_serializers and factory_girl_rails gems installed and configured. This answer also assumes you have defined a factory for the Sample resource. Serializer spec For the current version(0.10.0.rc3) of active_model_serializers at the time of writing, ActiveModel::Serializer classes do not receive to_json and are , instead, wrapped in an adapter class. To

How do I select which attributes I want for active model serializers relationships

女生的网名这么多〃 提交于 2019-11-30 03:45:18
问题 I am using the JSONAPI format along with Active Model Serializers to create an api with rails-api. I have a serializer which shows a specific post that has many topics and currently, under relationships, lists those topics. It currently only lists the id and type. I want to show the title of the topic as well. Some would say to use include: 'topics' in my controller, but I don't need the full topic record, just its title. Question : How do I specify which attributes I want to show from topics

Limiting Associations Cascade in Active Model Serializer

限于喜欢 提交于 2019-11-29 21:56:34
I'm having an issue with limiting the level of associations serialized within an active model resource. For example: A Game has many Teams which has many Players class GameSerializer < ActiveModel::Serializer attributes :id has_many :teams end class TeamSerializer < ActiveModel::Serializer attributes :id has_many :players end class PlayerSerializer < ActiveModel::Serializer attributes :id, :name end When I retrieve the JSON for the Team, it includes all the players in a sub array, as desired. When I retrieve the JSON for the Game, it includes all the Teams in a sub array, excellent, but also

Using ActiveModel::Serializer in Rails - JSON data differs between json and index response

给你一囗甜甜゛ 提交于 2019-11-29 15:39:37
问题 I'm using active_model_serializers gem to control the serialization data, and seeing some odd behavior. My code looks like so: model & serializer class User include Mongoid::Document field :first_name, :type => String field :last_name, :type => String def full_name first_name + " " + last_name end end class UserSerializer < ActiveModel::Serializer attributes :id, :first_name, :last_name, :full_name end controller class UsersController < ApplicationController respond_to :json, :html def index

Changing Active Model Serializers Default Adapter

眉间皱痕 提交于 2019-11-29 11:33:51
问题 If I want to switch from the default attributes adapter to the json api adapter, where would I do this? The getting started states this: Generally speaking, you as a user of AMS will write (or generate) these serializer classes. If you want to use a different adapter, such as a JsonApi, you can change this in an initializer: ActiveModel::Serializer.config.adapter = :json_api What initializer are they referring to? Do I create a new one? Sorry for the noob question 回答1: In general, initilizers