Class: Trellis::Page

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

TEMPLATE_FORMATS =
[:html, :xhtml, :haml, :textile, :markdown]
@@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 no do it in initialize since it’ll require super in child classes



397
398
399
400
401
402
# File 'lib/trellis/trellis.rb', line 397

def initialize # TODO this is Ugly.... should no 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

#loggerObject

Returns the value of attribute logger.



323
324
325
# File 'lib/trellis/trellis.rb', line 323

def logger
  @logger
end

#paramsObject

Returns the value of attribute params.



323
324
325
# File 'lib/trellis/trellis.rb', line 323

def params
  @params
end

#pathObject

Returns the value of attribute path.



323
324
325
# File 'lib/trellis/trellis.rb', line 323

def path
  @path
end

Class Method Details

.get_page(sym) ⇒ Object



389
390
391
# File 'lib/trellis/trellis.rb', line 389

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

.inherited(child) ⇒ Object

:nodoc:



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/trellis/trellis.rb', line 325

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.class_attr_accessor :layout
  child.meta_def(:add_stateful_component) { |tid,clazz| @stateful_components << [tid,clazz] }
 
  locate_template child        
  super
end

.inject_dependent_pages(target) ⇒ Object



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/trellis/trellis.rb', line 456

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
          xhtml_strict {
            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

.pages(*syms) ⇒ Object



375
376
377
# File 'lib/trellis/trellis.rb', line 375

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

.parsed_templateObject



365
366
367
368
369
370
371
372
373
# File 'lib/trellis/trellis.rb', line 365

def self.parsed_template
  # 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

.persistent(*fields) ⇒ Object



384
385
386
387
# File 'lib/trellis/trellis.rb', line 384

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

.route(path) ⇒ Object



379
380
381
382
# File 'lib/trellis/trellis.rb', line 379

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

.subclassesObject



393
394
395
# File 'lib/trellis/trellis.rb', line 393

def self.subclasses
  @@subclasses
end

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



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/trellis/trellis.rb', line 344

def self.template(body = nil, options = nil, &block)
  format = options[:format] 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
      html = BlueCloth.new(body).to_html
    else # assume the body is (x)html 
      html = body
    end
  end
  @template = Hpricot.XML(html)
  find_components
end

.template_registryObject



489
490
491
# File 'lib/trellis/trellis.rb', line 489

def self.template_registry
  @@template_registry
end

Instance Method Details

#inject_dependent_pagesObject

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



452
453
454
# File 'lib/trellis/trellis.rb', line 452

def inject_dependent_pages
  self.class.inject_dependent_pages(self)
end

#load_page_session_information(session) ⇒ Object



433
434
435
436
# File 'lib/trellis/trellis.rb', line 433

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

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



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/trellis/trellis.rb', line 404

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

  # execute the method passing the value if necessary 
  unless value
    method_result = send method.to_sym
  else
    method_result = send method.to_sym, value
  end

  # 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

#renderObject



443
444
445
446
447
448
# File 'lib/trellis/trellis.rb', line 443

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

#save_page_session_information(session) ⇒ Object



438
439
440
441
# File 'lib/trellis/trellis.rb', line 438

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