lib/ocean/ocean_application_controller.rb
def api_render(x, new: false, href: url_for(params), override_partial: false)
    if !x.is_a?(Array) && !(defined?(ActiveRecord) && x.is_a?(ActiveRecord::Relation))
      partial = override_partial || x.to_partial_path
      if new
        render partial: partial, object: x, status: 201, location: x
      else
        render partial: partial, object: x
      end
      return
    else
      resources = x.dup.collect { |m| render_to_string(partial: m.to_partial_path,
                                                   locals: {m.class.model_name.i18n_key => m}) }
      count = resources.count
      total_count = x.respond_to?(:unscope) ? x.unscope(:limit, :offset).count : count
      attrs = {count: count,
               total_count: total_count
              }
      links = {}
      if href
        links['self'] =  {href: href, type: 'application/json'}
        if params[:page]
          page = params[:page].to_i
          page_size = params[:page_size].to_i || x.collection_page_size
          total_pages = (total_count.to_f / page_size.to_f).ceil
          last_page = total_pages - 1
          prev_page = page > 0 && page - 1
          next_page = page < last_page && page + 1
          links['first_page'] = {href: url_for(params.merge(page: 0)), type: 'application/json'}
          links['last_page'] = {href: url_for(params.merge(page: last_page)), type: 'application/json'}
          links['previous_page'] = {href: url_for(params.merge(page: prev_page)), type: 'application/json'} if prev_page
          links['next_page'] = {href: url_for(params.merge(page: next_page)), type: 'application/json'} if next_page
        end
      end

      attrs['_links'] = links if links
      attrs['page'] = page if page
      attrs['page_size'] = page_size if page_size
      attrs['total_pages'] = total_pages if total_pages

      render text: '{"_collection":{"resources":[' + resources.join(',') + ']' +
        (attrs.collect do |k, v|
           ',"' + k.to_s + '":' + v.to_json
         end).join('') +
        '}}'
    end
  end
#
# This is the main rendering function in Ocean. The argument +x+ can be a resource
# or a collection of resources (which need not be of the same type).
#
# The keyword arg +new+, if true, sets the response HTTP status to 201 and also adds
# a +Location+ HTTP header with the URI of the resource.
#
# Rendering is done using partials only. These should by convention be located in
# their standard position, begin with an underscore, etc. The +ocean+ gem generator
# for resources creates a partial in the proper location.
#
This is the main rendering function in Ocean. The argument +x+ can be a resource
or a collection of resources (which need not be of the same type).

The keyword arg +new+, if true, sets the response HTTP status to 201 and also adds
a +Location+ HTTP header with the URI of the resource.

Rendering is done using partials only. These should by convention be located in
their standard position, begin with an underscore, etc. The +ocean+ gem generator
for resources creates a partial in the proper location.
This is the main rendering function in Ocean . The argument + x + can be a resource or a collection of resources ( which need not be of the same type ) . The keyword arg + new + if true sets the response HTTP status to 201 and also adds a + Location + HTTP header with the URI of the resource . Rendering is done using partials only . These should by convention be located in their standard position begin with an underscore etc . The + ocean + gem generator for resources creates a partial in the proper location .
def api_render(x, new: false, href: url_for(params), override_partial: false)
    if !x.is_a?(Array) && !(defined?(ActiveRecord) && x.is_a?(ActiveRecord::Relation))
      partial = override_partial || x.to_partial_path
      if new
        render partial: partial, object: x, status: 201, location: x
      else
        render partial: partial, object: x
      end
      return
    else
      resources = x.dup.collect { |m| render_to_string(partial: m.to_partial_path,
                                                   locals: {m.class.model_name.i18n_key => m}) }
      count = resources.count
      total_count = x.respond_to?(:unscope) ? x.unscope(:limit, :offset).count : count
      attrs = {count: count,
               total_count: total_count
              }
      links = {}
      if href
        links['self'] =  {href: href, type: 'application/json'}
        if params[:page]
          page = params[:page].to_i
          page_size = params[:page_size].to_i || x.collection_page_size
          total_pages = (total_count.to_f / page_size.to_f).ceil
          last_page = total_pages - 1
          prev_page = page > 0 && page - 1
          next_page = page < last_page && page + 1
          links['first_page'] = {href: url_for(params.merge(page: 0)), type: 'application/json'}
          links['last_page'] = {href: url_for(params.merge(page: last_page)), type: 'application/json'}
          links['previous_page'] = {href: url_for(params.merge(page: prev_page)), type: 'application/json'} if prev_page
          links['next_page'] = {href: url_for(params.merge(page: next_page)), type: 'application/json'} if next_page
        end
      end
      attrs['_links'] = links if links
      attrs['page'] = page if page
      attrs['page_size'] = page_size if page_size
      attrs['total_pages'] = total_pages if total_pages
      render text: '{"_collection":{"resources":[' + resources.join(',') + ']' +
        (attrs.collect do |k, v|
           ',"' + k.to_s + '":' + v.to_json
         end).join('') +
        '}}'
    end
  end
def api_render ( x , new : false , href : url_for ( params ) , override_partial : false ) if ! x . is_a? ( Array ) && ! ( defined? ( ActiveRecord ) && x . is_a? ( ActiveRecord :: Relation ) ) partial = override_partial || x . to_partial_path if new render partial : partial , object : x , status : 201 , location : x else render partial : partial , object : x end return else resources = x . dup . collect { | m | render_to_string ( partial : m . to_partial_path , locals : { m . class . model_name . i18n_key => m } ) } count = resources . count total_count = x . respond_to? ( :unscope ) ? x . unscope ( :limit , :offset ) . count : count attrs = { count : count , total_count : total_count } links = { } if href links [ 'self' ] = { href : href , type : 'application/json' } if params [ :page ] page = params [ :page ] . to_i page_size = params [ :page_size ] . to_i || x . collection_page_size total_pages = ( total_count . to_f / page_size . to_f ) . ceil last_page = total_pages - 1 prev_page = page > 0 && page - 1 next_page = page < last_page && page + 1 links [ 'first_page' ] = { href : url_for ( params . merge ( page : 0 ) ) , type : 'application/json' } links [ 'last_page' ] = { href : url_for ( params . merge ( page : last_page ) ) , type : 'application/json' } links [ 'previous_page' ] = { href : url_for ( params . merge ( page : prev_page ) ) , type : 'application/json' } if prev_page links [ 'next_page' ] = { href : url_for ( params . merge ( page : next_page ) ) , type : 'application/json' } if next_page end end attrs [ '_links' ] = links if links attrs [ 'page' ] = page if page attrs [ 'page_size' ] = page_size if page_size attrs [ 'total_pages' ] = total_pages if total_pages render text : '{"_collection":{"resources":[' + resources . join ( ',' ) + ']' + ( attrs . collect do | k , v | ',"' + k . to_s + '":' + v . to_json end ) . join ( '' ) + '}}' end end
This is the main rendering function in Ocean.
This is the main rendering function in Ocean .
x
new
href
url_for
params
override_partial