conventional_models 0.1.0 → 0.1.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.
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.0
1
+ 0.1.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{conventional_models}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Steve Hodgkiss"]
@@ -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
@@ -82,7 +82,7 @@ module ConventionalModels
82
82
  it "returns lines in the model definition" do
83
83
  @table.lines << "test"
84
84
  @model_code = @table.code
85
- @model_code.split("\n")[1].should == "test"
85
+ @model_code.split("\n")[1].should == " test"
86
86
  end
87
87
  end
88
88
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 1
9
+ version: 0.1.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Steve Hodgkiss