citier 0.1.13 → 0.1.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Manifest CHANGED
@@ -7,4 +7,5 @@ lib/citier/core_ext.rb
7
7
  lib/citier/instance_methods.rb
8
8
  lib/citier/root_instance_methods.rb
9
9
  lib/citier/sql_adapters.rb
10
+ lib/citier/relation_methods.rb
10
11
  Manifest
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('citier', '0.1.13') do |p|
5
+ Echoe.new('citier', '0.1.14') do |p|
6
6
  p.description = "CITIER (Class Inheritance & Table Inheritance Embeddings for Rails) is a solution for single and multiple class table inheritance.
7
7
  For full information: http://peterhamilton.github.com/citier/
8
8
  For the original version by ALTRABio see www.github.com/altrabio/"
data/citier.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{citier}
5
- s.version = "0.1.13"
5
+ s.version = "0.1.14"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Peter Hamilton, Originally from Laurent Buffat, Pierre-Emmanuel Jouve"]
@@ -18,7 +18,8 @@ Gem::Specification.new do |s|
18
18
  "lib/citier/instance_methods.rb",
19
19
  "lib/citier/child_instance_methods.rb",
20
20
  "lib/citier/root_instance_methods.rb",
21
- "lib/citier/sql_adapters.rb"]
21
+ "lib/citier/sql_adapters.rb",
22
+ "lib/citier/relation_methods.rb"]
22
23
  s.files = ["Rakefile",
23
24
  "lib/citier.rb",
24
25
  "lib/citier/acts_as_citier.rb",
@@ -28,6 +29,7 @@ Gem::Specification.new do |s|
28
29
  "lib/citier/child_instance_methods.rb",
29
30
  "lib/citier/root_instance_methods.rb",
30
31
  "lib/citier/sql_adapters.rb",
32
+ "lib/citier/relation_methods.rb",
31
33
  "Manifest",
32
34
  "citier.gemspec"]
33
35
  s.homepage = %q{https://github.com/peterhamilton/citier/}
@@ -0,0 +1,96 @@
1
+ module ActiveRecord
2
+ class Relation
3
+
4
+ alias_method :relation_delete_all, :delete_all
5
+ def delete_all(conditions = nil)
6
+ return relation_delete_all(conditions) if !@klass.acts_as_citier?
7
+
8
+ return relation_delete_all(conditions) if conditions
9
+
10
+ deleted = true
11
+ ids = nil
12
+ c = @klass
13
+
14
+ bind_values.each do |bind_value|
15
+ if bind_value[0].name == "id"
16
+ ids = bind_value[1]
17
+ break
18
+ end
19
+ end
20
+ ids ||= where_values_hash["id"] || where_values_hash[:id]
21
+ where_hash = ids ? { :id => ids } : nil
22
+
23
+ deleted &= c.base_class.where(where_hash).relation_delete_all
24
+ while c.superclass != ActiveRecord::Base
25
+ if c.const_defined?(:Writable)
26
+ citier_debug("Deleting back up hierarchy #{c}")
27
+ deleted &= c::Writable.where(where_hash).delete_all
28
+ end
29
+ c = c.superclass
30
+ end
31
+
32
+ deleted
33
+ end
34
+
35
+ alias_method :relation_to_a, :to_a
36
+ def to_a
37
+ return relation_to_a if !@klass.acts_as_citier?
38
+
39
+ records = relation_to_a
40
+
41
+ c = @klass
42
+
43
+ if records.all? { |record| record.class == c }
44
+ return records
45
+ end
46
+
47
+ full_records = []
48
+ ids_wanted = {}
49
+
50
+ # Map all the ids wanted per type
51
+ records.each do |record|
52
+ if record.class == c # We don't need to find the record again if this is already the correct one
53
+ full_records << record
54
+ next
55
+ end
56
+
57
+ ids_wanted[record.class] ||= []
58
+ ids_wanted[record.class] << record.id
59
+ end
60
+
61
+ # Find all wanted records
62
+ ids_wanted.each do |type_class, ids|
63
+ full_records.push(*type_class.find(ids))
64
+ end
65
+
66
+ # Make a new array with the found records at the right places
67
+ records.each do |record|
68
+ full_record = full_records.find { |full_record| full_record.id == record.id }
69
+ record.force_attributes(full_record.instance_variable_get(:@attributes), :merge => true, :clear_caches => false)
70
+ end
71
+
72
+ return records
73
+ end
74
+
75
+ alias_method :relation_apply_finder_options, :apply_finder_options
76
+ def apply_finder_options(options)
77
+ return relation_apply_finder_options(options) if !@klass.acts_as_citier?
78
+
79
+ relation = self
80
+
81
+ # With option :no_children set to true, only records of type self will be returned.
82
+ # So Root.all(:no_children => true) won't return Child records.
83
+ no_children = options.delete(:no_children)
84
+ if no_children
85
+ relation = clone
86
+
87
+ c = @klass
88
+
89
+ self_type = c.superclass == ActiveRecord::Base ? nil : c.name
90
+ relation = relation.where(:type => self_type)
91
+ end
92
+
93
+ relation.relation_apply_finder_options(options)
94
+ end
95
+ end
96
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 13
9
- version: 0.1.13
8
+ - 14
9
+ version: 0.1.14
10
10
  platform: ruby
11
11
  authors:
12
12
  - Peter Hamilton, Originally from Laurent Buffat, Pierre-Emmanuel Jouve
@@ -47,6 +47,7 @@ extra_rdoc_files:
47
47
  - lib/citier/child_instance_methods.rb
48
48
  - lib/citier/root_instance_methods.rb
49
49
  - lib/citier/sql_adapters.rb
50
+ - lib/citier/relation_methods.rb
50
51
  files:
51
52
  - Rakefile
52
53
  - lib/citier.rb
@@ -57,6 +58,7 @@ files:
57
58
  - lib/citier/child_instance_methods.rb
58
59
  - lib/citier/root_instance_methods.rb
59
60
  - lib/citier/sql_adapters.rb
61
+ - lib/citier/relation_methods.rb
60
62
  - Manifest
61
63
  - citier.gemspec
62
64
  has_rdoc: true