serialization

ruby
class PostSerializer < ActiveModel::Serializer
  attributes :id, :title, :excerpt, :body, :published_at, :views, :likes_count, :comments_count

  attribute :can_edit, if: :current_user_can_edit?

  belongs_to :author, serializer: UserSummarySerializer

Serializers with ActiveModel::Serializers

rails api serialization
by Alex Kumar 2 tabs
ruby
class PostsController < ApplicationController
  def index
    posts = Post.for_feed.page(params[:page]).per(25)

    render json: {
      data: posts.map { |post| PostSerializer.new(post).as_json },

N+1 Proof Serialization with preloaded associations

rails activerecord performance
by codesnips 3 tabs
ruby
class NotifyFollowersJob
  include Sidekiq::Job

  sidekiq_options queue: :notifications, retry: 5

  def perform(post_id, actor_id)

Safer Background Job Arguments (Serialize IDs only)

rails reliability sidekiq
by codesnips 3 tabs
ruby
class ProjectPresenter
  def initialize(project, include_tasks: true)
    @project = project
    @include_tasks = include_tasks
  end

Building Nested JSON Presenters in Rails Without ActiveModel::Serializer

rails presenter json
by codesnips 3 tabs
ruby
class OrderPresenter < SimpleDelegator
  attr_reader :current_user

  def initialize(order, current_user:)
    super(order)
    @current_user = current_user

Presenter + fast_jsonapi Serializer for Clean JSON:API Responses in Rails

rails json-api serialization
by codesnips 3 tabs
ruby
module DefensiveDeserialization
  extend ActiveSupport::Concern

  MissingRecord = Struct.new(:gid) do
    def missing?
      true

Defensive Deserialization for ActiveJob

rails activejob reliability
by codesnips 3 tabs
ruby
module Notifications
  class RecipientSerializer < ActiveJob::Serializers::ObjectSerializer
    def serialize?(argument)
      argument.is_a?(Notifications::Recipient)
    end

Enqueuing Notification Batches with a Custom Active Job Argument Serializer for Value Objects

rails active-job serialization
by codesnips 4 tabs
rust
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug)]
struct Config {
    host: String,
    port: u16,

serde for zero-copy serialization and deserialization

rust serde serialization
by Marcus Chen 1 tab
ruby
module Api
  module V1
    class UsersController < ApplicationController
      before_action :authenticate_user!, except: [:index, :show]
      before_action :set_user, only: [:show, :update, :destroy]

RESTful API design with Rails

ruby rails api
by Sarah Mitchell 4 tabs
typescript
import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

Global ValidationPipe with class-validator DTOs and Transform in NestJS

nestjs validation dto
by codesnips 3 tabs
ruby
class User < ApplicationRecord
  normalizes :phone, with: ->(value) { PhoneNormalizer.call(value) }, apply_to_nil: false

  validates :phone,
            presence: true,
            uniqueness: { case_sensitive: false },

Normalizing Phone Numbers on Assignment with Rails normalizes and a Custom Serializer

rails activerecord normalization
by codesnips 3 tabs