active_record_schema 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +107 -20
  2. data/VERSION +1 -1
  3. data/active_record_schema.gemspec +1 -1
  4. metadata +2 -2
data/README.md CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  *ActiveRecordSchema* is an ActiveRecord extension that allows you to write the database schema for a model within the model itself and to generate migrations directly from models.
4
4
 
5
- Unlike other libraries (eg. mini_record) ActiveRecordSchema is not an alternative to Rails migrations, but rather a tool to simplify their use enhancing their positive sides and contrasting their defects.
5
+ Unlike other libraries (eg. mini_record) ActiveRecordSchema is not an alternative to Rails migrations, but rather a tool to simplify their use.
6
6
 
7
7
  Install
8
8
 
9
- gem 'active_record_schema', :git => "git@github.com:mcasimir/active_record_schema.git"
9
+ gem 'active_record_schema'
10
10
 
11
11
  Update your bundle
12
12
 
@@ -17,13 +17,9 @@ Update your bundle
17
17
  Create a model
18
18
 
19
19
  class Post < ActiveRecord::Base
20
- schema do
21
-
22
20
  field :title
23
21
  field :body, :as => :text
24
22
  belongs_to :author, :class_name => "User"
25
-
26
- end
27
23
  end
28
24
 
29
25
  Run a migration generator each time you want to commit changes to database
@@ -42,34 +38,128 @@ Will generate the following migration
42
38
  end
43
39
  end
44
40
 
41
+ Now you can migrate the database.
42
+
43
+ Later in the life cycle of the project... add a new field to `Post`
44
+
45
+ class Post < ActiveRecord::Base
46
+ field :title
47
+ field :body, :as => :text
48
+ belongs_to :author, :class_name => "User"
49
+ field :pubdate, :as => :datetime
50
+ end
51
+
52
+ Generate easily a new migration for the change:
53
+
54
+ rails g migration add_pubdate_to_posts --from Post
55
+
56
+ Will generate:
57
+
58
+ class AddPubdateToPosts < ActiveRecord::Migration
59
+ def change
60
+ add_column :posts, :pubdate, :datetime
61
+ end
62
+ end
63
+
64
+
65
+ ## Has and Belongs To Many (HBTM) associations
66
+
67
+ Lets try to add a HBTM association to our `Post` model
68
+
69
+ _ex._
70
+
71
+ # content.rb
72
+ class Post < ActiveRecord::Base
73
+ field :title
74
+ field :body, :as => :text
75
+ belongs_to :author, :class_name => "User"
76
+ field :pubdate, :as => :datetime
77
+
78
+ has_and_belongs_to_many :voters, :class_name => "User"
79
+ end
80
+
81
+
82
+ Now running
83
+
84
+ rails g migration add_voters_to_posts --from Post
85
+
86
+ Will generate:
87
+
88
+ class AddVotersToPosts < ActiveRecord::Migration
89
+ def change
90
+ create_table :contents_users, :id => false do |t|
91
+ t.integer "content_id"
92
+ t.integer "user_id"
93
+ end
94
+ add_index :contents_users, "content_id"
95
+ add_index :contents_users, "user_id"
96
+ end
97
+ end
45
98
 
46
99
  ## Single Table Inheritance (STI)
47
100
 
101
+ Call `#inheritable` inside the base class of your hierarchy to add the `type` column required by Single Table Inheritance.
102
+
48
103
  _ex._
49
104
 
50
105
  # content.rb
51
106
  class Content < ActiveRecord::Base
52
- schema(:inheritable => true) do
53
- field :title
107
+ inheritable
108
+
109
+ field :title
110
+
111
+ has_and_belongs_to_many :voters, :class_name => "User"
112
+ belongs_to :author, :class_name => "User"
54
113
 
55
- timestamps!
56
- end
114
+ timestamps
57
115
  end
116
+
58
117
 
59
118
  # article.rb
60
119
  class Article < Content
61
- schema do
62
- field :body, :as => :text
63
- end
120
+ field :body, :as => :text
64
121
  end
122
+
65
123
 
66
124
  # video.rb
67
125
  class Video < Content
68
- schema do
69
- field :url
70
- end
126
+ field :url
127
+ end
128
+
129
+ run
130
+
131
+ rails g migration init_contents --from Content
132
+
133
+ same as
134
+
135
+ rails g migration init_contents --from Article
136
+
137
+ same as
138
+
139
+ rails g migration init_contents --from Video
140
+
141
+ Will generate the following migration
142
+
143
+ class InitContents < ActiveRecord::Migration
144
+ def change
145
+ add_column :contents, :type, :string
146
+ add_column :contents, :title, :string
147
+ add_column :contents, :author_id, :string
148
+ add_column :contents, :body, :text
149
+ add_column :contents, :url, :string
150
+
151
+ add_index :contents, :author_id
152
+
153
+ create_table :contents_users, :id => false do |t|
154
+ t.integer "content_id"
155
+ t.integer "user_id"
156
+ end
157
+ add_index :contents_users, "content_id"
158
+ add_index :contents_users, "user_id"
159
+ end
71
160
  end
72
-
161
+
162
+
73
163
 
74
164
  ## Contributing to active_record_schema
75
165
 
@@ -81,9 +171,6 @@ _ex._
81
171
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
82
172
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
83
173
 
84
- ## Coming Soon
85
-
86
- * Automatically generate migrations for Join tables for HBTM
87
174
 
88
175
  ---
89
176
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "active_record_schema"
8
- s.version = "0.2.0"
8
+ s.version = "0.2.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["mcasimir"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -89,7 +89,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  segments:
91
91
  - 0
92
- hash: 1365904965282049359
92
+ hash: -757246031215524947
93
93
  required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements: