associator 0.0.1 → 0.1.1

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.
@@ -1,3 +1,3 @@
1
1
  module Associator
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/associator.rb CHANGED
@@ -8,23 +8,39 @@ class ActiveRecord::Base
8
8
  @@from_type = name.downcase
9
9
  if opts[:with]
10
10
  @@to_type = opts[:with].to_s.downcase
11
+ @@class_name = opts[:class_name] || false
12
+ @@table_name = @@to_type.pluralize if @@to_type
13
+ @@table_name = @@class_name.pluralize if @@class_name
14
+ @@table_name = opts[:table] if opts[:table]
15
+
11
16
  # http://stackoverflow.com/questions/8545360/finder-sql-does-not-parse-string-with-rails
12
- has_many @@to_type, :finder_sql => ->(record) do
13
- record = self if(record.nil?)
14
- "
15
- SELECT 'groups'.* FROM groups
16
- INNER JOIN 'associations' ON ('groups'.'id' = 'associations'.'to'
17
- AND 'associations'.'from' = #{record.id}
18
- AND 'associations'.'from_type' = '#{@@from_type}'
19
- AND 'associations'.'to_type' = '#{@@to_type}'
20
- )
21
- "
22
- end
17
+ if @@class_name
18
+ has_many @@to_type, :class_name => @@class_name, :finder_sql => ->(record) do
19
+ finder record
20
+ end
21
+ else
22
+ has_many @@to_type, :finder_sql => ->(record) do
23
+ finder record
24
+ end
25
+ end
23
26
  end
24
27
  end
25
28
 
26
29
  def add_associated obj
27
30
  Associator::Association.create(:from => id, :from_type => @@from_type, :to => obj.id, :to_type => @@to_type, :key => "#{id}_#{obj.id}")
31
+ #Association.create(:from => id, :from_type => @@from_type, :to => obj.id, :to_type => @@to_type, :key => "#{id}_#{obj.id}")
28
32
  end
29
33
 
34
+ private
35
+ def finder record
36
+ record = self if(record.nil?)
37
+ "
38
+ SELECT '#{@@table_name}'.* FROM '#{@@table_name}'
39
+ INNER JOIN 'associations' ON ('#{@@table_name}'.'id' = 'associations'.'to'
40
+ AND 'associations'.'from' = #{record.id}
41
+ AND 'associations'.'from_type' = '#{@@from_type}'
42
+ AND 'associations'.'to_type' = '#{@@to_type}'
43
+ )
44
+ "
45
+ end
30
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: associator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-16 00:00:00.000000000Z
12
+ date: 2012-02-17 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &13266980 !ruby/object:Gem::Requirement
16
+ requirement: &9784660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.1.3
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *13266980
24
+ version_requirements: *9784660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &13266380 !ruby/object:Gem::Requirement
27
+ requirement: &9776560 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *13266380
35
+ version_requirements: *9776560
36
36
  description: Associate any model to some model.
37
37
  email:
38
38
  - mabarroso@mabarroso.com
@@ -96,7 +96,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
96
96
  version: '0'
97
97
  segments:
98
98
  - 0
99
- hash: 3173667480573276137
99
+ hash: 1356832244272143988
100
100
  required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  none: false
102
102
  requirements:
@@ -105,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  version: '0'
106
106
  segments:
107
107
  - 0
108
- hash: 3173667480573276137
108
+ hash: 1356832244272143988
109
109
  requirements: []
110
110
  rubyforge_project:
111
111
  rubygems_version: 1.8.10