GithubHelp home page GithubHelp logo

isabella232 / serialize_with_options Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vigetlabs/serialize_with_options

0.0 0.0 0.0 42 KB

Simple XML and JSON APIs for your Rails app

Home Page: http://www.viget.com/extend/simple-apis-using-serializewithoptions

License: MIT License

Ruby 100.00%

serialize_with_options's Introduction

SerializeWithOptions

Code Climate Build Status

This plugin is designed to make creating XML and JSON APIs for your Rails apps dead simple. We noticed a lot of repetition when creating API responses in our controllers. With this plugin, you can set the serialization options for a model with a simple DSL, rather than repeating them in every controller that includes it.

Example

Here is a simple example of SerializeWithOptions in action:

class User < ActiveRecord::Base
  has_many :posts

  serialize_with_options do
    methods   :post_count
    includes  :posts
    except    :email
  end

  serialize_with_options(:with_email) do
    methods   :post_count
    includes  :posts
  end

  def post_count
    self.posts.count
  end
end

class Post < ActiveRecord::Base
  has_many :comments
  belongs_to :user

  serialize_with_options do
    only      :title
    includes  :user, :comments
  end
end

class Comment < ActiveRecord::Base
  belongs_to :post
end

With these directives in place, we can call @post.to_xml (or @post.to_json) and it's as if we entered:

@post.to_xml(:include => { :user => { :methods => :post_count, :except => :email }, :comments => { } })

In our controller, we can just say:

def show
  @post = Post.find(params[:id])

  respond_to do |format|
    format.html
    format.xml { render :xml => @post }
    format.json { render :json => @post }
  end
end

All serialization options are enclosed in a serialize_with_options block. There are four options, lifted directly from ActiveRecord's serialization API: methods are the methods to add to the default attributes, only are the attributes to include, excluding all others, except are the attributes to leave out, and includes are the associated models.

If an included model has its own serialize_with_options block, its methods, only, and except will be respected. However, the included model's includes directive will be ignored (only one level of nesting is supported). If you need more than one level of nesting, you can use a hash to set your included models, rather than an array.

The serialize_with_options class method takes an optional argument for naming a configuration set (see the User model above). This is useful if you need to multiple serialization configuration sets. You can access these secondary configuration sets by passing the set name to the serialization method, e.g., @post.to_xml(:with_email).

Installation

Simply add "serialize_with_options" to your Gemfile:

gem "serialize_with_options"

And run "bundle install"


Copyright (c) 2009-2014 David Eisinger & Zachary Porter (Viget Labs), released under the MIT license.


Code At Viget

Visit code.viget.com to see more projects from Viget.

serialize_with_options's People

Contributors

dce avatar brianjlandau avatar nickmerwin avatar pius avatar zporter 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.