# encoding:utf-8
class AdminPermission < ActiveRecord::Base
  has_paper_trail
  CAN_OPTIONS = %w(manage create update read write import export history)
  # attr_accessible :model, :can
  validates_inclusion_of :can, in: CAN_OPTIONS
  validates :model, :can, presence: true

  has_and_belongs_to_many :admin_roles, join_table: :admin_permissions_admin_roles


  rails_admin do
    navigation_label '系统管理'
    weight -20
    configure :admin_roles do
      inverse_of :admin_permissions
    end
    list do
      filters [:model,:can]
    end
    edit do
      field :model, :enum do
        enum do
          RailsAdmin::Config.models.collect do |model|
            model_name = model.abstract_model.model_name
            [eval("#{model_name}.model_name.human"), model_name]
          end
        end
      end
      field :can, :enum do
        enum do
          CAN_OPTIONS.collect do |can|
            [I18n.t(can), can]
          end
        end
      end
    end
  end

  def name
    model_name = model.present? ? eval("#{model}.model_name.human") : ''
    "#{I18n.t(self.can)}_#{model_name}"
  end
end
