app/controllers/spotlight/pages_controller.rb in blacklight-spotlight-0.0.2 vs app/controllers/spotlight/pages_controller.rb in blacklight-spotlight-0.0.3

- old
+ new

@@ -1,24 +1,25 @@ module Spotlight class PagesController < Spotlight::ApplicationController before_filter :authenticate_user!, except: [:show] load_resource :exhibit, class: Spotlight::Exhibit, only: [:index, :new, :create, :update_all] - load_and_authorize_resource only: [:show, :edit, :update, :destroy, :update_all] - load_and_authorize_resource through: :exhibit, only: [:index, :new, :create] + load_and_authorize_resource through: :exhibit, shallow: true, instance_name: 'page' - before_filter :cast_page_instance_variable + before_filter :attach_breadcrumbs include Blacklight::Base include Blacklight::Catalog::SearchContext copy_blacklight_config_from(CatalogController) - helper_method :get_search_results, :get_solr_response_for_doc_id, :page_model, :page_collection_name + helper_method :get_search_results, :get_solr_response_for_doc_id, :get_solr_response_for_field_values, :page_collection_name # GET /exhibits/1/pages def index + # set up a model the inline "add a new page" form + @page = CanCan::ControllerResource.new(self).send(:build_resource) end # GET /pages/1 def show end @@ -32,21 +33,22 @@ end # POST /exhibits/1/pages def create @page.attributes = page_params + @page.last_edited_by = @page.created_by = current_user if @page.save redirect_to [@page.exhibit, page_collection_name], notice: 'Page was successfully created.' else render action: 'new' end end # PATCH/PUT /pages/1 def update - if @page.update(page_params) + if @page.update(page_params.merge(last_edited_by: current_user)) redirect_to [@page.exhibit, page_collection_name], notice: 'Page was successfully updated.' else render action: 'edit' end end @@ -71,22 +73,27 @@ def update_all_page_params params.require(:exhibit).permit("#{page_collection_name}_attributes" => [:id, :published, :title, :weight, :display_sidebar, :parent_page_id ]) end def human_name - page_collection_name.to_s.humanize + @human_name ||= page_collection_name.humanize end + alias page_collection_name controller_name + + def attach_breadcrumbs + load_exhibit + add_breadcrumb @exhibit.title, @exhibit + end + + def load_exhibit + @exhibit ||= @page.exhibit + end + private - # We don't have a generic page model since Spotlight::Page is now a concern. - def page_model - "" - end - def page_collection_name - page_model.to_s.pluralize.to_sym - end + # Only allow a trusted parameter "white list" through. def page_params - params.require(page_model).permit(:title, :content) + params.require(controller_name.singularize).permit(:title, :content) end end end