Sha256: 2be87c14cf50b02997c963ff26b196fb262513a30ee7b93457917aabd2f2b6fb

Contents?: true

Size: 1.2 KB

Versions: 10

Compression:

Stored size: 1.2 KB

Contents

module N4j::Node::AutoRelate
  extend ActiveSupport::Concern
  included do
    define_singleton_method "#{model_name.param_key}_root" do
      model_root
    end
    after_initialize :relate_to_root
  end
  module ClassMethods
    def model_root
      # Turned off cache here until I can figure out how to keep this from messing with tests.
      # root = instance_variable_get("@#{model_root_name}")
      # return root if root

      root = follow_path_from_root || create_root_node
      instance_variable_set("@#{model_root_name}", root)
    end

    def model_root_name
      variable_name = model_name.param_key
      model_root_name = "#{variable_name}_root"
    end

    def follow_path_from_root
      service_root.relationships('outgoing_relationships', model_root_name).first.try(:end)
    end

    def create_root_node
      node = RootNode.new({:name => model_name.human})
      node.new_relationships << GenericRelationship.new(:start => service_root, :end => node, :type => "#{model_root_name}")
      node.save
      node
    end
  end

  def relate_to_root
    unless persisted?
      new_relationships << GenericRelationship.new(:start => self.class.model_root, :end => self, :type => 'is_a')
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
n4j-0.0.1.7 lib/n4j/auto_relate.rb
n4j-0.0.1.6.3 lib/n4j/auto_relate.rb
n4j-0.0.1.6.1 lib/n4j/auto_relate.rb
n4j-0.0.1.6 lib/n4j/auto_relate.rb
n4j-0.0.1.5 lib/n4j/auto_relate.rb
n4j-0.0.1.4 lib/n4j/auto_relate.rb
n4j-0.0.1.3 lib/n4j/auto_relate.rb
n4j-0.0.1.2 lib/n4j/auto_relate.rb
n4j-0.0.1.1 lib/n4j/auto_relate.rb
n4j-0.0.1 lib/n4j/auto_relate.rb