class ActiveRecord::View
Public Class Methods
based_on(model)
click to toggle source
Clones all applicable associations from model
to this view and provides an instance method to_model
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.
# File lib/active_record/view.rb, line 19 def based_on(model) define_method("to_#{model.name.demodulize.underscore}") do becomes(model) end model.reflect_on_all_associations.each do |assoc| clone_association(model, assoc) end end
clone_association(model, *associations)
click to toggle source
Clone one or more associations from model
to this view class.
NOTE: Currently only belongs_to
, has_many
(withouth :through
), and has_and_belongs_to_many
associations are supported.
# File lib/active_record/view.rb, line 34 def clone_association(model, *associations) associations.each do |association| r = case association when String, Symbol model.reflect_on_association(association.to_sym) when ActiveRecord::Reflection::AssociationReflection association else raise ArgumentError, "Unrecognized association #{association.inspect}; must be a Symbol, String, or AssociationReflection." end case r.macro when :belongs_to #if self.column_names.include?(r.primary_key_name.to_s) # primary_key_name is deprecated in rails 4 if self.column_names.include?(r.foreign_key.to_s) 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 :foreign_key => r.foreign_key ) belongs_to r.name, options end end when :has_many ### TODO :through assocications options = r.options.merge( :class_name => r.class_name, # :foreign_key => r.primary_key_name :foreign_key => r.foreign_key ) has_many r.name, options when :has_and_belongs_to_many options = r.options.merge( :class_name => r.class_name, # :foreign_key => r.primary_key_name, :foreign_key => r.foreign_key, :association_foreign_key => r.association_foreign_key ) has_and_belongs_to_many r.name, options when :has_one ### TODO end end end
Public Instance Methods
readonly?()
click to toggle source
# File lib/active_record/view.rb, line 8 def readonly? true end