anaf_habtm 0.0.89 → 0.0.92

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  gem 'rails', '3.0.0.beta4'
2
2
  gem 'sqlite3-ruby', :require => 'sqlite3'
3
3
  gem 'jeweler'
4
+ gem "da_huangs_ruby_extensions"
4
5
 
data/Rakefile CHANGED
@@ -1,32 +1,8 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'rake/rdoctask'
4
-
5
- VERSION = "0.0.85"
6
-
7
- desc 'Default: run unit tests.'
8
- task :default => :test
9
-
10
- desc 'Test the anaf_habtm plugin.'
11
- Rake::TestTask.new(:test) do |t|
12
- t.libs << 'lib'
13
- t.libs << 'test'
14
- t.pattern = 'test/**/*_test.rb'
15
- t.verbose = true
16
- end
17
1
 
18
- desc 'Generate documentation for the anaf_habtm plugin.'
19
- Rake::RDocTask.new(:rdoc) do |rdoc|
20
- rdoc.rdoc_dir = 'rdoc'
21
- rdoc.title = 'AnafHabtm'
22
- rdoc.options << '--line-numbers' << '--inline-source'
23
- rdoc.rdoc_files.include('README')
24
- rdoc.rdoc_files.include('lib/**/*.rb')
25
- end
26
2
  PKG_FILES = FileList[
27
- 'anaf_habtm.gemspec',
3
+ 'anaf_habtm.gemspec',
28
4
  'Gemfile',
29
- 'install.rb',
5
+ 'install.rb',
30
6
  'Rakefile', 'README', 'uninstall.rb',
31
7
  'assets/**/*',
32
8
  'lib/**/*',
@@ -34,11 +10,13 @@ PKG_FILES = FileList[
34
10
  'test/**/*'
35
11
  ]
36
12
 
13
+ require 'rubygems'
14
+ require 'rake'
15
+
37
16
  begin
38
17
  require 'jeweler'
39
18
  Jeweler::Tasks.new do |s|
40
19
  s.name = "anaf_habtm"
41
- s.version = VERSION
42
20
  s.author = "Tyler Gannon"
43
21
  s.email = "t--g__a--nnon@gmail.com"
44
22
  s.homepage = "http://github.com/tylergannon/anaf_habtm"
@@ -49,11 +27,44 @@ begin
49
27
  s.require_path = "lib"
50
28
  s.has_rdoc = false
51
29
  s.extra_rdoc_files = ["README"]
30
+ end
52
31
 
32
+ Jeweler::GemcutterTasks.new
33
+ rescue LoadError
34
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
35
+ end
36
+
37
+ require 'rake/testtask'
38
+ Rake::TestTask.new(:test) do |test|
39
+ test.libs << 'lib' << 'test'
40
+ test.pattern = 'test/**/test_*.rb'
41
+ test.verbose = true
42
+ end
43
+
44
+ begin
45
+ require 'rcov/rcovtask'
46
+ Rcov::RcovTask.new do |test|
47
+ test.libs << 'test'
48
+ test.pattern = 'test/**/test_*.rb'
49
+ test.verbose = true
53
50
  end
54
51
  rescue LoadError
55
- puts "Jeweler not available. Install it with: gem install jeweler"
52
+ task :rcov do
53
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
54
+ end
56
55
  end
57
56
 
57
+ task :test => :check_dependencies
58
58
 
59
+ task :default => :test
60
+
61
+ require 'rake/rdoctask'
62
+ Rake::RDocTask.new do |rdoc|
63
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
64
+
65
+ rdoc.rdoc_dir = 'rdoc'
66
+ rdoc.title = "da_huangs_ruby_extensions #{version}"
67
+ rdoc.rdoc_files.include('README*')
68
+ rdoc.rdoc_files.include('lib/**/*.rb')
69
+ end
59
70
 
data/anaf_habtm.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{anaf_habtm}
8
- s.version = "0.0.89"
8
+ s.version = "0.0.92"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tyler Gannon"]
12
- s.date = %q{2010-07-13}
12
+ s.date = %q{2010-07-15}
13
13
  s.description = %q{accepts_nested_attributes_for habtm}
14
14
  s.email = %q{t--g__a--nnon@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -40,7 +40,7 @@ Gem::Specification.new do |s|
40
40
  s.rubygems_version = %q{1.3.7}
41
41
  s.summary = %q{accepts_nested_attributes_for habtm}
42
42
  s.test_files = [
43
-
43
+ "test/anaf_habtm_test.rb",
44
44
  "test/test_helper.rb"
45
45
  ]
46
46
 
@@ -10,55 +10,43 @@ module AnafHabtm
10
10
  end
11
11
  end
12
12
 
13
- # options = {:named=>"name",
14
- # :ar_options=>{:autosave=>true}}
15
- def anaf_habtm(association, options={}, &block)
16
- raise "Must specify :find or a code block." if (block.nil? && !options.has_key?(:find) && !options[:create])
17
- class_eval do
18
- # Define a proc that will look up the (potentially) existing object
19
- finder = proc {|id| association.to_s.singularize.camelize.constantize.where(:id=>id).first
20
- }
21
-
22
- # Define a proc that will set the association collection
23
- set_collection = proc {|me, coll| me.send("#{association.to_s.tableize}=", coll)}
24
- has_and_belongs_to_many association.to_sym, options[:ar_options]||={}
25
- # Define the actual association setter.
26
- define_method "#{association.to_s.tableize}_attributes=", lambda{|attributes_for_association|
27
- coll = []
28
- klass = association.to_s.singularize.camelize.constantize
29
- attributes_for_association.each_value do |params|
30
- next if params["_destroy"] == "1"
31
- obj = finder.call(params["id"]) if params.has_key?("id")
32
- params.extend(AnafHabtm::HashExtension)
33
- params_copy = params.copy_without_destroy
34
- # ActiveRecord::Base.attributes=() doesn't like extra parameters.
35
- if block.nil?
36
- unless obj
37
- srch = klass
38
- options[:find].each do |scope, name|
39
- if name.class==Hash
40
- srch = klass.send(scope, params[name.first[0].to_s][name.first[1].to_s])
41
- else
42
- srch = klass.send(scope, params[name.to_s])
43
- end
44
- obj = srch.first
45
- end
46
- obj = obj ||= klass.new if options[:create]
13
+ # Needs to call scopes on a class and pass it values that are
14
+ # pulled from a hash called obj_attr
15
+ def make_scope_string(klass, find_opts)
16
+ _scopes = []
17
+ find_opts.each{ |scope, attr_name|
18
+ if attr_name.class == Hash
19
+ _scopes << "#{scope.to_s}(obj_attr[#{name.first[0].to_s}][#{name.first[1].to_s}])"
20
+ else
21
+ _scopes << "#{scope.to_s}(obj_attr[#{name.to_s}])"
22
+ end
23
+ }
24
+ _scopes.join(".")
25
+ end
47
26
 
48
- unless obj
49
- raise "Couldn't find or create #{association} named #{name}."
50
- end
27
+ def anaf_habtm(association, opts={})
28
+ raise "Must specify :find " unless options.has_key?(:find)
29
+ klass = association.to_s.singularize.camelize
30
+ association = association.to_s.underscore.tableize
31
+ scope_string = make_scope_string(association, find_opts[:find])
32
+ class_eval <<END
33
+ def #{association}_attributes=(attr_hash)
34
+ obj_coll = []
35
+ attr_hash.each_value do |obj_attr|
36
+ next if obj_attr["_destroy"] == "1"
37
+ obj = #{klass}.find(obj_attr["id"]) if obj_attr.has_key?("id")
38
+ obj_attr.extend(AnafHabtm::HashExtension)
39
+ obj_attr = obj_attr.copy_without_destroy
40
+ obj = #{klass}.#{scope_string}.first unless obj
41
+ if obj && obj.update_attributes(obj_attr)
42
+ obj_coll << obj
43
+ elsif (obj = #{klass}.new(obj_attributes)) && obj.save
44
+ obj_coll << obj
51
45
  end
52
- obj.attributes = params_copy
53
- obj.save
54
- coll << obj
55
- else
56
- coll << block.call(params_copy, obj)
57
46
  end
47
+ self.#{association} = obj_coll
58
48
  end
59
- set_collection.call(self, coll)
60
- }
61
- end
49
+ END
62
50
  end
63
51
 
64
52
  def association_text(association, opts={})
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: anaf_habtm
3
3
  version: !ruby/object:Gem::Version
4
- hash: 173
4
+ hash: 167
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 89
10
- version: 0.0.89
9
+ - 92
10
+ version: 0.0.92
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tyler Gannon
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-07-13 00:00:00 -07:00
18
+ date: 2010-07-15 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -80,4 +80,5 @@ signing_key:
80
80
  specification_version: 3
81
81
  summary: accepts_nested_attributes_for habtm
82
82
  test_files:
83
+ - test/anaf_habtm_test.rb
83
84
  - test/test_helper.rb