dbview_cti 0.0.1

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.
Files changed (113) hide show
  1. data/.gitignore +9 -0
  2. data/Gemfile +31 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +219 -0
  5. data/Rakefile +27 -0
  6. data/dbview_cti.gemspec +24 -0
  7. data/lib/db_view_cti/connection_adapters/schema_statements.rb +44 -0
  8. data/lib/db_view_cti/loader.rb +19 -0
  9. data/lib/db_view_cti/migration/command_recorder.rb +19 -0
  10. data/lib/db_view_cti/model/cti.rb +129 -0
  11. data/lib/db_view_cti/model/extensions.rb +25 -0
  12. data/lib/db_view_cti/names.rb +33 -0
  13. data/lib/db_view_cti/railtie.rb +11 -0
  14. data/lib/db_view_cti/sql_generation/migration/base.rb +92 -0
  15. data/lib/db_view_cti/sql_generation/migration/factory.rb +19 -0
  16. data/lib/db_view_cti/sql_generation/migration/postgresql.rb +107 -0
  17. data/lib/db_view_cti/sql_generation/model.rb +60 -0
  18. data/lib/db_view_cti/version.rb +3 -0
  19. data/lib/dbview_cti.rb +35 -0
  20. data/lib/tasks/view-based-cti_tasks.rake +4 -0
  21. data/spec/dummy-rails-3/.rspec +1 -0
  22. data/spec/dummy-rails-3/Rakefile +7 -0
  23. data/spec/dummy-rails-3/app/assets/javascripts/application.js +15 -0
  24. data/spec/dummy-rails-3/app/assets/stylesheets/application.css +13 -0
  25. data/spec/dummy-rails-3/app/controllers/application_controller.rb +3 -0
  26. data/spec/dummy-rails-3/app/helpers/application_helper.rb +2 -0
  27. data/spec/dummy-rails-3/app/mailers/.gitkeep +0 -0
  28. data/spec/dummy-rails-3/app/models/.gitkeep +0 -0
  29. data/spec/dummy-rails-3/app/models/car.rb +4 -0
  30. data/spec/dummy-rails-3/app/models/motor_cycle.rb +4 -0
  31. data/spec/dummy-rails-3/app/models/motor_vehicle.rb +4 -0
  32. data/spec/dummy-rails-3/app/models/rocket_engine.rb +4 -0
  33. data/spec/dummy-rails-3/app/models/space_ship.rb +4 -0
  34. data/spec/dummy-rails-3/app/models/space_shuttle.rb +4 -0
  35. data/spec/dummy-rails-3/app/models/vehicle.rb +4 -0
  36. data/spec/dummy-rails-3/app/views/layouts/application.html.erb +14 -0
  37. data/spec/dummy-rails-3/config/application.rb +68 -0
  38. data/spec/dummy-rails-3/config/boot.rb +10 -0
  39. data/spec/dummy-rails-3/config/database.yml +37 -0
  40. data/spec/dummy-rails-3/config/environment.rb +5 -0
  41. data/spec/dummy-rails-3/config/environments/development.rb +37 -0
  42. data/spec/dummy-rails-3/config/environments/production.rb +67 -0
  43. data/spec/dummy-rails-3/config/environments/test.rb +37 -0
  44. data/spec/dummy-rails-3/config/initializers/backtrace_silencers.rb +7 -0
  45. data/spec/dummy-rails-3/config/initializers/dbview_cti.rb +8 -0
  46. data/spec/dummy-rails-3/config/initializers/inflections.rb +15 -0
  47. data/spec/dummy-rails-3/config/initializers/mime_types.rb +5 -0
  48. data/spec/dummy-rails-3/config/initializers/secret_token.rb +7 -0
  49. data/spec/dummy-rails-3/config/initializers/session_store.rb +8 -0
  50. data/spec/dummy-rails-3/config/initializers/wrap_parameters.rb +14 -0
  51. data/spec/dummy-rails-3/config/locales/en.yml +5 -0
  52. data/spec/dummy-rails-3/config/routes.rb +58 -0
  53. data/spec/dummy-rails-3/config.ru +4 -0
  54. data/spec/dummy-rails-3/db/migrate/20130711214909_create_vehicles.rb +10 -0
  55. data/spec/dummy-rails-3/db/migrate/20130713133605_create_motor_vehicles.rb +13 -0
  56. data/spec/dummy-rails-3/db/migrate/20130713133705_create_cars.rb +13 -0
  57. data/spec/dummy-rails-3/db/migrate/20130713153631_create_motor_cycles.rb +12 -0
  58. data/spec/dummy-rails-3/db/migrate/20130816022212_create_space_ships.rb +13 -0
  59. data/spec/dummy-rails-3/db/migrate/20130817014120_create_space_shuttles.rb +14 -0
  60. data/spec/dummy-rails-3/db/migrate/20130817024220_create_rocket_engines.rb +12 -0
  61. data/spec/dummy-rails-3/db/migrate/20130819040414_add_reliability_to_space_ships.rb +13 -0
  62. data/spec/dummy-rails-3/db/schema.rb +74 -0
  63. data/spec/dummy-rails-3/lib/assets/.gitkeep +0 -0
  64. data/spec/dummy-rails-3/log/.gitkeep +0 -0
  65. data/spec/dummy-rails-3/public/404.html +26 -0
  66. data/spec/dummy-rails-3/public/422.html +26 -0
  67. data/spec/dummy-rails-3/public/500.html +25 -0
  68. data/spec/dummy-rails-3/public/favicon.ico +0 -0
  69. data/spec/dummy-rails-3/script/rails +6 -0
  70. data/spec/dummy-rails-4/.rspec +1 -0
  71. data/spec/dummy-rails-4/Rakefile +6 -0
  72. data/spec/dummy-rails-4/app/assets/images/.keep +0 -0
  73. data/spec/dummy-rails-4/app/assets/javascripts/application.js +13 -0
  74. data/spec/dummy-rails-4/app/assets/stylesheets/application.css +13 -0
  75. data/spec/dummy-rails-4/app/controllers/application_controller.rb +5 -0
  76. data/spec/dummy-rails-4/app/controllers/concerns/.keep +0 -0
  77. data/spec/dummy-rails-4/app/helpers/application_helper.rb +2 -0
  78. data/spec/dummy-rails-4/app/mailers/.keep +0 -0
  79. data/spec/dummy-rails-4/app/views/layouts/application.html.erb +14 -0
  80. data/spec/dummy-rails-4/bin/bundle +3 -0
  81. data/spec/dummy-rails-4/bin/rails +4 -0
  82. data/spec/dummy-rails-4/bin/rake +4 -0
  83. data/spec/dummy-rails-4/config/application.rb +28 -0
  84. data/spec/dummy-rails-4/config/boot.rb +5 -0
  85. data/spec/dummy-rails-4/config/database.yml +37 -0
  86. data/spec/dummy-rails-4/config/environment.rb +5 -0
  87. data/spec/dummy-rails-4/config/environments/development.rb +29 -0
  88. data/spec/dummy-rails-4/config/environments/production.rb +80 -0
  89. data/spec/dummy-rails-4/config/environments/test.rb +36 -0
  90. data/spec/dummy-rails-4/config/initializers/backtrace_silencers.rb +7 -0
  91. data/spec/dummy-rails-4/config/initializers/dbview_cti.rb +8 -0
  92. data/spec/dummy-rails-4/config/initializers/filter_parameter_logging.rb +4 -0
  93. data/spec/dummy-rails-4/config/initializers/inflections.rb +16 -0
  94. data/spec/dummy-rails-4/config/initializers/mime_types.rb +5 -0
  95. data/spec/dummy-rails-4/config/initializers/secret_token.rb +12 -0
  96. data/spec/dummy-rails-4/config/initializers/session_store.rb +3 -0
  97. data/spec/dummy-rails-4/config/initializers/wrap_parameters.rb +14 -0
  98. data/spec/dummy-rails-4/config/locales/en.yml +23 -0
  99. data/spec/dummy-rails-4/config/routes.rb +56 -0
  100. data/spec/dummy-rails-4/config.ru +4 -0
  101. data/spec/dummy-rails-4/db/schema.rb +74 -0
  102. data/spec/dummy-rails-4/lib/assets/.keep +0 -0
  103. data/spec/dummy-rails-4/log/.keep +0 -0
  104. data/spec/dummy-rails-4/public/404.html +58 -0
  105. data/spec/dummy-rails-4/public/422.html +58 -0
  106. data/spec/dummy-rails-4/public/500.html +57 -0
  107. data/spec/dummy-rails-4/public/favicon.ico +0 -0
  108. data/spec/models/car_spec.rb +58 -0
  109. data/spec/models/motor_vehicle_spec.rb +44 -0
  110. data/spec/models/space_shuttle_spec.rb +22 -0
  111. data/spec/models/vehicle_spec.rb +90 -0
  112. data/spec/spec_helper.rb +40 -0
  113. metadata +228 -0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy*/db/*.sqlite3
5
+ spec/dummy*/log/*.log
6
+ spec/dummy*/tmp/
7
+ spec/dummy*/.sass-cache
8
+ .project
9
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,31 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in view_based_cti.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # jquery-rails is used by the dummy application
9
+ gem "jquery-rails"
10
+
11
+ # Declare any dependencies that are still in development here instead of in
12
+ # your gemspec. These might include edge Rails or gems from your path or
13
+ # Git. Remember to move these dependencies to your gemspec before releasing
14
+ # your gem to rubygems.org.
15
+
16
+ # To use debugger
17
+ # gem 'debugger'
18
+
19
+ # taken from http://schneems.com/post/50991826838/testing-against-multiple-rails-versions
20
+ rails_version = ENV["RAILS_VERSION"] || "default"
21
+
22
+ rails = case rails_version
23
+ when "master"
24
+ { :github => "rails/rails"}
25
+ when "default"
26
+ ">= 3.2.13"
27
+ else
28
+ "~> #{rails_version}"
29
+ end
30
+
31
+ gem "rails", rails
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,219 @@
1
+ # dbview_cti
2
+
3
+ This gem implements [Class Table Inheritance](http://martinfowler.com/eaaCatalog/classTableInheritance.html) (CTI)
4
+ for Rails, as an alternative to Single Table Inheritance (STI). The implementation is based on database views.
5
+
6
+ Currently, only PostgreSQL (version >= 9.1) is supported. The gem works for both Rails 3.2 and Rails 4 apps.
7
+
8
+ ## Installation
9
+
10
+ Add the following to your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'dbview_cti'
14
+ ```
15
+
16
+ ## Example
17
+
18
+ Suppose we would like to have the following class hierarchy: a Vehicle superclass, its subclass MotorVehicle, and Car and MotorCycle wich are subclasses of MotorVehicle:
19
+
20
+ ```ruby
21
+ class Vehicle < ActiveRecord::Base
22
+ end
23
+
24
+ class MotorVehicle < Vehicle
25
+ end
26
+
27
+ class Car < MotorVehicle
28
+ end
29
+
30
+ class MotorCycle < MotorVehicle
31
+ end
32
+ ```
33
+
34
+ With Class Table Inheritance, we use a separate database table for each class. Each table only stores the attributes that are specific to that class.
35
+ dbview_cti uses database views to combine attributes from several tables in the hierarchy in an almost transparent way, so the resulting objects behave like
36
+ normal ActiveRecord models (with a few exceptions).
37
+
38
+ In order to do this dbview_cti has to know all descendants of a class, so we modify our class definitions:
39
+
40
+ ```ruby
41
+ class Vehicle < ActiveRecord::Base
42
+ cti_base_class
43
+ end
44
+
45
+ class MotorVehicle < Vehicle
46
+ cti_derived_class
47
+ end
48
+
49
+ class Car < MotorVehicle
50
+ cti_derived_class
51
+ end
52
+
53
+ class MotorCycle < MotorVehicle
54
+ cti_derived_class
55
+ end
56
+ ```
57
+
58
+ In the base class in the hierarchy (Vehicle in this case), we add a call to `cti_base_class`, in all derived classes we add `cti_derived_class`.
59
+ It is important to add these before running any migrations for the models, otherwise the database views won't be generated correctly (it is always possible to have them
60
+ regenerated, see further).
61
+
62
+ Now it's time to create the database tables (and views) using Rails migrations. The table for the base class is created using a standard migration, e.g.
63
+
64
+ ```ruby
65
+ class CreateVehicles < ActiveRecord::Migration
66
+ def change
67
+ create_table :vehicles do |t|
68
+ t.string :name
69
+ t.integer :mass
70
+
71
+ t.timestamps
72
+ end
73
+ end
74
+ end
75
+ ```
76
+
77
+ For the derived classes, we use `cti_create_view(class_name)`, a method that dbview_cti adds to migrations:
78
+
79
+ ```ruby
80
+ class CreateMotorVehicles < ActiveRecord::Migration
81
+ def change
82
+ create_table :motor_vehicles do |t|
83
+ t.references :vehicle
84
+ t.string :fuel
85
+ t.integer :number_of_wheels
86
+
87
+ t.timestamps
88
+ end
89
+
90
+ cti_create_view('MotorVehicle')
91
+ end
92
+ end
93
+ ```
94
+
95
+ There are two things to note in this migration:
96
+
97
+ 1. The table references the table used for the parent class (vehicles in this case)
98
+ 2. We call `cti_create_view(class_name)` to create the necessary database view and triggers, and to tell ActiveRecord to use the database view instead of the motor_vehicles table.
99
+
100
+ The migrations for Car and MotorCycle are very similar (i.e. they reference the parent table and call cti_create_view):
101
+
102
+ ```ruby
103
+ class CreateCars < ActiveRecord::Migration
104
+ def change
105
+ create_table :cars do |t|
106
+ t.references :motor_vehicle
107
+ t.boolean :stick_shift
108
+ t.boolean :convertible
109
+
110
+ t.timestamps
111
+ end
112
+
113
+ cti_create_view('Car')
114
+ end
115
+ end
116
+
117
+ class CreateMotorCycles < ActiveRecord::Migration
118
+ def change
119
+ create_table :motor_cycles do |t|
120
+ t.references :motor_vehicle
121
+ t.boolean :offroad
122
+
123
+ t.timestamps
124
+ end
125
+
126
+ cti_create_view('MotorCycle')
127
+ end
128
+ end
129
+ ```
130
+
131
+ After running `rake db:migrate` we can now use our new models, e.g. in the rails console:
132
+
133
+ 1.9.3-p448 :001 > c = Car.new
134
+ => #<Car id: nil, name: nil, mass: nil, fuel: nil, number_of_wheels: nil, stick_shift: nil, convertible: nil, created_at: nil, updated_at: nil>
135
+ 1.9.3-p448 :003 > c.name = 'Audi'
136
+ => "Audi"
137
+ 1.9.3-p448 :004 > c.stick_shift = true
138
+ => true
139
+ 1.9.3-p448 :005 > c.save!
140
+ => true
141
+
142
+ Note that Car has all attributes of the vehicles, motor_vehicles and cars tables combined. When saving, the attributes are stored in their corresponding tables.
143
+
144
+ ## API
145
+
146
+ ### Models
147
+
148
+ dbview_cti adds two class methods to ActiveRecord::Base:
149
+
150
+ * `cti_base_class`, which should be included in the class definition of the base class of the hierarchy.
151
+ * `cti_derived_class`, which should be included in the class definition of every other class in the hierarchy.
152
+
153
+ When either `cti_base_class` or `cti_derived_class` are used in the definition of a model, the model is equipped with the following instance methods:
154
+
155
+ * `convert_to(class_name)`. Use this to convert an object to another class. Both `convert_to('MotorVehicle')` and `convert_to(:motor_vehicle)` are ok. Example:
156
+
157
+ 1.9.3-p448 :003 > c = Car.create(:name => 'Audi')
158
+ => #<Car id: 2, name: "Audi", mass: nil, fuel: nil, number_of_wheels: nil, stick_shift: nil, convertible: nil, created_at: "2013-08-20 01:17:07", updated_at: "2013-08-20 01:17:07">
159
+ 1.9.3-p448 :004 > v = c.convert_to(:vehicle)
160
+ => #<Vehicle id: 3, name: "Audi", mass: nil, created_at: "2013-08-20 01:17:07", updated_at: "2013-08-20 01:17:07">
161
+ 1.9.3-p448 :005 > v.convert_to(:car) # convert back to Car
162
+ => #<Car id: 2, name: "Audi", mass: nil, fuel: nil, number_of_wheels: nil, stick_shift: nil, convertible: nil, created_at: "2013-08-20 01:17:07", updated_at: "2013-08-20 01:17:07">
163
+ 1.9.3-p448 :009 > v.convert_to(:motor_cycle) # since v is a car we cannot convert it to a MotorCycle
164
+ => nil
165
+
166
+ * `specialize` converts an object to its 'true' (i.e. most specialized or most derived) class. Example:
167
+
168
+ 1.9.3-p448 :010 > c = Car.create(:name => 'Volvo')
169
+ => #<Car id: 4, name: "Volvo", mass: nil, fuel: nil, number_of_wheels: nil, stick_shift: nil, convertible: nil, created_at: "2013-08-20 01:27:26", updated_at: "2013-08-20 01:27:26">
170
+ 1.9.3-p448 :011 > mv = MotorVehicle.create(:name => 'Trike')
171
+ => #<MotorVehicle id: 5, name: "Trike", mass: nil, fuel: nil, number_of_wheels: nil, created_at: "2013-08-20 01:28:06", updated_at: "2013-08-20 01:28:06">
172
+ 1.9.3-p448 :012 > c.convert_to(:vehicle).specialize
173
+ => #<Car id: 4, name: "Volvo", mass: nil, fuel: nil, number_of_wheels: nil, stick_shift: nil, convertible: nil, created_at: "2013-08-20 01:27:26", updated_at: "2013-08-20 01:27:26">
174
+ 1.9.3-p448 :013 > mv.convert_to(:vehicle).specialize
175
+ => #<MotorVehicle id: 5, name: "Trike", mass: nil, fuel: nil, number_of_wheels: nil, created_at: "2013-08-20 01:28:06", updated_at: "2013-08-20 01:28:06">
176
+
177
+ * `type` returns the 'true' (i.e. most specialized) class of an object (the class that the object is converted to when calling `specialize`).
178
+
179
+ ### Migrations
180
+
181
+ dbview_cti adds two methods to migrations:
182
+
183
+ * `cti_create_view(class_name)`
184
+ * `cti_drop_view(class_name)`
185
+
186
+ See the example migrations above about how to use `cti_create_view`. `cti_drop_view` can be used to drop a view created by `cti_create_view`
187
+ (e.g. in the `down` method of a migration.)
188
+
189
+ It is also possible to recreate the database views (and triggers). This is necessary when you want to change one of the tables in the hierarchy,
190
+ since then the views of all subclasses have to be recreated. dbview_cti provides the `cti_recreate_views_after_change_to(class_name)` method
191
+ (to be used with a block) to do this:
192
+
193
+ ```ruby
194
+ class AddReliabilityToMotorVehicles < ActiveRecord::Migration
195
+ def up
196
+ cti_recreate_views_after_change_to('MotorVehicle') do
197
+ add_column(:motor_vehicles, :reliability, :integer)
198
+ end
199
+ end
200
+
201
+ def down
202
+ cti_recreate_views_after_change_to('MotorVehicle') do
203
+ remove_column(:motor_vehicles, :reliability)
204
+ end
205
+ end
206
+ end
207
+ ```
208
+
209
+ The `change` syntax is not (yet?) supported for recreating database views.
210
+
211
+ ## Notes
212
+
213
+ * Using dbview_cti doesn't interfere with foreign key constraints. In fact, I recommend adding foreign key constraints
214
+ between the tables in a CTI hierarchy (e.g. using [foreigner](https://github.com/matthuhiggins/foreigner)).
215
+ * Take care when using database id's. Since the data for a Car object is spread over several tables,
216
+ the id of a Car instance will generally be different than the id of the MotorVehicle instance you get when you
217
+ convert the Car instance to a MotorVehicle.
218
+ * The gem intercepts calls to destroy to make sure all rows in all tables are removed. This is not the case for
219
+ delete_all, however, so avoid using delete_all for classes in the CTI hierarchy.
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ViewBasedCti'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "db_view_cti/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "dbview_cti"
9
+ s.version = DBViewCTI::VERSION
10
+ s.authors = ["Michaël Van Damme"]
11
+ s.email = ["michael.vandamme@vub.ac.be"]
12
+ s.homepage = "https://github.com/mvdamme/dbview_cti"
13
+ s.summary = "Class Table Inheritance (CTI) for Rails."
14
+ s.description = "This gem implements Class Table Inheritance (CTI) for Rails using database views."
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+
19
+ s.add_dependency "rails", ">= 3.2.0"
20
+
21
+ s.add_development_dependency "rspec-rails"
22
+ s.add_development_dependency "activerecord-postgresql-adapter"
23
+ s.add_development_dependency "foreigner"
24
+ end
@@ -0,0 +1,44 @@
1
+ module DBViewCTI
2
+ module ConnectionAdapters
3
+ module SchemaStatements
4
+
5
+ def cti_create_view(class_name, options = {})
6
+ generator = DBViewCTI::SQLGeneration::Migration::Factory.generator(class_name)
7
+ cti_execute_sql(generator.create_view_sql)
8
+ cti_execute_sql(generator.create_trigger_sql)
9
+ end
10
+
11
+ def cti_drop_view(class_name, options = {})
12
+ generator = DBViewCTI::SQLGeneration::Migration::Factory.generator(class_name)
13
+ cti_execute_sql(generator.drop_trigger_sql)
14
+ cti_execute_sql(generator.drop_view_sql)
15
+ end
16
+
17
+ # use with block in up/down methods
18
+ def cti_recreate_views_after_change_to(class_name, options = {})
19
+ klass = class_name.constantize
20
+ classes = [ class_name ] + klass.cti_all_descendants
21
+ # drop all views in reverse order
22
+ classes.reverse.each do |kklass|
23
+ cti_drop_view(kklass, options)
24
+ end
25
+ yield # perform table changes in block (e.g. add column)
26
+ # recreate views in forward order
27
+ classes.each do |kklass|
28
+ cti_create_view(kklass, options)
29
+ end
30
+ end
31
+
32
+ # Needed since sqlite only executes the first statement in a string containing multiple
33
+ # statements
34
+ def cti_execute_sql(sql)
35
+ return execute(sql) if sql.is_a?(String)
36
+ sql.map do |query|
37
+ execute(query)
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+
@@ -0,0 +1,19 @@
1
+ module DBViewCTI
2
+
3
+ def self.load
4
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.module_eval do
5
+ include DBViewCTI::ConnectionAdapters::SchemaStatements
6
+ end
7
+
8
+ if defined?(ActiveRecord::Migration::CommandRecorder)
9
+ ActiveRecord::Migration::CommandRecorder.class_eval do
10
+ include DBViewCTI::Migration::CommandRecorder
11
+ end
12
+ end
13
+
14
+ ActiveRecord::Base.class_eval do
15
+ include DBViewCTI::Model::Extensions
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,19 @@
1
+ module DBViewCTI
2
+ module Migration
3
+ module CommandRecorder
4
+
5
+ def cti_create_view(*args)
6
+ record(:cti_create_view, args)
7
+ end
8
+
9
+ def cti_drop_view(*args)
10
+ record(:cti_drop_view, args)
11
+ end
12
+
13
+ def invert_cti_create_view(args)
14
+ [:cti_drop_view, args]
15
+ end
16
+
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,129 @@
1
+ module DBViewCTI
2
+ module Model
3
+ module CTI
4
+ extend ActiveSupport::Concern
5
+
6
+ def specialize
7
+ class_name, id = type(true)
8
+ return self if class_name == self.class.name
9
+ class_name.constantize.find(id)
10
+ end
11
+
12
+ # Return the 'true' (i.e. most specialized) classname of this object
13
+ # When return_id is true, the 'specialized' database id is also returned
14
+ def type(return_id = false)
15
+ query, levels = self.class.cti_outer_join_sql(id)
16
+ result = self.class.connection.execute(query).first
17
+ # replace returned ids with the levels corresponding to their classes
18
+ result_levels = result.inject({}) do |hash, (k,v)|
19
+ hash[k] = levels[k] unless v.nil?
20
+ hash
21
+ end
22
+ # find class with maximum level value
23
+ foreign_key = result_levels.max_by { |k,v| v }.first
24
+ class_name = DBViewCTI::Names.table_to_class_name(foreign_key[0..-4])
25
+ if return_id
26
+ id_ = result[foreign_key].to_i
27
+ [class_name, id_]
28
+ else
29
+ class_name
30
+ end
31
+ end
32
+
33
+ def convert_to(type)
34
+ type_string = type.to_s
35
+ type_string = type_string.camelize if type.is_a?(Symbol)
36
+ return self if type_string == self.class.name
37
+ query = self.class.cti_inner_join_sql(id, type_string)
38
+ # query is nil when we try to cenvert to an descendant class (instead of an ascendant),
39
+ # or when we try to convert to a class outside of the hierarchy
40
+ if query.nil?
41
+ specialized = specialize
42
+ return nil if specialized == self
43
+ return specialized.convert_to(type_string)
44
+ end
45
+ result = self.class.connection.execute(query).first
46
+ id = result[ DBViewCTI::Names.foreign_key(type.to_s) ]
47
+ return nil if id.nil?
48
+ type_string.constantize.find(id.to_i)
49
+ end
50
+
51
+ # change destroy and delete methods to operate on most specialized obect
52
+ included do
53
+ alias_method_chain :destroy, :specialize
54
+ alias_method_chain :delete, :specialize
55
+ # destroy! seems te be defined in Rails 4
56
+ alias_method_chain :destroy!, :specialize if self.method_defined?(:destroy!)
57
+ end
58
+
59
+ def destroy_with_specialize
60
+ specialize.destroy_without_specialize
61
+ end
62
+
63
+ def destroy_with_specialize!
64
+ specialize.destroy_without_specialize!
65
+ end
66
+
67
+ def delete_with_specialize
68
+ specialize.delete_without_specialize
69
+ end
70
+
71
+ module ClassMethods
72
+
73
+ def cti_base_class?
74
+ !!@cti_base_class
75
+ end
76
+
77
+ def cti_derived_class?
78
+ !!@cti_derived_class
79
+ end
80
+
81
+ attr_accessor :cti_descendants, :cti_ascendants
82
+
83
+ # registers a derived class and its descendants in the current class
84
+ # class_name: name of derived class (the one calling cti_register_descendants on this class)
85
+ # descendants: the descendants of the derived class
86
+ def cti_register_descendants(class_name, descendants = {})
87
+ @cti_descendants ||= {}
88
+ @cti_descendants[class_name] = descendants
89
+ if cti_derived_class?
90
+ # call up the chain. This will also cause the register_ascendants callbacks
91
+ self.superclass.cti_register_descendants(self.name, @cti_descendants)
92
+ end
93
+ # call back to calling class
94
+ @cti_ascendants ||= []
95
+ class_name.constantize.cti_register_ascendants(@cti_ascendants + [ self.name ])
96
+ end
97
+
98
+ # registers the ascendants of the current class. Called on this class by the parent class.
99
+ # ascendants: array of ascendants. The first element is the highest level class, derived
100
+ # classes follow, the last element is the parent of this class.
101
+ def cti_register_ascendants(ascendants)
102
+ @cti_ascendants = ascendants
103
+ end
104
+
105
+ # returns a list of all descendants
106
+ def cti_all_descendants
107
+ result = []
108
+ block = Proc.new do |klass, descendants|
109
+ result << klass
110
+ descendants.each(&block)
111
+ end
112
+ @cti_descendants ||= {}
113
+ @cti_descendants.each(&block)
114
+ result
115
+ end
116
+
117
+ include DBViewCTI::SQLGeneration::Model
118
+
119
+ # this method is only used in testing. It returns the number of rows present in the real database
120
+ # table, not the number of rows present in the view (as returned by count)
121
+ def cti_table_count
122
+ result = connection.execute("SELECT COUNT(*) FROM #{DBViewCTI::Names.table_name(self)};")
123
+ result[0]['count'].to_i
124
+ end
125
+
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,25 @@
1
+ module DBViewCTI
2
+ module Model
3
+ module Extensions
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+
8
+ def cti_base_class
9
+ self.class_eval { include(DBViewCTI::Model::CTI) }
10
+ @cti_base_class = true
11
+ end
12
+
13
+ def cti_derived_class
14
+ # there is no need to include DBViewCTI::Model::CTI in derived classes
15
+ # (as we do in cti_base_class), since it is included in the base class
16
+ # and we inherit from that
17
+ @cti_derived_class = true
18
+ self.table_name = DBViewCTI::Names.view_name(self)
19
+ self.superclass.cti_register_descendants(self.name)
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,33 @@
1
+ module DBViewCTI
2
+ module Names
3
+
4
+ def self.view_name(klass)
5
+ self.table_name(klass) + '_view'
6
+ end
7
+
8
+ def self.table_name(klass)
9
+ ActiveSupport::Inflector.tableize( self.class_name(klass) )
10
+ end
11
+
12
+ def self.foreign_key(klass)
13
+ ActiveSupport::Inflector.foreign_key( self.class_name(klass) )
14
+ end
15
+
16
+ def self.class_name(klass)
17
+ klass.is_a?(String) ? klass : klass.name
18
+ end
19
+
20
+ def self.trigger_name(klass)
21
+ self.table_name(klass) + '_trig'
22
+ end
23
+
24
+ def self.trigger_function_name(klass)
25
+ self.table_name(klass) + '_trgfunc'
26
+ end
27
+
28
+ def self.table_to_class_name(table_name)
29
+ ActiveSupport::Inflector.classify( table_name )
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ module DBViewCTI
2
+
3
+ class Railtie < Rails::Railtie
4
+ initializer 'dbview_cti.load' do
5
+ ActiveSupport.on_load :active_record do
6
+ DBViewCTI.load
7
+ end
8
+ end
9
+ end
10
+
11
+ end