Class: DocumentsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/documents_controller.rb

Instance Method Summary collapse

Instance Method Details

#archiveObject

Show all pages in any Document for a particular month/year /*path/archive/:month/:year

Raises:

  • (ActiveRecord::RecordNotFound)


28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/documents_controller.rb', line 28

def archive
  render :file => cache_file and return if cached_file?
  @document = Document.public.find_by_path(params[:path].join('/'))
  raise ActiveRecord::RecordNotFound if @document.nil?

 
  @documents = @document.archive_for(params[:month], params[:year]).paginate :page => params[:page], :per_page => Settings.documents.per_page
  render :template => view_for(:suffix => '_archive')
  cache_this_page!
end

#createObject

public facing creation of documents



61
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
98
99
100
101
102
103
104
105
# File 'app/controllers/documents_controller.rb', line 61

def create
  @document = Document.public.find(params[:id])

  begin
    do_human_test
  rescue
    flash.now[:notice] = 'You did not add up the numbers correctly, please try again.'

    new_document = Document.new
    new_document.body = params[:document][:body]

    eval("@new_#{params[:label]} = new_document")

    setup_view_environment
    render :template => view_for
    return
  end

  params[:document][:state] = nil # prevent auto approval hack (FIXME: use attr_protected)


  if @document.meta_definition_for(params[:label]).allowed? current_user, 'create'

    new_document = Document.new(params[:document])
    new_document.parent = @document
    new_document.label = params[:label]
    new_document.author = current_user || User.anonymous
    new_document.published_at = Time.now

    if new_document.save
      flash[:notice] = new_document.meta_definition.flash_messages['create'] || "Your #{params[:label]} has been saved"
      if new_document.meta_definition.notify_admins
        DocumentMailer.deliver_new_document(new_document)
      end
      redirect_to document_path(@document)
    else
      flash.now[:notice] = 'Could not be saved'
      eval("@new_#{params[:label]} = new_document")
      setup_view_environment
      render :template => view_for
    end
  else
    render :text => 'Not Allowed' and return
  end
end

#searchObject

Allows searching of pages in a folder /:permalink/search?search=hello



41
42
43
44
45
46
47
48
49
# File 'app/controllers/documents_controller.rb', line 41

def search
  params[:search] ||= {}
  @document = Document.public.find_by_path(params[:path].join('/'))
  params[:search].merge!(:parent_id => @document.id)
  params[:search][:state_eq] = 'published'
  @documents = Document.search(params[:search]).paginate :page => params[:page], :per_page => Settings.documents.per_page
  setup_view_environment
  render :template => view_for(:suffix => '_search')
end

#search_allObject

Allow Document wide search /search?terms=hello



53
54
55
56
57
58
# File 'app/controllers/documents_controller.rb', line 53

def search_all
  params[:search] ||= {}
  params[:search][:state_eq] = 'published'
  @documents = Document.search(params[:search]).paginate :page => params[:page], :per_page => Settings.documents.per_page
  render :template => '/pages/search'
end

#showObject

Show a document: Route using catch-all, /*path, or, pass specific ‘id’, :path => ‘gallery’

Raises:

  • (ActiveRecord::RecordNotFound)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/documents_controller.rb', line 7

def show    
  render :file => cache_file and return if cached_file?
  
  @document = Document.find_by_path(request_path)

  raise ActiveRecord::RecordNotFound unless @document and (@document.published? or @document.allowed? current_user, 'read')

  respond_to do |format|
    format.html do
      setup_view_environment
      render :template => view_for
    end
    format.xml { render :xml => @document.to_xml(:only => [:title, :summary, :body, :published_at, :permalink]) }
    format.rss { render :template => 'pages/feed.rss.builder' }
  end

  cache_this_page!
end