Sha256: 4d04135507109e088883f292f862b612136613ddd55ccc288a97ffb4622889ed

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

module Popolo
  class AreasController < PopoloController
    inherit_resources
    # inherited_resources assumes the routes are namespaced. If an engine is
    # mounted at root, however, there will be no namespace.
    self.resources_configuration[:self][:route_prefix] = nil

    respond_to :html, :json
    actions :index, :show
    custom_actions collection: :nested_index, resource: :nested_show

    before_filter :validate_path, only: [:nested_index, :nested_show]

    def index
      @areas = Area.roots
      index!
    end

    def show
      @area = Area.find_by_slug_or_id(params[:id])
      show!
    end

    def nested_index
      @areas = @area.children

      nested_index! do |format|
        format.html { render action: 'index'}
      end
    end

    def nested_show
      nested_show! do |format|
        format.html { render action: 'show'}
      end
    end

  protected

    # @raises [Mongoid::Errors::DocumentNotFound] if a resource is improperly nested
    def validate_path
      parts = params[:path].split '/'
      parts.each do |part|
        @area = Area.find_by(parent_id: @area, slug: part)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
popolo-0.0.1 app/controllers/popolo/areas_controller.rb