database_introspection 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 +4 -4
- data/README.md +12 -3
- data/lib/database_introspection/dynamic_model/relations_analyser.rb +20 -13
- data/lib/database_introspection/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c1727c6aec864d179a3b23c796659882a15ed521
|
4
|
+
data.tar.gz: b5e58598eef2358c11bd5c7629c6686c3537cada
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1762338b41cd573f2ccedd5df814a43ed12bb6c1e8d295a83059886e3403bde0012731a5c898b1e95a6ef0985442dfaa0074ce85dce915166e794f405637beb
|
7
|
+
data.tar.gz: e0c74af9319c4033d337e1fb1c26b63829bb11b53b90b8eea158ea5cb0fba433f389c31c990f77c5166afc44ad73d0d972115e7230b06e699076fe7ff3951084
|
data/README.md
CHANGED
@@ -1,9 +1,18 @@
|
|
1
1
|
# Database Introspection
|
2
2
|
|
3
|
-
[This gem][gemref] will introspect the database and create __dynamically__ ActiveRecord::Base descendants that can be
|
3
|
+
[This gem][gemref] will introspect the database and create __dynamically__ ActiveRecord::Base descendants that can be
|
4
|
+
used by your application, including some Rails associations helper methods.
|
4
5
|
|
5
|
-
It is intended to be primarily used within rails applications but nothing prevents you to use standalone, provided
|
6
|
-
|
6
|
+
It is intended to be primarily used within rails applications but nothing prevents you to use standalone, provided
|
7
|
+
the fact you are already connected to a database.
|
8
|
+
This gem does a bit the reverse action of what you do with Rails generator, when you want to generate the database from
|
9
|
+
you migrations.
|
10
|
+
|
11
|
+
Basically, it will infer the database and create ActiveRecord::Base descendants to handle tables already defined in the database.
|
12
|
+
|
13
|
+
The cool point is that it will as well guess from column names in the tables the relations between tables and dynamically
|
14
|
+
create the associations in the generated classes and thus all helper methods (for belongs_to, has_many and even has_many
|
15
|
+
:through).
|
7
16
|
|
8
17
|
|
9
18
|
## Installation
|
@@ -102,8 +102,8 @@ class DynamicModel::RelationsAnalyser
|
|
102
102
|
|
103
103
|
def analyses_has_many_through_association(model, associations)
|
104
104
|
# As there are multiple belongs_to in this class, all combinations
|
105
|
-
# should lead to a
|
106
|
-
#
|
105
|
+
# should lead to a has_many :through
|
106
|
+
# Wow, Ruby rocks !!
|
107
107
|
associations.combination(2).each do |left, right|
|
108
108
|
@alterations[left[:class]][:has_many_through] ||= []
|
109
109
|
@alterations[right[:class]][:has_many_through] ||= []
|
@@ -145,26 +145,33 @@ class DynamicModel::RelationsAnalyser
|
|
145
145
|
|
146
146
|
|
147
147
|
def add_belongs_to_behaviour(model, description)
|
148
|
-
field_name = description[:class].list_name.singularize
|
149
|
-
|
150
|
-
|
148
|
+
field_name = description[:class].list_name.singularize.to_sym
|
149
|
+
options = {foreign_key: description[:key], class_name: description[:class].name}
|
150
|
+
model.belongs_to field_name, options
|
151
|
+
puts " - belongs_to :#{field_name}, #{options.inspect}"
|
151
152
|
end
|
152
153
|
|
153
154
|
def add_has_many_behaviour(model, description)
|
154
|
-
field_name = description[:class].list_name
|
155
|
-
|
156
|
-
|
155
|
+
field_name = description[:class].list_name.to_sym
|
156
|
+
options = {class_name: description[:class].name}
|
157
|
+
model.has_many field_name, options
|
158
|
+
puts " - has_many :#{field_name}, #{options.inspect}"
|
157
159
|
end
|
158
160
|
|
161
|
+
# TODO: maybe check the intermediate class and depending upon the fact it contains a primary key, if only 2 fields
|
162
|
+
# exist. It may be a habtm instead of has_many :through...
|
159
163
|
def add_has_many_through_behaviour(model, description)
|
160
|
-
|
164
|
+
field_name = description[:class].list_name.to_sym
|
165
|
+
options = {through: description[:middle_class].list_name.to_sym, source: description[:class].list_name.singularize}
|
166
|
+
model.has_many field_name, options
|
167
|
+
puts " - has_many :#{field_name}, #{options.inspect}"
|
161
168
|
end
|
162
169
|
|
163
|
-
|
164
170
|
def add_has_one_behaviour(model, description)
|
165
|
-
field_name = description[:class].list_name.singularize
|
166
|
-
|
167
|
-
|
171
|
+
field_name = description[:class].list_name.singularize.to_sym
|
172
|
+
options = {class_name: description[:class].name}
|
173
|
+
model.has_one field_name, options
|
174
|
+
puts " - has_one :#{field_name}, #{options.inspect}"
|
168
175
|
end
|
169
176
|
|
170
177
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: database_introspection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- L.Briais
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-05-
|
11
|
+
date: 2013-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|