Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add a few explanations to AR::View.

  • Loading branch information...
commit e313215c86bec21662fd737c4d3b0a7be146ed83 1 parent 3db917d
@mschuerig mschuerig authored
Showing with 23 additions and 7 deletions.
  1. +23 −7 lib/active_record/view.rb
View
30 lib/active_record/view.rb
@@ -1,5 +1,7 @@
-module ActiveRecord
+# A base class for database views.
+# It is primarily useful for views that are centered around a single table/model
+module ActiveRecord # :nodoc:
class View < Base
self.abstract_class = true
@@ -8,6 +10,12 @@ def readonly?
end
class << self
+ # Clones all applicable associations from +model+ to this view
+ # and provides an instance method
+ # <tt>to_<em>model</em></tt>
+ # that casts a view object to an object of the kind view is
+ # based on. This latter object may be missing attributes; to fill
+ # them in, call #reload.
def based_on(model)
define_method("to_#{model.name.demodulize.underscore}") do
becomes(model)
@@ -18,6 +26,11 @@ def based_on(model)
end
end
+ # Clone one or more associations from +model+ to this view class.
+ #
+ # NOTE: Currently only <tt>belongs_to</tt>, <tt>has_many</tt> (withouth
+ # <tt>:through</tt>), and <tt>has_and_belongs_to_many</tt> associations
+ # are supported.
def clone_association(model, *associations)
associations.each do |association|
r = case association
@@ -30,13 +43,14 @@ def clone_association(model, *associations)
end
case r.macro
when :belongs_to
- ### TODO handle polymorphic associations
if self.column_names.include?(r.primary_key_name.to_s)
- options = r.options.merge(
- :class_name => r.class_name,
- :foreign_key => r.primary_key_name
- )
- belongs_to r.name, options
+ if !r.options[:foreign_type] || self.column_names.include?(r.options[:foreign_type])
+ options = r.options.merge(
+ :class_name => r.class_name,
+ :foreign_key => r.primary_key_name
+ )
+ belongs_to r.name, options
+ end
end
when :has_many
### TODO :through assocications
@@ -52,6 +66,8 @@ def clone_association(model, *associations)
:association_foreign_key => r.association_foreign_key
)
has_and_belongs_to_many r.name, options
+ when :has_one
+ ### TODO
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.