public
Description: A Ruby web application framework
Homepage: http://www.mackframework.com
Clone URL: git://github.com/markbates/mack.git
Click here to lend your support to: mack and make a donation at www.pledgie.com !
Actually made radio_button work right. :) [#19]
markbates (author)
Thu Aug 14 13:08:07 -0700 2008
commit  d83e931e8bd246ca664547f181fdc30c516d56c4
tree    f18a9b009e5ac199e8b98a59bf904ec1deb20cc6
parent  eec9f0b42187b957405790fc1780600de062f0c7
...
30
31
32
33
34
35
36
37
38
 
 
 
 
 
 
39
40
41
...
51
52
53
 
 
 
 
 
54
55
56
...
30
31
32
 
 
 
 
 
33
34
35
36
37
38
39
40
41
42
...
52
53
54
55
56
57
58
59
60
61
62
0
@@ -30,12 +30,13 @@ class Symbol
0
     Thread.current[:view_template].hidden_field(self, *args)
0
   end
0
   
0
-  # See Mack::ViewHelpers::FormHelpers image_submit for more information
0
-  def image_submit(*args)
0
-    Thread.current[:view_template].image_submit(self, *args)
0
-  end
0
-  
0
   # See Mack::ViewHelpers::FormHelpers label_tag for more information
0
+  # 
0
+  # Examples:
0
+  #   @user = User.new
0
+  #   <%= :user.label_tag :email %> # => <label for="user_email">Email</label>
0
+  #   <%= :i_dont_exist.label_tag %> # => <label for="i_dont_exist">I don't exist</label>
0
+  #   <%= :i_dont_exist.label_tag :value => "Hello" %> # => <label for="i_dont_exist">Hello</label>
0
   def label_tag(*args)
0
     Thread.current[:view_template].label(self, *args)
0
   end
0
@@ -51,6 +52,11 @@ class Symbol
0
   end
0
   
0
   # See Mack::ViewHelpers::FormHelpers radio_button for more information
0
+  # 
0
+  # Examples:
0
+  #   @user = User.new(:level => 1)
0
+  #   <%= :user.radio_button :level %> # => <input checked="checked" id="user_level" name="user[level]" type="radio" />
0
+  #   <%= :i_dont_exist.radio_button %> # => <input id="i_dont_exist" name="i_dont_exist" type="radio" />
0
   def radio_button(*args)
0
     Thread.current[:view_template].radio_button(self, *args)
0
   end
...
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
140
141
142
 
 
 
 
 
 
 
 
 
 
 
 
143
144
145
 
146
147
148
 
149
150
151
152
153
...
180
181
182
 
 
 
183
184
185
...
67
68
69
 
 
 
 
 
 
 
 
 
70
71
72
...
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
 
152
153
 
154
155
156
...
183
184
185
186
187
188
189
190
191
0
@@ -67,15 +67,6 @@ module Mack
0
         content_tag(:label, fe.options, content)
0
       end
0
 
0
-      def radio_button(name, *args)
0
-        build_form_element(name, {:type => :radio}, *args) do |var, fe, options|
0
-          if options[:value]
0
-            options.merge!(:checked => "checked")
0
-          end
0
-          options.delete(:value)
0
-        end
0
-      end
0
-
0
       def select(name, *args)
0
         var = instance_variable_get("@#{name}")
0
         fe = FormElement.new(*args)
0
@@ -140,14 +131,26 @@ module Mack
0
         build_form_element(name, {:type => :password}, *args)
0
       end
0
       
0
+      def radio_button(name, *args)
0
+        build_form_element(name, {:type => :radio, :value => ""}, *args) do |var, fe, options|
0
+          if fe.options[:value]
0
+            if fe.options[:value] == options[:value]
0
+              options.merge!(:checked => "checked")
0
+            end
0
+          elsif options[:value]
0
+            options.merge!(:checked => "checked")
0
+          end
0
+        end
0
+      end
0
+      
0
       private
0
       def build_form_element(name, options, *original_args)
0
         var = instance_variable_get("@#{name}")
0
+        fe = FormElement.new(*original_args)
0
         options = {:name => name, :id => name}.merge(options)
0
         if var.nil?
0
-          return non_content_tag(:input, options)
0
+          return non_content_tag(:input, options.merge(fe.options))
0
         else
0
-          fe = FormElement.new(*original_args)
0
           unless fe.calling_method == :to_s
0
             options.merge!(:name => "#{name}[#{fe.calling_method}]", :id => "#{name}_#{fe.calling_method}")
0
           end
0
@@ -180,6 +183,9 @@ module Mack
0
           else
0
             raise ArgumentError.new("You must provide either a Symbol, a String, a Hash, or a combination thereof.")
0
           end
0
+          if self.options[:checked]
0
+            self.options[:checked] = :checked
0
+          end
0
         end
0
 
0
       end
...
151
152
153
154
 
 
155
156
157
158
 
 
159
160
161
162
163
164
165
166
167
168
169
170
171
172
 
 
 
173
174
175
...
151
152
153
 
154
155
156
157
158
 
159
160
161
162
163
 
 
 
 
 
 
 
 
 
 
 
164
165
166
167
168
169
0
@@ -151,25 +151,19 @@ describe Mack::ViewHelpers::FormHelpers do
0
   describe "radio_button" do
0
     
0
     it "should create a nested radio_button for a model" do
0
-      radio_button(:cop, :tos).should == %{<input id="cop_tos" name="cop[tos]" type="radio" />}
0
+      radio_button(:cop, :level).should == %{<input checked="checked" id="cop_level" name="cop[level]" type="radio" value="1" />}
0
+      radio_button(:cop, :level, :value => "twoa").should == %{<input id="cop_level" name="cop[level]" type="radio" value="twoa" />}
0
     end
0
     
0
     it "should create a non-nested radio_button for a simple object" do
0
-      radio_button(:simple).should == %{<input checked="checked" id="simple" name="simple" type="radio" />}
0
+      radio_button(:simple).should == %{<input checked="checked" id="simple" name="simple" type="radio" value="hi" />}
0
+      radio_button(:simple, :value => "twob").should == %{<input id="simple" name="simple" type="radio" value="twob" />}
0
     end
0
     
0
     it "should create a non-nested radio_button for just a symbol" do
0
-      radio_button(:unknown).should == %{<input id="unknown" name="unknown" type="radio" />}
0
-    end
0
-    
0
-    it "should be checked if the value is true" do
0
-      @cop.tos = true
0
-      radio_button(:cop, :tos).should == %{<input checked="checked" id="cop_tos" name="cop[tos]" type="radio" />}
0
-    end
0
-    
0
-    it "should be unchecked if the value is false" do
0
-      @cop.tos = false
0
-      radio_button(:cop, :tos).should == %{<input id="cop_tos" name="cop[tos]" type="radio" />}
0
+      radio_button(:unknown).should == %{<input id="unknown" name="unknown" type="radio" value="" />}
0
+      radio_button(:unknown, :value => "twoc").should == %{<input id="unknown" name="unknown" type="radio" value="twoc" />}
0
+      radio_button(:unknown, :value => "twod", :checked => true).should == %{<input checked="checked" id="unknown" name="unknown" type="radio" value="twod" />}
0
     end
0
     
0
   end

Comments