acts_as_relatable 0.0.4 → 0.0.5
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 +4 -1
- data/lib/acts_as_relatable/relationship.rb +13 -0
- data/spec/acts_as_relatable/relationship_spec.rb +30 -6
- data/spec/spec_helper.rb +1 -0
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -10,7 +10,7 @@ Gem installation :
|
|
10
10
|
|
11
11
|
Or you can put it in your Gemfile :
|
12
12
|
|
13
|
-
gem "acts_as_relatable", "~> 0.0.
|
13
|
+
gem "acts_as_relatable", "~> 0.0.5"
|
14
14
|
|
15
15
|
and then run :
|
16
16
|
|
@@ -61,6 +61,9 @@ Followed by :
|
|
61
61
|
|
62
62
|
@butter.relates_to!(@pancake, false)
|
63
63
|
|
64
|
+
# You can also create relationships with the ActsAsRelatable::Relationship class method
|
65
|
+
|
66
|
+
ActsAsRelatable::Relationship.make_between(@butter, @pancake) # Passing false as a third argument make it "one sided"
|
64
67
|
|
65
68
|
=== Fetching relationships
|
66
69
|
|
@@ -7,5 +7,18 @@ module ActsAsRelatable
|
|
7
7
|
validates :relator_type, :presence => true
|
8
8
|
validates :related_id, :presence => true
|
9
9
|
validates :related_type, :presence => true
|
10
|
+
validate :self_relation
|
11
|
+
|
12
|
+
# This method relates two object together
|
13
|
+
def self.make_between(relator, related, bothsided=true)
|
14
|
+
relator.relates_to!(related, bothsided)
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
# It's impossible to create a relation with self as related object.
|
20
|
+
def self_relation
|
21
|
+
errors.add(:base, "could not create a relation that links to the same element!") if related == relator
|
22
|
+
end
|
10
23
|
end
|
11
24
|
end
|
@@ -7,13 +7,21 @@ describe ActsAsRelatable::Relationship do
|
|
7
7
|
it { should validate_presence_of :related_id }
|
8
8
|
it { should validate_presence_of :related_type }
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
before {
|
11
|
+
@product = Product.create(:name => 'product1')
|
12
|
+
@shop = Shop.create(:name => 'shop1')
|
13
|
+
}
|
14
|
+
|
15
|
+
context :custom_validations do
|
16
|
+
it "doesn't relates when related is self" do
|
17
|
+
expect {
|
18
|
+
@shop.relates_to!(@shop)
|
19
|
+
}.to_not change(ActsAsRelatable::Relationship, :count)
|
20
|
+
end
|
21
|
+
end
|
14
22
|
|
15
|
-
|
16
|
-
}
|
23
|
+
context :associations do
|
24
|
+
before { @product.relates_to! @shop }
|
17
25
|
|
18
26
|
context :relator do
|
19
27
|
it "returns the object that made the relation" do
|
@@ -28,4 +36,20 @@ describe ActsAsRelatable::Relationship do
|
|
28
36
|
end
|
29
37
|
end
|
30
38
|
|
39
|
+
|
40
|
+
context :make_between do
|
41
|
+
context "relates two objects" do
|
42
|
+
it "both sided by default" do
|
43
|
+
expect {
|
44
|
+
ActsAsRelatable::Relationship.make_between(@product, @shop)
|
45
|
+
}.to change(ActsAsRelatable::Relationship, :count).by(2)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "only one side" do
|
49
|
+
expect {
|
50
|
+
ActsAsRelatable::Relationship.make_between(@product, @shop, false)
|
51
|
+
}.to change(ActsAsRelatable::Relationship, :count).by(1)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
31
55
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -32,6 +32,7 @@ if File.exists?(database_yml)
|
|
32
32
|
ActiveRecord::Base.establish_connection(config)
|
33
33
|
end
|
34
34
|
|
35
|
+
Dir.mkdir("./tmp") unless File.directory?("./tmp")
|
35
36
|
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "../tmp/debug.log"))
|
36
37
|
ActiveRecord::Base.default_timezone = :utc
|
37
38
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: acts_as_relatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- "Fr\xC3\xA9d\xC3\xA9ric Malamitsas"
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-08-
|
13
|
+
date: 2011-08-19 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
requirements: []
|
101
101
|
|
102
102
|
rubyforge_project: acts_as_relatable
|
103
|
-
rubygems_version: 1.8.
|
103
|
+
rubygems_version: 1.8.8
|
104
104
|
signing_key:
|
105
105
|
specification_version: 3
|
106
106
|
summary: Link AR objects easily with polymorphic associations
|