Module: Rabl::Partials
Constant Summary
Constants included from Helpers
Instance Method Summary collapse
-
#fetch_source(file, options = {}) ⇒ Object
Returns source for a given relative file fetch_source(“show”, :view_path => “…”) => “…contents…”.
-
#object_to_hash(object, options = {}, &block) ⇒ Object
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 } => { … } object_to_hash(, :source => “…”) { attribute :full_name } => { … } options must have :source (rabl file contents) options can have :source_location (source filename).
-
#partial(file, options = {}, &block) ⇒ Object
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.
Methods included from Helpers
#collection_root_name, #context_scope, #data_name, #data_object, #data_object_attribute, #determine_object_root, #escape_output, #fetch_result_from_cache, #is_collection?, #is_name_value?, #is_object?, #object_root_name, #template_cache_configured?
Instance Method Details
#fetch_source(file, options = {}) ⇒ Object
Returns source for a given relative file fetch_source(“show”, :view_path => “…”) => “…contents…”
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rabl/partials.rb', line 32 def fetch_source(file, ={}) view_paths = Array([:view_path]) + Array(Rabl.configuration.view_paths) Rabl.source_cache(file, view_paths) do file_path = if defined?(Padrino) && context_scope.respond_to?(:settings) && context_scope.respond_to?(:resolve_template) fetch_padrino_source(file, ) elsif defined?(Rails) && context_scope.respond_to?(:view_paths) _view_paths = view_paths + Array(context_scope.view_paths.to_a) fetch_rails_source(file, ) || fetch_manual_template(_view_paths, file) elsif defined?(Sinatra) && context_scope.respond_to?(:settings) fetch_sinatra_source(file, ) 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] end end |
#object_to_hash(object, options = {}, &block) ⇒ Object
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 } => { … } object_to_hash(, :source => “…”) { attribute :full_name } => { … } options must have :source (rabl file contents) options can have :source_location (source filename)
23 24 25 26 27 28 |
# File 'lib/rabl/partials.rb', line 23 def object_to_hash(object, ={}, &block) return object if object.nil? return [] if is_collection?(object) && object.blank? # empty collection = .reverse_merge(:format => "hash", :view_path => @_view_path, :root => ([:root] || false)) Rabl::Engine.new([:source], ).render(@_scope, :object => object, :locals => [:locals], &block) end |
#partial(file, options = {}, &block) ⇒ Object
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
9 10 11 12 13 14 15 |
# File 'lib/rabl/partials.rb', line 9 def partial(file, ={}, &block) raise ArgumentError, "Must provide an :object option to render a partial" unless .has_key?(:object) object, view_path = .delete(:object), [:view_path] || @_view_path source, location = self.fetch_source(file, :view_path => view_path) = .merge(:source => source, :source_location => location, :template => file) self.object_to_hash(object, , &block) end |