public
Description: Making assigning values to Active Record associations easier
Clone URL: git://github.com/bwyrosdick/association-attributes.git
works for both has_many_attr and has_one_attr
bwyrosdick (author)
Thu Mar 20 05:39:58 -0700 2008
commit  aa20d0fa81856be414aeced0700c974c3533907b
tree    40c9c23dec46716fd81262e67317a00b2a93b364
parent  eff1c0ff3824a6330879d24ba864bb37a600be81
0
...
1
2
3
4
 
5
6
7
8
9
10
 
 
 
 
 
11
12
13
...
1
2
3
 
4
5
6
7
8
9
 
10
11
12
13
14
15
16
17
0
@@ -1,13 +1,17 @@
0
 Association Attributes
0
 =======
0
 
0
-Introduction goes here.
0
+creates an attribute method that allows to you to set associations with a param hash of that object. Made for use with fields_for.
0
 
0
 
0
 Example
0
 =======
0
 
0
-Example goes here.
0
+has_one :address, :as => :addressable, :dependent => :destroy
0
+has_one_attr :address
0
+
0
+has_many :phone_numbers, :as => :phoneable, :dependent => :destroy, :order => 'name'
0
+has_many_attr :phone_numbers
0
 
0
 
0
 Copyright (c) 2008 CommonThread, released under the MIT license
...
9
10
11
 
 
 
 
 
 
12
13
14
...
20
21
22
23
 
24
25
26
...
28
29
30
31
 
 
 
 
 
 
 
32
33
34
35
36
37
38
39
40
41
42
43
44
 
 
45
46
47
...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
...
9
10
11
12
13
14
15
16
17
18
19
20
...
26
27
28
 
29
30
31
32
...
34
35
36
 
37
38
39
40
41
42
43
44
45
 
 
 
 
46
 
 
 
 
 
 
47
48
49
50
51
...
53
54
55
 
 
 
 
 
 
 
 
 
 
56
57
0
@@ -9,6 +9,12 @@ module CommonThread
0
       def has_many_attr(association_name, options = {})
0
         eval "after_create :update_#{association_name}_attributes"
0
         
0
+ unless_condition = 'false'
0
+ if options[:check_blank]
0
+ blank_fields = options[:check_blank].is_a?(Array) ? options[:check_blank] : [options[:check_blank]]
0
+ unless_condition = blank_fields.map{|field| "(assoc_attr['#{field}'].blank? and assoc_attr[:#{field}].blank?)"}.join ' or '
0
+ end
0
+
0
         class_eval %{
0
           def update_#{association_name}_attributes
0
             self.#{association_name}_attributes = @#{association_name}_attributes if @#{association_name}_attributes
0
@@ -20,7 +26,7 @@ module CommonThread
0
             else
0
               for assoc_attr in attributes
0
                 #{association_name} = assoc_attr['id'].blank? ? self.#{association_name}.new : self.#{association_name}.find(assoc_attr['id'])
0
- #{association_name}.update_attributes(assoc_attr) unless #{build_unless_condition options[:check_blank]}
0
+ #{association_name}.update_attributes(assoc_attr) unless #{unless_condition}
0
               end
0
             end
0
           end
0
@@ -28,20 +34,18 @@ module CommonThread
0
       end
0
       
0
       def has_one_attr(association_name, options = {})
0
- eval "after_create :update_#{association_name}_attributes"
0
+ # eval "after_create :update_#{association_name}_attributes"
0
+
0
+ unless_condition = 'false'
0
+ if options[:check_blank]
0
+ blank_fields = options[:check_blank].is_a?(Array) ? options[:check_blank] : [options[:check_blank]]
0
+ unless_condition = blank_fields.map{|field| "(assoc_attr['#{field}'].blank? and assoc_attr[:#{field}].blank?)"}.join ' or '
0
+ end
0
         
0
         class_eval %{
0
- def update_#{association_name}_attributes
0
- self.#{association_name}_attributes = @#{association_name}_attributes if @#{association_name}_attributes
0
- end
0
-
0
           def #{association_name}_attributes=(attributes)
0
- if self.new_record?
0
- @#{association_name}_attributes = attributes
0
- else
0
- #{association_name} = self.#{association_name} || self.build_#{association_name}
0
- #{association_name}.update_attributes(assoc_attr) unless #{build_unless_condition options[:check_blank]}
0
- end
0
+ #{association_name} = self.#{association_name} || self.build_#{association_name}
0
+ #{association_name}.update_attributes(attributes) unless #{unless_condition}
0
           end
0
         }
0
       end
0
@@ -49,15 +53,5 @@ module CommonThread
0
         
0
     module HelperMethods
0
     end
0
-
0
- private
0
- def build_unless_condition(blank_fields)
0
- if blank_fields
0
- blank_fields = [blank_fields] unless blank_fields.is_a?(Array)
0
- blank_fields.map{|field| "(assoc_attr['#{field}'].blank? and assoc_attr[:#{field}].blank?)"}.join ' or '
0
- else
0
- "false"
0
- end
0
- end
0
   end
0
 end

Comments

    No one has commented yet.