Returns source for a given relative file #fetch_source("show", :view_path => "...") => "...contents..."
# File lib/rabl/partials.rb, line 32 def fetch_source(file, options={}) view_paths = Array(options[:view_path]) + Array(Rabl.configuration.view_paths) Rabl.source_cache(file, view_paths) do file_path = if defined?(Padrino) && context_scope.respond_to?(:settings) fetch_padrino_source(file, options) elsif defined?(Rails) && context_scope.respond_to?(:view_paths) _view_paths = view_paths + Array(context_scope.view_paths.to_a) fetch_rails_source(file, options) || fetch_manual_template(_view_paths, file) elsif defined?(Sinatra) && context_scope.respond_to?(:settings) fetch_sinatra_source(file, options) else # generic template resolution fetch_manual_template(view_paths, file) end unless File.exist?(file_path.to_s) raise "Cannot find rabl template '#{file}' within registered (#{view_paths.map(&:to_s).inspect}) view paths!" end [File.read(file_path.to_s), file_path.to_s] if file_path end end
Returns a hash based representation of any data object given ejs template
block #object_to_hash(@user) {
attribute :full_name } => { ... } #object_to_hash(@user,
:source => "...") { attribute :full_name } => { ... }
user">object_to_hash( at
# File lib/rabl/partials.rb, line 23
def object_to_hash(object, options={}, &block)
return object unless is_object?(object) || is_collection?(object)
return [] if is_collection?(object) && object.blank? # empty collection
engine_options = options.reverse_merge(:format => "hash", :view_path => @_view_path, :root => (options[:root] || false))
Rabl::Engine.new(options[:source], engine_options).render(@_scope, :object => object, &block)
end
Renders a partial hash based on another rabl template partial("users/show", :object => @user) options must have :object options can have :view_path, :child_root, :root
# File lib/rabl/partials.rb, line 9 def partial(file, options={}, &block) raise ArgumentError, "Must provide an :object option to render a partial" unless options.has_key?(:object) object, view_path = options.delete(:object), options[:view_path] || @_view_path source, location = self.fetch_source(file, :view_path => view_path) engine_options = options.merge(:source => source, :source_location => location) self.object_to_hash(object, engine_options, &block) end