class Administrate::Generators::DashboardGenerator

Constants

ATTRIBUTE_OPTIONS_MAPPING
ATTRIBUTE_TYPE_MAPPING
COLLECTION_ATTRIBUTE_LIMIT
DEFAULT_FIELD_TYPE
READ_ONLY_ATTRIBUTES

Public Instance Methods

create_dashboard_definition() click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 39
def create_dashboard_definition
  template(
    "dashboard.rb.erb",
    Rails.root.join("app/dashboards/#{file_name}_dashboard.rb"),
  )
end
create_resource_controller() click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 46
def create_resource_controller
  destination = Rails.root.join(
    "app/controllers/#{namespace}/#{file_name.pluralize}_controller.rb",
  )

  template("controller.rb.erb", destination)
end

Private Instance Methods

association_type(attribute) click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 126
def association_type(attribute)
  relationship = klass.reflections[attribute.to_s]
  if relationship.has_one?
    "Field::HasOne"
  elsif relationship.collection?
    "Field::HasMany"
  elsif relationship.polymorphic?
    "Field::Polymorphic"
  else
    "Field::BelongsTo"
  end
end
attributes() click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 60
def attributes
  attrs = (
    klass.reflections.keys +
    klass.columns.map(&:name) -
    redundant_attributes
  )

  primary_key = attrs.delete(klass.primary_key)
  created_at = attrs.delete("created_at")
  updated_at = attrs.delete("updated_at")

  [
    primary_key,
    *attrs.sort,
    created_at,
    updated_at,
  ].compact
end
column_type_for_attribute(attr) click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 109
def column_type_for_attribute(attr)
  if enum_column?(attr)
    :enum
  else
    column_types(attr)
  end
end
column_types(attr) click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 122
def column_types(attr)
  klass.columns.find { |column| column.name == attr }.try(:type)
end
enum_column?(attr) click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 117
def enum_column?(attr)
  klass.respond_to?(:defined_enums) &&
    klass.defined_enums.keys.include?(attr)
end
field_type(attribute) click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 98
def field_type(attribute)
  type = column_type_for_attribute(attribute.to_s)

  if type
    ATTRIBUTE_TYPE_MAPPING.fetch(type, DEFAULT_FIELD_TYPE) +
      options_string(ATTRIBUTE_OPTIONS_MAPPING.fetch(type, {}))
  else
    association_type(attribute)
  end
end
form_attributes() click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 79
def form_attributes
  attributes - READ_ONLY_ATTRIBUTES
end
inspect_hash_as_ruby(hash) click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 151
def inspect_hash_as_ruby(hash)
  hash.map do |key, value|
    v_str = value.respond_to?(:call) ? proc_string(value) : value.inspect
    "#{key}: #{v_str}"
  end.join(", ")
end
klass() click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 139
def klass
  @klass ||= Object.const_get(class_name)
end
namespace() click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 56
def namespace
  options[:namespace]
end
options_string(options) click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 143
def options_string(options)
  if options.any?
    ".with_options(#{inspect_hash_as_ruby(options)})"
  else
    ""
  end
end
proc_string(value) click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 158
def proc_string(value)
  source = value.source_location
  proc_string = IO.readlines(source.first)[source.second - 1]
  proc_string[/->[^}]*} | (lambda|proc).*end/x]
end
redundant_attributes() click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 83
def redundant_attributes
  klass.reflections.keys.flat_map do |relationship|
    redundant_attributes_for(relationship)
  end.compact
end
redundant_attributes_for(relationship) click to toggle source
# File lib/generators/administrate/dashboard/dashboard_generator.rb, line 89
def redundant_attributes_for(relationship)
  case association_type(relationship)
  when "Field::Polymorphic"
    [relationship + "_id", relationship + "_type"]
  when "Field::BelongsTo"
    relationship + "_id"
  end
end