Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reinit on reload #945

Merged
merged 3 commits into from
Aug 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][unreleased]

### Fixed
- Fixed errors when trying to call `#{association}_ids=` on an unpersisted node with UUIDs or an array thereof.
- Removed extra Cypher query to replace relationships when working with unpersisted nodes and `association=`.
- Bug related to Rails reloading an app and returning nodes without first reinitializing models, resulting in CypherNodes.

## [5.1.4] - 08-24-2015

### Fixed
Expand Down
1 change: 1 addition & 0 deletions lib/neo4j.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
require 'neo4j/active_node/query/query_proxy_find_in_batches'
require 'neo4j/active_node/query/query_proxy_eager_loading'
require 'neo4j/active_node/query/query_proxy_link'
require 'neo4j/active_node/labels/reloading'
require 'neo4j/active_node/labels'
require 'neo4j/active_node/id_property/accessor'
require 'neo4j/active_node/id_property'
Expand Down
9 changes: 1 addition & 8 deletions lib/neo4j/active_node/labels.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module ActiveNode
# Provides a mapping between neo4j labels and Ruby classes
module Labels
extend ActiveSupport::Concern
include Neo4j::ActiveNode::Labels::Reloading

WRAPPED_CLASSES = []
MODELS_FOR_LABELS_CACHE = {}
Expand Down Expand Up @@ -75,17 +76,9 @@ def self.clear_wrapped_models
WRAPPED_CLASSES.clear
end

protected

module ClassMethods
include Neo4j::ActiveNode::QueryMethods

def before_remove_const
associations.each_value(&:queue_model_refresh!)
MODELS_FOR_LABELS_CACHE.clear
WRAPPED_CLASSES.clear
end

# Returns the object with the specified neo4j id.
# @param [String,Integer] id of node to find
def find(id)
Expand Down
21 changes: 21 additions & 0 deletions lib/neo4j/active_node/labels/reloading.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Neo4j::ActiveNode::Labels
module Reloading
extend ActiveSupport::Concern

MODELS_TO_RELOAD = []

def self.reload_models!
MODELS_TO_RELOAD.each(&:constantize)
MODELS_TO_RELOAD.clear
end

module ClassMethods
def before_remove_const
associations.each_value(&:queue_model_refresh!)
MODELS_FOR_LABELS_CACHE.clear
WRAPPED_CLASSES.each { |c| MODELS_TO_RELOAD << c.name }
WRAPPED_CLASSES.clear
end
end
end
end
6 changes: 6 additions & 0 deletions lib/neo4j/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ module Neo4j
class Railtie < ::Rails::Railtie
config.neo4j = ActiveSupport::OrderedOptions.new

if const_defined?(:ActionDispatch)
ActionDispatch::Reloader.to_prepare do
Neo4j::ActiveNode::Labels::Reloading.reload_models!
end
end

# Add ActiveModel translations to the I18n load_path
initializer 'i18n' do
config.i18n.load_path += Dir[File.join(File.dirname(__FILE__), '..', '..', '..', 'config', 'locales', '*.{rb,yml}')]
Expand Down
21 changes: 21 additions & 0 deletions spec/e2e/active_model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -733,4 +733,25 @@ def self.named_jim
end
end
end

# TODO: Rewrite/move into unit tests once master is merged into 5.1.x.
describe 'model reloading' do
before { stub_active_node_class('MyModel') }

describe 'before_remove_const' do
it 'populates the MODELS_TO_RELOAD set' do
expect { MyModel.before_remove_const }.to change { Neo4j::ActiveNode::Labels::Reloading::MODELS_TO_RELOAD.count }
end
end

describe 'reload_models!' do
let!(:reload_cache) { Neo4j::ActiveNode::Labels::Reloading::MODELS_TO_RELOAD }
before { MyModel.before_remove_const }
it 'constantizes the models and clears list of models to reload' do
expect(reload_cache).to receive(:each)
expect(reload_cache).to receive(:clear)
Neo4j::ActiveNode::Labels::Reloading.reload_models!
end
end
end
end