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, target_name, options = {}) ⇒ BelongsTo

Returns a new instance of BelongsTo.



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

def initialize(owner, target_name, options = {})
  @owner, @target_name, @options = owner, target_name, 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

#required?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/active_admin/resource/belongs_to.rb', line 31

def required?
  !optional?
end

#targetObject

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



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

def target
  key = @target_name.to_s.camelize
  namespace.resources[key] or raise TargetNotFound, "Could not find #{key} in" +
    " #{namespace.name} with #{namespace.resources.map(&:resource_name)}"
end