Skip to content

Commit

Permalink
Deprecate set_table_name in favour of self.table_name= or defining yo…
Browse files Browse the repository at this point in the history
…ur own method.
  • Loading branch information
jonleighton committed Nov 29, 2011
1 parent f73f534 commit 0b72a04
Show file tree
Hide file tree
Showing 21 changed files with 138 additions and 72 deletions.
16 changes: 8 additions & 8 deletions actionpack/test/activerecord/polymorphic_routes_test.rb
Expand Up @@ -2,36 +2,36 @@
require 'fixtures/project' require 'fixtures/project'


class Task < ActiveRecord::Base class Task < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
end end


class Step < ActiveRecord::Base class Step < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
end end


class Bid < ActiveRecord::Base class Bid < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
end end


class Tax < ActiveRecord::Base class Tax < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
end end


class Fax < ActiveRecord::Base class Fax < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
end end


class Series < ActiveRecord::Base class Series < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
end end


module Blog module Blog
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
end end


class Blog < ActiveRecord::Base class Blog < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
end end


def self.use_relative_model_naming? def self.use_relative_model_naming?
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/fixtures/developer.rb
Expand Up @@ -5,5 +5,5 @@ class Developer < ActiveRecord::Base
end end


class DeVeLoPeR < ActiveRecord::Base class DeVeLoPeR < ActiveRecord::Base
set_table_name "developers" self.table_name = "developers"
end end
16 changes: 16 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,21 @@
## Rails 3.2.0 (unreleased) ## ## Rails 3.2.0 (unreleased) ##


* Deprecated `set_table_name`. Use `self.table_name=` instead, or define your own
`self.table_name` method:

class Project < ActiveRecord::Base
self.table_name = "project"
end

class Post < ActiveRecord::Base
def self.table_name
"special_" + super
end
end
Post.table_name # => "special_posts"

*Jon Leighton*

* Generated association methods are created within a separate module to allow overriding and * Generated association methods are created within a separate module to allow overriding and
composition using `super`. For a class named `MyModel`, the module is named composition using `super`. For a class named `MyModel`, the module is named
`MyModel::GeneratedFeatureMethods`. It is included into the model class immediately after `MyModel::GeneratedFeatureMethods`. It is included into the model class immediately after
Expand Down
81 changes: 59 additions & 22 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -23,6 +23,7 @@
require 'active_support/core_ext/module/introspection' require 'active_support/core_ext/module/introspection'
require 'active_support/core_ext/object/duplicable' require 'active_support/core_ext/object/duplicable'
require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/blank'
require 'active_support/deprecation'
require 'arel' require 'arel'
require 'active_record/errors' require 'active_record/errors'
require 'active_record/log_subscriber' require 'active_record/log_subscriber'
Expand Down Expand Up @@ -624,14 +625,61 @@ def serialize(attr_name, class_name = Object)
# the table name guess for an Invoice class becomes "myapp_invoices". # the table name guess for an Invoice class becomes "myapp_invoices".
# Invoice::Lineitem becomes "myapp_invoice_lineitems". # Invoice::Lineitem becomes "myapp_invoice_lineitems".
# #
# You can also overwrite this class method to allow for unguessable # You can also set your own table name explicitly:
# links, such as a Mouse class with a link to a "mice" table. Example:
# #
# class Mouse < ActiveRecord::Base # class Mouse < ActiveRecord::Base
# set_table_name "mice" # self.table_name = "mice"
# end # end
#
# Alternatively, you can override the table_name method to define your
# own computation. (Possibly using <tt>super</tt> to manipulate the default
# table name.) Example:
#
# class Post < ActiveRecord::Base
# def self.table_name
# "special_" + super
# end
# end
# Post.table_name # => "special_posts"
def table_name def table_name
reset_table_name reset_table_name unless defined?(@table_name)
@table_name
end

# Sets the table name explicitly. Example:
#
# class Project < ActiveRecord::Base
# self.table_name = "project"
# end
#
# You can also just define your own <tt>self.table_name</tt> method; see
# the documentation for ActiveRecord::Base#table_name.
def table_name=(value)
@quoted_table_name = nil
@arel_table = nil
@table_name = value
@relation = Relation.new(self, arel_table)
end

def set_table_name(value = nil, &block) #:nodoc:
if block
ActiveSupport::Deprecation.warn(
"Calling set_table_name is deprecated. If you need to lazily evaluate " \
"the table name, define your own `self.table_name` class method. You can use `super` " \
"to get the default table name where you would have called `original_table_name`."
)

@quoted_table_name = nil
define_attr_method :table_name, value, &block
@arel_table = nil
@relation = Relation.new(self, arel_table)
else
ActiveSupport::Deprecation.warn(
"Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead."
)

self.table_name = value
end
end end


# Returns a quoted version of the table name, used to construct SQL statements. # Returns a quoted version of the table name, used to construct SQL statements.
Expand All @@ -641,9 +689,13 @@ def quoted_table_name


# Computes the table name, (re)sets it internally, and returns it. # Computes the table name, (re)sets it internally, and returns it.
def reset_table_name #:nodoc: def reset_table_name #:nodoc:
return if abstract_class? if superclass.abstract_class?

self.table_name = superclass.table_name || compute_table_name
self.table_name = compute_table_name elsif abstract_class?
self.table_name = superclass == Base ? nil : superclass.table_name
else
self.table_name = compute_table_name
end
end end


def full_table_name_prefix #:nodoc: def full_table_name_prefix #:nodoc:
Expand All @@ -668,21 +720,6 @@ def reset_sequence_name #:nodoc:
default default
end end


# Sets the table name. If the value is nil or false then the value returned by the given
# block is used.
#
# class Project < ActiveRecord::Base
# set_table_name "project"
# end
def set_table_name(value = nil, &block)
@quoted_table_name = nil
define_attr_method :table_name, value, &block
@arel_table = nil

@relation = Relation.new(self, arel_table)
end
alias :table_name= :set_table_name

# Sets the name of the inheritance column to use to the given value, # Sets the name of the inheritance column to use to the given value,
# or (if the value # is nil or false) to the value returned by the # or (if the value # is nil or false) to the value returned by the
# given block. # given block.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/mysql/schema_test.rb
Expand Up @@ -14,7 +14,7 @@ def setup
@db_name = db @db_name = db


@omgpost = Class.new(ActiveRecord::Base) do @omgpost = Class.new(ActiveRecord::Base) do
set_table_name "#{db}.#{table}" self.table_name = "#{db}.#{table}"
def self.name; 'Post'; end def self.name; 'Post'; end
end end
end end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/mysql2/schema_test.rb
Expand Up @@ -14,7 +14,7 @@ def setup
@db_name = db @db_name = db


@omgpost = Class.new(ActiveRecord::Base) do @omgpost = Class.new(ActiveRecord::Base) do
set_table_name "#{db}.#{table}" self.table_name = "#{db}.#{table}"
def self.name; 'Post'; end def self.name; 'Post'; end
end end
end end
Expand Down
10 changes: 5 additions & 5 deletions activerecord/test/cases/adapters/postgresql/schema_test.rb
Expand Up @@ -26,23 +26,23 @@ class SchemaTest < ActiveRecord::TestCase
PK_TABLE_NAME = 'table_with_pk' PK_TABLE_NAME = 'table_with_pk'


class Thing1 < ActiveRecord::Base class Thing1 < ActiveRecord::Base
set_table_name "test_schema.things" self.table_name = "test_schema.things"
end end


class Thing2 < ActiveRecord::Base class Thing2 < ActiveRecord::Base
set_table_name "test_schema2.things" self.table_name = "test_schema2.things"
end end


class Thing3 < ActiveRecord::Base class Thing3 < ActiveRecord::Base
set_table_name 'test_schema."things.table"' self.table_name = 'test_schema."things.table"'
end end


class Thing4 < ActiveRecord::Base class Thing4 < ActiveRecord::Base
set_table_name 'test_schema."Things"' self.table_name = 'test_schema."Things"'
end end


class Thing5 < ActiveRecord::Base class Thing5 < ActiveRecord::Base
set_table_name 'things' self.table_name = 'things'
end end


def setup def setup
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/postgresql/view_test.rb
Expand Up @@ -14,7 +14,7 @@ class ViewTest < ActiveRecord::TestCase
] ]


class ThingView < ActiveRecord::Base class ThingView < ActiveRecord::Base
set_table_name 'test_schema.view_things' self.table_name = 'test_schema.view_things'
end end


def setup def setup
Expand Down
Expand Up @@ -4,7 +4,7 @@


module Namespaced module Namespaced
class Post < ActiveRecord::Base class Post < ActiveRecord::Base
set_table_name 'posts' self.table_name = 'posts'
has_one :tagging, :as => :taggable, :class_name => 'Tagging' has_one :tagging, :as => :taggable, :class_name => 'Tagging'
end end
end end
Expand Down
Expand Up @@ -23,7 +23,7 @@
require 'active_support/core_ext/string/conversions' require 'active_support/core_ext/string/conversions'


class ProjectWithAfterCreateHook < ActiveRecord::Base class ProjectWithAfterCreateHook < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
has_and_belongs_to_many :developers, has_and_belongs_to_many :developers,
:class_name => "DeveloperForProjectWithAfterCreateHook", :class_name => "DeveloperForProjectWithAfterCreateHook",
:join_table => "developers_projects", :join_table => "developers_projects",
Expand All @@ -39,7 +39,7 @@ def add_david
end end


class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
set_table_name 'developers' self.table_name = 'developers'
has_and_belongs_to_many :projects, has_and_belongs_to_many :projects,
:class_name => "ProjectWithAfterCreateHook", :class_name => "ProjectWithAfterCreateHook",
:join_table => "developers_projects", :join_table => "developers_projects",
Expand All @@ -48,7 +48,7 @@ class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
end end


class ProjectWithSymbolsForKeys < ActiveRecord::Base class ProjectWithSymbolsForKeys < ActiveRecord::Base
set_table_name 'projects' self.table_name = 'projects'
has_and_belongs_to_many :developers, has_and_belongs_to_many :developers,
:class_name => "DeveloperWithSymbolsForKeys", :class_name => "DeveloperWithSymbolsForKeys",
:join_table => :developers_projects, :join_table => :developers_projects,
Expand All @@ -57,7 +57,7 @@ class ProjectWithSymbolsForKeys < ActiveRecord::Base
end end


class DeveloperWithSymbolsForKeys < ActiveRecord::Base class DeveloperWithSymbolsForKeys < ActiveRecord::Base
set_table_name 'developers' self.table_name = 'developers'
has_and_belongs_to_many :projects, has_and_belongs_to_many :projects,
:class_name => "ProjectWithSymbolsForKeys", :class_name => "ProjectWithSymbolsForKeys",
:join_table => :developers_projects, :join_table => :developers_projects,
Expand All @@ -66,7 +66,7 @@ class DeveloperWithSymbolsForKeys < ActiveRecord::Base
end end


class DeveloperWithCounterSQL < ActiveRecord::Base class DeveloperWithCounterSQL < ActiveRecord::Base
set_table_name 'developers' self.table_name = 'developers'
has_and_belongs_to_many :projects, has_and_belongs_to_many :projects,
:class_name => "DeveloperWithCounterSQL", :class_name => "DeveloperWithCounterSQL",
:join_table => "developers_projects", :join_table => "developers_projects",
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/associations/join_model_test.rb
Expand Up @@ -733,7 +733,7 @@ def find_post_with_dependency(post_id, association, association_name, dependency
class_name = "PostWith#{association.to_s.classify}#{dependency.to_s.classify}" class_name = "PostWith#{association.to_s.classify}#{dependency.to_s.classify}"
Post.find(post_id).update_column :type, class_name Post.find(post_id).update_column :type, class_name
klass = Object.const_set(class_name, Class.new(ActiveRecord::Base)) klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))
klass.set_table_name 'posts' klass.table_name = 'posts'
klass.send(association, association_name, :as => :taggable, :dependent => dependency) klass.send(association, association_name, :as => :taggable, :dependent => dependency)
klass.find(post_id) klass.find(post_id)
end end
Expand Down
27 changes: 20 additions & 7 deletions activerecord/test/cases/base_test.rb
Expand Up @@ -1431,36 +1431,49 @@ def test_set_table_name_with_value
k = Class.new( ActiveRecord::Base ) k = Class.new( ActiveRecord::Base )
k.table_name = "foo" k.table_name = "foo"
assert_equal "foo", k.table_name assert_equal "foo", k.table_name
k.set_table_name "bar"
assert_deprecated do
k.set_table_name "bar"
end
assert_equal "bar", k.table_name assert_equal "bar", k.table_name
end end


def test_switching_between_table_name def test_switching_between_table_name
assert_difference("GoodJoke.count") do assert_difference("GoodJoke.count") do
Joke.set_table_name "cold_jokes" Joke.table_name = "cold_jokes"
Joke.create Joke.create


Joke.set_table_name "funny_jokes" Joke.table_name = "funny_jokes"
Joke.create Joke.create
end end
end end


def test_quoted_table_name_after_set_table_name def test_quoted_table_name_after_set_table_name
klass = Class.new(ActiveRecord::Base) klass = Class.new(ActiveRecord::Base)


klass.set_table_name "foo" klass.table_name = "foo"
assert_equal "foo", klass.table_name assert_equal "foo", klass.table_name
assert_equal klass.connection.quote_table_name("foo"), klass.quoted_table_name assert_equal klass.connection.quote_table_name("foo"), klass.quoted_table_name


klass.set_table_name "bar" klass.table_name = "bar"
assert_equal "bar", klass.table_name assert_equal "bar", klass.table_name
assert_equal klass.connection.quote_table_name("bar"), klass.quoted_table_name assert_equal klass.connection.quote_table_name("bar"), klass.quoted_table_name
end end


def test_set_table_name_with_block def test_set_table_name_with_block
k = Class.new( ActiveRecord::Base ) k = Class.new( ActiveRecord::Base )
k.set_table_name { "ks" } assert_deprecated do
assert_equal "ks", k.table_name k.set_table_name "foo"
k.set_table_name { original_table_name + "ks" }
end
assert_equal "fooks", k.table_name
end

def test_set_table_name_with_inheritance
k = Class.new( ActiveRecord::Base )
def k.name; "Foo"; end
def k.table_name; super + "ks"; end
assert_equal "foosks", k.table_name
end end


def test_set_primary_key_with_value def test_set_primary_key_with_value
Expand Down

0 comments on commit 0b72a04

Please sign in to comment.