database_introspection 0.1.1 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25c5d7a4888eafde894853b452a714f634b652ae
4
- data.tar.gz: eb2e2cedbe5a5ff900049ba10fd485a1ed70e9bb
3
+ metadata.gz: c1727c6aec864d179a3b23c796659882a15ed521
4
+ data.tar.gz: b5e58598eef2358c11bd5c7629c6686c3537cada
5
5
  SHA512:
6
- metadata.gz: 76cee6c4b5202dceb394f5efd85f67928d173b73ab52aa7d6bfd2eb2be6c6adb6fb131cd822f9655b977efddc2d303ea8ebe55c42648db9921a29015b7c5086b
7
- data.tar.gz: 69824f5c5e775c980fe1081f48c83a02c3f88a4ca812c5643ea9ba25221f5eee1152e606a350816316607775b2ff1d908c885b3ab3c7710a6bd14be845dee8cd
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 used by your application, including some Rails associations helper methods.
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 the fact you are already connected to a database.
6
- This gem does a bit the reverse action of what you do with Rails generator, when you want to generate the database from you migrations.
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 has_many_through
106
- # Waouh, Ruby rocks !!
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
- model.belongs_to field_name, :foreign_key => description[:key], :class_name => description[:class].name
150
- puts " - belongs_to :#{field_name}, :foreign_key => #{description[:key]}, :class_name => #{description[:class].name}"
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
- model.has_many field_name, :class_name => description[:class].name
156
- puts " - has_many :#{field_name}, :class_name => #{description[:class].name}"
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
- puts " - has_many #{description[:class]} through #{description[:middle_class]}"
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
- model.has_one field_name, :class_name => description[:class].name
167
- puts " - has_one :#{field_name}, :class_name => #{description[:class].name}"
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
@@ -1,3 +1,3 @@
1
1
  module DatabaseIntrospection
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  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.1.1
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-25 00:00:00.000000000 Z
11
+ date: 2013-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler