class Madmin::Generators::ResourceGenerator

Public Instance Methods

eager_load() click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 8
def eager_load
  Rails.application.eager_load!
end
generate_controller() click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 16
def generate_controller
  destination = Rails.root.join("app/controllers/madmin/#{file_path.pluralize}_controller.rb")
  template("controller.rb", destination)
end
generate_resource() click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 12
def generate_resource
  template "resource.rb", "app/madmin/resources/#{file_path}_resource.rb"
end
generate_route() click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 21
def generate_route
  if route_namespace_exists?
    route "resources :#{plural_name}", namespace: class_path, indentation: separated_routes_file? ? 2 : 4, sentinel: /namespace :madmin do\s*\n/m
  else
    route "resources :#{plural_name}", namespace: [:madmin] + class_path
  end
end

Private Instance Methods

associations() click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 31
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
attributes() click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 42
def attributes
  model.attribute_names + virtual_attributes - redundant_attributes
end
formatted_options_for_attribute(name) click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 95
def formatted_options_for_attribute(name)
  options = options_for_attribute(name)
  return if options.blank?

  ", " + options.map { |key, value|
    "#{key}: #{value}"
  }.join(", ")
end
model() click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 91
def model
  @model ||= class_name.constantize
end
options_for_attribute(name) click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 104
def options_for_attribute(name)
  if %w[id created_at updated_at].include?(name)
    {form: false}

  # has_secure_passwords should only show on forms
  elsif name.ends_with?("_confirmation") || virtual_attributes.include?("#{name}_confirmation")
    {index: false, show: false}

  # Counter cache columns are typically not editable
  elsif name.ends_with?("_count")
    {form: false}

  # Attributes without a database column
  elsif !model.column_names.include?(name)
    {index: false}
  end
end
redundant_attributes() click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 67
def redundant_attributes
  redundant = []

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

  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
virtual_attributes() click to toggle source
# File lib/generators/madmin/resource/resource_generator.rb, line 46
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

  # 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