anaf_habtm 0.0.8 → 0.0.81

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/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rake'
2
2
  require 'rake/testtask'
3
3
  require 'rake/rdoctask'
4
4
 
5
- VERSION = "0.0.8"
5
+ VERSION = "0.0.81"
6
6
 
7
7
  desc 'Default: run unit tests.'
8
8
  task :default => :test
data/anaf_habtm.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{anaf_habtm}
8
- s.version = "0.0.8"
8
+ s.version = "0.0.81"
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"]
@@ -1,44 +1,44 @@
1
1
 
2
- module AnafHabtm::ActiveRecord
3
- def anaf_habtm(association, options={}, &block)
4
- class_eval do
5
- # Define a proc that will look up the (potentially) existing object
6
- finder = proc {|id| association.to_s.singularize.camelize.constantize.where(:id=>id).first
7
- }
8
-
9
- # Define a proc that will set the association collection
10
- set_collection = proc {|me, coll| me.send("#{association.to_s.tableize}=", coll)}
11
- has_and_belongs_to_many association.to_sym, options
12
- # Define the actual association setter.
13
- define_method "#{association.to_s.tableize}_attributes=", lambda{|attributes_for_association|
14
- coll = []
2
+ module AnafHabtm
3
+ module ActiveRecord
4
+ def anaf_habtm(association, options={}, &block)
5
+ class_eval do
6
+ # Define a proc that will look up the (potentially) existing object
7
+ finder = proc {|id| association.to_s.singularize.camelize.constantize.where(:id=>id).first
8
+ }
15
9
 
16
- attributes_for_association.each_value do |params|
17
- next if params["_destroy"] == "1"
18
- obj = finder.call(params["id"]) if params.has_key?("id")
19
- params.extend(AnafHabtm::HashExtension)
20
- # ActiveRecord::Base.attributes=() doesn't like extra parameters.
21
- coll << block.call(params.copy_without_destroy, obj)
22
- end
23
- set_collection.call(self, coll)
24
- }
10
+ # Define a proc that will set the association collection
11
+ set_collection = proc {|me, coll| me.send("#{association.to_s.tableize}=", coll)}
12
+ has_and_belongs_to_many association.to_sym, options
13
+ # Define the actual association setter.
14
+ define_method "#{association.to_s.tableize}_attributes=", lambda{|attributes_for_association|
15
+ coll = []
16
+
17
+ attributes_for_association.each_value do |params|
18
+ next if params["_destroy"] == "1"
19
+ obj = finder.call(params["id"]) if params.has_key?("id")
20
+ params.extend(AnafHabtm::HashExtension)
21
+ # ActiveRecord::Base.attributes=() doesn't like extra parameters.
22
+ coll << block.call(params.copy_without_destroy, obj)
23
+ end
24
+ set_collection.call(self, coll)
25
+ }
26
+ end
25
27
  end
26
- end
27
-
28
- def named_association(member, klass, attribute, create=nil)
29
- member = member.to_s
30
- klass = klass.name
31
- attribute = attribute.to_s
32
- if create
33
- class_eval "def #{member}_#{attribute}=(#{attribute});
34
- return if #{attribute}.blank?
35
- self.#{member} = #{klass}.find_or_create_by_#{attribute}(#{attribute})
36
- end;"
37
- else
38
- class_eval "def #{member}_#{attribute}=(#{attribute}); self.#{member} = #{klass}.find_by_#{attribute}(#{attribute}) unless #{attribute}.blank?; end;"
39
- end
40
- class_eval "def #{member}_#{attribute}; #{member}.#{attribute} if #{member}; end;"
41
- end
42
28
 
43
-
29
+ def named_association(member, klass, attribute, create=nil)
30
+ member = member.to_s
31
+ klass = klass.name
32
+ attribute = attribute.to_s
33
+ if create
34
+ class_eval "def #{member}_#{attribute}=(#{attribute});
35
+ return if #{attribute}.blank?
36
+ self.#{member} = #{klass}.find_or_create_by_#{attribute}(#{attribute})
37
+ end;"
38
+ else
39
+ class_eval "def #{member}_#{attribute}=(#{attribute}); self.#{member} = #{klass}.find_by_#{attribute}(#{attribute}) unless #{attribute}.blank?; end;"
40
+ end
41
+ class_eval "def #{member}_#{attribute}; #{member}.#{attribute} if #{member}; end;"
42
+ end
43
+ end
44
44
  end
@@ -1,20 +1,22 @@
1
- module AnafHabtm::ApplicationHelperMethods
2
- def remove_child_link(name, form_builder)
3
- form_builder.hidden_field(:_destroy) + link_to_function(name, "remove_child(this)", :tabindex => "0")
4
- end
5
-
6
- def add_child_link(name, child, form_builder)
7
- # puts "||#{form_builder}||"
8
- fields = escape_javascript(new_child_fields(child, form_builder))
9
- link_to_function(name, h("add_child(this, \"#{child}\", \"#{fields}\")"))
10
- end
1
+ module AnafHabtm
2
+ module ApplicationHelperMethods
3
+ def remove_child_link(name, form_builder)
4
+ form_builder.hidden_field(:_destroy) + link_to_function(name, "remove_child(this)", :tabindex => "0")
5
+ end
11
6
 
12
- def new_child_fields(child, form_builder)
13
- output = ""
14
- form_builder.fields_for(child.pluralize.to_sym, child.camelize.constantize.new, :child_index => 'NEW_RECORD') do |f|
15
- output += render(:partial => child.underscore, :locals => { :f => f })
7
+ def add_child_link(name, child, form_builder)
8
+ # puts "||#{form_builder}||"
9
+ fields = escape_javascript(new_child_fields(child, form_builder))
10
+ link_to_function(name, h("add_child(this, \"#{child}\", \"#{fields}\")"))
16
11
  end
17
- output
18
- end
12
+
13
+ def new_child_fields(child, form_builder)
14
+ output = ""
15
+ form_builder.fields_for(child.pluralize.to_sym, child.camelize.constantize.new, :child_index => 'NEW_RECORD') do |f|
16
+ output += render(:partial => child.underscore, :locals => { :f => f })
17
+ end
18
+ output
19
+ end
20
+ end
19
21
  end
20
22
 
data/lib/hash.rb CHANGED
@@ -1,9 +1,12 @@
1
- module AnafHabtm::HashExtension
2
- KEYS_TO_AVOID = ["_destroy", "id"]
3
- def copy_without_destroy
4
- a = {}
5
- self.each {|key,val| a[key]=val unless KEYS_TO_AVOID.include?(key.to_s)}
6
- a
1
+
2
+ module AnafHabtm
3
+ module HashExtension
4
+ KEYS_TO_AVOID = ["_destroy", "id"]
5
+ def copy_without_destroy
6
+ a = {}
7
+ self.each {|key,val| a[key]=val unless KEYS_TO_AVOID.include?(key.to_s)}
8
+ a
9
+ end
7
10
  end
8
11
  end
9
12
 
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: 15
4
+ hash: 189
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 8
10
- version: 0.0.8
9
+ - 81
10
+ version: 0.0.81
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tyler Gannon