Module: Neo4jAncestry::ActiveRecordAdditions
- Defined in:
- lib/models/neo4j_ancestry/active_record_additions.rb
Instance Method Summary collapse
-
#attributes_to_copy_to_neo4j(&block) ⇒ Object
Attributes to copy over to the neo4j database.
-
#has_many_for_rails_3_and_4(association_name, conditions_hash, options) ⇒ Object
The has_many method changes from Rails 3 to Rails 4.
-
#has_neo4j_ancestry(options) ⇒ Object
Example options: parent_class_names: %w(Group), child_class_names: %w(Group User).
Instance Method Details
#attributes_to_copy_to_neo4j(&block) ⇒ Object
Attributes to copy over to the neo4j database. This is just a wrapper for the ‘neoidable’ method of the neoid gem.
https://github.com/elado/neoid
Example:
attributes_to_copy_to_neo4j do |c|
c.field :name
c.field :name_length do
self.name.length
end
end
94 95 96 |
# File 'lib/models/neo4j_ancestry/active_record_additions.rb', line 94 def attributes_to_copy_to_neo4j(&block) neoidable(&block) end |
#has_many_for_rails_3_and_4(association_name, conditions_hash, options) ⇒ Object
The has_many method changes from Rails 3 to Rails 4. Since this gem supports both rails versions, this method is a wrapper.
102 103 104 105 106 107 108 |
# File 'lib/models/neo4j_ancestry/active_record_additions.rb', line 102 def has_many_for_rails_3_and_4(association_name, conditions_hash, ) if Rails.version.start_with? "4" has_many(association_name, -> { where conditions_hash }, ) elsif Rails.version.start_with? "3" has_many(association_name, .merge({conditions: conditions_hash})) end end |
#has_neo4j_ancestry(options) ⇒ Object
Example options:
parent_class_names: %w(Group),
child_class_names: %w(Group User)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/models/neo4j_ancestry/active_record_additions.rb', line 9 def has_neo4j_ancestry() link_class_name = 'Neo4jAncestry::Link' # Links (direct relationships between objects) are stored # via ActiveRecord in the mysql database. (The neo4j database contains only # redundant information and is used for fast queries.) # has_many :links_as_parent, foreign_key: :parent_id, class_name: link_class_name has_many :links_as_child, foreign_key: :child_id, class_name: link_class_name parent_class_names = [:parent_class_names] || [] child_class_names = [:child_class_names] || [] # links_as_child for specific parent object classes and # links_as_parent for specific child object classes, e.g. # group.links_as_child_for_groups # group.links_as_parent_for_groups # group.links_as_parent_for_users # parent_class_names.each do |parent_class_name| has_many_for_rails_3_and_4( "links_as_child_for_#{parent_class_name.underscore.pluralize}".to_sym, { parent_type: parent_class_name }, { as: :child, class_name: link_class_name } ) end child_class_names.each do |child_class_name| has_many_for_rails_3_and_4( "links_as_parent_for_#{child_class_name.underscore.pluralize}".to_sym, { child_type: child_class_name }, { as: :parent, class_name: link_class_name } ) end # parent and child associations for specific object classes, e.g. # group.parent_groups # group.child_groups # group.child_users # parent_class_names.each do |parent_class_name| has_many( "parent_#{parent_class_name.underscore.pluralize}".to_sym, through: "links_as_child_for_#{parent_class_name.underscore.pluralize}".to_sym, as: :structureable, foreign_key: :parent_id, source: 'parent', source_type: parent_class_name ) end child_class_names.each do |child_class_name| has_many( "child_#{child_class_name.underscore.pluralize}".to_sym, through: "links_as_parent_for_#{child_class_name.underscore.pluralize}".to_sym, as: :structureable, foreign_key: :child_id, source: 'child', source_type: child_class_name ) end # Use the neoid gem to have this object represented as node # in the neo4j graph database. # include Neoid::Node # Copy the name attribute to the neo4j nodes. # Other attributes can be copied as well by using the 'attributes_to_copy_to_neo4j'. # attributes_to_copy_to_neo4j do |c| c.field :name end # Include the instance methods for interaction with the neo4j graph. # include Neo4jAncestry::NodeInstanceMethods end |