Giter Site home page Giter Site logo

serializer_field_filter's Introduction

serializer_field_filter

field filter for active_model_serializers

Install

gem install 'serializer_field_filter'

Usage

  class BaseSerializer < ActiveModel::Serializer
    include SerializerFieldFilter::Relation
  end

  class CourceSerializer < BaseSerializer
    attributes :id, :name
    has_one_with_filter :classroom
    belongs_to_with_filter :teacher
    has_many_with_filter :students
  end

  class ClassroomSerializer < BaseSerializer
    attributes :id, :name
  end

  class TeacherSerializer < BaseSerializer
    attributes :id, :name
  end

  class StudentSerializer < BaseSerializer
    attributes :id, :name
  end

  class BaseModel
    include ActiveModel::Model
    include ActiveModel::Serialization
  end

  class Cource < BaseModel
    attr_accessor :id, :name, :classroom, :teacher, :students
  end

  class Classroom < BaseModel
    attr_accessor :id, :name
  end

  class Teacher < BaseModel
    attr_accessor :id, :name
  end

  class Student < BaseModel
    attr_accessor :id, :name
  end

  classroom = Classroom.new(id: 1, name: "classroom")
  teacher   = Teacher.new(id: 1, name: "teacher")
  student_1 = Student.new(id: 1, name: "student")
  student_2 = Student.new(id: 2, name: "student")
  cource    = Cource.new(id: 1, name: "cource", classroom: classroom, teacher: teacher, students: [student_1, student_2])

  filter = SerializerFieldFilter.init(%w(id name classroom teacher.name students.id))

  json = ActiveModelSerializers::SerializableResource.new(cource, filter.resource_options).as_json
  # => {
  #   id: 1,
  #   name: 'cource',
  #   classroom: { id: 1, name: 'classroom' },
  #   teacher: { name: 'teacher' },
  #   students: [
  #     { id: 1 },
  #     { id: 2 }
  #   ]
  # }

serializer_field_filter's People

Contributors

zhanglinjie avatar

Stargazers

Benjamin Fleischer avatar 赵龙文 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.