Class: Trellis::Page

Inherits:
Object show all
Extended by:
Advisable
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



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

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.



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

def application
  @application
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#pathObject

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.apply_filter(name, options = {}) ⇒ Object



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

def self.apply_filter(name, options = {})
  filter = Application.filters[name]
  methods = options[:to] == :all ? self.public_instance_methods(false) : [options[:to]]
  methods << :get if options[:to] == :all
  methods = methods - [:before_load, :after_load, :before_render, :after_render, :underscore_class_name]
  Application.logger.debug "in #{self} applying filter #{name} to methods: #{methods.join(', ')}"
  
  methods.each do |method|
    case filter.kind
    when :around
      around method do |target| 
        filter.block.call(self) { target.call } 
      end
    when :before
      before method do filter.block.call(self) end
    when :after 
      after method do filter.block.call(self) end
    end
  end
end

.domObject



589
590
591
592
593
594
595
596
597
# File 'lib/trellis/trellis.rb', line 589

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



603
604
605
# File 'lib/trellis/trellis.rb', line 603

def self.format
  @format
end

.get_page(sym) ⇒ Object



642
643
644
# File 'lib/trellis/trellis.rb', line 642

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

.inherited(child) ⇒ Object

:nodoc:



534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
# File 'lib/trellis/trellis.rb', line 534

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



715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/trellis/trellis.rb', line 715

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



552
553
554
# File 'lib/trellis/trellis.rb', line 552

def self.layout
  @layout
end

.pages(*syms) ⇒ Object



607
608
609
# File 'lib/trellis/trellis.rb', line 607

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

.persistent(*fields) ⇒ Object



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

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

.route(path) ⇒ Object



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

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

.subclassesObject



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

def self.subclasses
  @@subclasses
end

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



556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/trellis/trellis.rb', line 556

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



748
749
750
# File 'lib/trellis/trellis.rb', line 748

def self.template_registry
  @@template_registry
end

.to_xml(options = {}) ⇒ Object



599
600
601
# File 'lib/trellis/trellis.rb', line 599

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

Instance Method Details

#getObject



657
# File 'lib/trellis/trellis.rb', line 657

def get; self; end

#inject_dependent_pagesObject

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



711
712
713
# File 'lib/trellis/trellis.rb', line 711

def inject_dependent_pages
  self.class.inject_dependent_pages(self)
end

#load_page_session_information(session) ⇒ Object



688
689
690
691
# File 'lib/trellis/trellis.rb', line 688

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

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



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/trellis/trellis.rb', line 663

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



659
660
661
# File 'lib/trellis/trellis.rb', line 659

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

#renderObject



698
699
700
701
702
703
# File 'lib/trellis/trellis.rb', line 698

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

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



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

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

#save_page_session_information(session) ⇒ Object



693
694
695
696
# File 'lib/trellis/trellis.rb', line 693

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