Skip to content
This repository has been archived by the owner on Jan 5, 2018. It is now read-only.

A renamed version of default_xml_options that includes support for to_json as well as to_xml. Allows you to set default to_xml options on your ActiveRecord model so you do not have to include them every time you call to_xml.

License

jls/default_serialize_options

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DefaultSerializeOptions
=======================

Allows you to set default to_xml and to_json options on your ActiveRecord model
so you do not have to include them every time you call to_xml or to_json.

Options
=======

Default options to use for to_xml:
default_serialize_options :xml => { :include => [:items] }

Default options for use for to_json:
default_serialize_options :json => { :include => [:items] }

Default options for both to_xml and to_json:
default_serialize_options :all => { :include => [:items] }

If the keys :xml, :json or :all do not exist in the options the key :all is assumed. 
Which means
default_serialize_options :include => [:items] *is the same as* default_serialize_options :all => {:include => [:items]}

Example
=======

class Download < ActiveRecord::Base
  
  default_serialize_options :xml => { :include => [:items, :author] },
                            :json => { :include => [:author] }
  
end

class DownloadController < ApplicationController
  
  def show
    @download = current_user.downloads.find(params[:id])
    respond_to do |format|
      format.html
      format.xml { 
        render :xml => @download.to_xml 
        # Which is the same as
        #render :xml => @download.to_xml(:include => [:items, :author])
      } 
    end
  end
end

You can also override the defaults you have specified in default_xml_options by
passing the options directly to to_xml.

# Adding a new option
render :xml => @download.to_xml(:skip_instruct => true)
# The same as
render :xml => @download.to_xml(:skip_instruct => true, :include => [:items, :author])

# Overriding a default
render :xml => @download.to_xml(:include => [:author])

If you want to ignore the defaults completely pass :ignore_defaults => true as an option
render :xml => @download.to_xml(:ignore_defaults => true, :include => [:items])


Copyright (c) 2009 James Smith, released under the MIT license

About

A renamed version of default_xml_options that includes support for to_json as well as to_xml. Allows you to set default to_xml options on your ActiveRecord model so you do not have to include them every time you call to_xml.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages