forked from thoughtbot/administrate
/
routes_generator_spec.rb
125 lines (94 loc) · 3.44 KB
/
routes_generator_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require "rails_helper"
require "support/constant_helpers"
require "support/generator_spec_helpers"
require "generators/administrate/routes/routes_generator"
describe Administrate::Generators::RoutesGenerator, :generator do
before { stub_generator_dependencies }
after { reset_routes }
describe "routes" do
it "populates default dashboards based on current ActiveRecord models" do
routes = file("config/routes.rb")
run_generator
[:customers, :line_items, :orders, :products].each do |model|
expect(routes).to contain("resources :#{model}")
end
end
it "populates default dashboards under the given namespace" do
routes = file("config/routes.rb")
run_generator ["--namespace", "manager"]
expect(routes).to contain("namespace :manager")
end
it "does not populate routes when no models exist" do
routes = file("config/routes.rb")
allow(ActiveRecord::Base).to receive(:descendants).and_return([])
run_generator
expect(routes).not_to contain("namespace :admin")
end
it "skips namespaced models with a warning" do
routes = file("config/routes.rb")
output = run_generator
expect(routes).not_to contain("blog")
expect(routes).not_to contain("post")
expect(output).to include <<-MSG.strip_heredoc
WARNING: Unable to generate a dashboard for Blog::Post.
- Administrate does not yet support namespaced models.
MSG
end
it "skips models that aren't backed by the database with a warning" do
begin
class ModelWithoutDBTable < ApplicationRecord; end
routes = file("config/routes.rb")
output = run_generator
expect(routes).not_to contain("model_without_db_table")
expect(output).to include("WARNING: Unable to generate a dashboard " \
"for ModelWithoutDBTable.")
ensure
remove_constants :ModelWithoutDBTable
end
end
it "skips models that don't have a named constant" do
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Schema.define { create_table(:foos) }
end
_unnamed_model = Class.new(ApplicationRecord) do
def self.table_name
:foos
end
end
run_generator
routes = file("config/routes.rb")
expect(routes).to have_correct_syntax
end
it "skips abstract models without a warning" do
stub_generator_dependencies
routes = file("config/routes.rb")
begin
class AbstractModel < ApplicationRecord
self.abstract_class = true
end
output = run_generator
expect(routes).not_to contain("abstract_model")
expect(output).not_to include("WARNING: Unable to generate a "\
"dashboard for AbstractModel")
end
end
it "groups together warnings related to the same model" do
class TestModelNamespace
class ModelWithoutDBTable < ApplicationRecord; end
end
model_name = TestModelNamespace::ModelWithoutDBTable.to_s
output = run_generator
warning = "WARNING: Unable to generate a dashboard " \
"for #{model_name}."
occurrences = output.scan(warning).count
expect(occurrences).to eq(1)
ensure
remove_constants :TestModelNamespace
end
end
it "creates a root route for the admin namespace" do
routes = file("config/routes.rb")
run_generator
expect(routes).to contain('root to: "customers#index')
end
end