module Alchemy::Custom::Model::PagesControllerDec

Public Instance Methods

get_custom_model_string() click to toggle source
# File lib/alchemy/custom/model/pages_controller_dec.rb, line 114
def get_custom_model_string

  children_page_layout = Alchemy::PageLayout.get(@page.page_layout)

  children_page_layout["custom_model"]

end
load_custom_model_page() click to toggle source
Calls superclass method
# File lib/alchemy/custom/model/pages_controller_dec.rb, line 34
def load_custom_model_page
  if !@page.nil?
    custom_model_string = get_custom_model_string

    unless custom_model_string.blank?

      custom_model = custom_model_string.classify.constantize

      @q = custom_model.ransack(params[:q])
      @custom_elements = @q.result.
        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?
        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

          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

        end

      end

    end

  end
end
not_found_error!(msg = "Not found \" click to toggle source
# File lib/alchemy/custom/model/pages_controller_dec.rb, line 130
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
  else
    raise ActionController::RoutingError, msg
  end
end
page_not_found_after_custom_model!() click to toggle source
# File lib/alchemy/custom/model/pages_controller_dec.rb, line 122
def page_not_found_after_custom_model!
  not_found_error!("Alchemy::Page not found \"#{request.fullpath}\"")
end
paginate_per() click to toggle source
# File lib/alchemy/custom/model/pages_controller_dec.rb, line 126
def paginate_per
  ArchimediaPgsearch::SEARCH_RESULTS_PAGINATION_NUMBER
end
search_results() click to toggle source
# File lib/alchemy/custom/model/pages_controller_dec.rb, line 103
def search_results
  pages = Alchemy::Page.published.contentpages.with_language(Alchemy::Language.current.id)
  # Since CanCan cannot (oh the irony) merge +accessible_by+ scope with pg_search scopes,
  # we need to fake a page object here
  if can? :show, Alchemy::Page.new(restricted: true, public_on: Date.current)
    pages.search(params[:query], params[:whitelist_custom_model])
  else
    pages.not_restricted.search(params[:query], params[:whitelist_custom_model])
  end
end
set_404_after() click to toggle source
# File lib/alchemy/custom/model/pages_controller_dec.rb, line 139
def set_404_after
  if @page.page_layout == "not_found"
    response.status = 404
  end
end
sitemap() click to toggle source
# File lib/alchemy/custom/model/pages_controller_dec.rb, line 16
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