Module: Believer::ModelSchema::ClassMethods
- Defined in:
- lib/believer/model_schema.rb
Instance Method Summary collapse
-
#full_table_name_prefix ⇒ Object
:nodoc:.
-
#original_table_name ⇒ Object
:nodoc:.
-
#quoted_table_name ⇒ Object
Returns a quoted version of the table name, used to construct SQL statements.
-
#reset_table_name ⇒ Object
Computes the table name, (re)sets it internally, and returns it.
-
#set_table_name(value = nil, &block) ⇒ Object
:nodoc:.
-
#table_name ⇒ Object
Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending directly from ActiveRecord::Base.
-
#table_name=(value) ⇒ Object
Sets the table name explicitly.
Instance Method Details
#full_table_name_prefix ⇒ Object
:nodoc:
129 130 131 |
# File 'lib/believer/model_schema.rb', line 129 def full_table_name_prefix #:nodoc: (parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix end |
#original_table_name ⇒ Object
:nodoc:
96 97 98 |
# File 'lib/believer/model_schema.rb', line 96 def original_table_name #:nodoc: deprecated_original_property_getter :table_name end |
#quoted_table_name ⇒ Object
Returns a quoted version of the table name, used to construct SQL statements.
120 121 122 |
# File 'lib/believer/model_schema.rb', line 120 def quoted_table_name @quoted_table_name ||= "`#{table_name}`" end |
#reset_table_name ⇒ Object
Computes the table name, (re)sets it internally, and returns it.
125 126 127 |
# File 'lib/believer/model_schema.rb', line 125 def reset_table_name #:nodoc: self.table_name = compute_table_name end |
#set_table_name(value = nil, &block) ⇒ Object
:nodoc:
114 115 116 117 |
# File 'lib/believer/model_schema.rb', line 114 def set_table_name(value = nil, &block) #:nodoc: deprecated_property_setter :table_name, value, block @quoted_table_name = nil end |
#table_name ⇒ Object
Guesses the table name (in forced lower-case) based on the name of the class in the inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy looks like: Reply < Message < ActiveRecord::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 < ActiveRecord::Base
end
file class table_name
invoice.rb Invoice invoices
class Invoice < ActiveRecord::Base
class Lineitem < ActiveRecord::Base
end
end
file class table_name
invoice.rb Invoice::Lineitem invoice_lineitems
module Invoice
class Lineitem < ActiveRecord::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 < ActiveRecord::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 < ActiveRecord::Base
def self.table_name
"special_" + super
end
end
Post.table_name # => "special_posts"
91 92 93 94 |
# File 'lib/believer/model_schema.rb', line 91 def table_name reset_table_name unless defined?(@table_name) @table_name end |
#table_name=(value) ⇒ Object
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.
108 109 110 111 112 |
# File 'lib/believer/model_schema.rb', line 108 def table_name=(value) @original_table_name = @table_name if defined?(@table_name) @table_name = value && value.to_s @quoted_table_name = nil end |