anaf_habtm 0.0.8 → 0.0.81
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/anaf_habtm.gemspec +1 -1
- data/lib/anaf_active_record.rb +39 -39
- data/lib/application_helper_methods.rb +18 -16
- data/lib/hash.rb +9 -6
- metadata +3 -3
data/Rakefile
CHANGED
data/anaf_habtm.gemspec
CHANGED
data/lib/anaf_active_record.rb
CHANGED
@@ -1,44 +1,44 @@
|
|
1
1
|
|
2
|
-
module AnafHabtm
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
coll
|
22
|
-
|
23
|
-
|
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
|
2
|
-
|
3
|
-
|
4
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
18
|
-
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
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:
|
4
|
+
hash: 189
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 81
|
10
|
+
version: 0.0.81
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tyler Gannon
|