conventional_models 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +55 -1
- data/VERSION +1 -1
- data/conventional_models.gemspec +1 -1
- data/lib/conventional_models/table.rb +1 -1
- data/spec/conventional_models/table_spec.rb +1 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,6 +1,60 @@
|
|
1
1
|
= ConventionalModels
|
2
2
|
|
3
|
-
Generate ActiveRecord models with basic relationships based on conventions.
|
3
|
+
Generate ActiveRecord models automatically with basic relationships based on conventions.
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
gem install conventional_models
|
8
|
+
|
9
|
+
== Example
|
10
|
+
|
11
|
+
We have a table called Page and another called ContentItems (not following ActiveRecord conventions).
|
12
|
+
|
13
|
+
# point active_record to a database
|
14
|
+
require 'rubygems'
|
15
|
+
require 'active_record'
|
16
|
+
ActiveRecord::Base.establish_connection(:database => 'db.sqlite', :adapter => 'sqlite3')
|
17
|
+
|
18
|
+
# set your conventions
|
19
|
+
require 'conventional_models'
|
20
|
+
ConventionalModels.configure do
|
21
|
+
primary_key_name "Id"
|
22
|
+
table_name do |table|
|
23
|
+
table
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# use the models
|
28
|
+
page = Page.create :Name => 'test'
|
29
|
+
page.content_items.create :Name => 'content1'
|
30
|
+
|
31
|
+
# have a look at the generated code
|
32
|
+
puts Page.model_code
|
33
|
+
puts ContentItem.model_code
|
34
|
+
|
35
|
+
Output:
|
36
|
+
|
37
|
+
class ::Page < ::ActiveRecord::Base
|
38
|
+
set_primary_key "Id"
|
39
|
+
set_table_name "Page"
|
40
|
+
has_many :content_items, :class_name => 'ContentItem', :primary_key => 'Id', :foreign_key => 'Page_id'
|
41
|
+
end
|
42
|
+
class ::ContentItem < ::ActiveRecord::Base
|
43
|
+
set_primary_key "Id"
|
44
|
+
set_table_name "ContentItem"
|
45
|
+
belongs_to :page, :class_name => 'Page'
|
46
|
+
end
|
47
|
+
|
48
|
+
== Default conventions
|
49
|
+
|
50
|
+
ConventionalModels.configure do
|
51
|
+
belongs_to_matcher {|column| column.name.end_with? "_id"}
|
52
|
+
belongs_to_name {|column| column.name.gsub(/_id$/, "")}
|
53
|
+
primary_key_name "id"
|
54
|
+
table_name {|table_name| table_name.pluralize}
|
55
|
+
class_name {|table_name| table_name.singularize.camelize}
|
56
|
+
ignore_tables "schema_migrations", "sqlite_sequence", "sysdiagrams"
|
57
|
+
end
|
4
58
|
|
5
59
|
== Note on Patches/Pull Requests
|
6
60
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.1
|
data/conventional_models.gemspec
CHANGED
@@ -31,7 +31,7 @@ module ConventionalModels
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def code
|
34
|
-
"class ::#{@name.singularize.camelize} < ::ActiveRecord::Base\n#{@lines.join("\n")}\nend"
|
34
|
+
"class ::#{@name.singularize.camelize} < ::ActiveRecord::Base\n#{@lines.map{|l| " #{l}"}.join("\n")}\nend"
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|