-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Extract an administrate:manifest
generator
#362
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
lib/generators/administrate/manifest/manifest_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
Rails.application.eager_load! | ||
require "rails/generators/base" | ||
|
||
module Administrate | ||
module Generators | ||
class ManifestGenerator < Rails::Generators::Base | ||
source_root File.expand_path("../templates", __FILE__) | ||
|
||
def create_dashboard_manifest | ||
template( | ||
"dashboard_manifest.rb.erb", | ||
Rails.root.join("app/dashboards/dashboard_manifest.rb"), | ||
) | ||
end | ||
|
||
def warn_about_invalid_models | ||
namespaced_models.each do |invalid_model| | ||
puts "WARNING: Unable to generate a dashboard for #{invalid_model}." | ||
puts " Administrate does not yet support namespaced models." | ||
end | ||
|
||
models_without_tables.each do |invalid_model| | ||
puts "WARNING: Unable to generate a dashboard for #{invalid_model}." | ||
puts " It is not connected to a database table." | ||
end | ||
|
||
unnamed_constants.each do |invalid_model| | ||
puts "NOTICE: Skipping dynamically generated model #{invalid_model}." | ||
end | ||
end | ||
|
||
private | ||
|
||
def dashboard_resources | ||
valid_dashboard_models.map do |model| | ||
model.to_s.pluralize.underscore | ||
end | ||
end | ||
|
||
def valid_dashboard_models | ||
database_models - invalid_database_models | ||
end | ||
|
||
def database_models | ||
ActiveRecord::Base.descendants | ||
end | ||
|
||
def invalid_database_models | ||
models_without_tables + namespaced_models + unnamed_constants | ||
end | ||
|
||
def models_without_tables | ||
database_models.reject(&:table_exists?) | ||
end | ||
|
||
def namespaced_models | ||
database_models.select { |model| model.to_s.include?("::") } | ||
end | ||
|
||
def unnamed_constants | ||
ActiveRecord::Base.descendants.reject { |d| d.name == d.to_s } | ||
end | ||
end | ||
end | ||
end |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require "rails_helper" | ||
require "generators/administrate/manifest/manifest_generator" | ||
require "support/generator_spec_helpers" | ||
require "support/constant_helpers" | ||
|
||
describe Administrate::Generators::ManifestGenerator, :generator do | ||
describe "dashboard_manifest" do | ||
it "is copied to the dashboards folder" do | ||
stub_generator_dependencies | ||
manifest = file("app/dashboards/dashboard_manifest.rb") | ||
|
||
run_generator | ||
|
||
expect(manifest).to exist | ||
expect(manifest).to have_correct_syntax | ||
expect(manifest).to contain("class DashboardManifest") | ||
expect(manifest).to contain("DASHBOARDS = [") | ||
expect(manifest).to contain("ROOT_DASHBOARD = DASHBOARDS.first") | ||
end | ||
|
||
it "populates default dashboards based on current ActiveRecord models" do | ||
stub_generator_dependencies | ||
manifest = file("app/dashboards/dashboard_manifest.rb") | ||
|
||
run_generator | ||
|
||
[:customers, :line_items, :orders, :products].each do |model| | ||
expect(manifest).to contain(":#{model}") | ||
end | ||
expect(manifest).not_to contain("Delayed::Backend::ActiveRecord::Job") | ||
end | ||
|
||
it "skips namespaced models with a warning" do | ||
stub_generator_dependencies | ||
manifest = file("app/dashboards/dashboard_manifest.rb") | ||
|
||
run_generator | ||
|
||
expect(manifest).not_to contain("delayed/backend/active_record/jobs") | ||
end | ||
|
||
it "skips models that aren't backed by the database" do | ||
begin | ||
class ModelWithoutDBTable < ActiveRecord::Base; end | ||
stub_generator_dependencies | ||
manifest = file("app/dashboards/dashboard_manifest.rb") | ||
|
||
run_generator | ||
|
||
expect(manifest).not_to contain("model_without_db_table") | ||
ensure | ||
remove_constants :ModelWithoutDBTable | ||
end | ||
end | ||
|
||
it "skips models that don't have a named constant" do | ||
stub_generator_dependencies | ||
ActiveRecord::Schema.define { create_table(:foos) } | ||
_unnamed_model = Class.new(ActiveRecord::Base) do | ||
def self.table_name | ||
:foos | ||
end | ||
end | ||
|
||
run_generator | ||
|
||
manifest = file("app/dashboards/dashboard_manifest.rb") | ||
expect(manifest).to have_correct_syntax | ||
end | ||
end | ||
|
||
def stub_generator_dependencies | ||
allow(Rails::Generators).to receive(:invoke) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error is only raised in the test suite.
When a user runs this generator without
DashboardManifest
defined, the generator will invoke the manifest generator to fill in the missing piece.In the test suite, we stub out the invocation of the manifest generator. The generator does not run, and
DashboardManifest
remains undefined. When the install generator tries to useDashboardManifest
as the basis for the installation, it comes up empty and raises an error.