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 !
Almost finished adding html form helpers. [#19]
markbates (author)
Thu Aug 14 11:59:41 -0700 2008
commit  620bd22da9f75b782db693879b8b654bb4587ab2
tree    56023b70e25fc607f6a627e2b28b6ccf61f76648
parent  25aed1978f46fb7a93ba6695563aa9ae4103c20d
...
25
26
27
28
29
30
31
32
 
 
 
 
33
...
25
26
27
 
 
 
 
28
29
30
31
32
33
0
@@ -25,7 +25,7 @@ Mack::ControllerHelpers.constants.each do |cont|
0
 end
0
 
0
 # Find view level Helpers and include them into the Mack::Rendering::ViewTemplate
0
-# Mack::ViewHelpers.constants.each do |cont|
0
-#   h = "Mack::ViewHelpers::#{cont}".constantize
0
-#   h.include_safely_into(Mack::Rendering::ViewTemplate)
0
-# end
0
\ No newline at end of file
0
+Mack::ViewHelpers.constants.each do |cont|
0
+  h = "Mack::ViewHelpers::#{cont}".constantize
0
+  Mack::Rendering::ViewTemplate.send(:include, h)
0
+end
0
\ No newline at end of file
...
3
4
5
6
7
8
9
...
3
4
5
 
6
7
8
0
@@ -3,7 +3,6 @@ module Mack
0
     # This class is used to do all the view level bindings.
0
     # It allows for seperation between the Mack::Controller and the view levels.
0
     class ViewTemplate
0
-      include Mack::ViewHelpers
0
       
0
       # Allows access to any options passed into the template.
0
       attr_accessor :options
...
77
78
79
 
 
 
 
 
 
 
 
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
82
83
...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
0
@@ -77,7 +77,37 @@ module Mack
0
       end
0
 
0
       def select(name, *args)
0
+        var = instance_variable_get("@#{name}")
0
+        fe = FormElement.new(*args)
0
+        options = {:name => name, :id => name}
0
+        unless fe.calling_method == :to_s
0
+          options.merge!(:name => "#{name}[#{fe.calling_method}]", :id => "#{name}_#{fe.calling_method}")
0
+        end
0
+
0
+        content = ""
0
 
0
+        opts = fe.options[:options]
0
+        unless opts.nil?
0
+          sopts = opts
0
+          if opts.is_a?(Array)
0
+          elsif opts.is_a?(Hash)
0
+            sopts = []
0
+            opts.sort.each do |k,v|
0
+              sopts << [k, v]
0
+            end
0
+          else
0
+            raise ArgumentError.new(":options must be either an Array of Arrays or a Hash!")
0
+          end
0
+          sel_value = var.send(fe.calling_method) if var
0
+          sel_value = fe.options[:selected] if fe.options[:selected]
0
+          sopts.each do |kv|
0
+            content << %{<option value="#{kv[1]}" #{kv[1].to_s == sel_value.to_s ? "selected" : ""}>#{kv[0]}</option>}
0
+          end
0
+          fe.options.delete(:selected)
0
+          fe.options.delete(:options)
0
+        end
0
+        
0
+        return content_tag(:select, options.merge(fe.options), content)
0
       end
0
 
0
       def text_area(name, *args)
...
18
19
20
 
21
22
23
...
175
176
177
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
178
179
180
...
18
19
20
21
22
23
24
...
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
0
@@ -18,6 +18,7 @@ describe Mack::ViewHelpers::FormHelpers do
0
     @cop.bio_file = "~/bio.doc"
0
     @simple = "hi"
0
     @default_file = "~/resume.doc"
0
+    @select_options = [["one", 1], ["two", 2], ["three", 3]]
0
   end
0
   
0
   describe "check_box" do
0
@@ -175,6 +176,30 @@ describe Mack::ViewHelpers::FormHelpers do
0
   
0
   describe "select" do
0
     
0
+    it "should create a nested select tag for a model" do
0
+      select(:cop, :level).should == %{<select id="cop_level" name="cop[level]"></select>}
0
+    end
0
+    
0
+    it "should create a non-nested select tag for a simple model" do
0
+      select(:simple).should == %{<select id="simple" name="simple"></select>}
0
+    end
0
+    
0
+    it "should build the options from a given array of arrays" do
0
+      select(:simple, :options => @select_options).should == %{<select id="simple" name="simple"><option value="1" >one</option><option value="2" >two</option><option value="3" >three</option></select>}
0
+    end
0
+    
0
+    it "should build the options from a given hash" do
0
+      select(:simple, :options => {"one" => 1, "two" => 2, "three" => 3}).should == %{<select id="simple" name="simple"><option value="1" >one</option><option value="3" >three</option><option value="2" >two</option></select>}
0
+    end
0
+    
0
+    it "should mark an option as selected if the model has it seleceted" do
0
+      select(:cop, :level, :options => @select_options).should == %{<select id="cop_level" name="cop[level]"><option value="1" selected>one</option><option value="2" >two</option><option value="3" >three</option></select>}
0
+    end
0
+    
0
+    it "should mark an option as selected if the selected options is available" do
0
+      select(:simple, :options => @select_options, :selected => 1).should == %{<select id="simple" name="simple"><option value="1" selected>one</option><option value="2" >two</option><option value="3" >three</option></select>}
0
+    end
0
+    
0
   end
0
   
0
   describe "text_area" do

Comments