CSC/ECE 517 Fall 2014/ch1a 24 sa: Difference between revisions
No edit summary |
|||
Line 14: | Line 14: | ||
Let us review some vocabulary before getting to the usage of '''Active Model Serializers'''. | Let us review some vocabulary before getting to the usage of '''Active Model Serializers'''. | ||
==Active Model== | ==Active Model== | ||
'''[http://railscasts.com/episodes/219-active-model Active Model]''' is closely related to the widely used concept of '''[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord Active Record]''' | '''[http://railscasts.com/episodes/219-active-model Active Model]''' is closely related to the widely used concept of '''[http://en.wikibooks.org/wiki/Ruby_on_Rails/ActiveRecord Active Record]''' in the Rails world. '''Active Record''' is an object that maps directly to the data in our database. An '''Active Record''' takes care of fetching and saving data to the database. For eg. In an employee records application an Employee Active Record helps to persist a newly created/edited Employee. So how is an '''Active Model''' different? It may not always be the case that we want our Employee model to be backed by a database. . But we might want to have all the functionality that an Active Record provides (for eg. validations on the Employee model to check if name is present on the Employee Active Record before persisting it or checking if the active record got dirtied because of an attribute change etc. ). An example of this is as and when an Employee create form is submitted, we might want to just send an email to the admin with the new employee data once we validate that data. An '''Active Model''' does just this. Infact, the rails developers segregated all the non database functionality out of an '''Active Record''' into an '''Active Model'''. <link active model to http://railscasts.com/episodes/219-active-model > | ||
==Serializer== | ==Serializer== |
Revision as of 00:56, 19 September 2014
Active Model Serializers
Ruby on Rails offers strong features to write API based server applications, which expose data through a well designed API and serve data in various formats as requested. One of these features is Active Model Serializers which help the API designer to have fine control over the response generated by the API to a data request. Active Model Serializers help programmers do this very easily and cleanly without breaking separation of concerns in a rails application. They promote convention over configuration, thus reducing code bloat and increasing code scalibility and readability.
Introduction
Let us review some vocabulary before getting to the usage of Active Model Serializers.
Active Model
Active Model is closely related to the widely used concept of Active Record in the Rails world. Active Record is an object that maps directly to the data in our database. An Active Record takes care of fetching and saving data to the database. For eg. In an employee records application an Employee Active Record helps to persist a newly created/edited Employee. So how is an Active Model different? It may not always be the case that we want our Employee model to be backed by a database. . But we might want to have all the functionality that an Active Record provides (for eg. validations on the Employee model to check if name is present on the Employee Active Record before persisting it or checking if the active record got dirtied because of an attribute change etc. ). An example of this is as and when an Employee create form is submitted, we might want to just send an email to the admin with the new employee data once we validate that data. An Active Model does just this. Infact, the rails developers segregated all the non database functionality out of an Active Record into an Active Model. <link active model to http://railscasts.com/episodes/219-active-model >
Serializer
Serializer is a small program that takes an object in memory (eg: could be a C# object in an ASP.NET application, or an ActiveModel object in a Rails application) and converts this information into format that is transferrable over the wire eg. JSON and XML. This is required because the client may be written in Java and there is no way for it to read an Active Model Ruby Object that exists in memory on the server side.
API Server Application
API Server application is an application which serves data requests coming from a variety of clients (eg: mobile, desktop) in various formats such as JSON, XML. For eg. An employee data web service which returns employee age and name in both JSON and XML
Why use Active Model Serializers?
It is not always the case that the api application serves a request by responding with data as it is stored on the server. The api designer has a task of exposing just the right data to the clients and hence needs fine control over exactly what part of the data on the server is returned as a response. For Example : Imagine a server which responds to employee age and name requests. However the Employee object on the server side may have many other attributes such as salary etc. The api designer would then just want to pick the correct set of attributes from Employee object and return them as json. Also the clients may want to receive the json formatted data with key value pairs appearing in a very specific way which suits their needs. However a designer may find that the default method rails provides to convert an object to json does not meet the client requirements. This calls for using Active Model Serializers. <draw diagram of a api server application which serves data in various formats>
How to use Active Model Serializer?
Step1 : Add to the gem file the following line : gem 'active_model_serializers'
Step2 : Run the bundle command <tell them where> to install the gem.
Step3 : This gem provides a serializer generator for the selected Active Model. For eg. if employee was your Active Model run : rails g serializer employee
This would create a file employee_serializer.rb containing this in the newly created app/serializers directory:
class EmployeeSerializer < ActiveModel::Serializer attributes :id end
Step 4: Add your custom attributes and any other customizations to this file. For eg: We are adding a url attribute to get details of this employee although the Employee Active Model does not have any such attribute. We add it because the client might be interested in it. That is the power of Active Model Serializer. It does not require us to define another property on the Employee Active Model to return its Url.
class EmployeeSerializer < ActiveModel::Serializer attributes :id, :name, :age, :url def url employee_url(object) end end
Step 5: Thats it! Whenever an Employee is requested the custom EmployeeSerializer does the task of serializing the employee to json and will now include a url key value pair to view the Employee.