Class: Infold::Association

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
ActiveModel::Model
Defined in:
lib/infold/property/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, kind:, table:, field_group: [], name: nil, **attrs) ⇒ Association

Returns a new instance of Association.



35
36
37
38
39
40
41
42
# File 'lib/infold/property/association.rb', line 35

def initialize(field, kind:, table:, field_group: [], name: nil, **attrs)
  @field = field
  @kind = kind
  @table = table
  @field_group = field_group
  @name = name || field.name
  super(**attrs)
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



16
17
18
# File 'lib/infold/property/association.rb', line 16

def class_name
  @class_name
end

#dependentObject

Returns the value of attribute dependent.



16
17
18
# File 'lib/infold/property/association.rb', line 16

def dependent
  @dependent
end

#fieldObject (readonly)

Returns the value of attribute field.



8
9
10
# File 'lib/infold/property/association.rb', line 8

def field
  @field
end

#field_groupObject (readonly)

Returns the value of attribute field_group.



8
9
10
# File 'lib/infold/property/association.rb', line 8

def field_group
  @field_group
end

#foreign_keyObject

Returns the value of attribute foreign_key.



16
17
18
# File 'lib/infold/property/association.rb', line 16

def foreign_key
  @foreign_key
end

#kindObject (readonly)

Returns the value of attribute kind.



8
9
10
# File 'lib/infold/property/association.rb', line 8

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/infold/property/association.rb', line 8

def name
  @name
end

#name_fieldObject



81
82
83
# File 'lib/infold/property/association.rb', line 81

def name_field
  @name_field.presence || 'id'
end

#tableObject (readonly)

Returns the value of attribute table.



8
9
10
# File 'lib/infold/property/association.rb', line 8

def table
  @table
end

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/infold/property/association.rb', line 44

def belongs_to?
  kind.to_sym == :belongs_to
end

#belongs_to_show_path(object) ⇒ Object



77
78
79
# File 'lib/infold/property/association.rb', line 77

def belongs_to_show_path(object)
  "admin_#{model_name( :snake)}_path(#{object})"
end

#find_or_initialize_field(field_name) ⇒ Object



69
70
71
# File 'lib/infold/property/association.rb', line 69

def find_or_initialize_field(field_name)
  field_group.find_or_initialize_field(field_name)
end

#has_child?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/infold/property/association.rb', line 56

def has_child?
  !belongs_to?
end

#has_many?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/infold/property/association.rb', line 48

def has_many?
  kind.to_sym == :has_many
end

#has_one?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/infold/property/association.rb', line 52

def has_one?
  kind.to_sym == :has_one
end

#model_name(*attr) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/infold/property/association.rb', line 60

def model_name(*attr)
  name = class_name.presence || self.name.singularize.camelize
  name = name.underscore if attr.include?(:snake)
  name = name.camelize if attr.include?(:camel)
  name = name.singularize if attr.include?(:single)
  name = name.pluralize if attr.include?(:multi)
  name
end

#search_pathObject



73
74
75
# File 'lib/infold/property/association.rb', line 73

def search_path
  "admin_#{model_name(:multi, :snake)}_path"
end