module ExtScaffoldCoreExtensions
  module Array

    # return Ext compatible JSON form of an Array, i.e.:
    #  {"results": n, 
    #   "posts": [ {"id": 1, "title": "First Post",
    #               "body": "This is my first post.",
    #               "published": true, ... },
    #               ...
    #            ]
    #  }
    def to_ext_json(options = {})
      if given_class = options.delete(:class)
        element_class = (given_class.is_a?(Class) ? given_class : given_class.to_s.classify.constantize)
      else
        element_class = first.class
      end
      element_count = options.delete(:count) || self.length

      { :results => element_count, element_class.to_s.underscore.pluralize => self }.to_json(options)
    end

  end
end

module ExtScaffoldCoreExtensions
  module Array

    # return Ext compatible JSON form of an Array, i.e.:
    #  {"results": n, 
    #   "posts": [ {"id": 1, "title": "First Post",
    #               "body": "This is my first post.",
    #               "published": true, ... },
    #               ...
    #            ]
    #  }
    def to_ext_json(options = {})
      if given_class = options.delete(:class)
        element_class = (given_class.is_a?(Class) ? given_class : given_class.to_s.classify.constantize)
      else
        element_class = first.class
      end
      element_count = options.delete(:count) || self.length

      # Only apply the ActiveRecord JSON options
      if (options[:ar_options])
        r2 = self.map { | i | i.to_json(options[:ar_options]) }
        r = "{ \"results\" : #{element_count}, \"#{element_class.to_s.underscore.pluralize}\" : [#{r2.join(", ")}] }" 
      else
        r = { :results => element_count, element_class.to_s.underscore.pluralize => self }.to_json(options)
      end
      return r
    end

  end
end

Allow ActiveRecord to_json options to be specified, like :only or :methods (reduces JSON payload)
Allow ActiveRecord to_json options to be specified, like :only or :methods (reduces JSON payload)
