Class: DcBookRenderer

Inherits:
DcRenderer
  • Object
show all
Includes:
DcApplicationHelper
Defined in:
app/helpers/dc_book_renderer.rb

Instance Method Summary collapse

Instance Method Details

#chapterObject

chapter



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/dc_book_renderer.rb', line 62

def chapter
  html = "<div class='#{@opts[:div]}'>"
#  texts   = DcBookChapter.where(_id: @parent.params[:book_id], active: true).sort(chapter: 1).to_a
  book    = DcBook.find_by(link: @parent.params[:book_id])
  chapter = DcBookChapter.find_by(dc_book_id: book.id, link: @parent.params[:chapter_id])
  if chapter
    prev_chapter = DcBookChapter.where(dc_book_id: book._id, :chapter.lt => chapter.chapter)
                                .order_by(chapter: -1).limit(1).first
    next_chapter = DcBookChapter.where(dc_book_id: book._id, :chapter.gt => chapter.chapter)
                                .order_by(chapter: 1).limit(1).first
  else
    prev_chapter, next_chapter = nil, nil 
  end
#  
  if @opts[:edit_mode] > 1
    html << dc_link_for_create({ controller: 'cmsedit', table: 'dc_book_chapter;dc_book_text', ids: chapter._id, title: t('dc_book.new_text') }) 
    dc_add2_record_cookie( { "dc_book_text.author" => chapter.author } ) if chapter
  end
#   
  if chapter
    texts = chapter.dc_book_texts.where(active: true).to_a
    if texts.size > 0
      replies = chapter.dc_replies.where(active: true).order(created_at: 1).
                page(@parent.params[:page]).per(20)      
      versions = texts.inject([]) {|r,e| r << [e.version, e._id] }
# display specific version when text_id is specified      
      text = @parent.params[:text_id] ? chapter.dc_book_texts.find(@parent.params[:text_id]) : texts.last
      @parent.page_title = "#{book.title}-#{chapter.title}"
      html << @parent.render( partial: 'dc_book/chapter', 
                              locals: { chapter: chapter, replies: replies, text: text, versions: versions, 
                              prev_chapter: prev_chapter, next_chapter: next_chapter}, 
                              formats: [:html] )
    end
  end
  html << '</div>'
end

#listObject

List all books



33
34
35
36
37
38
39
40
41
# File 'app/helpers/dc_book_renderer.rb', line 33

def list
  html = "<div class='#{@opts[:div]}'>"
  if @opts[:edit_mode] > 1
    html << dc_link_for_create({ controller: 'cmsedit', table: 'dc_book', title: t('dc_book.new_book') }) 
  end
  books = DcBook.where(active: true).sort(title: 1).to_a
  html << @parent.render( partial: 'dc_book/list', locals: { books: books }, formats: [:html] )
  html << '</div>'
end

#render_htmlObject

render html



102
103
104
105
# File 'app/helpers/dc_book_renderer.rb', line 102

def render_html
  method = @parent.params[:method] || 'list'
  respond_to?(method) ? send(method) : "Error DcBook: Method #{method} doesn't exist!"
end

#tocObject

Table of contents



46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/dc_book_renderer.rb', line 46

def toc
  html = "<div class='#{@opts[:div]}'>"
  if @opts[:edit_mode] > 1
    dc_add2_record_cookie( { "dc_book_chapter.dc_book_id" => @parent.params[:book_id] } )
    html << dc_link_for_create({ controller: 'cmsedit', table: 'dc_book_chapter', title: t('dc_book.new_chapter') }) 
  end
  book = DcBook.find_by(link: @parent.params[:book_id])
  @parent.page_title = book.title
  chapters = DcBookChapter.where(dc_book_id: book._id, active: true).sort(chapter: 1).to_a
  html << @parent.render( partial: 'dc_book/toc', locals: { chapters: chapters, book: book }, formats: [:html] )
  html << '</div>'
end