module Volt::Associations

Public Class Methods

included(base) click to toggle source
# File lib/volt/models/associations.rb, line 69
def self.included(base)
  base.send :extend, ClassMethods
end

Private Instance Methods

association_with_root_model(method_name) { |root| ... } click to toggle source

Currently the has_many and belongs_to associations only work on the store collection, this method checks to make sure we are on store and returns the root reference to it.

# File lib/volt/models/associations.rb, line 77
def association_with_root_model(method_name)
  persistor = self.persistor || (respond_to?(:save_to) && save_to && save_to.persistor)

  # Check if we are on the store collection
  if persistor.is_a?(Volt::Persistors::ModelStore) ||
     persistor.is_a?(Volt::Persistors::Page)
    # Get the root node
    root = persistor.try(:root_model)

    # Yield to the block passing in the root node
    yield(root)
  else
    # raise an error about the method not being supported on this collection
    fail "#{method_name} currently only works on the store and page collection (support for other collections coming soon)"
  end
end