Class: ChiliProject::Liquid::Tags::ChildPages

Inherits:
Tag
  • Object
show all
Defined in:
lib/chili_project/liquid/tags/child_pages.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ ChildPages

Returns a new instance of ChildPages


17
18
19
20
21
22
23
24
25
26
27
# File 'lib/chili_project/liquid/tags/child_pages.rb', line 17

def initialize(tag_name, markup, tokens)
  markup = markup.strip.gsub(/["']/, '')
  if markup.present?
    tag_args = markup.split(',')
    @args, @options = extract_macro_options(tag_args, :parent)
  else
    @args = []
    @options = {}
  end
  super
end

Instance Method Details

#render(context) ⇒ Object

Raises:


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/chili_project/liquid/tags/child_pages.rb', line 29

def render(context)
  # inside of a project
  @project = Project.find(context['project'].identifier) if context['project'].present?

  if @args.present?
    page_name = @args.first.to_s
    cross_project_page = page_name.include?(':')

    page = Wiki.find_page(page_name, :project => (cross_project_page ? nil : @project))
  # FIXME: :object and :attribute should be variables, not registers
  elsif context.registers[:object].is_a?(WikiContent)
    page = context.registers[:object].page
    page_name = page.title
  elsif @project
    return render_all_pages(context)
  else
    raise TagError.new('With no argument, this tag can be called from projects only.')
  end

  raise TagError.new("No such page '#{page_name}'") if page.nil? || !User.current.allowed_to?(:view_wiki_pages, page.wiki.project)
  pages = ([page] + page.descendants).group_by(&:parent_id)
  context.registers[:view].render_page_hierarchy(pages, @options[:parent] ? page.parent_id : page.id)
end