heritage 0.1.0 → 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.
- data/README.textile +7 -22
- data/heritage_demo/Gemfile.lock +1 -1
- data/heritage_demo/app/models/blog_post.rb +3 -0
- data/heritage_demo/app/models/category.rb +5 -0
- data/heritage_demo/app/models/post.rb +2 -0
- data/heritage_demo/app/views/blog_posts/_form.html.erb +16 -2
- data/heritage_demo/config/locales/en.yml +9 -0
- data/heritage_demo/db/migrate/20110412094758_create_categories.rb +15 -0
- data/heritage_demo/db/schema.rb +8 -1
- data/heritage_demo/test/fixtures/categories.yml +11 -0
- data/heritage_demo/test/unit/category_test.rb +8 -0
- data/lib/heritage/active_record/acts_as_heir.rb +12 -1
- data/lib/heritage/version.rb +1 -1
- metadata +8 -4
data/README.textile
CHANGED
@@ -65,7 +65,7 @@ We need a @heir_id@ column of type @integer@ and a @heir_type@ column of type @s
|
|
65
65
|
drop_table :blog_posts
|
66
66
|
end
|
67
67
|
end
|
68
|
-
|
68
|
+
</pre>
|
69
69
|
|
70
70
|
When this is done and the database is migrated, we can begin using the models.
|
71
71
|
|
@@ -209,6 +209,10 @@ If you absolutely need to do a direct delete in the database, then you need to m
|
|
209
209
|
|
210
210
|
For instance, if you manually delete a @BlogPost@ that is heir of @Post@, then you need to first find the right @Post@, then delete the heir and finally delete the predecessor.
|
211
211
|
|
212
|
+
h2. Advanced usage
|
213
|
+
|
214
|
+
It is always possible to traverse between a predecessor and it's associated heir, through the @predecessor@ method of an heir, and the @heir@ method of a predecessor.
|
215
|
+
|
212
216
|
h2. Questions, Feedback
|
213
217
|
|
214
218
|
Feel free to message me on Github (murui)
|
@@ -222,25 +226,6 @@ h2. Credits
|
|
222
226
|
Credits goes out to Gerry from TechSpry.com for the idea for this implementation:
|
223
227
|
http://techspry.com/ruby_and_rails/multiple-table-inheritance-in-rails-3/
|
224
228
|
|
225
|
-
h2.
|
226
|
-
|
227
|
-
Copyright (c) 2011 Benjamin Media A/S
|
228
|
-
|
229
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
230
|
-
a copy of this software and associated documentation files (the
|
231
|
-
"Software"), to deal in the Software without restriction, including
|
232
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
233
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
234
|
-
permit persons to whom the Software is furnished to do so, subject to
|
235
|
-
the following conditions:
|
236
|
-
|
237
|
-
The above copyright notice and this permission notice shall be
|
238
|
-
included in all copies or substantial portions of the Software.
|
229
|
+
h2. License
|
239
230
|
|
240
|
-
|
241
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
242
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
243
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
244
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
245
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
246
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
231
|
+
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/InteractiveResource" property="dct:title" rel="dct:type">Heritage</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="https://github.com/BenjaminMedia/Heritage" property="cc:attributionName" rel="cc:attributionURL">Thomas Dippel @ Benjamin Media A/S</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="http://techspry.com/ruby_and_rails/multiple-table-inheritance-in-rails-3/" rel="dct:source">techspry.com</a>
|
data/heritage_demo/Gemfile.lock
CHANGED
@@ -1,10 +1,24 @@
|
|
1
|
+
<% if @blog_post.errors.any? %>
|
2
|
+
<p>
|
3
|
+
<%#= @blog_post.errors.inspect %>
|
4
|
+
</p>
|
5
|
+
<div id="errorExplanation">
|
6
|
+
<h2><%= pluralize(@blog_post.errors.count, "error") %> prohibited this record from being saved:</h2>
|
7
|
+
<ul>
|
8
|
+
<% @blog_post.errors.full_messages.each do |msg| %>
|
9
|
+
<li><%= msg %></li>
|
10
|
+
<% end %>
|
11
|
+
</ul>
|
12
|
+
</div>
|
13
|
+
<% end %>
|
14
|
+
|
1
15
|
<% form_for @blog_post do |f| -%>
|
2
16
|
<p>
|
3
|
-
<%= f.label :title
|
17
|
+
<%= f.label :title %>
|
4
18
|
<%= f.text_field :title %>
|
5
19
|
</p>
|
6
20
|
<p>
|
7
|
-
<%= f.label :body
|
21
|
+
<%= f.label :body %>
|
8
22
|
<%= f.text_field :body %>
|
9
23
|
</p>
|
10
24
|
<p>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class CreateCategories < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :categories do |t|
|
4
|
+
t.string :name
|
5
|
+
t.timestamps
|
6
|
+
end
|
7
|
+
|
8
|
+
add_column :posts, :category_id, :integer
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.down
|
12
|
+
remove_column :posts, :category_id
|
13
|
+
drop_table :categories
|
14
|
+
end
|
15
|
+
end
|
data/heritage_demo/db/schema.rb
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
#
|
11
11
|
# It's strongly recommended to check this file into your version control system.
|
12
12
|
|
13
|
-
ActiveRecord::Schema.define(:version =>
|
13
|
+
ActiveRecord::Schema.define(:version => 20110412094758) do
|
14
14
|
|
15
15
|
create_table "_blog_posts_old_20110411", :force => true do |t|
|
16
16
|
t.integer "predecessor_id"
|
@@ -30,6 +30,12 @@ ActiveRecord::Schema.define(:version => 20110411095655) do
|
|
30
30
|
t.text "body"
|
31
31
|
end
|
32
32
|
|
33
|
+
create_table "categories", :force => true do |t|
|
34
|
+
t.string "name"
|
35
|
+
t.datetime "created_at"
|
36
|
+
t.datetime "updated_at"
|
37
|
+
end
|
38
|
+
|
33
39
|
create_table "image_posts", :force => true do |t|
|
34
40
|
t.integer "aspect"
|
35
41
|
end
|
@@ -40,6 +46,7 @@ ActiveRecord::Schema.define(:version => 20110411095655) do
|
|
40
46
|
t.string "title"
|
41
47
|
t.datetime "created_at"
|
42
48
|
t.datetime "updated_at"
|
49
|
+
t.integer "category_id"
|
43
50
|
end
|
44
51
|
|
45
52
|
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
2
|
+
|
3
|
+
# This model initially had no columns defined. If you add columns to the
|
4
|
+
# model remove the '{}' from the fixture names and add the columns immediately
|
5
|
+
# below each fixture, per the syntax in the comments below
|
6
|
+
#
|
7
|
+
one: {}
|
8
|
+
# column: value
|
9
|
+
#
|
10
|
+
two: {}
|
11
|
+
# column: value
|
@@ -14,7 +14,8 @@ module Heritage
|
|
14
14
|
|
15
15
|
alias_method_chain :predecessor, :build
|
16
16
|
|
17
|
-
|
17
|
+
# Expose columns from the predecessor
|
18
|
+
self._predecessor_klass.columns.reject{|c| c.primary || c.name == Post.inheritance_column || c.name =~ /^heir_/}.map(&:name).each do |att|
|
18
19
|
define_method(att) do
|
19
20
|
predecessor.send(att)
|
20
21
|
end
|
@@ -23,6 +24,16 @@ module Heritage
|
|
23
24
|
end
|
24
25
|
end
|
25
26
|
|
27
|
+
# Expose associations from the predecessor
|
28
|
+
self._predecessor_klass.reflect_on_all_associations.reject{|a| a.name == :heir}.each do |association|
|
29
|
+
define_method(association.name) do
|
30
|
+
predecessor.send(association.name)
|
31
|
+
end
|
32
|
+
define_method("#{association.name}=") do |val|
|
33
|
+
predecessor.send("#{association.name}=",val)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
26
37
|
# We need to make sure that updated_at values in the predecessor table is updated when the heir is saved.
|
27
38
|
after_update :touch_predecessor
|
28
39
|
|
data/lib/heritage/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: heritage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Thomas Dippel
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-04-
|
18
|
+
date: 2011-04-12 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- heritage_demo/app/helpers/image_posts_helper.rb
|
49
49
|
- heritage_demo/app/helpers/posts_helper.rb
|
50
50
|
- heritage_demo/app/models/blog_post.rb
|
51
|
+
- heritage_demo/app/models/category.rb
|
51
52
|
- heritage_demo/app/models/image_post.rb
|
52
53
|
- heritage_demo/app/models/post.rb
|
53
54
|
- heritage_demo/app/views/blog_posts/_form.html.erb
|
@@ -80,6 +81,7 @@ files:
|
|
80
81
|
- heritage_demo/db/migrate/20110411095519_create_posts.rb
|
81
82
|
- heritage_demo/db/migrate/20110411095612_create_blog_posts.rb
|
82
83
|
- heritage_demo/db/migrate/20110411095655_create_image_posts.rb
|
84
|
+
- heritage_demo/db/migrate/20110412094758_create_categories.rb
|
83
85
|
- heritage_demo/db/schema.rb
|
84
86
|
- heritage_demo/db/seeds.rb
|
85
87
|
- heritage_demo/doc/README_FOR_APP
|
@@ -100,6 +102,7 @@ files:
|
|
100
102
|
- heritage_demo/public/stylesheets/.gitkeep
|
101
103
|
- heritage_demo/script/rails
|
102
104
|
- heritage_demo/test/fixtures/blog_posts.yml
|
105
|
+
- heritage_demo/test/fixtures/categories.yml
|
103
106
|
- heritage_demo/test/fixtures/image_posts.yml
|
104
107
|
- heritage_demo/test/fixtures/posts.yml
|
105
108
|
- heritage_demo/test/functional/blog_posts_controller_test.rb
|
@@ -108,6 +111,7 @@ files:
|
|
108
111
|
- heritage_demo/test/performance/browsing_test.rb
|
109
112
|
- heritage_demo/test/test_helper.rb
|
110
113
|
- heritage_demo/test/unit/blog_post_test.rb
|
114
|
+
- heritage_demo/test/unit/category_test.rb
|
111
115
|
- heritage_demo/test/unit/helpers/blog_posts_helper_test.rb
|
112
116
|
- heritage_demo/test/unit/helpers/image_posts_helper_test.rb
|
113
117
|
- heritage_demo/test/unit/helpers/posts_helper_test.rb
|