Class: PagesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/pages_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#robot?

Instance Method Details

#changedObject



18
19
20
21
22
23
24
25
26
# File 'app/controllers/pages_controller.rb', line 18

def changed
  @pages = Page.order("updated_at DESC").all
  @no_pad = true

  respond_to do |format|
    format.html
    format.xml { render :xml => @pages }
  end
end

#createObject

POST /pages POST /pages.xml



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/pages_controller.rb', line 116

def create
  @page = Page.new(params[:page])
  @courses = Course.unique_course_names

  @page.updated_by_user_id = current_user.id if current_user
  respond_to do |format|
    if @page.save
      flash[:notice] = 'Page was successfully created.'
      format.html { redirect_to(@page) }
      format.xml { render :xml => @page, :status => :created, :location => @page }
    else
      format.html { render :action => "new" }
      format.xml { render :xml => @page.errors, :status => :unprocessable_entity }
    end
  end
end

#editObject

GET /pages/1/edit



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
106
107
# File 'app/controllers/pages_controller.rb', line 74

def edit
  @page = Page.find_by_url(params[:id])
  @courses = Course.unique_course_names

  if @page.blank?
    flash[:error] = "Page with an id of #{params[:id]} is not in this system."
    redirect_to(pages_url) and return
  end

  unless @page.editable?(current_user)
    flash[:error] = "You don't have permission to do this action."
    redirect_to(page_url) and return
  end

  if @page.is_someone_else_currently_editing_page(current_user) && @page.timeout_has_not_passed


    flash[:notice] = "#{@page.current_edit_by.human_name} started editing this page
                      #{pluralize(((Time.now - @page.current_edit_started_at) / 1.minute).round, 'minute')} ago at
                      #{l @page.current_edit_started_at, :format => :detailed }"
    redirect_to(page_url) and return
  end

  @page.skip_version do
    @page.current_edit_by = current_user
    @page.current_edit_started_at = Time.now
    @page.save!
  end

  respond_to do |format|
    format.html # new.html.erb
    format.xml { render :xml => @page }
  end
end

#indexObject

GET /pages GET /pages.xml



9
10
11
12
13
14
15
16
# File 'app/controllers/pages_controller.rb', line 9

def index
  @pages = Page.all

  respond_to do |format|
    format.html # index.html.erb
    format.xml { render :xml => @pages }
  end
end

#newObject

GET /pages/new GET /pages/new.xml



60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/pages_controller.rb', line 60

def new
  @page = Page.new
  @page.title = params[:url].split('_').collect { |w| w.capitalize + ' ' }.join().chomp(' ') if params[:url]
  @page.url = params[:url]
  @page.course_id = params[:course_id].to_i
  @courses = Course.unique_course_names

  respond_to do |format|
    format.html # new.html.erb
    format.xml { render :xml => @page }
  end
end

#pluralize(number, text) ⇒ Object



109
110
111
112
# File 'app/controllers/pages_controller.rb', line 109

def pluralize(number, text)
  return number.to_s+' '+text.pluralize if number != 1
  number.to_s+' '+text
end

#revertObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/controllers/pages_controller.rb', line 182

def revert
  @page = Page.find_by_url(params[:id])

  if @page.blank?
    flash[:error] = "Page with an id of #{params[:id]} is not in this system."
    redirect_to(pages_url) and return
  end

  unless @page.editable?(current_user)
    flash[:error] = "You don't have permission to do this action."
    redirect_to(page_url) and return
  end

  respond_to do |format|
    if @page.revert_to! params[:version].to_i
      flash[:notice] = 'Page was successfully reverted.'
      format.html { redirect_to(@page) }
      format.xml { head :ok }
    else
      format.html { redirect_to page_path(@page, :history => true) }
      format.xml { render :xml => @page.errors, :status => :unprocessable_entity }
    end
  end
end

#showObject

GET /pages/1 GET /pages/1.xml



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/pages_controller.rb', line 30

def show
  @no_pad = true
  @page = Page.find_by_url(params[:id])
  @page.revert_to(params[:version].to_i) if params[:version]

  if @page.blank?
    flash[:error] = "Page with an id of #{params[:id]} is not in this system. You may create it using the form below."
    redirect_to(:controller => :pages, :action => :new, :url => params[:id]) and return
  end

  if @page.visible == false
    flash[:error] = "This page no longer exists."
    redirect_to(root_url) and return
  end

  unless @page.viewable?(current_user)
    flash[:error] = "You don't have permission to do this action."
    redirect_to(root_url) and return
  end

  @tab = params[:tab]

  respond_to do |format|
    format.html # show.html.erb
    format.xml { render :xml => @page }
  end
end

#updateObject

PUT /pages/1 PUT /pages/1.xml



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/controllers/pages_controller.rb', line 135

def update
  @page = Page.find_by_url(params[:id])
  @courses = Course.unique_course_names

  unless @page.editable?(current_user)
    flash[:error] = "You don't have permission to do this action."
    redirect_to(page_url) and return
  end

  respond_to do |format|
    format.html do
      update_page_edit_info(@page)
      if @page.update_attributes(params[:page])
        unless params[:timeout_flag].blank?
          flash[:notice] = "We thought you left, so we saved your page for you."
        else
          flash[:notice] = 'Page was successfully updated.'
        end

        redirect_to(@page)
      else
        render :action => "edit"
      end
    end

    format.xml do
      update_page_edit_info(@page)
      if @page.update_attributes(params[:page])
        head :ok
      else
        render :xml => @page.errors, :status => :unprocessable_entity
      end
    end

    format.json do
      # Do not bump up the version number for auto save
      @page.skip_version do
        if @page.update_attributes(params[:page])
          render :json => {:code => "success", :message => "", :new_post_path => page_path(@page)}
        else
          render :json => {:code => "failed", :message => "Automatic save failed"}
        end
      end
    end
  end
end