module Believer::ModelSchema::ClassMethods
Public Instance Methods
Returns a quoted version of the table name, used to construct SQL statements.
# File lib/believer/model_schema.rb, line 113 def quoted_table_name @quoted_table_name ||= "`#{table_name}`" end
Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending directly from Believer::Base
. So if the hierarchy looks like: Reply < Message < Believer::Base
, then Message is used to guess the table name even when called on Reply. The rules used to do the guess are handled by the Inflector class in Active Support, which knows almost all common English inflections. You can add new inflections in config/initializers/inflections.rb.
Nested classes are given table names prefixed by the singular form of the parent’s table name. Enclosing modules are not considered.
Examples¶ ↑
class Invoice < Believer::Base end file class table_name invoice.rb Invoice invoices class Invoice < Believer::Base class Lineitem < Believer::Base end end file class table_name invoice.rb Invoice::Lineitem invoice_lineitems module Invoice class Lineitem < Believer::Base end end file class table_name invoice/lineitem.rb Invoice::Lineitem lineitems
Additionally, the class-level table_name_prefix
is prepended and the table_name_suffix
is appended. So if you have “myapp_” as a prefix, the table name guess for an Invoice class becomes “myapp_invoices”. Invoice::Lineitem becomes “myapp_invoice_lineitems”.
You can also set your own table name explicitly:
class Mouse < Believer::Base self.table_name = "mice" end
Alternatively, you can override the table_name
method to define your own computation. (Possibly using super
to manipulate the default table name.) Example:
class Post < Believer::Base def self.table_name "special_" + super end end Post.table_name # => "special_posts"
# File lib/believer/model_schema.rb, line 93 def 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 self.table_name
method; see the documentation for ActiveRecord::Base#table_name.
# File lib/believer/model_schema.rb, line 106 def table_name=(value) @original_table_name = @table_name if defined?(@table_name) @table_name = value && value.to_s @quoted_table_name = nil end
Private Instance Methods
Computes and returns a table name according to default conventions.
# File lib/believer/model_schema.rb, line 136 def compute_table_name "#{full_table_name_prefix}#{undecorated_table_name(name)}#{table_name_suffix}" end
Guesses the table name, but does not decorate it with prefix and suffix information.
# File lib/believer/model_schema.rb, line 129 def undecorated_table_name(class_name = base_class.name) table_name = class_name.to_s.demodulize.underscore table_name = table_name.pluralize if pluralize_table_names table_name end