to_json_options - Define to_json options in the model classes
author: Heiko Seebach email address: heiko (dot) seebach (at) web (dot) de license: MIT project website: http://to-json-options.rubyforge.org or http://rubyforge.org/projects/to-json-options/ private website of the author: https://www.xing.com/profile/Heiko_Seebach
Since Rails 2.0, an ActiveRecord model is able to serialize itself as a JSON string. Default is, that all attributes of the model are included in the serialization result.
This is done simply by calling:
ModelClass.to_json
For Details see http://api.rubyonrails.com/classes/ActiveRecord/Serialization.html
As described there, one can parameterize the to_json call, using keys like :include, :only, :method or :except. It‘s also possible to define the options for associate classes, as shown in the above documentation example:
konata.to_json(:include => { :posts => {
:include => { :comments => {:only => :body } },
:only => :title } })
So the model classes have no clue about how they are serialized. From one point of view, this is a clean approach, it‘s "separation of concerns".
But, what if you‘ve a tree of associated model classes included in one to_json call, or if you have different kinds of trees often including the same model classes? Wouldn‘t it be a "railsy" approach, if you declared the serialization parameters inside each model class?
That‘s exactly, what this plugin is made for.
Simply install using
script/plugin install http://to-json-options.rubyforge.org/svn/trunk/plugins/to_json_options
(or
ruby script\plugin install http://to-json-options.rubyforge.org/svn/trunk/plugins/to_json_options
under Windows)
First take a look at these examples:
class User < ActiveRecord::Base
to_json_options :except=>:password
...
end
or
class User < ActiveRecord::Base
to_json_options :only=>[:login, :id]
...
end
As you can see, the options are simply declared inside the model instead in the to_json call itself. This works for all the standard options (:only, except etc.) as described in the above documentation.
If you override keys in subclassess, they are not merged like the standard Ruby Hash merges, but they‘re added to the already existing values:
This means, that
class ExtendedUser < User
to_json_options :only=>:extended_login
...
end
will work as it was defined like this
to_json_options :only=>[:login,:id,:extended_login]
You may also define several keys in one line like
to_json_options :only=>[:login,:id], :methods=>:decrypted_password
You can define options to be scoped in a context. Whenever you pass this context in the parameters of the to_json_call, you can activate these options. If you omit a context in the to_json call, all options that are defined only with a context will be ignored.
Example:
class User < ActiveRecord::Base
to_json_options :very_verbose :except=>:password
to_json_options :not_very_verbose :only=>{:id, :login]
...
end
To activate the second declaration, you can simply call the to_json like this
user.to_json :context=>:very_verbose
Whenever you omit a name for the context, a context will automatically be created and named :default So calling
user.to_json :context=>:default
is similar to
user.to_json
Simply replace all the above said about to_json with to_xml. It will behave exactly the same.
Rails 2.0+