associate_by 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -2,4 +2,5 @@ README.markdown
2
2
  Rakefile
3
3
  init.rb
4
4
  lib/associate_by.rb
5
+ test/associate_by_test.rb
5
6
  Manifest
@@ -14,13 +14,15 @@ Example:
14
14
 
15
15
  You could assign a Product to a Category using the product name
16
16
 
17
+ *WARNING: This gem only works with Rails 3. It might work with Rails 2, but I haven't tried it.*
18
+
17
19
  ## Instalation
18
20
 
19
21
  ### Rails 3
20
22
 
21
23
  Add the gem to your Gemfile
22
24
 
23
- gem 'associate_by'
25
+ gem 'associate_by', '>=0.1.1'
24
26
 
25
27
  Install using bundle
26
28
 
@@ -49,3 +51,6 @@ It will associate the Category with the Product so,
49
51
 
50
52
  c.products # => [p]
51
53
 
54
+ ## Todo
55
+
56
+ Finish this document
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('associate_by', '0.1.0') do |p|
5
+ Echoe.new('associate_by', '0.1.1') do |p|
6
6
  p.description = "Associate objects using a specific attribute"
7
7
  p.url = "http://github.com/crowdint/associate_by"
8
8
  p.author = "David Padilla"
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{associate_by}
5
- s.version = "0.1.0"
5
+ s.version = "0.1.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["David Padilla"]
@@ -10,13 +10,14 @@ Gem::Specification.new do |s|
10
10
  s.description = %q{Associate objects using a specific attribute}
11
11
  s.email = %q{david@crowdint.com}
12
12
  s.extra_rdoc_files = ["README.markdown", "lib/associate_by.rb"]
13
- s.files = ["README.markdown", "Rakefile", "init.rb", "lib/associate_by.rb", "Manifest", "associate_by.gemspec"]
13
+ s.files = ["README.markdown", "Rakefile", "init.rb", "lib/associate_by.rb", "test/associate_by_test.rb", "Manifest", "associate_by.gemspec"]
14
14
  s.homepage = %q{http://github.com/crowdint/associate_by}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Associate_by", "--main", "README.markdown"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{associate_by}
18
18
  s.rubygems_version = %q{1.3.7}
19
19
  s.summary = %q{Associate objects using a specific attribute}
20
+ s.test_files = ["test/associate_by_test.rb"]
20
21
 
21
22
  if s.respond_to? :specification_version then
22
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -22,8 +22,7 @@ module AssociateBy
22
22
  object = association.to_s.singularize.camelize.constantize.where(["#{method} = ?", self.send("#{association.to_s.singularize}_#{method}")]).first
23
23
 
24
24
  # If the object doesn't exist and the parameter create is true, then create it
25
- if object.nil? && self.send("#{association.to_s.singularize}_#{method}_create?")
26
- Rails.logger.info("AAAAAAAAAAAAAAAAAAAAAAAAAAAA")
25
+ if object.nil? && !(self.send("#{association.to_s.singularize}_#{method}").empty?) && self.send("#{association.to_s.singularize}_#{method}_create?")
27
26
  object = association.to_s.singularize.camelize.constantize.create({"#{method}" => self.send("#{association.to_s.singularize}_#{method}")})
28
27
  end
29
28
 
@@ -0,0 +1,113 @@
1
+ require 'test/unit'
2
+
3
+ require 'rubygems'
4
+ gem 'activerecord', '>=3.0.0.beta4'
5
+ require 'active_record'
6
+ require "#{File.dirname(__FILE__)}/../init"
7
+
8
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
9
+
10
+ def setup_db
11
+ ActiveRecord::Schema.define(:version => 1) do
12
+ create_table :parents do |t|
13
+ t.column :name, :string
14
+ end
15
+
16
+ create_table :children do |t|
17
+ t.column :parent_id, :integer
18
+ t.column :name, :string
19
+ end
20
+ end
21
+ end
22
+
23
+ def teardown_db
24
+ ActiveRecord::Base.connection.tables.each do |table|
25
+ ActiveRecord::Base.connection.drop_table(table)
26
+ end
27
+ end
28
+
29
+ class Category < ActiveRecord::Base
30
+ has_many :products
31
+ associate_by :products, :name
32
+
33
+ def self.table_name
34
+ "parents"
35
+ end
36
+ end
37
+
38
+ class Product < ActiveRecord::Base
39
+ associate_by :category, :name
40
+ belongs_to :category
41
+
42
+ def self.table_name
43
+ "children"
44
+ end
45
+ end
46
+
47
+ class Actor < ActiveRecord::Base
48
+ associate_by :movie, :name, :create => true
49
+ belongs_to :movie
50
+
51
+ def self.table_name
52
+ "children"
53
+ end
54
+ end
55
+
56
+ class Movie < ActiveRecord::Base
57
+ associate_by :actors, :name, :create => true
58
+ has_many :actors
59
+
60
+ def self.table_name
61
+ "parents"
62
+ end
63
+ end
64
+
65
+ class AssociateByTest < Test::Unit::TestCase
66
+ def setup
67
+ setup_db
68
+ @category = Category.create(:name => 'Category')
69
+ @product = Product.create(:name => 'Product')
70
+ @movie= Movie.create(:name => 'Movie')
71
+ @actor= Actor.create(:name => 'Actor')
72
+ end
73
+
74
+ def teardown
75
+ teardown_db
76
+ end
77
+
78
+ def test_has_many_association
79
+ @category.product_name = @product.name
80
+ @category.save
81
+ assert_equal(true, @category.products.include?(@product))
82
+ end
83
+
84
+ def test_belongs_to_association
85
+ @product.category_name = @category.name
86
+ @product.save
87
+ assert_equal(@category, @product.category)
88
+ end
89
+
90
+ def test_create_has_many_association
91
+ @movie.actor_name = 'A Different Actor'
92
+ @movie.save
93
+ assert_equal(1, Actor.where(:name => 'A Different Actor').count)
94
+ end
95
+
96
+ def test_create_belongs_to_association
97
+ @actor.movie_name = 'A Different Movie'
98
+ @actor.save
99
+ assert_equal(1, Movie.where(:name => 'A Different Movie').count)
100
+ end
101
+
102
+ def test_belongs_to_reader
103
+ @product.category = @category
104
+ @product.save
105
+ assert_equal(@category.name, @product.category_name)
106
+ end
107
+
108
+ def test_has_many_reader
109
+ @category.products << @product
110
+ assert_equal("", @category.product_name)
111
+ end
112
+ end
113
+
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: associate_by
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Padilla
@@ -33,6 +33,7 @@ files:
33
33
  - Rakefile
34
34
  - init.rb
35
35
  - lib/associate_by.rb
36
+ - test/associate_by_test.rb
36
37
  - Manifest
37
38
  - associate_by.gemspec
38
39
  has_rdoc: true
@@ -75,5 +76,5 @@ rubygems_version: 1.3.7
75
76
  signing_key:
76
77
  specification_version: 3
77
78
  summary: Associate objects using a specific attribute
78
- test_files: []
79
-
79
+ test_files:
80
+ - test/associate_by_test.rb