Sha256: 537f42161371d9332ebf13a775fd99baa5975ae9575a34df0683590f29890505

Contents?: true

Size: 1.36 KB

Versions: 5

Compression:

Stored size: 1.36 KB

Contents

module Nodes
  class ApplicationController < ActionController::Base
    protect_from_forgery with: :exception

    def init
      @controller = params[:controller]
      if not %w(site user_sessions).include?(@controller)
        @object_name = @controller.singularize
        @model = @controller.singularize.split('/').map {|c| c.capitalize}.join('::').constantize if @controller
        @object = @model.find_by_id(params[:id]) if (params[:id] && @model)
      end
    end

    # ASC | DESC
    def sort
      if session[:sort] == params[:field] #change sort direction
        if session[:sort_direction] == 'DESC'
          session[:sort_direction] = 'ASC'
        else
          session[:sort_direction] = 'DESC'
        end
      else # create new sort direction
        session[:sort] = params[:field]
        session[:sort_direction] = 'ASC'
      end

      list = @model.all
      if session[:sort_direction] == 'ASC'
        list.to_a.sort! do |one, two|
          a = one.send(session[:sort])
          b = two.send(session[:sort])
          (a and b) ? a <=> b : (a ? -1 : 1)
        end
      else
        list.to_a.sort! do |one, two|
          a = one.send(session[:sort])
          b = two.send(session[:sort])
          (b and a) ? b <=> a : (b ? -1 : 1)
        end
      end
      instance_variable_set("@#{@controller}", list)
      render action: :index
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
nodes-0.2.8 app/controllers/nodes/application_controller.rb
nodes-0.2.7 app/controllers/nodes/application_controller.rb
nodes-0.2.6 app/controllers/nodes/application_controller.rb
nodes-0.2.5 app/controllers/nodes/application_controller.rb
nodes-0.2.4 app/controllers/nodes/application_controller.rb