app/controllers/admin/pages_controller.rb in mcms_pages-0.0.1 vs app/controllers/admin/pages_controller.rb in mcms_pages-0.0.2
- old
+ new
@@ -15,10 +15,11 @@
#includes helper file
include PagesHelper
# loading controller specific assets
before_filter :load_assets
+ after_filter :clean_pages, :only => [:update]
# GET /pages
# GET /pages.json
# finds all pages created so far and displays it
# It may change in future
@@ -83,19 +84,24 @@
# find page with given ID
#@page = Page.find(params[:id])
@page = Page.find_by_slug_or_id(params[:path], params[:id])
- #retrieving layout id from params and assigning to a variable
- @layout = @page["layout_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)
- # calling method to define parts of the page
- define_parts
-
#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 }
@@ -133,11 +139,11 @@
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" }
+ 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
@@ -151,27 +157,28 @@
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)
- # calling method to define parts of the page
- define_parts
-
#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" }
+ 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
@@ -185,10 +192,12 @@
def destroy
# find the page with with
@page = Page.find(params[:id])
+ PagePart.delete_page_parts(@page)
+
#destroy the page
@page.destroy
#sends in data in different format to index action
respond_to do |format|
@@ -207,22 +216,30 @@
def find_child
@page = Page.find_by_slug(params[:path])
logger.debug @page
- render :layout => false
+ #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
- # @param : Object(as a string)
+ # @param : string
# @return : None
- # @purpose : To render a page part dynamically
+ # @purpose : To render a page part pages dynamically
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'}
@@ -237,14 +254,14 @@
#calling library class AssetManager's methods to include specific js and css files
def load_assets
# class method call to include js files
- AssetManager.include_local_library [:application, :pageFormAdmin, "ckeditor/init"]
+ Asset.include_local_library [:application, :pageFormAdmin, "ckeditor/init"]
# class method call to include css files
- AssetManager.include_css ["mcms_pages/pages", "mcms_pages/page_styles"]
+ Asset.include_css ["mcms_pages/pages", "mcms_pages/page_form", "mcms_pages/tabs"]
end # end method
# @param : Array
# @return : None
@@ -252,11 +269,11 @@
def build_page_parts(parts)
#renders the form field for each part of layout
parts.each_with_index do |page_part, index|
- fragment = get_fragments(index, page_part)
+ fragment = get_fragments(page_part)
# check if same type of fragment is already created
if fragment.nil? or fragment.blank? or fragment.empty?
# if not then build child pages for all parts of the layout
@@ -273,31 +290,40 @@
end # end method
def find_pages_and_parts layout
- #calling find_all_pages method from Page model
- @pages = Page.find_all_pages
-
# storing the array returned by helper method to
# decide the page parts in a layout
@parts = Layout.find_page_parts(layout)
- end
+ end # end method
- def define_parts
+ # @param : None
+ # @return : None
+ # @purpose : To delete orphan record
+ def clean_pages
- # declare an empty hash
- @part_title = Hash.new
+ # find all records in the table mcms_page_parts
+ page_parts = PagePart.all
- # loop through each part of the page
- @parts.each_with_index do |part,index|
+ # loop through all page_parts array
+ page_parts.each do |page_part|
- # retrieve the id of page parts of the page based on title and store it as a hash element
- @part_title[part] = @page.page_parts.select("mcms_page_parts.id").find_by_title(part).id
+ # find page_part_page corresponding to a page_part
+ page_part_page = PagePartsPage.where("page_part_id = ?", page_part.id).first
+ # check if there is no page_part_page corresponding to a page_part
+ if page_part_page.nil? or page_part_page.blank?
+
+ # find page part with relevent id and delete it
+ part = PagePart.find(page_part.id)
+ part.destroy
+
+ end # end if
+
end # end loop
- end
+ end # end method
end #end class