2015-04-07 21 views
5

Tôi có ứng dụng Rails 4 sử dụng Active Admin 1.0.0.pre1 kết hợp với pundit 0.3.0 để ủy quyền đã hoạt động hoàn hảo, nhưng tôi gặp khó khăn trong việc tìm ra cách tự động tùy chỉnh biểu mẫu dựa trên người dùng vai trò.Làm cách nào để tự động xóa các mục nhập của Biểu mẫu quản trị viên đang hoạt động với các thuộc tính được phép Pundit?

Với những mô hình:

ActiveAdmin.register AdminUser do 
    permit_params do 
    Pundit.policy(current_admin_user, resource).permitted_attributes 
    end 

    form do |f| 
    f.inputs "Admin Details" do 
     f.input :role, as: :select, collection: [:manager, :admin] 
     f.input :email, as: :email 
     f.input :password 
     f.input :password_confirmation 
    end 
    f.actions 
    end 
end 

class AdminUserPolicy < ApplicationPolicy 
    def permitted_attributes 
    attributes = [:email, :password, :password_confirmation] 
    attributes += [:role] if user.has_role? :super_admin 
    attributes 
    end 
end 

Tôi muốn cho role đầu vào để được tự động xóa khỏi biểu mẫu.

Một lựa chọn sẽ là một cái gì đó dọc theo dòng:

permitted_attributes = Pundit.policy(current_admin_user, resource).permitted_attributes 

    form do |f| 
    f.inputs "Admin Details" do 
     f.input :role if permitted_attributes.include? :role 
     f.input :email 
     f.input :password 
     f.input :password_confirmation 
    end 
    f.actions 
    end 

nhưng, cách tiếp cận này đòi hỏi các nhà phát triển để nhớ được các thuộc tính cần được kiểm tra, có vẻ dễ bị lãng quên và không chính xác DRY. Có lẽ, tôi đang đi về điều này một cách sai lầm? Tất cả các đề xuất đều được chào đón.

+1

Tôi chắc chắn 95%, câu trả lời ẩn ở đâu đó dọc theo phương thức 'main_content' tại đây: https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/views/pages/form.rb. Tôi không có một dự án mở với AA ngay bây giờ để kiểm tra nó ra, nhưng bạn có thể ghi đè lên lớp đó và thử tự loại trừ đầu vào ': role' khỏi buider. –

Trả lời

1

chặn trước ActiveAdminForm bằng cách thêm một mô-đun ghi đè lên chính sách Pundit có vẻ hoạt động tốt. Đây là triển khai mà tôi đã thực hiện với:

# /lib/active_admin/permitted_active_admin_form.rb 
module PermittedActiveAdminForm 
    def permitted_attributes 
    policy = Pundit.policy(current_admin_user, resource) 
    policy.respond_to?(:permitted_attributes) ? policy.permitted_attributes : [] 
    end 

    def input(*args) 
    super(*args) if permitted_attributes.include? args[0] 
    end 
end 


# /config/initializers/active_admin.rb 
module ActiveAdmin 
    module Views 
    class ActiveAdminForm < FormtasticProxy 
     prepend PermittedActiveAdminForm 
    end 
    end 
end 

# /app/admin/admin_user.rb 
ActiveAdmin.register AdminUser do 
    permit_params do 
    resource ||= AdminUser 
    Pundit.policy(current_admin_user, resource).permitted_attributes 
    end 

    form do |f| 
    f.inputs "Admin Details" do 
     f.input :role, as: :select, collection: [:manager, :admin] 
     f.input :email, as: :email 
     f.input :password 
     f.input :password_confirmation 
    end 
    f.actions 
    end 
end 

Nhờ Andrey Deineko bắt đầu tôi xuống con đường bên phải.

0

Tôi biết về chuyên gia, chứ không phải active_admin. Với ý nghĩ đó, sử dụng mã bạn cung cấp, tôi sẽ chỉ ném một ý tưởng ra khỏi đó.

whitelist = Pundit.policy(current_admin_user, resource).permitted_attributes 
fields = %i(role email password password_confirmation) 

form do |f| 
    f.inputs "Admin Details" do 
    (fields & whitelist).each do |field| 
     f.input field 
    end 
    end 
    f.actions 
end 
+0

Cảm ơn bạn đã đề xuất! Vấn đề ở đây là các đối số cho 'f.input' khác nhau tùy thuộc vào trường vì vậy tôi sẽ cần gọi từng đối số một cách rõ ràng với các đối số chính xác. Tôi đã cập nhật ví dụ của mình để phản ánh tốt hơn điều đó. Trong cùng một vô ích, tôi xem xét việc xây dựng một băm trong đó trường đại diện cho khóa với giá trị là các tùy chọn. Sau đó, tôi có thể có một vòng lặp phương pháp trợ giúp trên băm để thực hiện các cuộc gọi chính xác, nhưng tôi đã hy vọng cho một hội nhập chặt chẽ hơn. –

Các vấn đề liên quan