Module: Believer::ModelSchema::ClassMethods
- Defined in:
- lib/believer/model_schema.rb
Instance Method Summary collapse
-
#full_table_name_prefix ⇒ 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.
-
#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 Believer::Base.
-
#table_name=(value) ⇒ Object
Sets the table name explicitly.
Instance Method Details
#full_table_name_prefix ⇒ Object
:nodoc:
122 123 124 |
# File 'lib/believer/model_schema.rb', line 122 def full_table_name_prefix #:nodoc: (parents.detect{ |p| p.respond_to?(:table_name_prefix) } || self).table_name_prefix end |
#quoted_table_name ⇒ Object
Returns a quoted version of the table name, used to construct SQL statements.
113 114 115 |
# File 'lib/believer/model_schema.rb', line 113 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.
118 119 120 |
# File 'lib/believer/model_schema.rb', line 118 def reset_table_name #:nodoc: self.table_name = compute_table_name 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 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"
93 94 95 96 |
# File 'lib/believer/model_schema.rb', line 93 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.
106 107 108 109 110 |
# 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 |