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.


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

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

Instance Attribute Details

#ownerObject (readonly)

The resource which initiated this relationship


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

def owner
  @owner
end

#target_nameObject (readonly)

The name of the relation


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

def target_name
  @target_name
end

Instance Method Details

#namespaceObject


36
37
38
# File 'lib/active_admin/resource/belongs_to.rb', line 36

def namespace
  @owner.namespace
end

#optional?Boolean

Returns:

  • (Boolean)

40
41
42
# File 'lib/active_admin/resource/belongs_to.rb', line 40

def optional?
  @options[:optional]
end

#required?Boolean

Returns:

  • (Boolean)

44
45
46
# File 'lib/active_admin/resource/belongs_to.rb', line 44

def required?
  !optional?
end

#resourceObject


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

def resource
  namespace.resources[@options[:class_name]] ||
    namespace.resources[@target_name.to_s.camelize]
end

#targetObject

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


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

def target
  resource or raise TargetNotFound.new (@options[:class_name] || @target_name.to_s.camelize), namespace
end

#to_paramObject


48
49
50
# File 'lib/active_admin/resource/belongs_to.rb', line 48

def to_param
  :"#{@target_name}_id"
end