Class: ActiveAdmin::Resource::BelongsTo

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/resource/belongs_to.rb

Defined Under Namespace

Classes: TargetNotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_resource, target_name, options = {}) ⇒ BelongsTo

Returns a new instance of BelongsTo.



12
13
14
15
# File 'lib/active_admin/resource/belongs_to.rb', line 12

def initialize(owner_resource, target_name, options = {})
  @owner, @target_name = owner_resource, target_name
  @options = options
end

Instance Attribute Details

#ownerObject (readonly)

The resource which initiated this relationship



10
11
12
# File 'lib/active_admin/resource/belongs_to.rb', line 10

def owner
  @owner
end

Instance Method Details

#namespaceObject



23
24
25
# File 'lib/active_admin/resource/belongs_to.rb', line 23

def namespace
  @owner.namespace
end

#optional?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/active_admin/resource/belongs_to.rb', line 27

def optional?
  @options[:optional]
end

#targetObject

Returns the target resource class or raises an exception if it doesn’t exist



18
19
20
21
# File 'lib/active_admin/resource/belongs_to.rb', line 18

def target
  namespace.resources.find_by_key(@target_name.to_s.camelize) or 
    raise TargetNotFound, "Could not find registered resource #{@target_name} in #{namespace.name} with #{namespace.resources.inspect}"
end