Class: Trellis::Page

Inherits:
Object show all
Includes:
Nokogiri::XML
Defined in:
lib/trellis/trellis.rb

Overview

– Page – Represents a Web Page in a Trellis Application. A Page can contain multiple components and it defines a template or view either as an external file (xml, xhtml, other) or programmatically using Markaby or HAML A Trellis Page contains listener methods to respond to events trigger by components in the same page or other pages

Constant Summary collapse

@@subclasses =
Hash.new
@@template_registry =
Hash.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePage

TODO this is Ugly.… should not do it in initialize since it’ll require super in child classes



609
610
611
612
613
614
# File 'lib/trellis/trellis.rb', line 609

def initialize # TODO this is Ugly.... should not do it in initialize since it'll require super in child classes
  self.class.stateful_components.each do |id_component|
    id_component[1].enhance_page(self, id_component[0])
  end
  @logger = Application.logger
end

Instance Attribute Details

#applicationObject

Returns the value of attribute application.



512
513
514
# File 'lib/trellis/trellis.rb', line 512

def application
  @application
end

#loggerObject

Returns the value of attribute logger.



512
513
514
# File 'lib/trellis/trellis.rb', line 512

def logger
  @logger
end

#paramsObject

Returns the value of attribute params.



512
513
514
# File 'lib/trellis/trellis.rb', line 512

def params
  @params
end

#pathObject

Returns the value of attribute path.



512
513
514
# File 'lib/trellis/trellis.rb', line 512

def path
  @path
end

Class Method Details

.domObject



569
570
571
572
573
574
575
576
577
# File 'lib/trellis/trellis.rb', line 569

def self.dom
  # try to reload the template if it wasn't found on during inherited
  # since it could have failed if the app was not mounted as root
  unless @template
    Application.logger.debug "parsed template was no loaded, attempting to load..."
    locate_template(self)
  end 
  @template
end

.formatObject



583
584
585
# File 'lib/trellis/trellis.rb', line 583

def self.format
  @format
end

.get_page(sym) ⇒ Object



601
602
603
# File 'lib/trellis/trellis.rb', line 601

def self.get_page(sym)
  @@subclasses[sym]
end

.inherited(child) ⇒ Object

:nodoc:



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/trellis/trellis.rb', line 514

def self.inherited(child) #:nodoc:
  sym = child.class_to_sym
  @@subclasses[sym] = child if sym
  
  child.instance_variable_set(:@name, child.underscore_class_name)
  child.attr_array(:pages, :create_accessor => false)
  child.attr_array(:components)
  child.attr_array(:stateful_components)
  child.attr_array(:persistents)
  child.class_attr_accessor :url_root
  child.class_attr_accessor :name
  child.class_attr_accessor :router
  child.meta_def(:add_stateful_component) { |tid,clazz| @stateful_components << [tid,clazz] }
 
  locate_template child        
  super
end

.inject_dependent_pages(target) ⇒ Object



672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/trellis/trellis.rb', line 672

def self.inject_dependent_pages(target)
  @pages.each do |sym|
    clazz = Page.get_page(sym)
    # if the injected page class is not found
    # throw an exception in production mode or
    # dynamically generate a page in development mode
    unless clazz
      target_class = sym.to_s.camelcase(:upper)
      Application.logger.debug "creating stand in page class #{target_class} for symbol #{sym}"

      clazz = Page.create_child(target_class) do

        def method_missing(sym, *args, &block)
          Application.logger.debug "faking response to #{sym}(#{args}) from #{self} an instance of #{self.class}"
          self
        end

        template do
          thtml {
            head { title "Stand-in Page" }
            body { h1 { text %[Stand-in Page for <trellis:value name="page_name"/>] }}
          }
        end
      end
      Page.subclasses[sym] = clazz
    end
    
    Application.logger.debug "injecting an instance of #{clazz} for #{sym}"
    target.instance_variable_set("@#{sym}".to_sym, clazz.new)
    target.meta_def(sym) { instance_variable_get("@#{sym}") }
  end
end

.layoutObject



532
533
534
# File 'lib/trellis/trellis.rb', line 532

def self.layout
  @layout
end

.pages(*syms) ⇒ Object



587
588
589
# File 'lib/trellis/trellis.rb', line 587

def self.pages(*syms)
  @pages = @pages | syms
end

.persistent(*fields) ⇒ Object



596
597
598
599
# File 'lib/trellis/trellis.rb', line 596

def self.persistent(*fields)
  instance_attr_accessor fields
  @persistents = @persistents | fields    
end

.route(path) ⇒ Object



591
592
593
594
# File 'lib/trellis/trellis.rb', line 591

def self.route(path)
  router = Router.new(:path => path, :page => self)
  self.instance_variable_set(:@router, router)
end

.subclassesObject



605
606
607
# File 'lib/trellis/trellis.rb', line 605

def self.subclasses
  @@subclasses
end

.template(body = nil, options = nil, &block) ⇒ Object



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'lib/trellis/trellis.rb', line 536

def self.template(body = nil, options = nil, &block)
  @format = (options[:format] if options) || :html
  @layout = (options[:layout] if options)
  if block_given?
    mab = Markaby::Builder.new({}, self, &block)
    html = mab.to_s
  else
    case @format
    when :haml
      html = Haml::Engine.new(body).render
    when :textile  
      html = RedCloth.new(body).to_html
    when :markdown
      if @layout
        html = BlueCloth.new(body).to_html
      else
        html = Markaby.build { thtml { body { text "#{BlueCloth.new(body).to_html}" } }}
      end
    else # assume the body is (x)html, also eruby is treated as (x)html at this point
      html = body
    end
  end
  
  # hack to prevent nokogiri form stripping namespace prefix on xml fragments
  if @layout 
    html = %[<div id="trellis_remove" xmlns:trellis="http://trellisframework.org/schema/trellis_1_0_0.xsd">#{html}</div>]
  end
  
  @template = Nokogiri::XML(html)

  find_components
end

.template_registryObject



705
706
707
# File 'lib/trellis/trellis.rb', line 705

def self.template_registry
  @@template_registry
end

.to_xml(options = {}) ⇒ Object



579
580
581
# File 'lib/trellis/trellis.rb', line 579

def self.to_xml(options = {})
  options[:no_declaration] ? dom.to_xml(:save_with => Node::SaveOptions::NO_DECLARATION) : dom.to_xml
end

Instance Method Details

#inject_dependent_pagesObject

inject an instance of each of the injected pages classes as instance variables of the current page



668
669
670
# File 'lib/trellis/trellis.rb', line 668

def inject_dependent_pages
  self.class.inject_dependent_pages(self)
end

#load_page_session_information(session) ⇒ Object



645
646
647
648
# File 'lib/trellis/trellis.rb', line 645

def load_page_session_information(session)
  load_persistent_fields_data(session)
  load_stateful_components_data(session)
end

#process_event(event, value, source, session) ⇒ Object



620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# File 'lib/trellis/trellis.rb', line 620

def process_event(event, value, source, session)
  method = source ? "on_#{event}_from_#{source}" : "on_#{event}"

  # execute the method passing the value if necessary
  method_result = value ? send(method.to_sym, Rack::Utils.unescape(value)) : send(method.to_sym)

  # determine navigation flow based on the return value of the method call
  if method_result
    if method_result.kind_of?(Trellis::Page)
      page = method_result
      # save the current page persistent information
      if self != method_result
        save_page_session_information(session)
        page.inject_dependent_pages
        page.call_if_provided(:before_load)
      end

      # save the persistent information before rendering a response
      page.save_page_session_information(session)
    end
  end   
  
  method_result
end

#redirect(path, status = nil) ⇒ Object



616
617
618
# File 'lib/trellis/trellis.rb', line 616

def redirect(path, status=nil)
  Redirect.new(path, status)
end

#renderObject



655
656
657
658
659
660
# File 'lib/trellis/trellis.rb', line 655

def render  
  call_if_provided(:before_render)
  result = Renderer.new(self).render
  call_if_provided(:after_render) 
  result      
end

#render_partial(name, locals = {}) ⇒ Object



662
663
664
# File 'lib/trellis/trellis.rb', line 662

def render_partial(name, locals={})
  Renderer.new(self).render_partial(name, locals)
end

#save_page_session_information(session) ⇒ Object



650
651
652
653
# File 'lib/trellis/trellis.rb', line 650

def save_page_session_information(session)
  save_persistent_fields_data(session)
  save_stateful_components_data(session)           
end