Skip to content

Commit

Permalink
WIP: Not getting anywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed Feb 2, 2023
1 parent 5590beb commit ba0819b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/generators/administrate/routes/routes_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def valid_dashboard_models
def database_models
ActiveRecord::Base.descendants.
reject(&:abstract_class?).
select { |klass| klass < ActiveRecord::Base && klass.name.present? && Kernel.const_defined?(klass.name)}
select { |klass| klass < ActiveRecord::Base && klass.name.present? && Object.constants.include?(klass.name.to_sym) }
end

def invalid_dashboard_models
Expand Down
45 changes: 27 additions & 18 deletions spec/generators/dashboard_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@
end
end

$schema_version = 0
def define_schema(&block)
ActiveRecord::Schema.define(version: $schema_version += 1, &block)
end

after do
define_schema { }
end

describe "dashboard definition file" do
it "has valid syntax" do
dashboard = file("app/dashboards/customer_dashboard.rb")
Expand All @@ -21,7 +30,7 @@
describe "#attribute_types" do
it "includes standard model attributes" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table(:foos) { |t| t.timestamps null: false }
end

Expand All @@ -43,7 +52,7 @@ class Foo < ApplicationRecord

it "includes user-defined database columns" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table(:foos) { |t| t.string :name }
end

Expand All @@ -63,7 +72,7 @@ class Foo < ApplicationRecord

it "sorts the attributes" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table(:foos, primary_key: :code) do |t|
t.string :col_2
t.string :col_1
Expand All @@ -88,7 +97,7 @@ class Foo < ApplicationRecord

it "defaults to a string column that is not searchable" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table(:foos) { |t| t.inet :ip_address }
end

Expand Down Expand Up @@ -117,7 +126,7 @@ class Foo < ApplicationRecord

it "assigns numeric fields a type of `Number`" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table :inventory_items do |t|
t.integer :quantity
t.float :price
Expand All @@ -142,7 +151,7 @@ class InventoryItem < ApplicationRecord
end

it "detects enum field as `Select`" do
ActiveRecord::Schema.define do
define_schema do
create_table :shipments do |t|
t.integer :status
end
Expand All @@ -163,7 +172,7 @@ class Shipment < ApplicationRecord
end

it "handles collection procs option in the 'Select' field" do
ActiveRecord::Schema.define do
define_schema do
create_table :shipments do |t|
t.integer :status
end
Expand Down Expand Up @@ -191,7 +200,7 @@ class Shipment < ApplicationRecord

it "detects boolean values" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table(:users) { |t| t.boolean :active }
end

Expand All @@ -212,7 +221,7 @@ class User < ApplicationRecord
it "assigns dates, times, and datetimes a type of `Date`, `DateTime` and
`Time` respectively" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table :events do |t|
t.date :start_date
t.time :start_time
Expand All @@ -238,7 +247,7 @@ class Event < ApplicationRecord

it "detects belongs_to relationships" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table(:comments) { |t| t.references :post }
end
class Comment < ApplicationRecord
Expand All @@ -258,7 +267,7 @@ class Comment < ApplicationRecord

it "detects polymorphic belongs_to relationships" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table :comments do |t|
t.references :commentable, polymorphic: true
end
Expand All @@ -281,7 +290,7 @@ class Comment < ApplicationRecord

it "detects has_one relationships" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table :accounts

create_table :profiles do |t|
Expand Down Expand Up @@ -312,7 +321,7 @@ class Ticket < ApplicationRecord
if ActiveRecord.version >= Gem::Version.new(5)
it "skips temporary attributes" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table :accounts
end

Expand All @@ -336,7 +345,7 @@ class Account < ApplicationRecord
describe "COLLECTION_ATTRIBUTES" do
it "is limited to a reasonable number of items" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table :foos do |t|
%i(a b c d e f g).each { |attr| t.string attr }
end
Expand Down Expand Up @@ -369,7 +378,7 @@ def table_attribute_limit
describe "FORM_ATTRIBUTES" do
it "does not include read-only attributes" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table :foos do |t|
t.string :name
t.timestamps null: true
Expand All @@ -395,7 +404,7 @@ class Foo < ApplicationRecord
describe "SHOW_PAGE_ATTRIBUTES" do
it "includes all attributes" do
begin
ActiveRecord::Schema.define do
define_schema do
create_table :foos do |t|
t.string :name
t.timestamps null: true
Expand Down Expand Up @@ -429,7 +438,7 @@ class Foo < ApplicationRecord

it "subclasses Admin::ApplicationController by default" do
begin
ActiveRecord::Schema.define { create_table :foos }
define_schema { create_table :foos }
class Foo < ApplicationRecord; end

run_generator ["foo"]
Expand All @@ -445,7 +454,7 @@ class Foo < ApplicationRecord; end

it "uses the given namespace to create controllers" do
begin
ActiveRecord::Schema.define { create_table :foos }
define_schema { create_table :foos }
class Foo < ApplicationRecord; end
module Manager
class ApplicationController < Administrate::ApplicationController; end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/constant_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ConstantHelpers
def remove_constants(*constants)
constants.each { |const| Object.send(:remove_const, const) }
constants.each { |const| Object.send(:remove_const, const.name.to_sym) }
rescue NameError => e
$stderr.puts "Warning from ConstantHelpers::remove_constants:\n\t#{e}"
end
Expand Down

0 comments on commit ba0819b

Please sign in to comment.