app/controllers/admin/pages_controller.rb in effective_pages-2.1.0 vs app/controllers/admin/pages_controller.rb in effective_pages-3.0.0
- old
+ new
@@ -1,113 +1,18 @@
module Admin
class PagesController < ApplicationController
- before_action(:authenticate_user!) # Devise
+ before_action(:authenticate_user!) if defined?(Devise)
+ before_action { EffectiveResources.authorize!(self, :admin, :effective_pages) }
- layout (EffectivePages.layout.kind_of?(Hash) ? EffectivePages.layout[:admin] : EffectivePages.layout)
+ include Effective::CrudController
- def index
- @datatable = EffectivePagesDatatable.new(self)
- @page_title = 'Pages'
-
- authorize_effective_pages!
+ if (config = EffectivePages.layout)
+ layout(config.kind_of?(Hash) ? config[:admin] : config)
end
- def new
- @page = Effective::Page.new()
- @page_title = 'New Page'
-
- authorize_effective_pages!
- end
-
- def create
- @page = Effective::Page.new(page_params)
- @page_title = 'New Page'
-
- authorize_effective_pages!
-
- if @page.save
- if params[:commit] == 'Save and Edit Content'
- redirect_to effective_regions.edit_path(effective_pages.page_path(@page), :exit => effective_pages.edit_admin_page_path(@page))
- elsif params[:commit] == 'Save and Add New'
- flash[:success] = 'Successfully created page'
- redirect_to effective_pages.new_admin_page_path
- elsif params[:commit] == 'Save and View'
- redirect_to effective_pages.page_path(@page)
- else
- flash[:success] = 'Successfully created page'
- redirect_to effective_pages.edit_admin_page_path(@page)
- end
- else
- flash.now[:danger] = 'Unable to create page'
- render :action => :new
- end
- end
-
- def edit
- @page = Effective::Page.find(params[:id])
- @page_title = 'Edit Page'
-
- authorize_effective_pages!
- end
-
- def update
- @page = Effective::Page.find(params[:id])
- @page_title = 'Edit Page'
-
- authorize_effective_pages!
-
- if @page.update_attributes(page_params)
- if params[:commit] == 'Save and Edit Content'
- redirect_to effective_regions.edit_path(effective_pages.page_path(@page), :exit => effective_pages.edit_admin_page_path(@page))
- elsif params[:commit] == 'Save and Add New'
- flash[:success] = 'Successfully updated page'
- redirect_to effective_pages.new_admin_page_path
- elsif params[:commit] == 'Save and View'
- redirect_to effective_pages.page_path(@page)
- elsif params[:commit] == 'Duplicate'
- begin
- page = @page.duplicate!
- flash[:success] = 'Successfully saved and duplicated page.'
- flash[:info] = "You are now editting the duplicated page. This new page has been created as a Draft."
- rescue => e
- flash.delete(:success)
- flash[:danger] = "Unable to duplicate page: #{e.message}"
- end
-
- redirect_to effective_pages.edit_admin_page_path(page || @page)
- else
- flash[:success] = 'Successfully updated page'
- redirect_to effective_pages.edit_admin_page_path(@page)
- end
- else
- flash.now[:danger] = 'Unable to update page'
- render :action => :edit
- end
- end
-
- def destroy
- @page = Effective::Page.find(params[:id])
-
- authorize_effective_pages!
-
- if @page.destroy
- flash[:success] = 'Successfully deleted page'
- else
- flash[:danger] = 'Unable to delete page'
- end
-
- redirect_to effective_pages.admin_pages_path
- end
-
- private
-
- def authorize_effective_pages!
- EffectivePages.authorize!(self, :admin, :effective_pages)
- EffectivePages.authorize!(self, action_name.to_sym, @page || Effective::Page)
- end
-
- def page_params
- params.require(:effective_page).permit(EffectivePages.permitted_params)
- end
+ submit :save, 'Save'
+ submit :save, 'Save and Add New', redirect: :new
+ submit :save, 'Save and View', redirect: -> { effective_pages.page_path(resource) }
+ submit :save, 'Duplicate', only: :edit, redirect: -> { effective_posts.new_admin_page_path(duplicate_id: resource.id) }
end
end