Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit module nesting #2532

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,5 @@ DEPENDENCIES
xpath (= 3.2.0)
yard

RUBY VERSION
ruby 3.2.2p53

BUNDLED WITH
2.4.22
59 changes: 59 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---

version: '2.4'

networks:
internal_network:
driver: bridge

volumes:
bundle_data:
driver: local
postgres_data:
driver: local

x-default-logging: &logging
logging:
options:
max-size: '12m'
max-file: '5'
driver: json-file

services:
console:
image: ruby
command: sleep 10000000;
environment:
CI: t
PGHOST: postgres
PGUSER: administrate
PGPASSWORD: administrate
<<: *logging
networks:
- internal_network
stdin_open: true
tty: true
volumes:
- .:/work
- bundle_data:/usr/local/bundle
working_dir: /work

postgres:
image: postgres
environment:
POSTGRES_USER: administrate
POSTGRES_DB: administrate_test
POSTGRES_PASSWORD: administrate
healthcheck:
test: pg_isready -U notroot -h localhost -p 5432 -d healthcheck-db || exit 1
interval: '10s'
retries: 20
start_period: '10s'
timeout: '7s'
<<: *logging
networks:
- internal_network
ports:
- 5432:5432
volumes:
- postgres_data:/var/lib/postgresql/data
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 -%>
31 changes: 31 additions & 0 deletions spec/generators/dashboard_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,42 @@ 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")

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