Class: ActionCommand::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/action_command/utils.rb

Overview

class with utilities for working with action_commands

Class Method Summary collapse

Class Method Details

.find_id(cls, item) ⇒ Object

Used for cases where you might have a active record object, or an integer id, or some kind of string identifier. If it is an active record object, this will return its id. If it is an integer, it will return it. Otherwise, it will pass the string to yield, which can lookup the object



23
24
25
26
27
# File 'lib/action_command/utils.rb', line 23

def self.find_id(cls, item)
  return item.id if item.is_a? cls
  return item if item.is_a? Integer
  return yield(item)
end

.find_object(cls, item) ⇒ Object

Used for cases where you might want to pass an action a User object, or the integer ID of a user object, or the unique email of a user object, and have the command operate on the user object. Converts an item into an object as follows:

  1. If item is an object of cls, then returns it

  2. If item is an integer, then assumes its and id and returns cls.find(item)

  3. Otherwise, executes the code block and passes it item.



13
14
15
16
17
# File 'lib/action_command/utils.rb', line 13

def self.find_object(cls, item)
  return item if item.is_a? cls
  return cls.find(item) if item.is_a? Integer
  return yield(item)
end