Skip to content

Commit c1f23b4

Browse files
Merge pull request #945 from neo4jrb/reinit_on_reload
Reinit on reload
2 parents f10ad17 + 5972d0f commit c1f23b4

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased][unreleased]
77

8+
### Fixed
9+
- Fixed errors when trying to call `#{association}_ids=` on an unpersisted node with UUIDs or an array thereof.
10+
- Removed extra Cypher query to replace relationships when working with unpersisted nodes and `association=`.
11+
- Bug related to Rails reloading an app and returning nodes without first reinitializing models, resulting in CypherNodes.
12+
813
## [5.1.4] - 08-24-2015
914

1015
### Fixed

lib/neo4j.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
require 'neo4j/active_node/query/query_proxy_find_in_batches'
6161
require 'neo4j/active_node/query/query_proxy_eager_loading'
6262
require 'neo4j/active_node/query/query_proxy_link'
63+
require 'neo4j/active_node/labels/reloading'
6364
require 'neo4j/active_node/labels'
6465
require 'neo4j/active_node/id_property/accessor'
6566
require 'neo4j/active_node/id_property'

lib/neo4j/active_node/labels.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module ActiveNode
33
# Provides a mapping between neo4j labels and Ruby classes
44
module Labels
55
extend ActiveSupport::Concern
6+
include Neo4j::ActiveNode::Labels::Reloading
67

78
WRAPPED_CLASSES = []
89
MODELS_FOR_LABELS_CACHE = {}
@@ -75,17 +76,9 @@ def self.clear_wrapped_models
7576
WRAPPED_CLASSES.clear
7677
end
7778

78-
protected
79-
8079
module ClassMethods
8180
include Neo4j::ActiveNode::QueryMethods
8281

83-
def before_remove_const
84-
associations.each_value(&:queue_model_refresh!)
85-
MODELS_FOR_LABELS_CACHE.clear
86-
WRAPPED_CLASSES.clear
87-
end
88-
8982
# Returns the object with the specified neo4j id.
9083
# @param [String,Integer] id of node to find
9184
def find(id)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Neo4j::ActiveNode::Labels
2+
module Reloading
3+
extend ActiveSupport::Concern
4+
5+
MODELS_TO_RELOAD = []
6+
7+
def self.reload_models!
8+
MODELS_TO_RELOAD.each(&:constantize)
9+
MODELS_TO_RELOAD.clear
10+
end
11+
12+
module ClassMethods
13+
def before_remove_const
14+
associations.each_value(&:queue_model_refresh!)
15+
MODELS_FOR_LABELS_CACHE.clear
16+
WRAPPED_CLASSES.each { |c| MODELS_TO_RELOAD << c.name }
17+
WRAPPED_CLASSES.clear
18+
end
19+
end
20+
end
21+
end

lib/neo4j/railtie.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ module Neo4j
55
class Railtie < ::Rails::Railtie
66
config.neo4j = ActiveSupport::OrderedOptions.new
77

8+
if const_defined?(:ActionDispatch)
9+
ActionDispatch::Reloader.to_prepare do
10+
Neo4j::ActiveNode::Labels::Reloading.reload_models!
11+
end
12+
end
13+
814
# Add ActiveModel translations to the I18n load_path
915
initializer 'i18n' do
1016
config.i18n.load_path += Dir[File.join(File.dirname(__FILE__), '..', '..', '..', 'config', 'locales', '*.{rb,yml}')]

spec/e2e/active_model_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,4 +733,25 @@ def self.named_jim
733733
end
734734
end
735735
end
736+
737+
# TODO: Rewrite/move into unit tests once master is merged into 5.1.x.
738+
describe 'model reloading' do
739+
before { stub_active_node_class('MyModel') }
740+
741+
describe 'before_remove_const' do
742+
it 'populates the MODELS_TO_RELOAD set' do
743+
expect { MyModel.before_remove_const }.to change { Neo4j::ActiveNode::Labels::Reloading::MODELS_TO_RELOAD.count }
744+
end
745+
end
746+
747+
describe 'reload_models!' do
748+
let!(:reload_cache) { Neo4j::ActiveNode::Labels::Reloading::MODELS_TO_RELOAD }
749+
before { MyModel.before_remove_const }
750+
it 'constantizes the models and clears list of models to reload' do
751+
expect(reload_cache).to receive(:each)
752+
expect(reload_cache).to receive(:clear)
753+
Neo4j::ActiveNode::Labels::Reloading.reload_models!
754+
end
755+
end
756+
end
736757
end

0 commit comments

Comments
 (0)