Skip to content

Commit

Permalink
Implementing through_any key for has_many
Browse files Browse the repository at this point in the history
  • Loading branch information
cheerfulstoic committed Jul 13, 2014
1 parent 7b36175 commit a23c638
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
7 changes: 4 additions & 3 deletions lib/neo4j/active_node/has_n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ def #{rel_type}
end

def has_many(name, options = {})
to, from, through = options.values_at(:to, :from, :through)
to, from, through, through_any = options.values_at(:to, :from, :through, :through_any)
raise ArgumentError, "Must specify either :to or :from" if not (to || from)
raise ArgumentError, "Cannot specify both :to and :from" if to && from

target_class = to || from
direction = to ? :outbound : :inbound
through_any ||= false

# TODO: auto-set through when missing

Expand All @@ -122,12 +123,12 @@ def has_many(name, options = {})

module_eval(%Q{
def #{name}
Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class.name}, start_object: self, relationship: #{through.inspect}, direction: #{direction.inspect})
Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class.name}, start_object: self, through_any: #{through_any}, relationship: #{through.inspect}, direction: #{direction.inspect})
end}, __FILE__, __LINE__)

instance_eval(%Q{
def #{name}
Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class.name}, query_proxy: self.query_proxy, relationship: #{through.inspect}, direction: #{direction.inspect})
Neo4j::ActiveNode::Query::QueryProxy.new(#{target_class.name}, query_proxy: self.query_proxy, through_any: #{through_any}, relationship: #{through.inspect}, direction: #{direction.inspect})
end}, __FILE__, __LINE__)

end
Expand Down
5 changes: 3 additions & 2 deletions lib/neo4j/active_node/query/query_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,12 @@ def add_links(links)

def association_arrow
if @association_options
relationship = @association_options[:through_any] ? '' : "[:#{@association_options[:relationship]}]"
case direction = @association_options[:direction].to_sym
when :outbound
"-[:#{@association_options[:relationship]}]->"
"-#{relationship}->"
when :inbound
"<-[:#{@association_options[:relationship]}]-"
"<-#{relationship}-"
else
raise ArgumentError, "Invalid relationship direction: #{direction}"
end
Expand Down

0 comments on commit a23c638

Please sign in to comment.