has_options 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/has_options.gemspec +1 -1
- data/lib/has_options/has_options.rb +21 -3
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.1
|
data/has_options.gemspec
CHANGED
@@ -17,22 +17,40 @@ module HasOptionsHash
|
|
17
17
|
|
18
18
|
class_eval <<-EOV
|
19
19
|
define_method "find_#{singular_association}_value" do |name|
|
20
|
-
(opt = association_name.find_by_name(name.to_s)) ? opt.value : nil
|
20
|
+
(opt = #{association_name}.find_by_name(name.to_s)) ? opt.value : nil
|
21
21
|
end
|
22
22
|
|
23
23
|
define_method "find_#{singular_association}_values" do |name|
|
24
|
-
association_name.find_all_by_name(name.to_s).collect(&:value)
|
24
|
+
#{association_name}.find_all_by_name(name.to_s).collect(&:value)
|
25
25
|
end
|
26
26
|
|
27
27
|
define_method "#{association_name}_hash" do
|
28
28
|
option_hash = Hash.new {|h,k| h[k] = []}
|
29
29
|
|
30
|
-
association_name.each do |option|
|
30
|
+
#{association_name}.each do |option|
|
31
31
|
option_hash[k] << option.value.to_s
|
32
32
|
end
|
33
33
|
|
34
34
|
option_hash
|
35
35
|
end
|
36
|
+
|
37
|
+
define_method "set_#{singular_association}!" do |name, *values|
|
38
|
+
name = name.to_s
|
39
|
+
existing = #{association_name}.find_all_by_name(name)
|
40
|
+
new_opts = []
|
41
|
+
values.each do |value|
|
42
|
+
if (match = existing.detect { |opt| opt.value == value})
|
43
|
+
existing.delete(match)
|
44
|
+
new_opts <<match
|
45
|
+
else
|
46
|
+
new_opts << #{association_name}.create!(:name => name, :value => value)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
existing.each { |opt| opt.destroy }
|
51
|
+
|
52
|
+
return new_opts
|
53
|
+
end
|
36
54
|
EOV
|
37
55
|
|
38
56
|
end
|