Class: Madmin::Field::Polymorphic

Inherits:
Madmin::Field show all
Defined in:
lib/madmin/field/polymorphic.rb

Overview

This field represents a polymorphic relationship.

Instance Attribute Summary collapse

Attributes inherited from Madmin::Field

#form, #index, #key, #label, #model, #options, #show, #type

Instance Method Summary collapse

Methods inherited from Madmin::Field

#to_partial_path, #value_for

Constructor Details

#initialize(args) ⇒ Polymorphic



9
10
11
12
13
# File 'lib/madmin/field/polymorphic.rb', line 9

def initialize(args)
  super(args)
  @polymorphic_display_value = option_or_default(:display_value, :name)
  @polymorphic_scope = :all
end

Instance Attribute Details

#polymorphic_display_valueObject (readonly)

Returns the value of attribute polymorphic_display_value.



6
7
8
# File 'lib/madmin/field/polymorphic.rb', line 6

def polymorphic_display_value
  @polymorphic_display_value
end

#polymorphic_scopeObject (readonly)

Returns the value of attribute polymorphic_scope.



7
8
9
# File 'lib/madmin/field/polymorphic.rb', line 7

def polymorphic_scope
  @polymorphic_scope
end

Instance Method Details

#polymorphic_id_for(resource) ⇒ Object

Returns the id for the associated polymorphic record.



17
18
19
# File 'lib/madmin/field/polymorphic.rb', line 17

def polymorphic_id_for(resource)
  value_for(resource).id
end

#polymorphic_id_paramObject

Returns the polymorphic ID param.



23
24
25
# File 'lib/madmin/field/polymorphic.rb', line 23

def polymorphic_id_param
  "#{key}_id".to_sym
end

#polymorphic_relationship_exists?(resource) ⇒ Boolean



27
28
29
# File 'lib/madmin/field/polymorphic.rb', line 27

def polymorphic_relationship_exists?(resource)
  resource.send(polymorphic_type_param) && resource.send(polymorphic_id_param)
end

#polymorphic_slug_for(resource) ⇒ Object

Returns the slug for the polymorphic resource.



33
34
35
# File 'lib/madmin/field/polymorphic.rb', line 33

def polymorphic_slug_for(resource)
  Object.const_get("::Madmin::Resources::#{resource.send("#{key}_type")}").new.slug
end

#polymorphic_type_paramObject

Returns the polymorphic type param.



39
40
41
# File 'lib/madmin/field/polymorphic.rb', line 39

def polymorphic_type_param
  "#{key}_type".to_sym
end

#polymorphic_value_for(resource) ⇒ Object

Returns the value to show in the admin for the polymorphic relationship.



46
47
48
# File 'lib/madmin/field/polymorphic.rb', line 46

def polymorphic_value_for(resource)
  value_for(resource).send(polymorphic_display_value)
end

#strong_params_keysObject

Returns the polymorphic stront params keys.



52
53
54
# File 'lib/madmin/field/polymorphic.rb', line 52

def strong_params_keys
  [polymorphic_id_param, polymorphic_type_param]
end