Sha256: 21b5109ef82a367254443bda9c8af227e0a8e1aa7d4535be422821a325f8f659

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

## serializer_field_filter

field filter for [active_model_serializers](https://github.com/rails-api/active_model_serializers)


### Install

gem install 'serializer_field_filter'

### Usage

```Ruby
  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 }
  #   ]
  # }
  ```

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
serializer_field_filter-0.0.3 README.md
serializer_field_filter-0.0.2 README.md
serializer_field_filter-0.0.1 README.md