Sha256: 76c3c769373127351406c4e1eb149258dbfbc7001420757b0dfc33c8753c41b3

Contents?: true

Size: 1.51 KB

Versions: 10

Compression:

Stored size: 1.51 KB

Contents

class Admin::DashboardController < Admin::ApplicationController

  respond_to :html, :json
  layout 'admin/application'

  def index
    @setting = SpudUserSetting.find_by(spud_user_id: current_user.id, key: 'dash_icon_order')
    apps = TbCore.admin_applications
    if @setting.nil? == false
      apps = arrange_apps(apps)
    end
    @admin_applications = apps.select do |admin_application|
      current_user.can_view_app?(admin_application)
    end
  end

  def change_sort
    @order = params[:order]
    @setting = SpudUserSetting.find_or_initialize_by(spud_user_id: current_user.id, key: 'dash_icon_order')
    @setting.value = @order
    if @setting.save
      respond_to do |format|
        format.json  { head :no_content }
      end
    end
  end

  def badges
    data = []
    @admin_applications = TbCore.admin_applications.select do |admin_application|
      if current_user.can_view_app?(admin_application)
        if admin_application[:badge]
          data << {key: admin_application[:key], badge_count: admin_application[:badge].call(current_user)}
        end
      end
    end
    render json: {data: data}
  end

  private

  def arrange_apps(applications)
    begin
      order = JSON.parse(@setting.value)
    rescue JSON::ParserError
      return applications
    end

    return applications.sort do |a, b|
      index_a = order.index(a[:url])
      index_b = order.index(b[:url])
      if index_a.nil?
        1
      elsif index_b.nil?
        -1
      else
        index_a <=> index_b
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
tb_core-1.5.4 app/controllers/admin/dashboard_controller.rb
tb_core-1.5.3 app/controllers/admin/dashboard_controller.rb
tb_core-1.5.2 app/controllers/admin/dashboard_controller.rb
tb_core-1.5.1 app/controllers/admin/dashboard_controller.rb
tb_core-1.5.0 app/controllers/admin/dashboard_controller.rb
tb_core-1.4.8 app/controllers/admin/dashboard_controller.rb
tb_core-1.4.7 app/controllers/admin/dashboard_controller.rb
tb_core-1.4.6 app/controllers/admin/dashboard_controller.rb
tb_core-1.4.5 app/controllers/admin/dashboard_controller.rb
tb_core-1.4.4 app/controllers/admin/dashboard_controller.rb