继承的资源没有正确索引

时间:2013-06-06 15:34:41

标签: ruby-on-rails ruby ruby-on-rails-3 activeadmin cancan

我有一个activeadmin应用程序,它使用cancan作为角色。管理员应该能够看到所有订单,经纪人应该只看到属于他们的订单。现在经纪人工作正常,但管理员只看到属于该用户的订单,而不是应用程序中的所有订单。

能力模型

class Ability
  include CanCan::Ability

  def initialize(user)

        return if user.nil? #non logged in user can use this.

        if user.broker?
            can [:index, :create, :read, :update, :new, :edit], [Order, Customer], :admin_user_id => user.id.to_s
            can :read, [OrderCategory, OrderType, OrderStatus, OrderPriority]
            cannot :index, [OrderCategory, OrderType, OrderStatus, OrderPriority]
            cannot :destroy, :all
        end

        if user.art?
            cannot :create, :all
            can :read, :all
            can :update, Order
            cannot :destroy, :all
        end


        if user.shipping?
            can :read, :all
            can :update, Order
            cannot :destroy, :all
        end

        if user.production?
            can [:create, :update], [Order, Customer]
            can :read, :all
        end

        if user.sales?
            can [:create, :read, :update], [Order, Customer]
            cannot :destroy, :all
        end

        if user.admin?
            can :manage, :all
        end
    end
end

订购资源

ActiveAdmin.register Order, :sort_order => "end_date_asc" do
    controller.authorize_resource :except => :index

    menu :label => "All Orders", :parent => "Sales", :priority => 2

    filter :name, label: "Order Name"
    filter :admin_user, :collection => proc { AdminUser.all.map{|u| [u.last_name, u.id] } }
    filter :order_category, label: "Category"
    filter :order_type, label: "Type"
    filter :order_status, label: "Status"
    filter :order_priority, label: "Priority"
    filter :customer, label: "Customer"
    filter :start_date, label: "Start Date"
    filter :end_date, label: "Due Date"
    filter :id, label: "Order ID#"

  controller do

     def begin_of_association_chain
      current_user 
    end
    end

    index do 
        selectable_column
        column "ID", :sortable => :id do |order|
            link_to order.id, admin_order_path(order)
        end
        column "Proof" do |order|
            image_tag order.proof_url(:proof).to_s
        end
        column "Name", :sortable => :name do |order|
      link_to order.name, admin_order_path(order)
    end
    column(:customer, :sortable => :customer_id)
    column("Category", :order_category, :sortable => :order_category_id) 
        column("Status", :order_status, :sortable => :order_status_id)
        column("Priority", :order_priority, :sortable => :order_priority_id)
    column("Due Date", :end_date, :sortable => :end_date)
    default_actions
  end


  form :partial => "form"

  show :title => :name do

    panel "Order Details" do
      attributes_table_for resource do
        row :id
        row :admin_user
        row :name
        row :order_category
        row :order_type
        row :order_status
        row :order_priority
        row :start_date
        row :end_date
      end
    end
      resource.line_items.each do |a|
        text_node(render :partial => "admin/line_items/show", :locals => { :line_item => a })
    end
    panel "Art Details" do
        attributes_table_for resource do
            row :print_location
            row :color_front
            row :color_back
            row :color_sleeve
            row(:artwork) do
                image_tag order.artwork_url(:thumb).to_s
            end
            row(:proof) do
                image_tag order.proof_url(:thumb).to_s
            end
          end
      end  
    end
end

1 个答案:

答案 0 :(得分:0)

我认为CanCan与您当前的问题无关。这段代码:

def begin_of_association_chain
  current_user 
end

修改用于获取模型对象列表的InheritedResource范围。您告诉控制器您始终希望current_user成为应用于查询的第一个范围。