acts-as-joinable 0.0.1.6 → 0.0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -67,15 +67,16 @@ If you would like to define accessors that scope the relationship based on the `
67
67
  If you need more fine-grained control over each relationship scope, you can use a block:
68
68
 
69
69
  class Post < ActiveRecord::Base
70
- acts_as_joinable_on :assets do
71
- has_one :featured_image, where(:...)
72
- has_many :thumbnails
73
- end
74
- acts_as_joinable_on :tags
70
+ acts_as_joinable_on :tags, :assets
75
71
  end
76
72
 
77
73
  @post = Post.new
78
74
  @post.featured_image = Image.first
79
75
  @post.thumbnails = Image.all
80
76
 
81
- The goal of this is to make it so you never have to create migrations or new classes, or rewrite the same code over and over again. Instead, you can just define `scopes` for cherry-picking the habtm items you'd like.
77
+ The goal of this is to make it so you never have to create migrations or new classes, or rewrite the same code over and over again. Instead, you can just define `scopes` for cherry-picking the habtm items you'd like.
78
+
79
+ Has 2 key properties on the join model that are pretty common:
80
+
81
+ - context: what the meaning of the join is
82
+ - position: what position the join is in relation to the context
data/Rakefile CHANGED
@@ -5,15 +5,15 @@ require 'rake/gempackagetask'
5
5
  spec = Gem::Specification.new do |s|
6
6
  s.name = "acts-as-joinable"
7
7
  s.authors = ["Lance Pollard"]
8
- s.version = "0.0.1.6"
8
+ s.version = "0.0.1.7"
9
9
  s.summary = "ActsAsJoinable: DRYing up Many-to-Many Relationships in ActiveRecord"
10
- s.homepage = "http://github.com/viatropos/cockpit"
10
+ s.homepage = "http://github.com/viatropos/acts-as-joinable"
11
11
  s.email = "lancejpollard@gmail.com"
12
12
  s.description = "DRYing up Many-to-Many Relationships in ActiveRecord"
13
13
  s.has_rdoc = false
14
14
  s.rubyforge_project = "acts-as-joinable"
15
15
  s.platform = Gem::Platform::RUBY
16
- s.files = %w(README.markdown Rakefile init.rb MIT-LICENSE) + Dir["{lib,rails,test}/**/*"] - Dir["test/tmp"]
16
+ s.files = %w(README.markdown Rakefile init.rb MIT-LICENSE) + Dir["{lib,rails,test,app}/**/*"] - Dir["test/tmp"]
17
17
  s.require_path = "lib"
18
18
  end
19
19
 
@@ -0,0 +1,3 @@
1
+ class ActsAsJoinable::Relationship < ActiveRecord::Base
2
+ acts_as_relationship
3
+ end
@@ -1,7 +1,15 @@
1
1
  module ActsAsJoinable
2
2
 
3
3
  def self.models
4
- @models ||= Dir[Rails.root + "/app/models/*.rb"].collect { |f| File.basename f, '.rb' }
4
+ unless @models
5
+ if defined?(Rails)
6
+ @models = Dir[Dir.pwd + "/app/models/*.rb"].collect { |f| File.basename f, '.rb' }
7
+ else
8
+ @models = []
9
+ end
10
+ end
11
+
12
+ @models
5
13
  end
6
14
 
7
15
  def self.models=(value)
@@ -26,8 +34,8 @@ module ActsAsJoinable
26
34
  class_inheritable_reader(:acts_as_joinable_config)
27
35
 
28
36
  class_eval do
29
- has_many :parent_relationships, :class_name => 'ActsAsJoinable::Relationship', :as => :child
30
- has_many :child_relationships, :class_name => 'ActsAsJoinable::Relationship', :as => :parent
37
+ has_many :parent_relationships, :class_name => 'ActsAsJoinable::Relationship', :as => :child, :dependent => :destroy
38
+ has_many :child_relationships, :class_name => 'ActsAsJoinable::Relationship', :as => :parent, :dependent => :destroy
31
39
 
32
40
  def self.joinable?
33
41
  true
@@ -56,4 +64,5 @@ end
56
64
 
57
65
  ActiveRecord::Base.send(:include, ActsAsJoinable) if defined?(ActiveRecord::Base)
58
66
 
67
+ Dir["#{File.dirname(__FILE__)}/../app/models/*"].each { |c| require c if File.extname(c) == ".rb" }
59
68
  Dir["#{File.dirname(__FILE__)}/acts_as_joinable/*"].each { |c| require c if File.extname(c) == ".rb" }
@@ -18,6 +18,8 @@ module ActsAsJoinable
18
18
  as = options[:as] || :parent
19
19
  end
20
20
 
21
+ class_name = options[:class_name] || nil
22
+
21
23
  contexts = options[:contexts] || []
22
24
  contexts = contexts.map(&:to_s).inject({}) {|hash, i| hash[i] = i.empty? ? i : "#{i}_"; hash}
23
25
 
@@ -43,8 +45,8 @@ module ActsAsJoinable
43
45
  :conditions => sql,
44
46
  :through => relationship_type,
45
47
  :source => role,
46
- :class_name => type.classify,
47
- :source_type => type.classify
48
+ :class_name => class_name ? class_name : type.classify,
49
+ :source_type => class_name ? class_name : type.classify
48
50
  }
49
51
 
50
52
  relationship_options = {
@@ -59,14 +61,13 @@ module ActsAsJoinable
59
61
  has_many type, joined_options
60
62
  has_many relationship_type, relationship_options
61
63
  end
62
-
64
+
63
65
  contexts.each do |context, context_prefix|
64
66
  context_type = "#{context_prefix}#{type}".to_sym
65
67
  relationship_type = "#{context_prefix}#{singular_type}_relationships".to_sym
66
68
  has_many relationship_type, relationship_options.merge(
67
69
  :conditions => ["#{ActsAsJoinable::Relationship.table_name}.context = ?", context.to_s]
68
70
  )
69
-
70
71
  has_many context_type, joined_options.merge(
71
72
  :through => relationship_type,
72
73
  :before_add => lambda do |parent, child|
@@ -123,6 +124,10 @@ module ActsAsJoinable
123
124
  relationships_for(role, type, context, :include => opposite_for(role)).map(&role)
124
125
  end
125
126
 
127
+ def join_for(role, type, context)
128
+ joins_for(role, type, context).first
129
+ end
130
+
126
131
  def set_joined(role, type, context, value)
127
132
  relationship = relationship_for(role, type, context, value) || ActsAsJoinable::Relationship.new
128
133
  clazz = get_join_class(type)
@@ -1,3 +1,3 @@
1
1
  require 'acts-as-joinable'
2
2
 
3
- ActiveRecord::Base.send :include, ActsAsJoinable
3
+ ActiveRecord::Base.send(:include, ActsAsJoinable)
@@ -32,6 +32,7 @@ ActiveRecord::Schema.define(:version => 1) do
32
32
  t.references :parent, :polymorphic => true
33
33
  t.references :child, :polymorphic => true
34
34
  t.string :context
35
+ t.integer :position
35
36
  t.timestamps
36
37
  end
37
38
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts-as-joinable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 71
4
+ hash: 69
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
9
  - 1
10
- - 6
11
- version: 0.0.1.6
10
+ - 7
11
+ version: 0.0.1.7
12
12
  platform: ruby
13
13
  authors:
14
14
  - Lance Pollard
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-06-20 00:00:00 -07:00
19
+ date: 2010-07-02 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies: []
22
22
 
@@ -43,8 +43,9 @@ files:
43
43
  - test/lib/tag.rb
44
44
  - test/test_acts_as_joinable.rb
45
45
  - test/test_helper.rb
46
+ - app/models/relationship.rb
46
47
  has_rdoc: true
47
- homepage: http://github.com/viatropos/cockpit
48
+ homepage: http://github.com/viatropos/acts-as-joinable
48
49
  licenses: []
49
50
 
50
51
  post_install_message: