acts_as_relatable 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc
CHANGED
@@ -26,6 +26,8 @@ Followed by :
|
|
26
26
|
|
27
27
|
== Usage
|
28
28
|
|
29
|
+
# Model definition
|
30
|
+
|
29
31
|
class Recipe < ActiveRecord::Base
|
30
32
|
acts_as_relatable :product
|
31
33
|
end
|
@@ -34,33 +36,91 @@ Followed by :
|
|
34
36
|
acts_as_relatable :recipe
|
35
37
|
end
|
36
38
|
|
39
|
+
# Create some entries
|
40
|
+
|
37
41
|
@bread = Product.create(:name => "Bread")
|
38
42
|
@butter = Product.create(:name => "Butter")
|
39
43
|
@pancake = Recipe.create(:name => "Pancakes")
|
40
44
|
|
45
|
+
|
41
46
|
#Creating relationships
|
47
|
+
|
42
48
|
@butter.relates_to!(@pancake) # #<ActsAsRelatable::Relationship id: 2, relator_id: 1, relator_type: "Recipe", related_id: 2, related_type: "Product">
|
43
49
|
@bread.relates_to!(@butter) # #<ActsAsRelatable::Relationship id: 4, relator_id: 2, relator_type: "Product", related_id: 1, related_type: "Product">
|
44
50
|
|
45
|
-
#By default, relationships are both-sided, it means that on the first line above,
|
46
|
-
#
|
51
|
+
# By default, relationships are both-sided, it means that on the first line above,
|
52
|
+
# @butter is related to @pancake, but @pancake is also related to @butter.
|
53
|
+
#If you don't want/need this behaviour, you can pass false as a second argument
|
54
|
+
# to the relates_to! instance method :
|
47
55
|
|
48
56
|
@butter.relates_to!(@pancake, false)
|
49
57
|
|
50
58
|
|
51
59
|
#Fetching relationships
|
60
|
+
|
52
61
|
@butter.related_recipes # [#<Recipe id: 1, name: "Pancakes">]
|
53
62
|
@butter.related_products # [#<Product id: 1, name: "Bread"]
|
54
63
|
|
64
|
+
|
55
65
|
@butter.relateds # {:recipes=>[#<Recipe id: 1, name: "Pancakes">], :products=>[#<Product id: 1, name: "Bread">]}
|
56
66
|
|
57
67
|
#Testing relationships
|
68
|
+
|
58
69
|
@butter.related_to? @bread # true
|
59
70
|
@bread.related_to? @pancake # false
|
60
71
|
|
72
|
+
|
61
73
|
#Destroying relationships (This instance method destroys both relationships if it's a both-sided one)
|
74
|
+
|
62
75
|
@butter.destroy_relation_with @pancake
|
63
76
|
|
77
|
+
== Relationship model extension
|
78
|
+
|
79
|
+
You can add class/instance methods to the ActsAsRelatable::Relationship model
|
80
|
+
by adding a module in your project. Here is how to do it :
|
81
|
+
|
82
|
+
Add a new file named relationship_extension.rb in your app/models directory.
|
83
|
+
|
84
|
+
Let's call this module RelationshipExtension. The file may look like :
|
85
|
+
|
86
|
+
module RelationshipExtension
|
87
|
+
def self.included(klass)
|
88
|
+
klass.extend(ClassMethods).relate
|
89
|
+
end
|
90
|
+
|
91
|
+
module ClassMethods
|
92
|
+
|
93
|
+
def relate
|
94
|
+
include RelationshipExtension::InstanceMethods
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
module InstanceMethods
|
100
|
+
def to_s
|
101
|
+
"#{relator.class} #{relator.id} is related with #{related.class} #{related.id}"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
In a shell, run :
|
107
|
+
|
108
|
+
r g acts_as_relatable:config
|
109
|
+
|
110
|
+
It will create a file called acts_as_relatable.rb in config/initializers
|
111
|
+
|
112
|
+
Open this file and uncomment this line :
|
113
|
+
|
114
|
+
ActsAsRelatable::Relationship.send(:include, RelationshipExtension)
|
115
|
+
|
116
|
+
I define a to_s instance method in this example. It is now a part of the ActsAsRelatable::Relationship model
|
117
|
+
and you can use it like that :
|
118
|
+
|
119
|
+
ActsAsRelatable::Relationship.first.to_s
|
120
|
+
|
121
|
+
it will return :
|
122
|
+
|
123
|
+
"Product 1 is related with Recipe 1"
|
64
124
|
|
65
125
|
== Contributing to acts_as_relatable
|
66
126
|
|
@@ -70,13 +130,15 @@ Followed by :
|
|
70
130
|
* Start a feature/bugfix branch
|
71
131
|
* Commit and push until you are happy with your contribution
|
72
132
|
* 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,
|
133
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version,
|
134
|
+
or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
74
135
|
|
75
136
|
== TODOS
|
76
137
|
|
77
138
|
* Rails 3.1 compatibility
|
78
139
|
* Add a make_between method in Relationship model
|
79
140
|
* Improve README
|
141
|
+
* Specs for generators
|
80
142
|
|
81
143
|
== Copyright
|
82
144
|
|
@@ -1 +1,2 @@
|
|
1
|
-
#
|
1
|
+
# Uncomment the following line if you want to add methods to the ActsAsRelatable::Relationship AR model
|
2
|
+
#ActsAsRelatable::Relationship.send(:include, RelationshipExtension)
|
@@ -1,34 +0,0 @@
|
|
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
|