Class: Madmin::ResourceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/madmin/resource_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ ResourceBuilder

Returns a new instance of ResourceBuilder.



5
6
7
# File 'lib/madmin/resource_builder.rb', line 5

def initialize(model)
  @model = model
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/madmin/resource_builder.rb', line 3

def model
  @model
end

Instance Method Details

#associationsObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/madmin/resource_builder.rb', line 9

def associations
  model.reflections.reject { |name, association|
    # Hide these special associations
    name.starts_with?("rich_text") ||
      name.ends_with?("_attachment") ||
      name.ends_with?("_attachments") ||
      name.ends_with?("_blob") ||
      name.ends_with?("_blobs")
  }.keys
end

#attributesObject



20
21
22
# File 'lib/madmin/resource_builder.rb', line 20

def attributes
  model.attribute_names + virtual_attributes - redundant_attributes
end

#redundant_attributesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/madmin/resource_builder.rb', line 52

def redundant_attributes
  redundant = []

  # has_secure_password columns
  redundant += model.attribute_types.keys.select { |k| k.ends_with?("_digest") }

  # ActiveRecord Store columns
  store_columns = model.stored_attributes.keys
  redundant += store_columns.map(&:to_s)

  model.reflections.each do |name, association|
    if association.has_one?
      next
    elsif association.collection?
      next
    elsif association.polymorphic?
      redundant << "#{name}_id"
      redundant << "#{name}_type"
    elsif name.starts_with?("rich_text")
      redundant << name
    else # belongs to
      redundant << "#{name}_id"
    end
  end

  redundant
end

#store_accessorsObject



24
25
26
# File 'lib/madmin/resource_builder.rb', line 24

def store_accessors
  model.stored_attributes.values.flatten
end

#virtual_attributesObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/madmin/resource_builder.rb', line 28

def virtual_attributes
  virtual = []

  # has_secure_password columns
  password_attributes = model.attribute_types.keys.select { |k| k.ends_with?("_digest") }.map { |k| k.delete_suffix("_digest") }
  virtual += password_attributes.map { |attr| [attr, "#{attr}_confirmation"] }.flatten

  # ActiveRecord Store columns
  virtual += store_accessors.map(&:to_s)

  # Add virtual attributes for ActionText and ActiveStorage
  model.reflections.each do |name, association|
    if name.starts_with?("rich_text")
      virtual << name.split("rich_text_").last
    elsif name.ends_with?("_attachment")
      virtual << name.split("_attachment").first
    elsif name.ends_with?("_attachments")
      virtual << name.split("_attachments").first
    end
  end

  virtual
end