active-model-serializers

EmberJS Data hasMany sideloading with ActiveModelSerializers

早过忘川 提交于 2019-12-12 04:09:51
问题 I'm using Ember Data canary build: 1.0.0-beta.8+canary.267214b9 together with a rails back-end and ActiveModelSerializer . My configuration on ember side looks like this: App.ApplicationSerializer = DS.ActiveModelSerializer.extend() App.ApplicationAdapter = DS.ActiveModelAdapter.extend namespace: "api/v1" App.Authentication = DS.Model.extend provider: DS.attr('string') user: DS.belongsTo('user') App.User = DS.Model.extend username: DS.attr('string') email: DS.attr('string') authentications:

How to render parent data of nested object via Active model serializers for Rails?

梦想与她 提交于 2019-12-12 03:49:26
问题 I am working on Rails 5 api only app. So this is my model serializer class MovieSerializer < ActiveModel::Serializer attributes :id ,:name,:release_year,:story,:in_theater,:poster,:score,:user_count belongs_to :age_rating belongs_to :company has_many :categories has_many :movie_celebrities end class MovieCelebritySerializer < ActiveModel::Serializer attributes :id,:vacancy,:title belongs_to :celebrity end class CelebritySerializer < ActiveModel::Serializer attributes :id, :first_name, :last

Adding root value to a response from has_many_through relationship

风格不统一 提交于 2019-12-12 00:21:41
问题 I have a has_many through relationship in rails app. I is associated as follows: class Car < ActiveRecord::Base has_many :car_fuel_types has_many :fuel_types , through: :car_fuel_types end class FuelType < ActiveRecord::Base has_many :car_fuel_types has_many :cars , through: :car_fuel_types end class CarFuelType < ActiveRecord::Base belongs_to :fuel_type belongs_to :car end In my cars_controller I want to get all to get all the Cars with a particular fuel_type, so I am writing the following

Use attribute with condition in activemodel serializer

泄露秘密 提交于 2019-12-11 08:56:26
问题 class ProjectSerializer < ActiveModel::Serializer attributes :id, :title end I use activemodel serializer to return title attribute with some conditions. Normally I can override title method but what I want is determine whether title attribute is returned or not with condition. 回答1: I'm not sure exactly what your use case is, but maybe you can use the awesomely magical include_ methods! They are the coolest! class ProjectSerializer < ActiveModel::Serializer attributes :id, :title def include

Active Model Serializer and Pundit deleting records during a Show CRUD action

北战南征 提交于 2019-12-11 05:14:00
问题 Okay, something is seriously broken here... I am using Active Model Serializer and Pundit for my Rails 5 JSONAPI server and Ember for my frontend application. I have User model and Pundit policy for User model which prevent non-authors from viewing unpublished stories and chapters. At the moment, I am seeing a weird problem which goes like this: 1. UserA creates StoryA, and two published chapters Chapter1 and Chapter2 2. UserA then creates two unpublished chapters Chapter3 and Chapter4 3.

Serialize array/relation with attributes using ActiveModel::Serializer

99封情书 提交于 2019-12-11 02:23:55
问题 I want to serialize relation using Active Model Serializers and I want to set some 'global' attributes (e.g. count) for this relation: { users: { total: 12, page: 2, users: [{}, {}, {}, ...] } } How could I do that? 回答1: During your render call in the controller, you can pass in the meta attribute. render @users, :each_serializer => UserSerializer, :meta => { :total => @users.count } This will produce the following JSON: { "users" : [...], "meta" : { "total" : 12 } } You can rename the meta

ActiveModelSerializers (0.10.0.rc3) an object's relation's relation is not generated with default FlattenJson adapter

…衆ロ難τιáo~ 提交于 2019-12-10 23:56:20
问题 Rails 5 Alpha version / Ruby 2.2.3 / active_model_serializers (0.10.0.rc3) ( henceforth referred as AMS ) GIT remote: https://github.com/rails/rails.git revision: 5217db2a7acb80b18475709018088535bdec6d30 GEM remote: https://rubygems.org/ specs: active_model_serializers (0.10.0.rc3) I already have a working API-only app utilizing Rabl-Rails to generate the JSON responses. I am working on making it ready to work with Rails 5 and as part of this evaluating Rails 5 inbuilt API features esp the

ActiveModel Serializer - Passing params to serializers

两盒软妹~` 提交于 2019-12-10 05:02:43
问题 AMS version: 0.9.7 I am trying to pass a parameter to an ActiveModel serializer without any luck. My (condensed) controller: class V1::WatchlistsController < ApplicationController def index currency = params[:currency] @watchlists = Watchlist.belongs_to_user(current_user) render json: @watchlists, each_serializer: WatchlistOnlySerializer end My serializer: class V1::WatchlistOnlySerializer < ActiveModel::Serializer attributes :id, :name, :created_at, :market_value attributes :id def filter

EmberJS 2.7 = has_many configuration for Ember-Data and Active Model Serializers, using Ember-Power-Select (and side loaded, not embedded data)

夙愿已清 提交于 2019-12-08 09:06:52
问题 This is a similar question to this one, except this is for the latest versions of Ember and Active Model Serializers (0.10.2). I have a simple Parent:Child relationship. app/models/trail.js import Ember from 'ember'; import DS from 'ember-data'; export default DS.Model.extend({ name: DS.attr(), // relationships employees: DS.hasMany('employee', { async: true }), }); app/models/employee.js import DS from 'ember-data'; import Person from '../models/person'; export default Person.extend({ status

Swagger UI for Rails API using ActiveModel's Serializer

倾然丶 夕夏残阳落幕 提交于 2019-12-08 06:18:49
问题 I was wondering if anyone's done this before where they generate API docs using Swagger UI for an API not also generated by Swagger. Here's what a simple example of mine looks like: class Api::V1::UsersController < Api::V1::BaseController swagger_controller :users, 'Users' swagger_api :show do summary 'Returns a user' param :path, :id, :integer, :optional, "User Id" notes '/api/v1/users/:id' response :ok, "Success", :Users response :unauthorized response :not_acceptable response :not_found