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

max

I have a helper method which generates "crud" links for a resource. 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.

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?

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
Max Williams
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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I check if an instance class is equal to a variable inside the class?

From Dev

How do I include a gem's class method in a Rails model?

From Dev

Rails how do I check for existance of a class when not instantiated yet?

From Dev

How do I expect a message whose argument is any instance of a class in RSpec?

From Dev

How do I create a method with a Class as an argument?

From Dev

How to correctly format instance variables on a Rails model class

From Dev

Rails model instance variable not declared in the model class

From Dev

How do I check for duplicates in a List<Model>

From Dev

How do I access the instance of an enclosing class?

From Dev

How do I access an instance of a class in Swift

From Dev

How can I check if my Django model instance is the root?

From Dev

How do I clone a Rails model attribute?

From Dev

How do I access my model class inside my module rails?

From Dev

How do I pass instance of an object as an argument in a function in python?

From Dev

How do you check the class of a Python csv.writer instance?

From Dev

How do i pass a class as an argument and then use a shared method of that class?

From Dev

How do I check a class in parse? - Swift

From Dev

How do I assign user_id and journal_id to the Deal model instance imported from excel (Rails)

From Dev

How can I check if a object is an instance of a specific class?

From Dev

How to add an instance of a model to an instance of another in Rails

From Dev

What is the use of instance variables in the rails model class

From Dev

How do I create a basic class and instance of that class in Forth?

From Dev

How do I template a class using an instance of another templated class?

From Dev

How do I create a basic class and instance of that class in Forth?

From Dev

How do I create an instance of a inner class in the outer class constructor

From Dev

How do I use "as" to check if an object is of a type passed by argument?

From Dev

How do I subscribe to a model instance in Sails.JS?

From Dev

How do I generate standardized model instance template code in django?

From Dev

How do I Bind an Instance of a Model to a Parameter in a Controller Action

Related Related

  1. 1

    How do I check if an instance class is equal to a variable inside the class?

  2. 2

    How do I include a gem's class method in a Rails model?

  3. 3

    Rails how do I check for existance of a class when not instantiated yet?

  4. 4

    How do I expect a message whose argument is any instance of a class in RSpec?

  5. 5

    How do I create a method with a Class as an argument?

  6. 6

    How to correctly format instance variables on a Rails model class

  7. 7

    Rails model instance variable not declared in the model class

  8. 8

    How do I check for duplicates in a List<Model>

  9. 9

    How do I access the instance of an enclosing class?

  10. 10

    How do I access an instance of a class in Swift

  11. 11

    How can I check if my Django model instance is the root?

  12. 12

    How do I clone a Rails model attribute?

  13. 13

    How do I access my model class inside my module rails?

  14. 14

    How do I pass instance of an object as an argument in a function in python?

  15. 15

    How do you check the class of a Python csv.writer instance?

  16. 16

    How do i pass a class as an argument and then use a shared method of that class?

  17. 17

    How do I check a class in parse? - Swift

  18. 18

    How do I assign user_id and journal_id to the Deal model instance imported from excel (Rails)

  19. 19

    How can I check if a object is an instance of a specific class?

  20. 20

    How to add an instance of a model to an instance of another in Rails

  21. 21

    What is the use of instance variables in the rails model class

  22. 22

    How do I create a basic class and instance of that class in Forth?

  23. 23

    How do I template a class using an instance of another templated class?

  24. 24

    How do I create a basic class and instance of that class in Forth?

  25. 25

    How do I create an instance of a inner class in the outer class constructor

  26. 26

    How do I use "as" to check if an object is of a type passed by argument?

  27. 27

    How do I subscribe to a model instance in Sails.JS?

  28. 28

    How do I generate standardized model instance template code in django?

  29. 29

    How do I Bind an Instance of a Model to a Parameter in a Controller Action

HotTag

Archive