简体   繁体   English

如何检查参数是模型的实例还是Rails中的类?

[英]How do I check if an argument is an instance of model or the class in Rails?

I have a helper method which generates "crud" links for a resource. 我有一个帮助方法,为资源生成“crud”链接。 Right now it only handles edit and destroy. 现在它只处理编辑和销毁。 But I want to refactor it so that it would create a new link if passed a model class instead of an instance. 但我想重构它,以便在传递模型类而不是实例时创建new链接。

Desired output: 期望的输出:

> crud_links_for_resource(Product)
> { :new => '<a href="/products/new">Create a new product.</a>'}

What is the best way to check if a variable is the class of a model or an instance? 检查变量是模型类还是实例的最佳方法是什么? I thought of using duck typing ( resource.respond_to? :new_record? ) but is there a better way? 我想过使用duck typing( resource.respond_to? :new_record? )但是有更好的方法吗?

module PermissionsHelper
  # Generates a hash of links to edit or destroy a resource
  # @param [ActiveModel] resource
  # @param [Actions] a list of crud actions to create links to
  # @param [Hash] kwargs optional hash to pass to link to
  # @option kwargs [String] :controller - controller name to use.
  #   Otherwise a guess is performed based on the resource class name.
  # @option kwargs [Hash] url_extras - passed to url_for. Can be used for nested resources.
  # @return [Hash] a list of links to actions which the user is allowed to perform
  def crud_links_for_resource(resource,  actions = [:destroy, :edit], **kwargs)
    privledges = actions.keep_if { |action| can? action, resource }
    privledges.each_with_object({}) do |action, hash|
      i18n_key = resource.model_name.i18n_key
      txt = t("#{ i18n_key }.#{action}")
      controller = kwargs[:controller] || i18n_key.to_s.pluralize
      url_extras = kwargs[:url_extras] || {}
      options = kwargs.except(:controller, :url_extras)
      case action
        when :destroy
          options.merge!(method: :delete, confirm: t("#{ i18n_key }.confirm_#{action}"))
        else
      end
      hash[action] = link_to(txt, { action: action, controller: controller, id: resource }.merge(url_extras) ,options)
    end
  end
end
if myobject.is_a?(Foo)
  #it's an instance of foo
else
  #it's something else
end

or 要么

if myobject.is_a?(Class)
  #it's a class
else
  #it's not
end

如果你想确保有问题的对象不仅是任何类而是模型类 - 即ActiveRecord::Base的(直接或间接)后代:

my_object.is_a?(Class) && my_object < ActiveRecord::Base

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Ruby on Rails的模型类中引用模型的实例? - How do you refer an instance of a model in model class in Ruby on Rails? Rails - 如何临时存储 rails model 实例? - Rails - How do I temporarily store a rails model instance? 在Rails中,如何测试在控制器中对Model实例所做的修改? - In Rails how do I test modifications to an instance of a Model that are made in a controller? 如何在Rails中访问父模型的实例变量? - How do I access instance variables of parent model in Rails? 如何在 Rails API 中使用 FK 检索模型的实例? - How do I retrieve a instance of the model with a FK in a Rails API? 如何在Rails模型中包含gem的class方法? - How do I include a gem's class method in a Rails model? Rails-如何访问父模型中的类? - Rails - How do I access a class in the parent model? Rails如何在未实例化的情况下检查类的存在性? - Rails how do I check for existance of a class when not instantiated yet? 我如何期待一条消息,其参数是RSpec中类的任何实例? - How do I expect a message whose argument is any instance of a class in RSpec? Rails 4,如何从表单中的字符串字段创建新的模型实例? - Rails 4, How do I create a new model instance from a string field in a form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM