ensured_schema 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile.lock +23 -0
- data/Rakefile +19 -0
- data/ensured_schema.gemspec +4 -0
- data/lib/ensured_schema/column.rb +9 -0
- data/lib/ensured_schema/ensured_table.rb +53 -0
- data/lib/ensured_schema/mysql_column.rb +17 -0
- data/lib/ensured_schema/schema.rb +7 -0
- data/lib/ensured_schema/schema_statements.rb +69 -0
- data/lib/ensured_schema/version.rb +1 -1
- data/lib/ensured_schema.rb +5 -155
- data/test/cases/ensured_schema_test.rb +206 -0
- data/test/cases/helper.rb +70 -0
- data/test/cases/migration_test.rb +1523 -0
- data/test/cases/repair_helper.rb +50 -0
- data/test/config.rb +5 -0
- data/test/connections/native_mysql/connection.rb +25 -0
- data/test/migrations/broken/100_migration_that_raises_exception.rb +10 -0
- data/test/migrations/decimal/1_give_me_big_numbers.rb +15 -0
- data/test/migrations/duplicate/1_people_have_last_names.rb +9 -0
- data/test/migrations/duplicate/2_we_need_reminders.rb +12 -0
- data/test/migrations/duplicate/3_foo.rb +7 -0
- data/test/migrations/duplicate/3_innocent_jointable.rb +12 -0
- data/test/migrations/duplicate_names/20080507052938_chunky.rb +7 -0
- data/test/migrations/duplicate_names/20080507053028_chunky.rb +7 -0
- data/test/migrations/interleaved/pass_1/3_innocent_jointable.rb +12 -0
- data/test/migrations/interleaved/pass_2/1_people_have_last_names.rb +9 -0
- data/test/migrations/interleaved/pass_2/3_innocent_jointable.rb +12 -0
- data/test/migrations/interleaved/pass_3/1_people_have_last_names.rb +9 -0
- data/test/migrations/interleaved/pass_3/2_i_raise_on_down.rb +8 -0
- data/test/migrations/interleaved/pass_3/3_innocent_jointable.rb +12 -0
- data/test/migrations/missing/1000_people_have_middle_names.rb +9 -0
- data/test/migrations/missing/1_people_have_last_names.rb +9 -0
- data/test/migrations/missing/3_we_need_reminders.rb +12 -0
- data/test/migrations/missing/4_innocent_jointable.rb +12 -0
- data/test/migrations/valid/1_people_have_last_names.rb +9 -0
- data/test/migrations/valid/2_we_need_reminders.rb +12 -0
- data/test/migrations/valid/3_innocent_jointable.rb +12 -0
- data/test/models/author.rb +146 -0
- data/test/models/auto_id.rb +4 -0
- data/test/models/binary.rb +2 -0
- data/test/models/bird.rb +3 -0
- data/test/models/book.rb +4 -0
- data/test/models/categorization.rb +5 -0
- data/test/models/category.rb +34 -0
- data/test/models/citation.rb +6 -0
- data/test/models/club.rb +13 -0
- data/test/models/column_name.rb +3 -0
- data/test/models/comment.rb +29 -0
- data/test/models/company.rb +171 -0
- data/test/models/company_in_module.rb +61 -0
- data/test/models/computer.rb +3 -0
- data/test/models/contact.rb +16 -0
- data/test/models/contract.rb +5 -0
- data/test/models/course.rb +3 -0
- data/test/models/customer.rb +73 -0
- data/test/models/default.rb +2 -0
- data/test/models/developer.rb +101 -0
- data/test/models/edge.rb +5 -0
- data/test/models/entrant.rb +3 -0
- data/test/models/essay.rb +3 -0
- data/test/models/event.rb +3 -0
- data/test/models/guid.rb +2 -0
- data/test/models/item.rb +7 -0
- data/test/models/job.rb +5 -0
- data/test/models/joke.rb +3 -0
- data/test/models/keyboard.rb +3 -0
- data/test/models/legacy_thing.rb +3 -0
- data/test/models/matey.rb +4 -0
- data/test/models/member.rb +12 -0
- data/test/models/member_detail.rb +5 -0
- data/test/models/member_type.rb +3 -0
- data/test/models/membership.rb +9 -0
- data/test/models/minimalistic.rb +2 -0
- data/test/models/mixed_case_monkey.rb +3 -0
- data/test/models/movie.rb +5 -0
- data/test/models/order.rb +4 -0
- data/test/models/organization.rb +6 -0
- data/test/models/owner.rb +5 -0
- data/test/models/parrot.rb +16 -0
- data/test/models/person.rb +16 -0
- data/test/models/pet.rb +5 -0
- data/test/models/pirate.rb +70 -0
- data/test/models/post.rb +100 -0
- data/test/models/price_estimate.rb +3 -0
- data/test/models/project.rb +30 -0
- data/test/models/reader.rb +4 -0
- data/test/models/reference.rb +4 -0
- data/test/models/reply.rb +46 -0
- data/test/models/ship.rb +10 -0
- data/test/models/ship_part.rb +5 -0
- data/test/models/sponsor.rb +4 -0
- data/test/models/subject.rb +4 -0
- data/test/models/subscriber.rb +8 -0
- data/test/models/subscription.rb +4 -0
- data/test/models/tag.rb +7 -0
- data/test/models/tagging.rb +10 -0
- data/test/models/task.rb +3 -0
- data/test/models/topic.rb +80 -0
- data/test/models/toy.rb +6 -0
- data/test/models/treasure.rb +8 -0
- data/test/models/vertex.rb +9 -0
- data/test/models/warehouse_thing.rb +5 -0
- data/test/schema/mysql_specific_schema.rb +24 -0
- data/test/schema/postgresql_specific_schema.rb +114 -0
- data/test/schema/schema.rb +493 -0
- data/test/schema/schema2.rb +6 -0
- data/test/schema/sqlite_specific_schema.rb +25 -0
- metadata +237 -7
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Testing
|
3
|
+
module RepairHelper
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ClassMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Toolbox
|
11
|
+
def self.record_validations(*model_classes)
|
12
|
+
model_classes.inject({}) do |repair, klass|
|
13
|
+
repair[klass] ||= {}
|
14
|
+
[:validate, :validate_on_create, :validate_on_update].each do |callback|
|
15
|
+
the_callback = klass.instance_variable_get("@#{callback.to_s}_callbacks")
|
16
|
+
repair[klass][callback] = (the_callback.nil? ? nil : the_callback.dup)
|
17
|
+
end
|
18
|
+
repair
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.reset_validations(recorded)
|
23
|
+
recorded.each do |klass, repairs|
|
24
|
+
[:validate, :validate_on_create, :validate_on_update].each do |callback|
|
25
|
+
klass.instance_variable_set("@#{callback.to_s}_callbacks", repairs[callback])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
def repair_validations(*model_classes)
|
33
|
+
setup do
|
34
|
+
@validation_repairs = ActiveRecord::Testing::RepairHelper::Toolbox.record_validations(*model_classes)
|
35
|
+
end
|
36
|
+
teardown do
|
37
|
+
ActiveRecord::Testing::RepairHelper::Toolbox.reset_validations(@validation_repairs)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def repair_validations(*model_classes, &block)
|
43
|
+
validation_repairs = ActiveRecord::Testing::RepairHelper::Toolbox.record_validations(*model_classes)
|
44
|
+
return block.call
|
45
|
+
ensure
|
46
|
+
ActiveRecord::Testing::RepairHelper::Toolbox.reset_validations(validation_repairs)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/test/config.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
print "Using native MySQL\n"
|
2
|
+
require_dependency 'models/course'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
ActiveRecord::Base.logger = Logger.new("debug.log")
|
6
|
+
|
7
|
+
# GRANT ALL PRIVILEGES ON activerecord_unittest.* to 'rails'@'localhost';
|
8
|
+
# GRANT ALL PRIVILEGES ON activerecord_unittest2.* to 'rails'@'localhost';
|
9
|
+
|
10
|
+
ActiveRecord::Base.configurations = {
|
11
|
+
'arunit' => {
|
12
|
+
:adapter => 'mysql',
|
13
|
+
:username => 'rails',
|
14
|
+
:encoding => 'utf8',
|
15
|
+
:database => 'activerecord_unittest',
|
16
|
+
},
|
17
|
+
'arunit2' => {
|
18
|
+
:adapter => 'mysql',
|
19
|
+
:username => 'rails',
|
20
|
+
:database => 'activerecord_unittest2'
|
21
|
+
}
|
22
|
+
}
|
23
|
+
|
24
|
+
ActiveRecord::Base.establish_connection 'arunit'
|
25
|
+
Course.establish_connection 'arunit2'
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class GiveMeBigNumbers < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :big_numbers do |table|
|
4
|
+
table.column :bank_balance, :decimal, :precision => 10, :scale => 2
|
5
|
+
table.column :big_bank_balance, :decimal, :precision => 15, :scale => 2
|
6
|
+
table.column :world_population, :decimal, :precision => 10
|
7
|
+
table.column :my_house_population, :decimal, :precision => 2
|
8
|
+
table.column :value_of_e, :decimal
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.down
|
13
|
+
drop_table :big_numbers
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class InnocentJointable < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table("people_reminders", :id => false) do |t|
|
4
|
+
t.column :reminder_id, :integer
|
5
|
+
t.column :person_id, :integer
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.down
|
10
|
+
drop_table "people_reminders"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class InnocentJointable < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table("people_reminders", :id => false) do |t|
|
4
|
+
t.column :reminder_id, :integer
|
5
|
+
t.column :person_id, :integer
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.down
|
10
|
+
drop_table "people_reminders"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class InnocentJointable < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table("people_reminders", :id => false) do |t|
|
4
|
+
t.column :reminder_id, :integer
|
5
|
+
t.column :person_id, :integer
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.down
|
10
|
+
drop_table "people_reminders"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class InnocentJointable < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table("people_reminders", :id => false) do |t|
|
4
|
+
t.column :reminder_id, :integer
|
5
|
+
t.column :person_id, :integer
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.down
|
10
|
+
drop_table "people_reminders"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class InnocentJointable < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table("people_reminders", :id => false) do |t|
|
4
|
+
t.column :reminder_id, :integer
|
5
|
+
t.column :person_id, :integer
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.down
|
10
|
+
drop_table "people_reminders"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class InnocentJointable < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table("people_reminders", :id => false) do |t|
|
4
|
+
t.column :reminder_id, :integer
|
5
|
+
t.column :person_id, :integer
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.down
|
10
|
+
drop_table "people_reminders"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,146 @@
|
|
1
|
+
class Author < ActiveRecord::Base
|
2
|
+
has_many :posts
|
3
|
+
has_many :very_special_comments, :through => :posts
|
4
|
+
has_many :posts_with_comments, :include => :comments, :class_name => "Post"
|
5
|
+
has_many :popular_grouped_posts, :include => :comments, :class_name => "Post", :group => "type", :having => "SUM(comments_count) > 1", :select => "type"
|
6
|
+
has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id'
|
7
|
+
has_many :posts_sorted_by_id_limited, :class_name => "Post", :order => 'posts.id', :limit => 1
|
8
|
+
has_many :posts_with_categories, :include => :categories, :class_name => "Post"
|
9
|
+
has_many :posts_with_comments_and_categories, :include => [ :comments, :categories ], :order => "posts.id", :class_name => "Post"
|
10
|
+
has_many :posts_containing_the_letter_a, :class_name => "Post"
|
11
|
+
has_many :posts_with_extension, :class_name => "Post" do #, :extend => ProxyTestExtension
|
12
|
+
def testing_proxy_owner
|
13
|
+
proxy_owner
|
14
|
+
end
|
15
|
+
def testing_proxy_reflection
|
16
|
+
proxy_reflection
|
17
|
+
end
|
18
|
+
def testing_proxy_target
|
19
|
+
proxy_target
|
20
|
+
end
|
21
|
+
end
|
22
|
+
has_one :post_about_thinking, :class_name => 'Post', :conditions => "posts.title like '%thinking%'"
|
23
|
+
has_one :post_about_thinking_with_last_comment, :class_name => 'Post', :conditions => "posts.title like '%thinking%'", :include => :last_comment
|
24
|
+
has_many :comments, :through => :posts
|
25
|
+
has_many :comments_containing_the_letter_e, :through => :posts, :source => :comments
|
26
|
+
has_many :comments_with_order_and_conditions, :through => :posts, :source => :comments, :order => 'comments.body', :conditions => "comments.body like 'Thank%'"
|
27
|
+
has_many :comments_with_include, :through => :posts, :source => :comments, :include => :post
|
28
|
+
|
29
|
+
has_many :thinking_posts, :class_name => 'Post', :conditions => { :title => 'So I was thinking' }, :dependent => :delete_all
|
30
|
+
has_many :welcome_posts, :class_name => 'Post', :conditions => { :title => 'Welcome to the weblog' }
|
31
|
+
|
32
|
+
has_many :comments_desc, :through => :posts, :source => :comments, :order => 'comments.id DESC'
|
33
|
+
has_many :limited_comments, :through => :posts, :source => :comments, :limit => 1
|
34
|
+
has_many :funky_comments, :through => :posts, :source => :comments
|
35
|
+
has_many :ordered_uniq_comments, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id'
|
36
|
+
has_many :ordered_uniq_comments_desc, :through => :posts, :source => :comments, :uniq => true, :order => 'comments.id DESC'
|
37
|
+
has_many :readonly_comments, :through => :posts, :source => :comments, :readonly => true
|
38
|
+
|
39
|
+
has_many :special_posts
|
40
|
+
has_many :special_post_comments, :through => :special_posts, :source => :comments
|
41
|
+
|
42
|
+
has_many :sti_posts, :class_name => 'StiPost'
|
43
|
+
has_many :sti_post_comments, :through => :sti_posts, :source => :comments
|
44
|
+
|
45
|
+
has_many :special_nonexistant_posts, :class_name => "SpecialPost", :conditions => "posts.body = 'nonexistant'"
|
46
|
+
has_many :special_nonexistant_post_comments, :through => :special_nonexistant_posts, :source => :comments, :conditions => "comments.post_id = 0"
|
47
|
+
has_many :nonexistant_comments, :through => :posts
|
48
|
+
|
49
|
+
has_many :hello_posts, :class_name => "Post", :conditions => "posts.body = 'hello'"
|
50
|
+
has_many :hello_post_comments, :through => :hello_posts, :source => :comments
|
51
|
+
has_many :posts_with_no_comments, :class_name => 'Post', :conditions => 'comments.id is null', :include => :comments
|
52
|
+
|
53
|
+
has_many :hello_posts_with_hash_conditions, :class_name => "Post",
|
54
|
+
:conditions => {:body => 'hello'}
|
55
|
+
has_many :hello_post_comments_with_hash_conditions, :through =>
|
56
|
+
:hello_posts_with_hash_conditions, :source => :comments
|
57
|
+
|
58
|
+
has_many :other_posts, :class_name => "Post"
|
59
|
+
has_many :posts_with_callbacks, :class_name => "Post", :before_add => :log_before_adding,
|
60
|
+
:after_add => :log_after_adding,
|
61
|
+
:before_remove => :log_before_removing,
|
62
|
+
:after_remove => :log_after_removing
|
63
|
+
has_many :posts_with_proc_callbacks, :class_name => "Post",
|
64
|
+
:before_add => Proc.new {|o, r| o.post_log << "before_adding#{r.id || '<new>'}"},
|
65
|
+
:after_add => Proc.new {|o, r| o.post_log << "after_adding#{r.id || '<new>'}"},
|
66
|
+
:before_remove => Proc.new {|o, r| o.post_log << "before_removing#{r.id}"},
|
67
|
+
:after_remove => Proc.new {|o, r| o.post_log << "after_removing#{r.id}"}
|
68
|
+
has_many :posts_with_multiple_callbacks, :class_name => "Post",
|
69
|
+
:before_add => [:log_before_adding, Proc.new {|o, r| o.post_log << "before_adding_proc#{r.id || '<new>'}"}],
|
70
|
+
:after_add => [:log_after_adding, Proc.new {|o, r| o.post_log << "after_adding_proc#{r.id || '<new>'}"}]
|
71
|
+
has_many :unchangable_posts, :class_name => "Post", :before_add => :raise_exception, :after_add => :log_after_adding
|
72
|
+
|
73
|
+
has_many :categorizations
|
74
|
+
has_many :categories, :through => :categorizations
|
75
|
+
|
76
|
+
has_many :categories_like_general, :through => :categorizations, :source => :category, :class_name => 'Category', :conditions => { :name => 'General' }
|
77
|
+
|
78
|
+
has_many :categorized_posts, :through => :categorizations, :source => :post
|
79
|
+
has_many :unique_categorized_posts, :through => :categorizations, :source => :post, :uniq => true
|
80
|
+
|
81
|
+
has_many :nothings, :through => :kateggorisatons, :class_name => 'Category'
|
82
|
+
|
83
|
+
has_many :author_favorites
|
84
|
+
has_many :favorite_authors, :through => :author_favorites, :order => 'name'
|
85
|
+
|
86
|
+
has_many :tagging, :through => :posts # through polymorphic has_one
|
87
|
+
has_many :taggings, :through => :posts, :source => :taggings # through polymorphic has_many
|
88
|
+
has_many :tags, :through => :posts # through has_many :through
|
89
|
+
has_many :post_categories, :through => :posts, :source => :categories
|
90
|
+
|
91
|
+
has_one :essay, :primary_key => :name, :as => :writer
|
92
|
+
|
93
|
+
belongs_to :author_address, :dependent => :destroy
|
94
|
+
belongs_to :author_address_extra, :dependent => :delete, :class_name => "AuthorAddress"
|
95
|
+
|
96
|
+
attr_accessor :post_log
|
97
|
+
|
98
|
+
def after_initialize
|
99
|
+
@post_log = []
|
100
|
+
end
|
101
|
+
|
102
|
+
def label
|
103
|
+
"#{id}-#{name}"
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
def log_before_adding(object)
|
108
|
+
@post_log << "before_adding#{object.id || '<new>'}"
|
109
|
+
end
|
110
|
+
|
111
|
+
def log_after_adding(object)
|
112
|
+
@post_log << "after_adding#{object.id}"
|
113
|
+
end
|
114
|
+
|
115
|
+
def log_before_removing(object)
|
116
|
+
@post_log << "before_removing#{object.id}"
|
117
|
+
end
|
118
|
+
|
119
|
+
def log_after_removing(object)
|
120
|
+
@post_log << "after_removing#{object.id}"
|
121
|
+
end
|
122
|
+
|
123
|
+
def raise_exception(object)
|
124
|
+
raise Exception.new("You can't add a post")
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
class AuthorAddress < ActiveRecord::Base
|
129
|
+
has_one :author
|
130
|
+
|
131
|
+
def self.destroyed_author_address_ids
|
132
|
+
@destroyed_author_address_ids ||= Hash.new { |h,k| h[k] = [] }
|
133
|
+
end
|
134
|
+
|
135
|
+
before_destroy do |author_address|
|
136
|
+
if author_address.author
|
137
|
+
AuthorAddress.destroyed_author_address_ids[author_address.author.id] << author_address.id
|
138
|
+
end
|
139
|
+
true
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
class AuthorFavorite < ActiveRecord::Base
|
144
|
+
belongs_to :author
|
145
|
+
belongs_to :favorite_author, :class_name => "Author"
|
146
|
+
end
|
data/test/models/bird.rb
ADDED
data/test/models/book.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
class Category < ActiveRecord::Base
|
2
|
+
has_and_belongs_to_many :posts
|
3
|
+
has_and_belongs_to_many :special_posts, :class_name => "Post"
|
4
|
+
has_and_belongs_to_many :other_posts, :class_name => "Post"
|
5
|
+
has_and_belongs_to_many :posts_with_authors_sorted_by_author_id, :class_name => "Post", :include => :authors, :order => "authors.id"
|
6
|
+
|
7
|
+
has_and_belongs_to_many(:select_testing_posts,
|
8
|
+
:class_name => 'Post',
|
9
|
+
:foreign_key => 'category_id',
|
10
|
+
:association_foreign_key => 'post_id',
|
11
|
+
:select => 'posts.*, 1 as correctness_marker')
|
12
|
+
|
13
|
+
has_and_belongs_to_many :post_with_conditions,
|
14
|
+
:class_name => 'Post',
|
15
|
+
:conditions => { :title => 'Yet Another Testing Title' }
|
16
|
+
|
17
|
+
has_and_belongs_to_many :popular_grouped_posts, :class_name => "Post", :group => "posts.type", :having => "sum(comments.post_id) > 2", :include => :comments
|
18
|
+
has_and_belongs_to_many :posts_gruoped_by_title, :class_name => "Post", :group => "title", :select => "title"
|
19
|
+
|
20
|
+
def self.what_are_you
|
21
|
+
'a category...'
|
22
|
+
end
|
23
|
+
|
24
|
+
has_many :categorizations
|
25
|
+
has_many :authors, :through => :categorizations, :select => 'authors.*, categorizations.post_id'
|
26
|
+
end
|
27
|
+
|
28
|
+
class SpecialCategory < Category
|
29
|
+
|
30
|
+
def self.what_are_you
|
31
|
+
'a special category...'
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
data/test/models/club.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
class Club < ActiveRecord::Base
|
2
|
+
has_many :memberships
|
3
|
+
has_many :members, :through => :memberships
|
4
|
+
has_many :current_memberships
|
5
|
+
has_one :sponsor
|
6
|
+
has_one :sponsored_member, :through => :sponsor, :source => :sponsorable, :source_type => "Member"
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def private_method
|
11
|
+
"I'm sorry sir, this is a *private* club, not a *pirate* club"
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class Comment < ActiveRecord::Base
|
2
|
+
named_scope :containing_the_letter_e, :conditions => "comments.body LIKE '%e%'"
|
3
|
+
named_scope :for_first_post, :conditions => { :post_id => 1 }
|
4
|
+
named_scope :for_first_author,
|
5
|
+
:joins => :post,
|
6
|
+
:conditions => { "posts.author_id" => 1 }
|
7
|
+
|
8
|
+
belongs_to :post, :counter_cache => true
|
9
|
+
|
10
|
+
def self.what_are_you
|
11
|
+
'a comment...'
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.search_by_type(q)
|
15
|
+
self.find(:all, :conditions => ["#{QUOTED_TYPE} = ?", q])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class SpecialComment < Comment
|
20
|
+
def self.what_are_you
|
21
|
+
'a special comment...'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class VerySpecialComment < Comment
|
26
|
+
def self.what_are_you
|
27
|
+
'a very special comment...'
|
28
|
+
end
|
29
|
+
end
|