acts_as_relatable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +7 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +85 -0
- data/Rakefile +11 -0
- data/init.rb +1 -0
- data/lib/acts_as_relatable.rb +11 -0
- data/lib/acts_as_relatable/relatable.rb +101 -0
- data/lib/acts_as_relatable/relationship.rb +11 -0
- data/lib/generators/acts_as_relatable/USAGE +3 -0
- data/lib/generators/acts_as_relatable/config_generator.rb +11 -0
- data/lib/generators/acts_as_relatable/migration_generator.rb +22 -0
- data/lib/generators/acts_as_relatable/templates/acts_as_relatable.rb +1 -0
- data/lib/generators/acts_as_relatable/templates/migration.rb +28 -0
- data/spec/acts_as_relatable/generators/config_generator_spec.rb +34 -0
- data/spec/acts_as_relatable/generators/migration_generator_spec.rb +0 -0
- data/spec/acts_as_relatable/relatable_spec.rb +146 -0
- data/spec/acts_as_relatable/relationship_spec.rb +31 -0
- data/spec/database.yml +19 -0
- data/spec/database.yml.sample +19 -0
- data/spec/models.rb +11 -0
- data/spec/schema.rb +23 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +60 -0
- metadata +108 -0
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Malamitsas
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
= acts_as_relatable
|
2
|
+
|
3
|
+
== Installation
|
4
|
+
|
5
|
+
(This gem has only been tested with Rails 3 for the moment, compatibility with Rails 3.1 is coming soon)
|
6
|
+
|
7
|
+
Gem installation :
|
8
|
+
|
9
|
+
gem install acts_as_relatable
|
10
|
+
|
11
|
+
Or you can put it in your Gemfile :
|
12
|
+
|
13
|
+
gem "acts_as_relatable"
|
14
|
+
|
15
|
+
and then run :
|
16
|
+
|
17
|
+
bundle install
|
18
|
+
|
19
|
+
You'll then need to run the migration generator for the Relationship model :
|
20
|
+
|
21
|
+
r generate acts_as_relatable:migration
|
22
|
+
|
23
|
+
Followed by :
|
24
|
+
|
25
|
+
rake db:migrate
|
26
|
+
|
27
|
+
== Usage
|
28
|
+
|
29
|
+
class Recipe < ActiveRecord::Base
|
30
|
+
acts_as_relatable :product
|
31
|
+
end
|
32
|
+
|
33
|
+
class Product < ActiveRecord::Base
|
34
|
+
acts_as_relatable :recipe
|
35
|
+
end
|
36
|
+
|
37
|
+
@bread = Product.create(:name => "Bread")
|
38
|
+
@butter = Product.create(:name => "Butter")
|
39
|
+
@pancake = Recipe.create(:name => "Pancakes")
|
40
|
+
|
41
|
+
#Creating relationships
|
42
|
+
@butter.relates_to!(@pancake) # #<ActsAsRelatable::Relationship id: 2, relator_id: 1, relator_type: "Recipe", related_id: 2, related_type: "Product">
|
43
|
+
@bread.relates_to!(@butter) # #<ActsAsRelatable::Relationship id: 4, relator_id: 2, relator_type: "Product", related_id: 1, related_type: "Product">
|
44
|
+
|
45
|
+
#By default, relationships are both-sided, it means that on the first line above, @butter is related to @pancake, but @pancake is also related to @butter.
|
46
|
+
#If you don't want/need this behaviour, you can pass false as a second argument to the relates_to! instance method :
|
47
|
+
|
48
|
+
@butter.relates_to!(@pancake, false)
|
49
|
+
|
50
|
+
|
51
|
+
#Fetching relationships
|
52
|
+
@butter.related_recipes # [#<Recipe id: 1, name: "Pancakes">]
|
53
|
+
@butter.related_products # [#<Product id: 1, name: "Bread"]
|
54
|
+
|
55
|
+
@butter.relateds # {:recipes=>[#<Recipe id: 1, name: "Pancakes">], :products=>[#<Product id: 1, name: "Bread">]}
|
56
|
+
|
57
|
+
#Testing relationships
|
58
|
+
@butter.related_to? @bread # true
|
59
|
+
@bread.related_to? @pancake # false
|
60
|
+
|
61
|
+
#Destroying relationships (This instance method destroys both relationships if it's a both-sided one)
|
62
|
+
@butter.destroy_relation_with @pancake
|
63
|
+
|
64
|
+
|
65
|
+
== Contributing to acts_as_relatable
|
66
|
+
|
67
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
68
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
69
|
+
* Fork the project
|
70
|
+
* Start a feature/bugfix branch
|
71
|
+
* Commit and push until you are happy with your contribution
|
72
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
73
|
+
* 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.
|
74
|
+
|
75
|
+
== TODOS
|
76
|
+
|
77
|
+
* Rails 3.1 compatibility
|
78
|
+
* Add a make_between method in Relationship model
|
79
|
+
* Improve README
|
80
|
+
|
81
|
+
== Copyright
|
82
|
+
|
83
|
+
Copyright (c) 2011 Malamitsas. See LICENSE.txt for
|
84
|
+
further details.
|
85
|
+
|
data/Rakefile
ADDED
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "acts_as_relatable"
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require "active_record"
|
2
|
+
require "active_record/version"
|
3
|
+
|
4
|
+
require "acts_as_relatable/relationship"
|
5
|
+
require "acts_as_relatable/relatable"
|
6
|
+
|
7
|
+
if defined?(ActiveRecord::Base)
|
8
|
+
ActiveRecord::Base.send :include, ActsAsRelatable::Relatable
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'rails/generators'
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module ActsAsRelatable
|
2
|
+
|
3
|
+
|
4
|
+
module Relatable
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def acts_as_relatable(*relatable_models)
|
12
|
+
# Create polymorphic associations
|
13
|
+
class_attribute :relatable_types
|
14
|
+
|
15
|
+
self.relatable_types = relatable_models.to_a.flatten.compact.map(&:to_sym) << self.to_s.underscore
|
16
|
+
|
17
|
+
has_many :relationships, :as => :relator, :order => "created_at desc", :class_name => "ActsAsRelatable::Relationship", :dependent => :destroy
|
18
|
+
has_many :incoming_relationships, :as => :related, :class_name => "ActsAsRelatable::Relationship", :dependent => :destroy
|
19
|
+
|
20
|
+
relatable_types.each do |rel|
|
21
|
+
|
22
|
+
has_many "related_#{rel.to_s.pluralize}",
|
23
|
+
:through => :relationships,
|
24
|
+
:source => "related_#{rel.to_s}",
|
25
|
+
:class_name => rel.to_s.classify,
|
26
|
+
:conditions => { 'relationships.related_type' => rel.to_s.classify }
|
27
|
+
|
28
|
+
has_many "relator_#{rel.to_s.pluralize}",
|
29
|
+
:through => :incoming_relationships,
|
30
|
+
:source => "relator_#{rel.to_s}",
|
31
|
+
:class_name => rel.to_s.classify,
|
32
|
+
:conditions => {'relationships.relator_type' => rel.to_s.classify}
|
33
|
+
|
34
|
+
ActsAsRelatable::Relationship.belongs_to "relator_#{rel.to_s}".to_sym, :class_name => rel.to_s.classify, :foreign_key => :relator_id
|
35
|
+
ActsAsRelatable::Relationship.belongs_to "related_#{rel.to_s}".to_sym, :class_name => rel.to_s.classify, :foreign_key => :related_id
|
36
|
+
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
include ActsAsRelatable::Relatable::InstanceMethods
|
41
|
+
extend ActsAsRelatable::Relatable::SingletonMethods
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
module SingletonMethods; end
|
46
|
+
|
47
|
+
module InstanceMethods
|
48
|
+
|
49
|
+
# This method creates relation between 2 objects.
|
50
|
+
# Returns the relationships in an array or false if the relation already exists.
|
51
|
+
# By default a bothsided relationship is created but this can be overiden by seting bothsided to false.
|
52
|
+
#
|
53
|
+
# Example :
|
54
|
+
# user = User.find 1
|
55
|
+
# place = Place.find 1
|
56
|
+
# user.relate_to! place
|
57
|
+
def relates_to!(some_object, bothsided=true)
|
58
|
+
return false if (self.related_to?(some_object) || self.eql?(some_object))
|
59
|
+
|
60
|
+
ActsAsRelatable::Relationship.unscoped.create(:related => some_object, :relator => self)
|
61
|
+
ActsAsRelatable::Relationship.unscoped.create(:related => self, :relator => some_object) if bothsided == true
|
62
|
+
end
|
63
|
+
|
64
|
+
# This method returns true if self is already related with some_object
|
65
|
+
def related_to? some_object
|
66
|
+
!!relation(some_object)
|
67
|
+
end
|
68
|
+
|
69
|
+
# This method returns the relationship between self and another_object, or nil
|
70
|
+
def relation some_object
|
71
|
+
ActsAsRelatable::Relationship.unscoped.where(:relator_id => self.id,
|
72
|
+
:related_id => some_object.id,
|
73
|
+
:relator_type => self.class.base_class.to_s,
|
74
|
+
:related_type => some_object.class.base_class.to_s).first
|
75
|
+
end
|
76
|
+
|
77
|
+
# This method returns relatable objects from a given Array of models
|
78
|
+
# Exemple : @category.relateds(:classes => ['Place', 'Event']) will return
|
79
|
+
# {"Place" => [related_place1, related_place2], "Event" => [related_event1, related_event2]}
|
80
|
+
def relateds(options = {})
|
81
|
+
relateds = {}
|
82
|
+
classes = options.try(:[], :classes) ? options[:classes] : relatable_types
|
83
|
+
classes.each do |c|
|
84
|
+
pluralized_rel = c.to_s.underscore.pluralize
|
85
|
+
relations = self.send("related_#{pluralized_rel}").limit(options[:limit] || 10)
|
86
|
+
relateds = relateds.merge(pluralized_rel.to_sym => relations) if relations.any?
|
87
|
+
end
|
88
|
+
relateds
|
89
|
+
end
|
90
|
+
|
91
|
+
# This method destroy the relationship between self (as a relator object) and some_object (as a related object).
|
92
|
+
def destroy_relation_with some_object
|
93
|
+
relationships_to_destroy = [relation(some_object), some_object.relation(self)]
|
94
|
+
relationships_to_destroy.each { |r| r.destroy if r }
|
95
|
+
relationships.reload
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ActsAsRelatable
|
2
|
+
class Relationship < ::ActiveRecord::Base
|
3
|
+
belongs_to :relator, :polymorphic => true, :foreign_key => :relator_id, :touch => true
|
4
|
+
belongs_to :related, :polymorphic => true, :foreign_key => :related_id, :touch => true
|
5
|
+
|
6
|
+
validates :relator_id, :presence => true, :uniqueness => {:scope => [:relator_type, :related_id, :related_type]}
|
7
|
+
validates :relator_type, :presence => true
|
8
|
+
validates :related_id, :presence => true
|
9
|
+
validates :related_type, :presence => true
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ActsAsRelatable
|
2
|
+
module Generators
|
3
|
+
class ConfigGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
def generate_ability
|
7
|
+
copy_file "acts_as_relatable.rb", "config/initializers/acts_as_relatable.rb"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActsAsRelatable
|
2
|
+
class MigrationGenerator < Rails::Generators::Base
|
3
|
+
include Rails::Generators::Migration
|
4
|
+
|
5
|
+
def self.source_root
|
6
|
+
@source_root ||= File.expand_path('../templates', __FILE__)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.next_migration_number(dirname)
|
10
|
+
if ActiveRecord::Base.timestamped_migrations
|
11
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
12
|
+
else
|
13
|
+
"%.3d" % (current_migration_number(dirname) + 1)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def create_migration_file
|
18
|
+
migration_template 'migration.rb', 'db/migrate/acts_as_relatable_migration.rb'
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
# This config file is useless for the moment but should be useful in a near future
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class ActsAsRelatableMigration < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
# Relationship table
|
4
|
+
create_table :relationships do |t|
|
5
|
+
t.integer :relator_id
|
6
|
+
t.string :relator_type
|
7
|
+
t.integer :related_id
|
8
|
+
t.string :related_type
|
9
|
+
t.timestamps
|
10
|
+
end
|
11
|
+
|
12
|
+
# Relationship indexes
|
13
|
+
add_index :relationships, ["related_id"], :name => "index_relationships_on_related_id"
|
14
|
+
add_index :relationships, ["related_type", "related_id"], :name => "poly_relationships_related"
|
15
|
+
add_index :relationships, ["relator_id"], :name => "index_relationships_on_relator_id"
|
16
|
+
add_index :relationships, ["relator_type", "relator_id"], :name => "poly_relationships_relator"
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def self.down
|
21
|
+
drop_table :relationships
|
22
|
+
|
23
|
+
remove_index :relationships, :name => "index_relationships_on_related_id"
|
24
|
+
remove_index :relationships, :name => "poly_relationships_related"
|
25
|
+
remove_index :relationships, :name => "index_relationships_on_relator_id"
|
26
|
+
remove_index :relationships, :name => "poly_relationships_relator"
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
# describe ActsAsRelatable::Generators::ConfigGenerator do
|
3
|
+
#
|
4
|
+
# context "generates config file" do
|
5
|
+
#
|
6
|
+
# before { setup }
|
7
|
+
# after { teardown }
|
8
|
+
#
|
9
|
+
# it "copy it in config/initializers" do
|
10
|
+
# Rails::Generators::Scripts::Generate.new.run([""], :destination => fake_rails_root)
|
11
|
+
# new_file = (file_list - @original_files).first
|
12
|
+
# assert_equal "definition.txt", File.basename(new_file)
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# def setup
|
16
|
+
# FileUtils.mkdir_p(fake_rails_root)
|
17
|
+
# @original_files = file_list
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# def teardown
|
21
|
+
# FileUtils.rm_r(fake_rails_root)
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# def fake_rails_root
|
25
|
+
# File.join(File.dirname(__FILE__), 'rails_root')
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# def file_list
|
29
|
+
# Dir.glob(File.join(fake_rails_root, "*"))
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
# end
|
File without changes
|
@@ -0,0 +1,146 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ActsAsRelatable::Relatable do
|
4
|
+
|
5
|
+
before { @tag = Tag.create(:name => "tag1") }
|
6
|
+
|
7
|
+
context :associations do
|
8
|
+
context :without_content do
|
9
|
+
subject { @tag.relationships }
|
10
|
+
it { should be_empty }
|
11
|
+
|
12
|
+
subject { @tag.incoming_relationships }
|
13
|
+
it { should be_empty }
|
14
|
+
|
15
|
+
subject { @tag.related_products }
|
16
|
+
it { should be_empty }
|
17
|
+
|
18
|
+
subject { @tag.related_shops }
|
19
|
+
it { should be_empty}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
context :methods do
|
24
|
+
before {
|
25
|
+
@products = {
|
26
|
+
:product1 => Product.create(:name => 'product1'),
|
27
|
+
:product2 => Product.create(:name => 'product2'),
|
28
|
+
:product3 => Product.create(:name => 'product3')
|
29
|
+
}
|
30
|
+
@shops = {
|
31
|
+
:shop1 => Shop.create(:name => 'shop1'),
|
32
|
+
:shop2 => Shop.create(:name => 'shop2'),
|
33
|
+
:shop3 => Shop.create(:name => 'shop3')
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
context :relates_to! do
|
38
|
+
|
39
|
+
context :both_sided do
|
40
|
+
before { @products[:product1].relates_to!(@shops[:shop3]) }
|
41
|
+
|
42
|
+
it "relates the first side" do
|
43
|
+
@products[:product1].related_to?(@shops[:shop3]).should be_true
|
44
|
+
end
|
45
|
+
|
46
|
+
it "relates the other side" do
|
47
|
+
@shops[:shop3].related_to?(@products[:product1]).should be_true
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context :one_sided do
|
52
|
+
before { @products[:product1].relates_to!(@shops[:shop3], :both_sided => false) }
|
53
|
+
|
54
|
+
it "relates the first side" do
|
55
|
+
@products[:product1].related_to?(@shops[:shop3]).should be_true
|
56
|
+
end
|
57
|
+
|
58
|
+
it "doesn't relate the other side" do
|
59
|
+
@shops[:shop3].related_to?(@products[:product1]).should be_false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
context :related_to? do
|
66
|
+
context :returns do
|
67
|
+
before { @products[:product1].relates_to!(@shops[:shop1]) }
|
68
|
+
|
69
|
+
it "true if self is related to the passed object" do
|
70
|
+
@products[:product1].related_to?(@shops[:shop1]).should be_true
|
71
|
+
end
|
72
|
+
|
73
|
+
it "false if self is not related to the passed object" do
|
74
|
+
@products[:product1].related_to?(@shops[:shop2]).should be_false
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context :relateds do
|
80
|
+
context :returns do
|
81
|
+
before {
|
82
|
+
@products[:product1].relates_to!(@products[:product3])
|
83
|
+
@products[:product1].relates_to!(@shops[:shop1])
|
84
|
+
}
|
85
|
+
|
86
|
+
it "elems of all classes if there is no class specified" do
|
87
|
+
@products[:product1].relateds.should == {:shops => [@shops[:shop1]], :products => [@products[:product3]]}
|
88
|
+
end
|
89
|
+
|
90
|
+
it "objects from the specified classes only" do
|
91
|
+
@products[:product1].relateds(:classes => ['Product']).should == {:products => [@products[:product3]]}
|
92
|
+
end
|
93
|
+
|
94
|
+
it "same results as related_products" do
|
95
|
+
@products[:product1].relateds(:classes => ['Product'])[:products].should.eql?(@products[:product1].related_products)
|
96
|
+
end
|
97
|
+
|
98
|
+
it "nil if the specified class doesn't have content" do
|
99
|
+
@products[:product1].relateds(:classes => ['Tag']).should == {}
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context :relation do
|
105
|
+
before { @products[:product1].relates_to!(@shops[:shop1]) }
|
106
|
+
|
107
|
+
context :returns do
|
108
|
+
it "an instance of ActsAsRelatable::Relationship" do
|
109
|
+
@products[:product1].relation(@shops[:shop1]).should be_an_instance_of ActsAsRelatable::Relationship
|
110
|
+
end
|
111
|
+
|
112
|
+
it "the related" do
|
113
|
+
@products[:product1].relation(@shops[:shop1]).related.should == @shops[:shop1]
|
114
|
+
end
|
115
|
+
|
116
|
+
it "the relator" do
|
117
|
+
@products[:product1].relation(@shops[:shop1]).relator.should == @products[:product1]
|
118
|
+
end
|
119
|
+
|
120
|
+
it "nil if there is no relation between self and the passed object" do
|
121
|
+
@products[:product1].relation(@shops[:shop3]).should be_nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
context :destroy_relation_with do
|
127
|
+
before {
|
128
|
+
@products[:product1].relates_to!(@products[:product3])
|
129
|
+
@products[:product1].relates_to!(@shops[:shop1])
|
130
|
+
}
|
131
|
+
|
132
|
+
context :after_destroy do
|
133
|
+
before { @products[:product1].destroy_relation_with(@products[:product3]) }
|
134
|
+
|
135
|
+
it "destroys the relationship containing the specified object" do
|
136
|
+
@products[:product1].related_to?(@products[:product3]).should be_false
|
137
|
+
end
|
138
|
+
|
139
|
+
it "keeps the other relationship" do
|
140
|
+
@products[:product1].related_to?(@shops[:shop1]).should be_true
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ActsAsRelatable::Relationship do
|
4
|
+
|
5
|
+
it { should validate_presence_of :relator_id }
|
6
|
+
it { should validate_presence_of :relator_type }
|
7
|
+
it { should validate_presence_of :related_id }
|
8
|
+
it { should validate_presence_of :related_type }
|
9
|
+
|
10
|
+
context :associations do
|
11
|
+
before {
|
12
|
+
@product = Product.create(:name => 'product1')
|
13
|
+
@shop = Shop.create(:name => 'shop1')
|
14
|
+
|
15
|
+
@product.relates_to! @shop
|
16
|
+
}
|
17
|
+
|
18
|
+
context :relator do
|
19
|
+
it "returns the object that made the relation" do
|
20
|
+
@product.relationships.first.relator.should == @product
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context :related do
|
25
|
+
it "returns the object that has been related" do
|
26
|
+
@product.relationships.first.related.should == @shop
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
data/spec/database.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
sqlite3:
|
2
|
+
adapter: sqlite3
|
3
|
+
database: acts_as_relatable.sqlite3
|
4
|
+
|
5
|
+
mysql:
|
6
|
+
adapter: mysql2
|
7
|
+
hostname: localhost
|
8
|
+
username: root
|
9
|
+
password:
|
10
|
+
database: acts_as_relatable
|
11
|
+
charset: utf8
|
12
|
+
|
13
|
+
postgresql:
|
14
|
+
adapter: postgresql
|
15
|
+
hostname: localhost
|
16
|
+
username: postgres
|
17
|
+
password:
|
18
|
+
database: acts_as_relatable
|
19
|
+
encoding: utf8
|
@@ -0,0 +1,19 @@
|
|
1
|
+
sqlite3:
|
2
|
+
adapter: sqlite3
|
3
|
+
database: acts_as_relatable.sqlite3
|
4
|
+
|
5
|
+
mysql:
|
6
|
+
adapter: mysql2
|
7
|
+
hostname: localhost
|
8
|
+
username: root
|
9
|
+
password:
|
10
|
+
database: acts_as_relatable
|
11
|
+
charset: utf8
|
12
|
+
|
13
|
+
postgresql:
|
14
|
+
adapter: postgresql
|
15
|
+
hostname: localhost
|
16
|
+
username: postgres
|
17
|
+
password:
|
18
|
+
database: acts_as_relatable
|
19
|
+
encoding: utf8
|
data/spec/models.rb
ADDED
data/spec/schema.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
ActiveRecord::Schema.define :version => 0 do
|
2
|
+
create_table :products, :force => true do |t|
|
3
|
+
t.string :name
|
4
|
+
t.integer :price
|
5
|
+
end
|
6
|
+
|
7
|
+
create_table :tags, :force => true do |t|
|
8
|
+
t.string :name
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table :shops, :force => true do |t|
|
12
|
+
t.string :name
|
13
|
+
t.integer :size
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :relationships, :force => true do |t|
|
17
|
+
t.integer :relator_id
|
18
|
+
t.string :relator_type
|
19
|
+
t.integer :related_id
|
20
|
+
t.string :related_type
|
21
|
+
t.timestamps
|
22
|
+
end
|
23
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require "bundler"
|
3
|
+
|
4
|
+
Bundler.require(:default)
|
5
|
+
|
6
|
+
require 'active_support/all'
|
7
|
+
|
8
|
+
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each {|f| require f}
|
9
|
+
|
10
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../lib/acts_as_relatable'))
|
11
|
+
|
12
|
+
db_name = ENV['DB'] || 'sqlite3'
|
13
|
+
database_yml = File.expand_path('../database.yml', __FILE__)
|
14
|
+
|
15
|
+
if File.exists?(database_yml)
|
16
|
+
ar_configuration = YAML.load_file(database_yml)
|
17
|
+
ActiveRecord::Base.configurations = ar_configuration
|
18
|
+
config = ActiveRecord::Base.configurations[db_name]
|
19
|
+
|
20
|
+
begin
|
21
|
+
ActiveRecord::Base.establish_connection(db_name)
|
22
|
+
ActiveRecord::Base.connection
|
23
|
+
rescue
|
24
|
+
case db_name
|
25
|
+
when /mysql/
|
26
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => nil))
|
27
|
+
ActiveRecord::Base.connection.create_database(config['database'], {:charset => 'utf8', :collection => 'utf8_unicode_ci'})
|
28
|
+
when /postgresql/
|
29
|
+
ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
|
30
|
+
ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => 'utf8'))
|
31
|
+
end
|
32
|
+
ActiveRecord::Base.establish_connection(config)
|
33
|
+
end
|
34
|
+
|
35
|
+
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "../tmp/debug.log"))
|
36
|
+
ActiveRecord::Base.default_timezone = :utc
|
37
|
+
|
38
|
+
ActiveRecord::Base.silence do
|
39
|
+
ActiveRecord::Migration.verbose = false
|
40
|
+
load(File.dirname(__FILE__) + '/schema.rb')
|
41
|
+
load(File.dirname(__FILE__) + '/models.rb')
|
42
|
+
end
|
43
|
+
|
44
|
+
else
|
45
|
+
raise "Please create #{database_yml} first to configure your database. Take a look at: #{database_yml}.sample"
|
46
|
+
end
|
47
|
+
|
48
|
+
#For generators
|
49
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../lib/generators/acts_as_relatable/config_generator.rb'))
|
50
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '../lib/generators/acts_as_relatable/migration_generator.rb'))
|
51
|
+
|
52
|
+
def clean_database!
|
53
|
+
models = [Product, Shop, Tag]
|
54
|
+
models.each do |model|
|
55
|
+
ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
clean_database!
|
60
|
+
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_relatable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- "Fr\xC3\xA9d\xC3\xA9ric Malamitsas"
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-16 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 3.0.9
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: activesupport
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "3.0"
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "2.5"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id003
|
48
|
+
description: Acts_as_relatable gem allows you to link/relate AR objects together easily with polymorphic associations.
|
49
|
+
email: fmalamitsas@gmail.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files: []
|
55
|
+
|
56
|
+
files:
|
57
|
+
- lib/acts_as_relatable/relatable.rb
|
58
|
+
- lib/acts_as_relatable/relationship.rb
|
59
|
+
- lib/acts_as_relatable.rb
|
60
|
+
- lib/generators/acts_as_relatable/config_generator.rb
|
61
|
+
- lib/generators/acts_as_relatable/migration_generator.rb
|
62
|
+
- lib/generators/acts_as_relatable/templates/acts_as_relatable.rb
|
63
|
+
- lib/generators/acts_as_relatable/templates/migration.rb
|
64
|
+
- lib/generators/acts_as_relatable/USAGE
|
65
|
+
- spec/acts_as_relatable/generators/config_generator_spec.rb
|
66
|
+
- spec/acts_as_relatable/generators/migration_generator_spec.rb
|
67
|
+
- spec/acts_as_relatable/relatable_spec.rb
|
68
|
+
- spec/acts_as_relatable/relationship_spec.rb
|
69
|
+
- spec/database.yml
|
70
|
+
- spec/database.yml.sample
|
71
|
+
- spec/models.rb
|
72
|
+
- spec/schema.rb
|
73
|
+
- spec/spec.opts
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- Gemfile
|
76
|
+
- LICENSE.txt
|
77
|
+
- Rakefile
|
78
|
+
- README.rdoc
|
79
|
+
- init.rb
|
80
|
+
homepage: http://github.com/fmalamitsas/acts_as_relatable
|
81
|
+
licenses: []
|
82
|
+
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: "0"
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
none: false
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: 1.3.4
|
100
|
+
requirements: []
|
101
|
+
|
102
|
+
rubyforge_project: acts_as_relatable
|
103
|
+
rubygems_version: 1.8.7
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: Link AR objects easily with polymorphic associations
|
107
|
+
test_files: []
|
108
|
+
|