module Skr::Concerns::AssociationExtensions::ClassMethods
Public Instance Methods
belongs_to(name, scope = nil, options = {} )
click to toggle source
Calls superclass method
# File lib/skr/concerns/association_extensions.rb, line 19 def belongs_to(name, scope = nil, options = {} ) opts = extract_private_options( scope, options ) associations = super handle_private_options( name, opts ) unless opts.empty? associations end
has_many(name, scope=nil,options={}, &extension )
click to toggle source
Calls superclass method
# File lib/skr/concerns/association_extensions.rb, line 26 def has_many(name, scope=nil,options={}, &extension ) opts = extract_private_options( scope, options ) associations = super handle_private_options( name, opts ) unless opts.empty? associations end
has_one(name, scope = nil, options = {})
click to toggle source
Calls superclass method
# File lib/skr/concerns/association_extensions.rb, line 12 def has_one(name, scope = nil, options = {}) opts = extract_private_options( scope, options ) associations = super handle_private_options( name, opts ) unless opts.empty? associations end
Private Instance Methods
extract_private_options( scope, options )
click to toggle source
# File lib/skr/concerns/association_extensions.rb, line 41 def extract_private_options( scope, options ) if scope.is_a?(Hash) scope.extract!( :export, :listen ) else options.extract!( :export, :listen ) end end
handle_private_options( name, opts )
click to toggle source
# File lib/skr/concerns/association_extensions.rb, line 35 def handle_private_options( name, opts ) association = reflect_on_association( name.to_sym ) setup_listeners( association, opts[:listen] ) if opts[:listen] setup_association_export( association, opts[:export] ) if opts[:export] end
setup_association_export( association, options )
click to toggle source
# File lib/skr/concerns/association_extensions.rb, line 78 def setup_association_export( association, options ) export_associations( association.name, options == true ? {} : options ) end
setup_listeners( association, listeners )
click to toggle source
This gets complex; We
First setup proc's for each listener, then attempt to load the associations's class If that succeds, we're done, otherwise we add it to PubSub's pending queue
# File lib/skr/concerns/association_extensions.rb, line 54 def setup_listeners( association, listeners ) targets = {} if listeners.any? && association.options[:inverse_of].nil? raise RuntimeError.new "Setting listener on #{name}##{association.name} " + "but the association does not have " + "an inverse_of specified." end listeners.each do | name, target | targets[ name ] = Proc.new{ | record, *args | record = record.send( association.inverse_of.name ) record.send( target, *( [record] + args ) ) if record } end begin klass = association.klass # This will throw if the class hasn't been loaded yet targets.each{ | name, proc | klass._add_event_listener( name, proc ) } rescue NameError pending = PendingEventListeners.all[association.class_name.demodulize] targets.each do | name, proc | pending[name] << proc end end end