class Ruhoh::Views::MasterView
Attributes
Public Class Methods
# File lib/ruhoh/views/master_view.rb, line 9 def initialize(ruhoh, pointer_or_data) @ruhoh = ruhoh define_resource_collection_namespaces(ruhoh) if pointer_or_data['id'] @pointer = pointer_or_data @page = collection.find(pointer_or_data) unless @page raise "Could not find the page with pointer: #{ pointer_or_data }" + "Finding this page is required because an 'id' key is being passed." end @page_data = @page.data.dup # legacy...working on removing this.. else @content = pointer_or_data['content'] @page_data = pointer_or_data @pointer = pointer_or_data end end
Public Instance Methods
# File lib/ruhoh/views/master_view.rb, line 48 def collection @pointer["resource"] ? __send__(@pointer["resource"]) : nil end
Public: Formats the path to the compiled file based on the URL.
Returns: [String] The relative path to the compiled file for this page.
# File lib/ruhoh/views/master_view.rb, line 120 def compiled_path path = @ruhoh.compiled_path(@page_data['url']) path = "index.html" if path.empty? path += '/index.html' unless path =~ /\.\w+$/ path end
# File lib/ruhoh/views/master_view.rb, line 60 def content render(@content || page.content) end
# File lib/ruhoh/views/master_view.rb, line 86 def debug(sub_context) Ruhoh::Friend.say { yellow "?debug:" magenta sub_context.class cyan sub_context.inspect } "<pre>#{sub_context.class}\n#{sub_context.pretty_inspect}</pre>" end
# File lib/ruhoh/views/master_view.rb, line 108 def gist @gist ||= Ruhoh::Views::Helpers::SimpleProxy.new({ matcher: /^[0-9]+$/, function: -> input { "<script src=\"https://gist.github.com/#{ input }.js\"></script>" } }) end
Delegate page
to the kind of resource this view is modeling.
# File lib/ruhoh/views/master_view.rb, line 44 def page collection ? collection.find(@pointer) : nil end
# File lib/ruhoh/views/master_view.rb, line 72 def page_collections @ruhoh.collections.acting_as_pages.map do |a| @ruhoh.collection(a) end end
NOTE: newline ensures proper markdown rendering.
# File lib/ruhoh/views/master_view.rb, line 65 def partial(name) partial = partials.find(name.to_s) partial ? partial.process.to_s + "\n" : Ruhoh::Friend.say { yellow "partial not found: '#{name}'" } end
# File lib/ruhoh/views/master_view.rb, line 96 def raw_code(sub_context) code = sub_context.gsub('{', '{').gsub('}', '}').gsub('<', '<').gsub('>', '>').gsub('_', "_") "<pre><code>#{code}</code></pre>\n" end
# File lib/ruhoh/views/master_view.rb, line 39 def render_content render('{{{page.content}}}') end
# File lib/ruhoh/views/master_view.rb, line 29 def render_full if page_layouts.empty? render_content else page_layouts.drop(1).reduce(render(page_layouts.first.content)) do |c, l| render(l.content, :content => c) end end end
# File lib/ruhoh/views/master_view.rb, line 78 def to_json(sub_context) sub_context.to_json end
# File lib/ruhoh/views/master_view.rb, line 82 def to_pretty_json(sub_context) JSON.pretty_generate(sub_context) end
My Post Title ===> my-post-title Handy for transforming ids into css-classes in your views. @returns
# File lib/ruhoh/views/master_view.rb, line 104 def to_slug(sub_context) Ruhoh::StringFormat.clean_slug(sub_context) end
# File lib/ruhoh/views/master_view.rb, line 52 def urls @ruhoh.collections.url_endpoints.merge({ 'base_path' => @ruhoh.config.base_path, 'production' => @ruhoh.config["production_url"], 'production_url' => @ruhoh.config["production_url"] }) end
Protected Instance Methods
# File lib/ruhoh/views/master_view.rb, line 129 def page_layouts return @page_layouts unless @page_layouts.nil? layout = if @page_data['layout'] layouts.find(@page_data['layout'], :all => true) or raise "Layout does not exist: #{@page_data['layout']}" elsif @page_data['layout'] != false # try default layouts.find(@pointer['resource'], :all => true) end @page_layouts = if layout.nil? [] else page_layouts = [layout] until layout.layout.nil? layout = layouts.find(layout.layout) or raise "Layout does not exist: #{layout.layout}" raise "Layout cycle detected when rendering #{@pointer}: \n #{ (page_layouts<<layout).map{|l| l.pointer["realpath"]}.join("\n") }" if page_layouts.include?(layout) page_layouts << layout end page_layouts end end
Private Instance Methods
Dynamically add method proxies to resource collections This is how collections are accessed throughout mustache’s global context. Also support calling ?to_<resource> contextual block helpers
# File lib/ruhoh/views/master_view.rb, line 161 def define_resource_collection_namespaces(ruhoh) ruhoh.collections.all.each do |method_name| (class << self; self; end).class_eval do define_method(method_name) do load_collection_view_for(method_name.to_s) end define_method("to_#{method_name}") do |*args| resource_generator_for(method_name, *args) end end end end
Load collection views dynamically when calling a resources name. Uses method_missing to catch calls to resource namespace. @returns for the calling resource.
# File lib/ruhoh/views/master_view.rb, line 178 def load_collection_view_for(resource) view = @ruhoh.collection(resource) view.master = self view end
Transforms an Array or String of resource ids into their corresponding resource objects. Uses method_missing to catch calls to ‘to_<resource>` contextual helper. @returns the resource modelView objects or raw data hash.
# File lib/ruhoh/views/master_view.rb, line 187 def resource_generator_for(resource, sub_context) collection_view = load_collection_view_for(resource) Array(sub_context).map { |id| collection_view.find(id) }.compact end