lib/alchemy/custom/model/pages_controller_dec.rb in alchemy-custom-model-2.2.0 vs lib/alchemy/custom/model/pages_controller_dec.rb in alchemy-custom-model-2.2.1

- old
+ new

@@ -5,16 +5,32 @@ included do skip_before_action :page_not_found!, only: [:index, :show] before_action :load_custom_model_page, only: [:show] - before_action :page_not_found_after_custom_model!, if: -> {@page.blank?}, only: [:index, :show] + before_action :page_not_found_after_custom_model!, if: -> { @page.blank? }, only: [:index, :show] before_action :perform_search, only: :show - after_action :set_404_after + after_action :set_404_after, except: :sitemap helper CustomModelHelper + def sitemap + @pages = Alchemy::Page.sitemap.reject do |page| + page.definition["custom_model_action"].to_s == "show" + end + @custom_elements = [] + Alchemy::Custom::Model.sitemaps_models.each do |model| + if model.respond_to? :to_sitemap + @custom_elements << model.to_sitemap + end + end + @custom_elements = @custom_elements.flatten + respond_to do |format| + format.xml { render layout: 'alchemy/sitemap' } + end + end + private def load_custom_model_page if !@page.nil? custom_model_string = get_custom_model_string @@ -28,49 +44,49 @@ page(params[:page]).per(params[:per_page]) @custom_elements = @custom_elements.only_current_language instance_variable_set("@#{custom_model_string.demodulize.downcase.pluralize}", @custom_elements) end + elsif action_name == "sitemap" + super else url = params[:urlname] url_params = url.match(/(?<page_name>.*)\/(?<custom_model_id>[^\/]+)$/) unless url_params.nil? - parent_page = Alchemy::Language.current.pages.contentpages.find_by( urlname: url_params[:page_name], language_code: params[:locale] || Alchemy::Language.current.code ) if parent_page.nil? - raise ActionController::RoutingError, "Parent Page not Found [#{url_params[:page_name]}]" - end + Rails.logger.warn "Parent Page not Found [#{url_params[:page_name]}]" + #TODO magari implementare ricerca children in base a una action es. edit new chow ecc + else + @page = parent_page.children.first + if @page.nil? + Rails.logger.warn "You have to define a subpage for custom model" + else + custom_model_string = get_custom_model_string - #TODO magari implementare ricerca children in base a una action es. edit new chow ecc + if custom_model_string.blank? + Rails.logger.warn "You have to specify custom_model in page_layouts config file" + else + custom_model = custom_model_string.classify.constantize + @custom_element = custom_model.only_current_language.friendly.find(url_params[:custom_model_id]) + instance_variable_set("@#{custom_model_string.demodulize.underscore}", @custom_element) + end - @page = parent_page.children.first + end - if @page.nil? - raise ActionController::RoutingError, "You have to define a subpage for custom model" end - custom_model_string = get_custom_model_string - - if custom_model_string.blank? - raise ActionController::RoutingError, "You have to specify custom_model in page_layouts config file" - else - custom_model = custom_model_string.classify.constantize - @custom_element = custom_model.only_current_language.friendly.find(url_params[:custom_model_id]) - instance_variable_set("@#{custom_model_string.demodulize.underscore}", @custom_element) - end - end - end end def perform_search return if params[:query].blank? @@ -93,27 +109,24 @@ else pages.not_restricted.search(params[:query], params[:whitelist_custom_model]) end end - def get_custom_model_string children_page_layout = Alchemy::PageLayout.get(@page.page_layout) children_page_layout["custom_model"] end - def page_not_found_after_custom_model! not_found_error!("Alchemy::Page not found \"#{request.fullpath}\"") end def paginate_per ArchimediaPgsearch::SEARCH_RESULTS_PAGINATION_NUMBER end - def not_found_error!(msg = "Not found \"#{request.fullpath}\"") not_found_page = Alchemy::Language.current.pages.published.find_by(page_layout: "not_found") if !not_found_page.nil? @page = not_found_page \ No newline at end of file