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.



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

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



8
9
10
# File 'lib/active_admin/resource/belongs_to.rb', line 8

def owner
  @owner
end

Instance Method Details

#namespaceObject



21
22
23
# File 'lib/active_admin/resource/belongs_to.rb', line 21

def namespace
  @owner.namespace
end

#optional?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/active_admin/resource/belongs_to.rb', line 25

def optional?
  @options[:optional]
end

#targetObject

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



16
17
18
19
# File 'lib/active_admin/resource/belongs_to.rb', line 16

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