Module: ShareLayouts::Helpers::ActionView

Defined in:
lib/share_layouts/helpers/action_view.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/share_layouts/helpers/action_view.rb', line 5

def self.included(base)
  base.class_eval do
    
    def radiant_layout(name = @radiant_layout)
      page = find_page
      assign_attributes!(page, name)
      page.build_parts_from_hash!(extract_captures) 
      page.render
    end
    
    def assign_attributes!(page, name = @radiant_layout)
      page.layout = Layout.find_by_name(name) || page.layout
      page.title = @title || @content_for_title || page.title || ''
      page.breadcrumb = @breadcrumb || @content_for_breadcrumb || page.breadcrumb || page.title
      page.breadcrumbs = @breadcrumbs || @content_for_breadcrumbs || nil
      page.url = request.path
      page.slug = page.url.split("/").last
      page.published_at ||= Time.now 
      page.request = request
      page.response = response
    end
    
    def extract_captures
      variables = instance_variables.grep(/@content_for_/)
      variables.inject({}) do |h, var|
        var =~ /^@content_for_(.*)$/
        key = $1.intern
        key = :body if key == :layout
        unless key == :title || key == :breadcrumbs
          h[key] = instance_variable_get(var)
        end
        h
      end
    end
    
    def find_page
      page = Page.find_by_url(request.path) rescue nil
      page.is_a?(RailsPage) ? page : RailsPage.new(:class_name => "RailsPage")
    end
    
  end
end