Sha256: d8644da9e77aca2ba76b4b4a5a1fdd0a873e828a1816f4da24edc2cd5106f7ad

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module Puffer
  class TreeBase < Puffer::Base
    unloadable

    helper 'puffer/helpers/puffer_tree'

    define_fieldset :tree, :fallbacks => :index

    def index
      return super if puffer_filters.any?
      @records = resource.collection_scope
      if session[:expanded].present?
        @records = @records.where(["depth in (0, 1) or parent_id in (#{session[:expanded].join(', ')})"]).arrange
      else
        @records = @records.with_depth([0, 1]).arrange
      end
      render 'tree'
    end

    member do
      get :inherit, :label => 'inherit'
      get :expand, :display => false
      get :collapse, :display => false
    end

    def inherit
      @parent = resource.member
      @record = @parent.children.new
      render 'new'
    end

    def expand
      @parent = resource.member
      session[:expanded] ||= []
      session[:expanded].push params[:id] if @parent
      session[:expanded].uniq!
      @records = resource.model.to_adapter.filter(@parent.self_and_descendants.where(:parent_id => [@parent.parent_id] + session[:expanded]), tree_fields).arrange
      render 'toggle'
    end

    def collapse
      @parent = resource.member
      session[:expanded] ||= []
      session[:expanded].delete params[:id]
      @records = resource.model.to_adapter.filter(resource.collection_scope.where(:id => [@parent.id]), tree_fields).arrange
      render 'toggle'
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
puffer-0.1.0 app/controllers/puffer/tree_base.rb