Class: Admin::PagesController

Inherits:
AdminController
  • Object
show all
Includes:
PagesHelper
Defined in:
app/controllers/admin/pages_controller.rb

Overview

@File Name : admin/pages_controller.rb

@Company Name              : Mindfire Solutions Private Limited
@Creator Name              : Vikram Kumar Mishra
@Date Created              : 2012-06-05
@Date Modified             : 2012-06-08
@Last Modification Details : Page part saved successfully
@Purpose                   : To setup the communication between page model and views related to pages under namespace admin.

Instance Method Summary collapse

Methods included from PagesHelper

#arrange_page_part, #build_page_part, #build_page_part_page, #deleteable?, #get_fragments, #layout_name, #layout_parts

Instance Method Details

#add_page_partObject

Returns : None.

Parameters:

  • :

    string

Returns:

  • : None



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/controllers/admin/pages_controller.rb', line 238

def add_page_part

  #retrieve parameters and assign these to  instance variables
  @title = params[:title]
  @index = params[:index]
  @part = params[:part]

  # render js.erb
  respond_to do |format|

    format.js{render :template => 'mcms_pages/admin/pages/add_page_part'}

  end # end respond_to

end

#createObject

POST /pages POST /pages.json saves page with page parts and redirects to the current created page



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/admin/pages_controller.rb', line 117

def create

  # retrieve page part attributes from params and assign it to a variable
  page_parts = params[:page][:page_parts_attributes]

  # sort the page_parts and reassign it to the params
  params[:page][:page_parts_attributes] = Hash[page_parts.sort]

  #creating the page object with its own and child attributes
  @page = Page.new(params[:page])

  #retrieving layout id from params and assigning to a variable
  @layout = params[:page][:layout_id]

  # calling method to find all pages and page parts
  find_pages_and_parts(@layout)

  # sends the data in different formats
  respond_to do |format|

    if @page.save #page saved successfully

      format.html { redirect_to admin_pages_path, notice: 'Page was successfully created.' }
      format.json { render json: @page, status: :created, location: @page }

    else #page saving failed, re-render the form

      format.html { render action: "new", :template => 'mcms_pages/admin/pages/new' }
      format.json { render json: @page.errors, status: :unprocessable_entity }

    end # end if

  end # end respond_to block

end

#destroyObject

DELETE /pages/1 DELETE /pages/1.json destroys the page on the basis of ID and redirects to all pages path



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/controllers/admin/pages_controller.rb', line 195

def destroy

  # find the page with with
  @page = Page.find(params[:id])

  # calling class method of PagePart model to delete page parts of a page
  PagePart.delete_page_parts(@page)

  #destroy the page
  @page.destroy

  #sends in data in different format to index action
  respond_to do |format|

    format.html { redirect_to admin_pages_url }
    format.json { head :no_content }

  end # end respond_to block

end

#editObject

GET /pages/1/edit finds the page with given ID and displays the fields with value to edit, i.e. edit form



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
108
109
110
111
# File 'app/controllers/admin/pages_controller.rb', line 83

def edit

  # find page with given ID
  #@page = Page.find(params[:id])
  @page = Page.find_by_slug_or_id(params[:path], params[:id])

  if @page.nil?

    raise ActiveRecord::RecordNotFound

  else

   #retrieving layout id from params and assigning to a variable
   @layout = @page["layout_id"]

  end # end if

  # calling method to find all pages and page parts
  find_pages_and_parts(@layout)

  #sends data in different formats
  respond_to do |format|

    format.html{render :template => 'mcms_pages/admin/pages/edit'}# edit.html.erb
    format.json { render json: @page }

  end # end respond_to block

end

#find_childObject

Returns : None.

Parameters:

  • :

    None

Returns:

  • : None



220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'app/controllers/admin/pages_controller.rb', line 220

def find_child

  # find page based on path or slug
  @page = Page.find_by_slug(params[:path])

  #sends in data in different format to index action
  respond_to do |format|

    format.html { render :template => 'mcms_pages/admin/pages/find_child', :layout => false }
    format.json { head :no_content }

  end # end respond_to block

end

#indexObject

GET /pages GET /pages.json finds all pages created so far and displays it It may change in future



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

def index

  # find all pages
  @pages = Page.all

  # send data in different format
  respond_to do |format|
    format.html{render :template => 'mcms_pages/admin/pages/index'}# index.html.erb
    format.json { render json: @pages }
  end # end respond_to

end

#newObject

GET /pages/new GET /pages/new.json renders the form for new page and also page part to save page and page part together successfully



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/admin/pages_controller.rb', line 43

def new

  # check if params[:layout_id] is present
  if not params[:layout_id]

    # if not redirect to layout page
    redirect_to admin_layouts_path(:parent_id => params[:parent_id])

  else # params[:layout_id] is present

    #initializing an empty object
    @page = Page.new

    #retrieve parent_id parameter
    @page.parent_id = params[:parent_id]

    #retrieving layout id from params and assigning to a variable
    @layout = params[:layout_id]

    # calling method to find all pages and page parts
    find_pages_and_parts(@layout)

    #calling method to build page parts
    build_page_parts(@parts)

    #sends data in different formats
    respond_to do |format|

      format.html{render :template => 'mcms_pages/admin/pages/new'}# new.html.erb
      format.json { render json: @page }

    end # end respond_to block

  end # end if

end

#updateObject

PUT /pages/1 PUT /pages/1.json Gets the ID via put method and updates the field and redirects to current edited page



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/admin/pages_controller.rb', line 157

def update

  #find the page with given id
  @page = Page.find(params[:id])

  #use updated_at manually, it will update the page table if user hits update button.
  # This may not be trivial, so it may change.
  @page.updated_at = Time.now

  #retrieving layout id from params and assigning to a variable
  @layout = @page[:layout_id]

  # calling method to find all pages and page parts
  find_pages_and_parts(@layout)

  #sends in data in different format
  respond_to do |format|

    if @page.update_attributes(params[:page]) #page updated successfully

      format.html { redirect_to admin_pages_path, notice: 'Page was successfully updated.' }
      format.json { head :no_content }

    else #page saving failed, re-renders edit template

      format.html { render action: "edit", :template => 'mcms_pages/admin/pages/edit' }
      format.json { render json: @page.errors, status: :unprocessable_entity }

    end # end if

  end # end respond_to block

end