Skip to content

Commit

Permalink
AR models should inherit from ApplicationRecord
Browse files Browse the repository at this point in the history
In investigating #581, we released the `ApplicationRecord` change
introduced in Rails 5 did not happen in our `example_app` models.
  • Loading branch information
nickcharlton committed Jul 12, 2019
1 parent 7ed5b07 commit 379ca51
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 40 deletions.
3 changes: 3 additions & 0 deletions spec/example_app/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
2 changes: 1 addition & 1 deletion spec/example_app/app/models/blog/post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Blog
class Post < ActiveRecord::Base
class Post < ApplicationRecord
validates :title, :body, presence: true
end
end
2 changes: 1 addition & 1 deletion spec/example_app/app/models/country.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Country < ActiveRecord::Base
class Country < ApplicationRecord
validates :code, :name, presence: true
end
2 changes: 1 addition & 1 deletion spec/example_app/app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Customer < ActiveRecord::Base
class Customer < ApplicationRecord
has_many :orders, dependent: :destroy
belongs_to :country, foreign_key: :country_code, primary_key: :code
has_many :log_entries, as: :logeable
Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/models/line_item.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class LineItem < ActiveRecord::Base
class LineItem < ApplicationRecord
belongs_to :order
belongs_to :product

Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/models/log_entry.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class LogEntry < ActiveRecord::Base
class LogEntry < ApplicationRecord
belongs_to :logeable, polymorphic: true

validate do |log_entry|
Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/models/order.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Order < ActiveRecord::Base
class Order < ApplicationRecord
belongs_to :customer

validates :customer, presence: true
Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/models/payment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Payment < ActiveRecord::Base
class Payment < ApplicationRecord
belongs_to :order
end
2 changes: 1 addition & 1 deletion spec/example_app/app/models/product.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Product < ActiveRecord::Base
class Product < ApplicationRecord
has_many :line_items, dependent: :destroy
has_one :product_meta_tag, dependent: :destroy

Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/models/product_meta_tag.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ProductMetaTag < ActiveRecord::Base
class ProductMetaTag < ApplicationRecord
belongs_to :product

validates :meta_title, :meta_description, presence: true
Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/models/series.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Series < ActiveRecord::Base
class Series < ApplicationRecord
validates :name, presence: true
end
46 changes: 23 additions & 23 deletions spec/generators/dashboard_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
create_table(:foos) { |t| t.timestamps null: false }
end

class Foo < ActiveRecord::Base
class Foo < ApplicationRecord
reset_column_information
end

Expand All @@ -47,7 +47,7 @@ class Foo < ActiveRecord::Base
create_table(:foos) { |t| t.string :name }
end

class Foo < ActiveRecord::Base
class Foo < ApplicationRecord
reset_column_information
end

Expand All @@ -67,7 +67,7 @@ class Foo < ActiveRecord::Base
create_table(:foos) { |t| t.inet :ip_address }
end

class Foo < ActiveRecord::Base
class Foo < ApplicationRecord
reset_column_information
end

Expand All @@ -91,7 +91,7 @@ class Foo < ActiveRecord::Base
end

it "looks for class_name options on has_many fields" do
class Customer < ActiveRecord::Base
class Customer < ApplicationRecord
reset_column_information
has_many :purchases, class_name: "Order", foreign_key: "purchase_id"
end
Expand All @@ -113,7 +113,7 @@ class Customer < ActiveRecord::Base
end
end

class InventoryItem < ActiveRecord::Base
class InventoryItem < ApplicationRecord
reset_column_information
end

Expand All @@ -138,7 +138,7 @@ class InventoryItem < ActiveRecord::Base
end
end

class Shipment < ActiveRecord::Base
class Shipment < ApplicationRecord
enum status: [:ready, :processing, :shipped]
reset_column_information
end
Expand All @@ -160,7 +160,7 @@ class Shipment < ActiveRecord::Base
create_table(:users) { |t| t.boolean :active }
end

class User < ActiveRecord::Base
class User < ApplicationRecord
reset_column_information
end

Expand All @@ -185,7 +185,7 @@ class User < ActiveRecord::Base
end
end

class Event < ActiveRecord::Base
class Event < ApplicationRecord
reset_column_information
end

Expand Down Expand Up @@ -214,15 +214,15 @@ class Event < ActiveRecord::Base
end
end

class Concert < ActiveRecord::Base
class Concert < ApplicationRecord
reset_column_information
has_many :tickets
has_many :attendees, through: :tickets, source: :person
has_many :venues, through: :tickets
has_many :numbers, through: :tickets
end

class Ticket < ActiveRecord::Base
class Ticket < ApplicationRecord
reset_column_information
belongs_to :concert
belongs_to :person
Expand All @@ -231,7 +231,7 @@ class Ticket < ActiveRecord::Base
end

class Number; end
class Person < ActiveRecord::Base
class Person < ApplicationRecord
reset_column_information
end

Expand All @@ -254,7 +254,7 @@ class Person < ActiveRecord::Base
ActiveRecord::Schema.define do
create_table(:comments) { |t| t.references :post }
end
class Comment < ActiveRecord::Base
class Comment < ApplicationRecord
belongs_to :post
end

Expand All @@ -278,8 +278,8 @@ class Comment < ActiveRecord::Base
t.references :recipient
end
end
class User < ActiveRecord::Base; end
class Invitation < ActiveRecord::Base
class User < ApplicationRecord; end
class Invitation < ApplicationRecord
belongs_to :sender, class_name: "User"
belongs_to :recipient, class_name: "User"
end
Expand All @@ -304,7 +304,7 @@ class Invitation < ActiveRecord::Base
t.references :commentable, polymorphic: true
end
end
class Comment < ActiveRecord::Base
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
end

Expand All @@ -330,12 +330,12 @@ class Comment < ActiveRecord::Base
end
end

class Account < ActiveRecord::Base
class Account < ApplicationRecord
reset_column_information
has_one :profile
end

class Ticket < ActiveRecord::Base
class Ticket < ApplicationRecord
reset_column_information
belongs_to :account
end
Expand All @@ -357,7 +357,7 @@ class Ticket < ActiveRecord::Base
create_table :accounts
end

class Account < ActiveRecord::Base
class Account < ApplicationRecord
reset_column_information
attribute :tmp_attribute, :boolean
end
Expand All @@ -383,7 +383,7 @@ class Account < ActiveRecord::Base
end
end

class Foo < ActiveRecord::Base
class Foo < ApplicationRecord
reset_column_information
end

Expand Down Expand Up @@ -414,7 +414,7 @@ def table_attribute_limit
end
end

class Foo < ActiveRecord::Base
class Foo < ApplicationRecord
reset_column_information
end

Expand All @@ -440,7 +440,7 @@ class Foo < ActiveRecord::Base
end
end

class Foo < ActiveRecord::Base
class Foo < ApplicationRecord
reset_column_information
end

Expand Down Expand Up @@ -468,7 +468,7 @@ class Foo < ActiveRecord::Base
it "subclasses Admin::ApplicationController by default" do
begin
ActiveRecord::Schema.define { create_table :foos }
class Foo < ActiveRecord::Base; end
class Foo < ApplicationRecord; end

run_generator ["foo"]
load file("app/controllers/admin/foos_controller.rb")
Expand All @@ -484,7 +484,7 @@ class Foo < ActiveRecord::Base; end
it "uses the given namespace to create controllers" do
begin
ActiveRecord::Schema.define { create_table :foos }
class Foo < ActiveRecord::Base; end
class Foo < ApplicationRecord; end
module Manager
class ApplicationController < Administrate::ApplicationController; end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/generators/routes_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

it "skips models that aren't backed by the database with a warning" do
begin
class ModelWithoutDBTable < ActiveRecord::Base; end
class ModelWithoutDBTable < ApplicationRecord; end
routes = file("config/routes.rb")

output = run_generator
Expand All @@ -69,7 +69,7 @@ class ModelWithoutDBTable < ActiveRecord::Base; end
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Schema.define { create_table(:foos) }
end
_unnamed_model = Class.new(ActiveRecord::Base) do
_unnamed_model = Class.new(ApplicationRecord) do
def self.table_name
:foos
end
Expand All @@ -86,7 +86,7 @@ def self.table_name
routes = file("config/routes.rb")

begin
class AbstractModel < ActiveRecord::Base
class AbstractModel < ApplicationRecord
self.abstract_class = true
end

Expand Down
8 changes: 4 additions & 4 deletions spec/lib/administrate/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MockDashboardWithAssociation
describe "#run" do
it "returns all records when no search term" do
begin
class User < ActiveRecord::Base; end
class User < ApplicationRecord; end
scoped_object = User.default_scoped
search = Administrate::Search.new(scoped_object,
MockDashboard,
Expand All @@ -48,7 +48,7 @@ class User < ActiveRecord::Base; end

it "returns all records when search is empty" do
begin
class User < ActiveRecord::Base; end
class User < ApplicationRecord; end
scoped_object = User.default_scoped
search = Administrate::Search.new(scoped_object,
MockDashboard,
Expand All @@ -63,7 +63,7 @@ class User < ActiveRecord::Base; end

it "searches using LOWER + LIKE for all searchable fields" do
begin
class User < ActiveRecord::Base; end
class User < ApplicationRecord; end
scoped_object = User.default_scoped
search = Administrate::Search.new(scoped_object,
MockDashboard,
Expand All @@ -88,7 +88,7 @@ class User < ActiveRecord::Base; end

it "converts search term LOWER case for latin and cyrillic strings" do
begin
class User < ActiveRecord::Base; end
class User < ApplicationRecord; end
scoped_object = User.default_scoped
search = Administrate::Search.new(scoped_object,
MockDashboard,
Expand Down

0 comments on commit 379ca51

Please sign in to comment.