has-relationship 0.0.5 → 0.0.6

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 CHANGED
@@ -0,0 +1,131 @@
1
+ = HasRelationship
2
+
3
+ This gem was based on insight provided by the article http://blog.hasmanythrough.com/2006/4/3/polymorphic-through.
4
+ It was developed because of a lack of solutions to be able to handle double polymorphism in rails. In other words,
5
+ there was no way to create an xref table (the middle table in a many-to-many relationship) that joined two unknown
6
+ tables together. There are obvious reasons for these limitations in rails, but nevertheless, a solution was needed.
7
+
8
+ HasRelationship hopefully is that solution. The user runs a migration that generates the "relationships" table. That
9
+ table is then used to join any two tables together and create a relationship between them. So now, when you want to
10
+ add a relationship between class 1 and class 2, you won't have to generate your own custom intermediate xref table
11
+ each time; the "relationships" table can be reused for each of your custom relationships.
12
+
13
+ == Installation
14
+
15
+ === Rails 3.0
16
+
17
+ Add the following to your Gemfile:
18
+
19
+ gem 'has-relationship'
20
+
21
+ ==== Post Installation
22
+
23
+ 1. rails generate has_relationship:migration
24
+ 2. rake db:migrate
25
+
26
+ == Usage
27
+
28
+ class User < ActiveRecord::Base
29
+ # Create a has-one-through relationship with the "tasks" table (Task class) through the "relationships" table
30
+ has_relationship :task
31
+
32
+ # Alternate approach to create a has-one-through relationship with the "tasks" table (Task class) through the "relationships" table
33
+ has_relationship "task"
34
+
35
+ # Alternate approach to create a has-one-through relationship with the "tasks" table (Task class) through the "relationships" table
36
+ has_relationship Task
37
+
38
+ # Alternate approach to create a has-one-through relationship with the "tasks" table (Task class) through the "relationships" table
39
+ has_relationship :tasks, :singular => true
40
+
41
+
42
+
43
+ # Create a has-many-through relationship with the "tasks" table (Task class) through the "relationships" table
44
+ has_relationship :tasks
45
+
46
+ # Create a has-many-through relationship called "other_tasks" with the "tasks" table (Task class) through the "relationships" table.
47
+ # Please note the addition of the :relationship attribute. This is optional but highly recommended, particularly when you're going
48
+ # to be declaring a "has_inverse_relationship" on the Task class, as shown below. This parameter sets the "relationship" field in
49
+ # the relationship table.
50
+ # If :relationship had not been set here, it still would have defaulted to "User_OtherTask"; the benefit however of manually declaring this
51
+ # is that it clear exactly what to name the relationship in your use of "has_inverse_relationship".
52
+ has_relationship :other_tasks, :class_name => "Task", :relationship => "User_OtherTask"
53
+
54
+ # Alternate approach to create a has-many-through relationship called "other_tasks" with the "tasks" table (Task class) through the "relationships" table
55
+ has_relationship :tasks, :as => :other_tasks, :relationship => "User_OtherTask"
56
+ end
57
+
58
+ class Task < ActiveRecord::Base
59
+ # Create a has-one-through relationship with the "users" table (User class) through the "relationships" table
60
+ has_inverse_relationship :user
61
+
62
+ # Alternate approach to create a has-one-through relationship with the "users" table (User class) through the "relationships" table
63
+ has_inverse_relationship "user"
64
+
65
+ # Alternate approach to create a has-one-through relationship with the "users" table (User class) through the "relationships" table
66
+ has_inverse_relationship User
67
+
68
+ # Alternate approach to create a has-one-through relationship with the "user" table (User class) through the "relationships" table
69
+ has_inverse_relationship :users, :singular => true
70
+
71
+
72
+
73
+ # Create a has-many-through relationship with the "users" table (User class) through the "relationships" table
74
+ has_inverse_relationship :users
75
+
76
+ # Create a has-many-through relationship called "other_users" with the "users" table (User class) through the "relationships" table
77
+ # Please note that here we specify the exact same :relationship as was used in the User class in its call to "has_relationship".
78
+ has_inverse_relationship :other_users, :class_name => "User", :relationship => "User_OtherTask"
79
+
80
+ # Alternate approach to create a has-many-through relationship called "other_users" with the "users" table (User class) through the "relationships" table
81
+ has_inverse_relationship :users, :as => :other_users, :relationship => "User_OtherTask"
82
+
83
+
84
+
85
+ # Create a has-one-through relationship with the "assignments" table (Assignment class) through the "relationships" table
86
+ has_inverse_relationship :assignment
87
+
88
+ # Create a has-many-through relationship with the "stories" table (Story Class) through the "relationships" table
89
+ has_relationship :stories
90
+ end
91
+
92
+ class Assignment < ActiveRecord::Base
93
+ # Create a has-many-through relationship with the "tasks" table (Task class) through the "relationships" table,
94
+ # and create another has-many-through relationship with the "stories" table (Story class) through the "relationships" table
95
+ has_relationship [:tasks, :stories]
96
+
97
+ # Create a has-many-through relationship called "other_tasks" with the "tasks" table (Task class) through the "relationships" table
98
+ has_relationship :tasks, :as => :other_tasks
99
+ end
100
+
101
+ class Story < ActiveRecord::Base
102
+ # Create a has-one-through relationship called "parent" with the "tasks" table (Task class) through the "relationships" table.
103
+ has_inverse_relationship :tasks, :as => :parent, :relationship => "Task_Story"
104
+
105
+ # Create a has-one-through relationship with the "tasks" table (Task class) through the "relationships" table.
106
+ has_inverse_relationship :task
107
+ end
108
+
109
+ @user = User.new(:name => "Bobby")
110
+ @user.task = Task.new
111
+ @user.tasks.build
112
+ @user.other_tasks << Task.new
113
+ @user.save
114
+
115
+ @assignment = Assignment.new
116
+ @assignment.tasks << Task.create
117
+ @assignment.stories.build
118
+ @assignment.other_tasks << Task.new
119
+ @assignment.save
120
+
121
+ @story = Story.new
122
+ @story.parent = Task.create
123
+ @story.task = Task.new
124
+ @story.save
125
+
126
+ Copyright (c) 2011 Andrew Hunter (http://github.com/hunterae) and Captico LLC. (http://captico.com/), released under the MIT license
127
+
128
+ == Special Thanks
129
+
130
+ HasRelationship was aided by some of the insight provided in the article http://blog.hasmanythrough.com/2006/4/3/polymorphic-through written by Josh Susser. Also many thanks to the ActsAsTaggableOn team[https://github.com/mbleigh/acts-as-taggable-on], as I have used your gem as a jumping point for writing my own, given the similar nature of the task at hand.
131
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -4,7 +4,7 @@ module HasRelationship
4
4
  class MigrationGenerator < Rails::Generators::Base
5
5
  include Rails::Generators::Migration
6
6
 
7
- desc "Generates migration for Tag and Tagging models"
7
+ desc "Generates migration for Relationship models"
8
8
 
9
9
  def self.orm
10
10
  Rails::Generators.options[:rails][:orm]
@@ -45,7 +45,7 @@ module HasRelationship
45
45
  has_one resource_name, :through => relationship_through, :class_name => class_name, :source => "relation2_#{class_name.downcase}".to_s, :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
46
46
  else
47
47
  has_many relationship_through, :as => :relation1, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
48
- has_many resource_name, :through => relationship_through, :class_name => class_name, :source => "relation2_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
48
+ has_many resource_name, :through => relationship_through, :readonly => false, :class_name => class_name, :source => "relation2_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation2_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
49
49
  end
50
50
  end
51
51
  end
@@ -95,7 +95,7 @@ module HasRelationship
95
95
  has_one resource_name, :through => relationship_through, :class_name => class_name, :source => "relation1_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation1_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
96
96
  else
97
97
  has_many relationship_through, :as => :relation2, :dependent => :destroy, :class_name => "HasRelationship::Relationship", :conditions => {:relationship => relationship}
98
- has_many resource_name, :through => relationship_through, :class_name => class_name, :source => "relation1_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation1_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
98
+ has_many resource_name, :through => relationship_through, :readonly => false, :class_name => class_name, :source => "relation1_#{class_name.downcase}".to_sym, :conditions => ["relationships.relation1_type = ? and relationships.relationship in (?)", class_name, relationships], :select => options[:select]
99
99
  end
100
100
  end
101
101
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has-relationship
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Andrew Hunter
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-23 00:00:00 -04:00
18
+ date: 2011-04-27 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies: []
21
21