Skip to content

Commit

Permalink
Initial draft of explicitly namespaces modules
Browse files Browse the repository at this point in the history
  • Loading branch information
sdwolfz committed Feb 25, 2024
1 parent 490dd57 commit 94c5600
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 47 deletions.
103 changes: 56 additions & 47 deletions lib/generators/administrate/dashboard/templates/dashboard.rb.erb
Original file line number Diff line number Diff line change
@@ -1,68 +1,77 @@
require "administrate/base_dashboard"

class <%= class_name %>Dashboard < Administrate::BaseDashboard
# ATTRIBUTE_TYPES
# a hash that describes the type of each of the model's fields.
#
# Each different type represents an Administrate::Field object,
# which determines how the attribute is displayed
# on pages throughout the dashboard.
ATTRIBUTE_TYPES = {
<% indent = String.new -%>
<% regular_class_path.map(&:capitalize).each do |module_namespace| -%>
<%= indent %>module <%= module_namespace %>
<% indent += ' ' -%>
<% end -%>
<%= indent %>class <%= file_name.capitalize %>Dashboard < Administrate::BaseDashboard
<%= indent %> # ATTRIBUTE_TYPES
<%= indent %> # a hash that describes the type of each of the model's fields.
<%= indent %> #
<%= indent %> # Each different type represents an Administrate::Field object,
<%= indent %> # which determines how the attribute is displayed
<%= indent %> # on pages throughout the dashboard.
<%= indent %> ATTRIBUTE_TYPES = {
<% attributes.each do |attr| -%>
<%= attr %>: <%= field_type(attr) %>,
<%= indent %><%= attr %>: <%= field_type(attr) %>,
<% end -%>
}.freeze
<%= indent %> }.freeze

# COLLECTION_ATTRIBUTES
# an array of attributes that will be displayed on the model's index page.
#
# By default, it's limited to four items to reduce clutter on index pages.
# Feel free to add, remove, or rearrange items.
COLLECTION_ATTRIBUTES = %i[
<%= indent %> # COLLECTION_ATTRIBUTES
<%= indent %> # an array of attributes that will be displayed on the model's index page.
<%= indent %> #
<%= indent %> # By default, it's limited to four items to reduce clutter on index pages.
<%= indent %> # Feel free to add, remove, or rearrange items.
<%= indent %> COLLECTION_ATTRIBUTES = %i[
<%=
attributes.first(COLLECTION_ATTRIBUTE_LIMIT).map do |attr|
" #{attr}"
indent + " #{attr}"
end.join("\n")
%>
].freeze
<%= indent %> ].freeze

# SHOW_PAGE_ATTRIBUTES
# an array of attributes that will be displayed on the model's show page.
SHOW_PAGE_ATTRIBUTES = %i[
<%= indent %> # SHOW_PAGE_ATTRIBUTES
<%= indent %> # an array of attributes that will be displayed on the model's show page.
<%= indent %> SHOW_PAGE_ATTRIBUTES = %i[
<%=
attributes.map do |attr|
" #{attr}"
indent + " #{attr}"
end.join("\n")
%>
].freeze
<%= indent %> ].freeze

# FORM_ATTRIBUTES
# an array of attributes that will be displayed
# on the model's form (`new` and `edit`) pages.
FORM_ATTRIBUTES = %i[
<%= indent %> # FORM_ATTRIBUTES
<%= indent %> # an array of attributes that will be displayed
<%= indent %> # on the model's form (`new` and `edit`) pages.
<%= indent %> FORM_ATTRIBUTES = %i[
<%=
form_attributes.map do |attr|
" #{attr}"
indent + " #{attr}"
end.join("\n")
%>
].freeze
<%= indent %> ].freeze

# COLLECTION_FILTERS
# a hash that defines filters that can be used while searching via the search
# field of the dashboard.
#
# For example to add an option to search for open resources by typing "open:"
# in the search field:
#
# COLLECTION_FILTERS = {
# open: ->(resources) { resources.where(open: true) }
# }.freeze
COLLECTION_FILTERS = {}.freeze
<%= indent %> # COLLECTION_FILTERS
<%= indent %> # a hash that defines filters that can be used while searching via the search
<%= indent %> # field of the dashboard.
<%= indent %> #
<%= indent %> # For example to add an option to search for open resources by typing "open:"
<%= indent %> # in the search field:
<%= indent %> #
<%= indent %> # COLLECTION_FILTERS = {
<%= indent %> # open: ->(resources) { resources.where(open: true) }
<%= indent %> # }.freeze
<%= indent %> COLLECTION_FILTERS = {}.freeze

# Overwrite this method to customize how <%= file_name.pluralize.humanize.downcase %> are displayed
# across all pages of the admin dashboard.
#
# def display_resource(<%= file_name %>)
# "<%= class_name %> ##{<%= file_name %>.id}"
# end
end
<%= indent %> # Overwrite this method to customize how <%= file_name.pluralize.humanize.downcase %> are displayed
<%= indent %> # across all pages of the admin dashboard.
<%= indent %> #
<%= indent %> # def display_resource(<%= file_name %>)
<%= indent %> # "<%= class_name %> ##{<%= file_name %>.id}"
<%= indent %> # end
<%= indent %>end
<% regular_class_path.reverse.each do |module_namespace| -%>
<% indent = indent[0...-2] -%>
<%= indent %>end
<% end -%>
34 changes: 34 additions & 0 deletions spec/generators/dashboard_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,45 @@ class ApplicationController < Administrate::ApplicationController; end
run_generator ["foo", "--namespace", "manager"]
load file("app/controllers/manager/foos_controller.rb")

binding.pry

expect(Manager::FoosController.ancestors)
.to include(Manager::ApplicationController)
ensure
remove_constants :Foo
Manager.send(:remove_const, :FoosController)
end

it "uses the given namespace and model socpe to create controller and dashboard" do
ActiveRecord::Schema.define { create_table :foo_bar_cars }
module Foo
module Bar
def self.table_name_prefix
"foo_bar_"
end

class Car < Administrate::Generators::TestRecord; end
end
end

module Manager
class ApplicationController < Administrate::ApplicationController; end
end

run_generator ["Foo::Bar::Car", "--namespace", "manager"]
load file("app/controllers/manager/cars_controller.rb")
load file("app/dashboards/car_dashboard.rb")

binding.pry

expect(Manager::CarsController.ancestors)
.to include(Manager::ApplicationController)

expect(Foo::Bar::CarDashboard.ancestors)
.to include(Administrate::BaseDashboard)
ensure
remove_constants :Foo
# Manager.send(:remove_const, :CarsController)
end
end
end

0 comments on commit 94c5600

Please sign in to comment.