Skip to content

Commit 1ae6293

Browse files
committed
Merge pull request rails#12177 from wangjohn/creating_application_model_template
Creating application model template
2 parents db2c4ab + 901b137 commit 1ae6293

File tree

235 files changed

+1134
-816
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

235 files changed

+1134
-816
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ application into three layers, each with a specific responsibility.
1111
The _Model layer_ represents your domain model (such as Account, Product,
1212
Person, Post, etc.) and encapsulates the business logic that is specific to
1313
your application. In Rails, database-backed model classes are derived from
14-
`ActiveRecord::Base`. Active Record allows you to present the data from
14+
`ActiveRecord::Base` through an application specific subclass called
15+
`ApplicationRecord`. Active Record allows you to present the data from
1516
database rows as objects and embellish these data objects with business logic
1617
methods. Although most Rails models are backed by a database, models can also
1718
be ordinary Ruby classes, or Ruby classes that implement a set of interfaces
@@ -23,7 +24,8 @@ providing a suitable response. Usually this means returning HTML, but Rails cont
2324
can also generate XML, JSON, PDFs, mobile-specific views, and more. Controllers load and
2425
manipulate models, and render view templates in order to generate the appropriate HTTP response.
2526
In Rails, incoming requests are routed by Action Dispatch to an appropriate controller, and
26-
controller classes are derived from `ActionController::Base`. Action Dispatch and Action Controller
27+
controller classes are derived from `ActionController::Base` through an application specific
28+
subclass called `ApplicationController`. Action Dispatch and Action Controller
2729
are bundled together in Action Pack. You can read more about Action Pack in its
2830
[README](actionpack/README.rdoc).
2931

activerecord/README.rdoc

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
Active Record connects classes to relational database tables to establish an
44
almost zero-configuration persistence layer for applications. The library
5-
provides a base class that, when subclassed, sets up a mapping between the new
6-
class and an existing table in the database. In the context of an application,
7-
these classes are commonly referred to as *models*. Models can also be
8-
connected to other models; this is done by defining *associations*.
5+
provides a base class, and subclassing an application specific version of
6+
this base class sets up a mapping between the new class and an existing
7+
table in the database. In the context of an application, these classes are
8+
commonly referred to as *models*. Models can also be connected to other
9+
models; this is done by defining *associations*.
910

1011
Active Record relies heavily on naming in that it uses class and association
1112
names to establish mappings between respective database tables and foreign key
@@ -17,7 +18,7 @@ A short rundown of some of the major features:
1718

1819
* Automated mapping between classes and tables, attributes and columns.
1920

20-
class Product < ActiveRecord::Base
21+
class Product < ApplicationRecord
2122
end
2223

2324
The Product class is automatically mapped to the table named "products",
@@ -37,7 +38,7 @@ A short rundown of some of the major features:
3738

3839
* Associations between objects defined by simple class methods.
3940

40-
class Firm < ActiveRecord::Base
41+
class Firm < ApplicationRecord
4142
has_many :clients
4243
has_one :account
4344
belongs_to :conglomerate
@@ -48,7 +49,7 @@ A short rundown of some of the major features:
4849

4950
* Aggregations of value objects.
5051

51-
class Account < ActiveRecord::Base
52+
class Account < ApplicationRecord
5253
composed_of :balance, class_name: 'Money',
5354
mapping: %w(balance amount)
5455
composed_of :address,
@@ -60,7 +61,7 @@ A short rundown of some of the major features:
6061

6162
* Validation rules that can differ for new or existing objects.
6263

63-
class Account < ActiveRecord::Base
64+
class Account < ApplicationRecord
6465
validates :subdomain, :name, :email_address, :password, presence: true
6566
validates :subdomain, uniqueness: true
6667
validates :terms_of_service, acceptance: true, on: :create
@@ -72,7 +73,7 @@ A short rundown of some of the major features:
7273

7374
* Callbacks available for the entire life cycle (instantiation, saving, destroying, validating, etc.).
7475

75-
class Person < ActiveRecord::Base
76+
class Person < ApplicationRecord
7677
before_destroy :invalidate_payment_plan
7778
# the `invalidate_payment_plan` method gets called just before Person#destroy
7879
end
@@ -82,7 +83,7 @@ A short rundown of some of the major features:
8283

8384
* Inheritance hierarchies.
8485

85-
class Company < ActiveRecord::Base; end
86+
class Company < ApplicationRecord; end
8687
class Firm < Company; end
8788
class Client < Company; end
8889
class PriorityClient < Client; end

activerecord/lib/active_record.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
module ActiveRecord
3232
extend ActiveSupport::Autoload
3333

34+
autoload :ApplicationConfiguration
3435
autoload :Base
3536
autoload :Callbacks
3637
autoload :Core

activerecord/lib/active_record/aggregations.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def clear_aggregation_cache #:nodoc:
1515
# existing object) and how it can be turned back into attributes (when the entity is saved to
1616
# the database).
1717
#
18-
# class Customer < ActiveRecord::Base
18+
# class Customer < ApplicationRecord
1919
# composed_of :balance, class_name: "Money", mapping: %w(balance amount)
2020
# composed_of :address, mapping: [ %w(address_street street), %w(address_city city) ]
2121
# end
@@ -136,7 +136,7 @@ def clear_aggregation_cache #:nodoc:
136136
# or an array. The <tt>:constructor</tt> and <tt>:converter</tt> options can be used to meet
137137
# these requirements:
138138
#
139-
# class NetworkResource < ActiveRecord::Base
139+
# class NetworkResource < ApplicationRecord
140140
# composed_of :cidr,
141141
# class_name: 'NetAddr::CIDR',
142142
# mapping: [ %w(network_address network), %w(cidr_range bits) ],
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
module ActiveRecord
2+
module ApplicationConfiguration
3+
extend ActiveSupport::Concern
4+
5+
# The <tt>ApplicationConfiguration</tt> module allows you to control different
6+
# configurations that have been set on a particular namespace or model. The
7+
# <tt>ApplicationRecord</tt> class for a particular model can be accessed and used
8+
# for finding configurations.
9+
#
10+
# To access this class, you can use the <tt>application_record</tt> method on
11+
# any subclass of <tt>ActiveRecord::Base</tt>. For example:
12+
#
13+
# class MyModel < ApplicationRecord
14+
# end
15+
#
16+
# ActiveRecord::Base.application_record # => ApplicationRecord
17+
# MyModel.application_record # => ApplicationRecord
18+
#
19+
# Moreover, you can use namespacing of modules with the <tt>application_record</tt>
20+
# method.
21+
#
22+
# module MyNamespace
23+
# class ApplicationRecord < ::ApplicationRecord
24+
# end
25+
#
26+
# class MyModel < ApplicationRecord
27+
# end
28+
# end
29+
#
30+
# ActiveRecord::Base.application_record # => ApplicationRecord
31+
# MyNamespace::MyModel.application_record # => MyNamespace::ApplicationRecord
32+
#
33+
# You may additionally pass in a class to <tt>application_record</tt>, and
34+
# it will look for the configuration of that class. For example:
35+
#
36+
# ActiveRecord::Base.application_record(MyNamespace::MyModel)
37+
# # => MyNamespace::ApplicationRecord
38+
#
39+
# If the class that is passed into <tt>application_record</tt> does not have
40+
# a configuration associated with it, then the method will return the value
41+
# that would have been returned without any argument.
42+
#
43+
# ActiveRecord::Base.application_reocrd('something_with_no_config')
44+
# # => ApplicationRecord
45+
#
46+
module ClassMethods
47+
def configs_from(mod)
48+
app_record = self
49+
define_singleton_method(:application_record) { |klass = nil| app_record }
50+
51+
mod.define_singleton_method(:application_record) { |klass = nil| app_record }
52+
end
53+
54+
def application_record(klass = nil)
55+
return base_app_record unless klass
56+
57+
klass = klass.class unless klass.respond_to?(:parents)
58+
59+
if klass.respond_to?(:application_record)
60+
klass.application_record
61+
elsif app_record = klass.parents.detect { |p| p.respond_to?(:application_record) }
62+
app_record
63+
else
64+
base_app_record
65+
end
66+
end
67+
68+
private
69+
70+
def base_app_record
71+
@base_app_record ||= defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base
72+
end
73+
end
74+
end
75+
end

0 commit comments

Comments
 (0)