associate_by 0.0.1 → 0.1.0

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/Manifest CHANGED
@@ -1,5 +1,5 @@
1
- Manifest
2
1
  README.markdown
3
2
  Rakefile
4
3
  init.rb
5
4
  lib/associate_by.rb
5
+ Manifest
data/README.markdown CHANGED
@@ -0,0 +1,51 @@
1
+ # associate_by
2
+
3
+ This gem helps you associate two objects using a specified field.
4
+
5
+ Example:
6
+
7
+ class Category < ActiveRecord::Base
8
+ has_many :products
9
+ end
10
+
11
+ class Product < ActiveRecord::Base
12
+ belongs_to :category
13
+ end
14
+
15
+ You could assign a Product to a Category using the product name
16
+
17
+ ## Instalation
18
+
19
+ ### Rails 3
20
+
21
+ Add the gem to your Gemfile
22
+
23
+ gem 'associate_by'
24
+
25
+ Install using bundle
26
+
27
+ bundle install
28
+
29
+ ## Usage
30
+
31
+ The parent class must have an association to the children class.
32
+
33
+ Just add to your parent class the following line:
34
+
35
+ associate_by :product, :name
36
+
37
+ This code will create two instance methods on the Category object, product_name and product_name=
38
+
39
+ If we had the example we mentioned earlier, we're telling the gem that we want to associate the
40
+ Product with the Category using the Product name.
41
+
42
+ Example:
43
+ c = Category.create(:name => "Parent")
44
+ p = Product.create(:name => "Child")
45
+
46
+ c.product_name = "Child"
47
+
48
+ It will associate the Category with the Product so,
49
+
50
+ c.products # => [p]
51
+
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.0.1') do |p|
5
+ Echoe.new('associate_by', '0.1.0') 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"
data/associate_by.gemspec CHANGED
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{associate_by}
5
- s.version = "0.0.1"
5
+ s.version = "0.1.0"
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"]
9
- s.date = %q{2010-07-13}
9
+ s.date = %q{2010-07-14}
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 = ["Manifest", "README.markdown", "Rakefile", "init.rb", "lib/associate_by.rb", "associate_by.gemspec"]
13
+ s.files = ["README.markdown", "Rakefile", "init.rb", "lib/associate_by.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"]
data/lib/associate_by.rb CHANGED
@@ -1,24 +1,47 @@
1
1
  module AssociateBy
2
+
2
3
  def self.included(base)
3
4
  base.extend(ClassMethods)
4
5
  end
5
6
 
6
7
  module ClassMethods
7
- def associate_by(association, method)
8
+ def associate_by(association, method, options = {})
9
+ attr_writer "#{association.to_s.singularize}_#{method}"
10
+
8
11
  define_method("#{association.to_s.singularize}_#{method}") do
9
- ""
12
+ begin
13
+ instance_variable_get("@#{association.to_s.singularize}_#{method}") || self.send("#{association.to_s}")[method]
14
+ rescue
15
+ ""
16
+ end
10
17
  end
18
+
19
+ define_method("#{association.to_s.singularize}_#{method}_create?", lambda { options[:create] || false} )
20
+
21
+ define_method("before_save_for_#{association.to_s.singularize}_#{method}") do
22
+ object = association.to_s.singularize.camelize.constantize.where(["#{method} = ?", self.send("#{association.to_s.singularize}_#{method}")]).first
23
+
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")
27
+ object = association.to_s.singularize.camelize.constantize.create({"#{method}" => self.send("#{association.to_s.singularize}_#{method}")})
28
+ end
11
29
 
12
- define_method("#{association.to_s.singularize}_#{method}=") do |value|
13
- object = association.to_s.singularize.camelize.constantize.where(["#{method} = ?", value]) unless value.nil?
14
30
  if object
15
- send(association) << object
31
+ if self.send("#{association.to_s}").is_a?(Array)
32
+ self.send("#{association.to_s}") << object
33
+ else
34
+ self.send("#{association.to_s.singularize}=", object)
35
+ end
16
36
  end
17
37
  end
38
+
39
+ before_save "before_save_for_#{association.to_s.singularize}_#{method}"
18
40
  end
19
41
  end
20
42
  end
21
43
 
44
+
22
45
  class ActiveRecord::Base
23
46
  include AssociateBy
24
47
  end
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: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - David Padilla
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-13 00:00:00 -05:00
18
+ date: 2010-07-14 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -29,11 +29,11 @@ extra_rdoc_files:
29
29
  - README.markdown
30
30
  - lib/associate_by.rb
31
31
  files:
32
- - Manifest
33
32
  - README.markdown
34
33
  - Rakefile
35
34
  - init.rb
36
35
  - lib/associate_by.rb
36
+ - Manifest
37
37
  - associate_by.gemspec
38
38
  has_rdoc: true
39
39
  homepage: http://github.com/crowdint/associate_by