Class: Goat::Page

Inherits:
Object show all
Includes:
FlashHelper
Defined in:
lib/goat.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FlashHelper

#flash

Constructor Details

#initialize(app) ⇒ Page

Returns a new instance of Page.



780
781
782
783
784
785
# File 'lib/goat.rb', line 780

def initialize(app)
  @request = app.request
  @channel = Channel.new
  @id = 'pg_' + String.random(10)
  @params = IndifferentHash.from_hash(request.params)
end

Instance Attribute Details

#canvasObject (readonly)

Returns the value of attribute canvas.



756
757
758
# File 'lib/goat.rb', line 756

def canvas
  @canvas
end

#channelObject (readonly)

Returns the value of attribute channel.



756
757
758
# File 'lib/goat.rb', line 756

def channel
  @channel
end

#idObject (readonly)

Returns the value of attribute id.



756
757
758
# File 'lib/goat.rb', line 756

def id
  @id
end

#layoutObject (readonly)

Returns the value of attribute layout.



756
757
758
# File 'lib/goat.rb', line 756

def layout
  @layout
end

#paramsObject (readonly)

Returns the value of attribute params.



756
757
758
# File 'lib/goat.rb', line 756

def params
  @params
end

#requestObject (readonly)

Returns the value of attribute request.



756
757
758
# File 'lib/goat.rb', line 756

def request
  @request
end

Class Method Details

.disable_live_updatesObject



760
# File 'lib/goat.rb', line 760

def self.disable_live_updates; @live_updates = false; end

.enable_live_updatesObject



761
# File 'lib/goat.rb', line 761

def self.enable_live_updates; @live_updates = true; end

.handle_request(app) ⇒ Object



772
773
774
775
776
777
778
# File 'lib/goat.rb', line 772

def self.handle_request(app)
  pg = self.new(app)
  
  raise "Page #{pg} has no id: did you forget to call super()?" unless pg.id

  pg.response
end

.live_updates_enabled?Boolean

Returns:

  • (Boolean)


762
# File 'lib/goat.rb', line 762

def self.live_updates_enabled?; @live_updates != false; end

Instance Method Details

#_domObject



805
# File 'lib/goat.rb', line 805

def _dom; self.dom; end

#empty_responseObject



787
788
789
# File 'lib/goat.rb', line 787

def empty_response
  Rack::Response.new
end

#erb(name, opts = {}, &blk) ⇒ Object



766
767
768
769
770
# File 'lib/goat.rb', line 766

def erb(name, opts={}, &blk)
  e = ERBRunner.new(@request, @response, @params)
  e.delegate = self
  e.erb(name, {:partial => false}.merge(opts), &blk)
end

#erb=(erb) ⇒ Object

TODO make it work properly, but not support embenned components



863
864
# File 'lib/goat.rb', line 863

def erb=(erb); @erb = erb; #TODO make it work properly, but not support embenned components
end

#haltObject

Raises:



791
792
793
# File 'lib/goat.rb', line 791

def halt
  raise Halt.new(empty_response)
end

#htmlObject



828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/goat.rb', line 828

def html
  layout = self.layout

  canvas = PageCanvas.new
  canvas.title = @title

  if @erb
    canvas.html = html_from_erb
  elsif methods.include?('dom')
    inject_html_from_dom(canvas)
  else
    raise "You should over-ride Page#dom or supply an erb template"
  end

  render_to_layout(canvas, layout)
end

#html_from_erbObject



807
808
809
# File 'lib/goat.rb', line 807

def html_from_erb
  html = self.erb(@erb, :layout => false)
end

#inject_html_from_dom(canvas) ⇒ Object



811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
# File 'lib/goat.rb', line 811

def inject_html_from_dom(canvas)
  exp = nil
  helper = ExpansionHelper.new

  Dynamic.let(:expander => helper) do
    exp = DOMTools.expanded_dom(self._dom)
  end

  distiller = DOMDistiller.new(exp, helper.components)

  canvas.html = DOMTools::HTMLBuilder.new(exp).html
  canvas.style << distiller.style
  canvas.script << distiller.script

  EM.next_tick { register_page(distiller) }
end

#register_page(distiller) ⇒ Object



795
796
797
798
799
800
801
802
803
# File 'lib/goat.rb', line 795

def register_page(distiller)
  live = distiller.all_components.select(&:live_enabled?)
  pg_spec = live.map do |c|
    c.pgid = @id
    c.skel
  end

  StateSrvClient.register_page(@id, self.user, pg_spec)
end

#render_to_layout(canvas, layout) ⇒ Object



854
855
856
857
858
859
860
861
# File 'lib/goat.rb', line 854

def render_to_layout(canvas, layout)
  layout ||= File.join(File.dirname(__FILE__), 'views/plain_layout.erb')

  erb(layout,
    :locals => {:page => self, :canvas => canvas},
    # don't want a layout in our layout so we can layout while we layout
    :layout => false) { canvas.html }
end

#responseObject

def render_component©

canvas.components << c
c.html

end



850
851
852
# File 'lib/goat.rb', line 850

def response
  Rack::Response.new(self.html, 200, {})
end

#title=(title) ⇒ Object



865
# File 'lib/goat.rb', line 865

def title=(title); @title = title; end

#userObject



867
# File 'lib/goat.rb', line 867

def user; end