somehow_has_relation 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Matteo Latini (mtylty)
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,82 @@
1
+ = SomehowHasRelation
2
+
3
+ A simple gem/plugin that can be used to define active_record relations between multiple models.
4
+ This means that, when proper has_many, has_one, belongs_to methods have been defined, you can
5
+ reach a far away (in tearms of relations) model by calling a single method.
6
+ It uses recursion so it is teoretically possible to ignite endless recursion loops... be careful!
7
+
8
+
9
+ == Installation
10
+
11
+ In <b>Rails 3</b>, add this to your Gemfile and run the +bundle+ command.
12
+
13
+ gem "somehow_has_relation"
14
+
15
+ In <b>Rails 2</b>, add this to your environment.rb file.
16
+
17
+ config.gem "somehow_has_relation"
18
+
19
+ Alternatively, you can install it as a plugin.
20
+
21
+ rails plugin install git://github.com/mtylty/somehow_has_relation.git
22
+
23
+ == Usage
24
+
25
+ Given you declared some models like this:
26
+
27
+ class Post < ActiveRecord::Base
28
+ belongs_to :author
29
+ has_many :comments
30
+ end
31
+
32
+ class Author < ActiveRecord::Base
33
+ has_many :posts
34
+
35
+ somehow_has :many => :comments, :through => :posts
36
+ end
37
+
38
+ class Comment < ActiveRecord::Base
39
+ belongs_to :post
40
+ end
41
+
42
+ You can then retrieve an author's posts' comments by calling:
43
+
44
+ Author.first.related_comments
45
+
46
+ What happens under the hood is that SomehowHasRelation recursively uses send() to look for what you have defined
47
+ with somehow_has. The length of the recursion is not limited to 3 models of course. Multiple somehow_has methods
48
+ can be defined over various models to achieve a chain of relations that ultimately handle all the recursions
49
+ and arrays and associations for you. At the end, you will always get an Array (flattened) containing the
50
+ related models.
51
+
52
+ You can also specify options such as:
53
+
54
+ somehow_has :one => :relation_name, :if => Proc.new{|model_instance| model_instance.created_at >= 1.day.ago}
55
+
56
+ somehow_has :many => :relation_name, :as => :use_this_method_name_instead_of_related_relation_name_method
57
+
58
+ == Testing
59
+
60
+ The documentation is very limited (this README), but the SomehowHasRelation was developed TDD style so feel free
61
+ to clone this repository and run the tests, or write some more :).
62
+
63
+ Inside the test directory, there are two dummy rails applications, one for rails2, the other for rails3.
64
+ The folder structure was taken and modified by josevalim's {enginex}[git://github.com/josevalim/enginex.git]
65
+ to support both rails2 and rails3 (see the About section on why).
66
+
67
+ To run the tests for a specific version of rails you can:
68
+
69
+ RAILS_VER=2 bundle install && rake test
70
+
71
+ or
72
+
73
+ RAILS_VER=3 bundle install && rake test
74
+
75
+ RAILS_VER defaults to 2, so, if you need to test the rails2 version, you can skip it.
76
+
77
+ == About
78
+
79
+ This gem was developed to support http://openwisp.caspur.it an OSS Wireless Internet Service Provider mainly made
80
+ with Ruby on Rails.
81
+ You can have a look at http://spider.caspur.it/projects/owm/repository to look at how SomehowHasRelation was used
82
+ on a complex rails application.
@@ -1,3 +1,3 @@
1
1
  module SomehowHasRelation
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -15,10 +15,12 @@ module SomehowHasRelation
15
15
  filter = params[:if]
16
16
  relation = params[:one] || params[:many]
17
17
  related = params[:as] || "#{prefix}_#{relation}"
18
+ to_flatten = params[:through] && params[:many]
18
19
 
19
20
  # Dynamic Instance Method related_%{relation_name}
20
21
  define_method(related) do
21
- params[:through] ? somehow_look_for(relation, params[:through]).flatten : send_and_filter(relation, filter)
22
+ somehow_got = params[:through] ? somehow_look_for(relation, params[:through]) : send_and_filter(relation, filter)
23
+ to_flatten ? somehow_got.flatten : somehow_got
22
24
  end
23
25
  end
24
26