Class: CanCanCan::Neo4j::AssociationConditions

Inherits:
Object
  • Object
show all
Defined in:
lib/cancancan/neo4j/association_conditions.rb

Overview

Constructs cypher conditions for associations conditions hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ AssociationConditions

Returns a new instance of AssociationConditions.



9
10
11
12
13
14
# File 'lib/cancancan/neo4j/association_conditions.rb', line 9

def initialize(options)
  @options = options
  @conditions_string = ''
  @cypher_matches = []
  construct_conditions
end

Instance Attribute Details

#conditions_stringObject (readonly)

Returns the value of attribute conditions_string.



7
8
9
# File 'lib/cancancan/neo4j/association_conditions.rb', line 7

def conditions_string
  @conditions_string
end

#cypher_matchesObject (readonly)

Returns the value of attribute cypher_matches.



7
8
9
# File 'lib/cancancan/neo4j/association_conditions.rb', line 7

def cypher_matches
  @cypher_matches
end

Instance Method Details

#append_and_to_conditions_stringObject



60
61
62
# File 'lib/cancancan/neo4j/association_conditions.rb', line 60

def append_and_to_conditions_string
  @conditions_string += ' AND ' unless @conditions_string.blank?
end

#append_association_conditions(conditions, relationship) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/cancancan/neo4j/association_conditions.rb', line 31

def append_association_conditions(conditions, relationship)
  return if conditions.blank?
  asso_conditions_obj = AssociationConditions.new(asso_conditions: conditions, parent_class: relationship.target_class, path: path_with_relationship(relationship))
  append_and_to_conditions_string
  @conditions_string += asso_conditions_obj.conditions_string
  @cypher_matches += asso_conditions_obj.cypher_matches
end

#append_matches(relationship) ⇒ Object



55
56
57
58
# File 'lib/cancancan/neo4j/association_conditions.rb', line 55

def append_matches(relationship)
  node_class = relationship.target_class
  @cypher_matches << CypherConstructorHelper.match_node_cypher(node_class)
end

#append_model_conditions(model_conditions, relationship, current_path) ⇒ Object



64
65
66
67
68
69
# File 'lib/cancancan/neo4j/association_conditions.rb', line 64

def append_model_conditions(model_conditions, relationship, current_path)
  return if model_conditions.blank?
  con_string = CypherConstructorHelper.construct_conditions_string(model_conditions, relationship.target_class, current_path)
  append_and_to_conditions_string
  @conditions_string += con_string
end

#append_path_to_conditions(relationship, model_conditions, rel_length) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/cancancan/neo4j/association_conditions.rb', line 39

def append_path_to_conditions(relationship, model_conditions, rel_length)
  target_class = relationship.target_class
  model_attr_exists = model_conditions.any? do |key, _|
    !target_class.associations_keys.include?(key)
  end
  end_node = model_attr_exists ? CypherConstructorHelper.match_node_cypher(target_class) : '()'
  arrow_cypher = relationship.arrow_cypher(nil, {}, false, false, rel_length)
  current_path = @options[:path] + arrow_cypher + end_node
  if model_attr_exists
    append_matches(relationship)
    append_and_to_conditions_string
    @conditions_string += current_path
  end
  current_path
end

#association_relation(association) ⇒ Object



27
28
29
# File 'lib/cancancan/neo4j/association_conditions.rb', line 27

def association_relation(association)
  @options[:parent_class].associations[association]
end

#construct_conditionsObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/cancancan/neo4j/association_conditions.rb', line 16

def construct_conditions
  @options[:asso_conditions].each do |association, conditions|
    relationship = association_relation(association)
    associations_conditions, model_conditions = CypherConstructorHelper.bifurcate_conditions(conditions)
    rel_length = associations_conditions.delete(:rel_length)
    current_path = append_path_to_conditions(relationship, model_conditions, rel_length)
    append_model_conditions(model_conditions, relationship, current_path)
    append_association_conditions(associations_conditions, relationship)
  end
end

#path_with_relationship(relationship) ⇒ Object



71
72
73
# File 'lib/cancancan/neo4j/association_conditions.rb', line 71

def path_with_relationship(relationship)
  @options[:path] + relationship.arrow_cypher + '()'
end