through_hierarchy 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 74f01013ac5a0df1c24c2ca7202d6de47936dcff
4
- data.tar.gz: 1c2e2a79defb580b6449e90535f6683725996497
2
+ SHA256:
3
+ metadata.gz: ed01a12faf0874f494e1622e5b4810c8195438a0a962032cbb49383e2d86e513
4
+ data.tar.gz: 11213859736477129737b3b026f4c689b37086a900a950a8bd853d4d127198a5
5
5
  SHA512:
6
- metadata.gz: 5026fbd509012ffd95864c8732fe08ca37dc398ea8ef36688f7d0cd76cdd42082aaae32b8022a02ae9725d5b98482cbacd22ef8ce150e3c8cfc4f561bfae1416
7
- data.tar.gz: 8b246b81fedf869e249b66dc9074c0bb922264bd840914c36b27f7a3209c7c4868330d9f0b10c5c68c86f91d5dc1fc61fb0c8c83f0d12aa5e792b5d62ca3a0ca
6
+ metadata.gz: 6a7ab21447264d5170415108931fed9ef426b9bdb9302c92af2e0385231b41d3690b541453061aa35b319b5d3aae8fb6b08f3216b876f598324c517cc4b000a6
7
+ data.tar.gz: e044a87e25e344be7979ccc38e96a98ac7ba88aaac000ac78882aec2b087ff9a395d0b24d95fcae6e3fb7ecf247d7bb884463305ba59b35288bda29ba1ca09fa
@@ -6,6 +6,7 @@ require 'through_hierarchy/associations/has_one.rb'
6
6
  require 'through_hierarchy/associations/has_many.rb'
7
7
  require 'through_hierarchy/associations/has_uniq.rb'
8
8
  require 'through_hierarchy/exceptions.rb'
9
+ require 'through_hierarchy/rails_utils.rb'
9
10
  require 'through_hierarchy/hierarchicals/hierarchical.rb'
10
11
  require 'through_hierarchy/hierarchicals/instance.rb'
11
12
 
@@ -14,6 +14,7 @@ module ThroughHierarchy
14
14
 
15
15
  def find(instance)
16
16
  results = get_matches(instance)
17
+ results = results.create_with(associated_instance(instance).create_with)
17
18
  results = results.instance_exec(&@scope) if @scope.present?
18
19
  return results
19
20
  end
@@ -10,10 +10,7 @@ module ThroughHierarchy
10
10
  private
11
11
 
12
12
  def get_joins
13
- arel = @associated.join_best_rank
14
- result = @model.joins(arel.join_sources).order(arel.orders)
15
- arel.constraints.each{|cc| result = result.where(cc)}
16
- return result
13
+ @associated.join_best_rank
17
14
  end
18
15
 
19
16
  end
@@ -22,10 +22,7 @@ module ThroughHierarchy
22
22
  end
23
23
 
24
24
  def get_joins
25
- arel = @associated.join_best_rank(group_by: @uniq)
26
- result = @model.joins(arel.join_sources).order(arel.orders)
27
- arel.constraints.each{|cc| result = result.where(cc)}
28
- return result
25
+ @associated.join_best_rank(group_by: @uniq)
29
26
  end
30
27
 
31
28
  end
@@ -6,11 +6,15 @@ module ThroughHierarchy
6
6
  class_attribute :hierarchical_associations
7
7
  self.hierarchical_associations = {}
8
8
  attr_reader :hierarchical_association_cache
9
- after_initialize :reset_hierarchical_association_cache
9
+ after_initialize :clear_hierarchical_association_cache
10
10
  end
11
11
 
12
- def reset_hierarchical_association_cache
13
- @hierarchical_association_cache = {}
12
+ def clear_hierarchical_association_cache(name = nil)
13
+ if name.nil?
14
+ @hierarchical_association_cache = {}
15
+ else
16
+ @hierarchical_association_cache.delete(name)
17
+ end
14
18
  end
15
19
 
16
20
  module ClassMethods
@@ -60,7 +60,7 @@ module ThroughHierarchy
60
60
  Arel.sql(
61
61
  "CASE `#{@source.name}`.`#{foreign_type_name}` " +
62
62
  hierarchy_models.map.with_index do |model, ii|
63
- "WHEN #{model.sanitize(model.base_class.to_s)} THEN #{ii} "
63
+ "WHEN #{ThroughHierarchy::RailsUtils.sanitize_sql(model.base_class.to_s)} THEN #{ii} "
64
64
  end.join +
65
65
  "END"
66
66
  )
@@ -93,16 +93,22 @@ module ThroughHierarchy
93
93
  # Join @model to @source only on best hierarchy matches
94
94
  ### FASTER METHOD: join source to source alias on source.rank < alias.rank where alias does not exist
95
95
  # This performs OK.
96
+ # TODO: return arel once we know how to use the binds properly
96
97
  def join_best_rank(group_by: nil)
97
98
  better_rank = spawn(@source.alias("better_hierarchy"))
98
- @model.joins(@hierarchy).arel.
99
+ join_condition_array = [
100
+ better_rank.filters,
101
+ better_rank.hierarchy_rank.lt(hierarchy_rank)
102
+ ]
103
+ join_condition_array << better_rank.source[group_by].eq(@source[group_by]) if group_by.present?
104
+ arel = @model.arel_table.
99
105
  join(@source).on(filters).
100
106
  join(better_rank.source, Arel::Nodes::OuterJoin).
101
- on(
102
- better_rank.filters.
103
- and(better_rank.hierarchy_rank.lt(hierarchy_rank))
104
- ).
107
+ on(and_conditions(join_condition_array)).
105
108
  where(better_rank.source[:id].eq(nil))
109
+ result = @model.joins(@hierarchy).joins(arel.join_sources).order(arel.orders)
110
+ arel.constraints.each{|cc| result = result.where(cc)}
111
+ return result
106
112
  end
107
113
 
108
114
  # # TODO: generate this dynamically based on existing columns and selects
@@ -154,4 +160,4 @@ module ThroughHierarchy
154
160
 
155
161
  end
156
162
  end
157
- end
163
+ end
@@ -12,7 +12,7 @@ module ThroughHierarchy
12
12
  end
13
13
 
14
14
  def filters
15
- or_conditions(hierarchy_instances.map{|instance| filter(instance)})
15
+ or_conditions(hierarchy_instances.compact.map{|instance| filter(instance)})
16
16
  end
17
17
 
18
18
  def filter(instance)
@@ -20,6 +20,13 @@ module ThroughHierarchy
20
20
  and(foreign_key_column.eq(instance_key(instance)))
21
21
  end
22
22
 
23
+ def create_with
24
+ {
25
+ foreign_type_name => instance_type(@instance),
26
+ foreign_key_name => instance_key(@instance)
27
+ }
28
+ end
29
+
23
30
  def instance_type(instance)
24
31
  instance.class.base_class.to_s
25
32
  end
@@ -0,0 +1,17 @@
1
+ module ThroughHierarchy
2
+ module RailsUtils
3
+ module_function
4
+
5
+ def sanitize_sql(string)
6
+ if rails_major_minor_version < 5.1
7
+ ActiveRecord::Base.sanitize(string)
8
+ else
9
+ ActiveRecord::Base.sanitize_sql_array(["?", string])
10
+ end
11
+ end
12
+
13
+ def rails_major_minor_version
14
+ Rails.version.split(".").first(2).join(".").to_f
15
+ end
16
+ end
17
+ end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: through_hierarchy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Schwartz
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2016-05-04 00:00:00.000000000 Z
@@ -27,11 +27,12 @@ files:
27
27
  - lib/through_hierarchy/hierarchicals/hierarchical.rb
28
28
  - lib/through_hierarchy/hierarchicals/instance.rb
29
29
  - lib/through_hierarchy/hierarchy.rb
30
+ - lib/through_hierarchy/rails_utils.rb
30
31
  homepage: https://github.com/ozydingo/through_hierarchy
31
32
  licenses:
32
33
  - MIT
33
34
  metadata: {}
34
- post_install_message:
35
+ post_install_message:
35
36
  rdoc_options: []
36
37
  require_paths:
37
38
  - lib
@@ -46,9 +47,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
46
47
  - !ruby/object:Gem::Version
47
48
  version: '0'
48
49
  requirements: []
49
- rubyforge_project:
50
- rubygems_version: 2.4.5
51
- signing_key:
50
+ rubygems_version: 3.0.8
51
+ signing_key:
52
52
  specification_version: 4
53
53
  summary: Has Many Through Hierarchy
54
54
  test_files: []