helpful_fields 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Readme.md CHANGED
@@ -36,6 +36,10 @@ Usage
36
36
  <% f.check_box_with_label :is_admin, 'Can destroy stuff?' %>
37
37
  <% f.radio_button_with_label :type, 'evil', 'No so nice' %>
38
38
 
39
+ TODO
40
+ ====
41
+ - support seperate options for label and input `radio_button_with_label 'xxx', 'yyy', true, 'Label', :label => {:style => 'float:left'}, :input => {:class => 'evil'}`
42
+
39
43
  Author
40
44
  ======
41
45
  [Michael Grosser](http://grosser.it)<br/>
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{helpful_fields}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2011-11-06}
12
+ s.date = %q{2011-11-09}
13
13
  s.email = %q{michael@grosser.it}
14
14
  s.files = [
15
15
  "Gemfile",
@@ -51,14 +51,9 @@ class HelpfulFields
51
51
  label_for = if options[:id]
52
52
  options[:id] # when id was changed, label has to be for this id
53
53
  else
54
- # name[hello][world] -> name_hello_world_
55
- # name[hello][] -> name_hello_world__
56
- # name -> name_
57
- clean_name = name.to_s.gsub('[]','_').gsub('][','_').gsub(/[\]\[]/,'_')
58
- clean_name += '_' unless clean_name =~ /_$/
59
- clean_name + value.to_s.downcase
54
+ sanitize_to_id(name) + '_' + sanitize_to_id(value)
60
55
  end
61
- radio_button_tag(name, value, checked) + label_tag(label_for, label)
56
+ radio_button_tag(name, value, checked, :id => label_for) + label_tag(label_for, label)
62
57
  end
63
58
 
64
59
  def params_radio_button_tag(name, value, options={})
@@ -72,11 +67,16 @@ class HelpfulFields
72
67
 
73
68
  module FormBuilder
74
69
  def check_box_with_label(field, label, options={})
75
- check_box(field, options) + label(field, label, options)
70
+ check_box(field, :id => options[:id]) + label(field, label, :for => options[:id])
76
71
  end
77
72
 
78
73
  def radio_button_with_label(field, value, label, options={})
79
- radio_button(field, value, options) + label("#{field}_#{value}", label, options)
74
+ unless id = options[:id]
75
+ object_s, fields_s, value_s = [@object_name, field, value].map{|f| @template.send(:sanitize_to_id, f) }
76
+ id = "#{object_s}_#{fields_s}_#{value_s}"
77
+ end
78
+
79
+ radio_button(field, value, :id => id) + label('_', label, :for => id)
80
80
  end
81
81
  end
82
82
  end
@@ -13,9 +13,19 @@ describe HelpfulFields do
13
13
  result = result.gsub("accept-charset=\"UTF-8\" ","")
14
14
  result = result.gsub(%r{<div .*?</div>},"")
15
15
  result = result.gsub('class="user_new" id="user_new" ',"")
16
+ assert_id_and_for_match(result)
16
17
  result
17
18
  end
18
19
 
20
+ def assert_id_and_for_match(html)
21
+ if html.include?(' for=') and html.include?(' id=')
22
+ id = html.match(/ id="(.*?)"/)[1]
23
+ _for = html.match(/ for="(.*?)"/)[1]
24
+ id.should_not == nil
25
+ id.should == _for
26
+ end
27
+ end
28
+
19
29
  it "has a VERSION" do
20
30
  HelpfulFields::VERSION.should =~ /^\d+\.\d+\.\d+$/
21
31
  end
@@ -118,10 +128,15 @@ describe HelpfulFields do
118
128
  end
119
129
 
120
130
  describe :params_check_box_with_label do
121
- it "its not surprise" do
131
+ it "works" do
122
132
  render('<%= params_check_box_with_label "foo[bar]", "1", "Click it" %>', :foo => {:bar => 1}).
123
133
  should == "<input checked=\"checked\" id=\"foo_bar\" name=\"foo[bar]\" type=\"checkbox\" value=\"1\" /><label for=\"foo_bar\">Click it</label>"
124
134
  end
135
+
136
+ it "works with given id" do
137
+ render('<%= params_check_box_with_label "foo[bar]", "1", "Click it", :id => "xxx" %>', :foo => {:bar => 1}).
138
+ should == "<input checked=\"checked\" id=\"xxx\" name=\"foo[bar]\" type=\"checkbox\" value=\"1\" /><label for=\"xxx\">Click it</label>"
139
+ end
125
140
  end
126
141
 
127
142
  describe :radio_button_with_label do
@@ -137,13 +152,23 @@ describe HelpfulFields do
137
152
 
138
153
  it "generates labels for arrays" do
139
154
  render('<%= radio_button_with_label "foo[]", "1", true, "Click it" %>').
140
- should == "<input checked=\"checked\" id=\"foo__1\" name=\"foo[]\" type=\"radio\" value=\"1\" /><label for=\"foo_1\">Click it</label>"
155
+ should == "<input checked=\"checked\" id=\"foo__1\" name=\"foo[]\" type=\"radio\" value=\"1\" /><label for=\"foo__1\">Click it</label>"
156
+ end
157
+
158
+ it "generates labels for uppercase/weird names" do
159
+ render('<%= radio_button_with_label "fooáßð[]", "UA§§;", true, "Click it" %>').
160
+ should == "<input checked=\"checked\" id=\"foo________UA_____\" name=\"foo\303\241\303\237\303\260[]\" type=\"radio\" value=\"UA\302\247\302\247;\" /><label for=\"foo________UA_____\">Click it</label>"
141
161
  end
142
162
 
143
163
  it "generates labels for nested attribute-arrays" do
144
164
  render('<%= radio_button_with_label "foo[bar][baz][]", "1", true, "Click it" %>').
145
165
  should == "<input checked=\"checked\" id=\"foo_bar_baz__1\" name=\"foo[bar][baz][]\" type=\"radio\" value=\"1\" /><label for=\"foo_bar_baz__1\">Click it</label>"
146
166
  end
167
+
168
+ it "uses given id" do
169
+ render('<%= radio_button_with_label "foo", "1", true, "Click it", :id => "xxx" %>').
170
+ should == "<input checked=\"checked\" id=\"xxx\" name=\"foo\" type=\"radio\" value=\"1\" /><label for=\"xxx\">Click it</label>"
171
+ end
147
172
  end
148
173
 
149
174
  describe :params_radio_button_tag do
@@ -209,14 +234,33 @@ describe HelpfulFields do
209
234
  end
210
235
 
211
236
  describe 'FormBuilder' do
212
- it "adds check_box_with_label" do
213
- render('<% form_for :user, nil, :url=> "/xxx" do |f| %> <%= f.check_box_with_label :simple, "Hes so simple" %> <% end %>').
214
- should == "<form action=\"/xxx\" method=\"post\"> <input name=\"user[simple]\" type=\"hidden\" value=\"0\" /><input id=\"user_simple\" name=\"user[simple]\" type=\"checkbox\" value=\"1\" /><label for=\"user_simple\">Hes so simple</label> </form>"
237
+ describe :check_box_with_label do
238
+ it "builds them" do
239
+ render('<% form_for :user, nil, :url=> "/xxx" do |f| %> <%= f.check_box_with_label :simple, "Hes so simple" %> <% end %>').
240
+ should == "<form action=\"/xxx\" method=\"post\"> <input name=\"user[simple]\" type=\"hidden\" value=\"0\" /><input id=\"user_simple\" name=\"user[simple]\" type=\"checkbox\" value=\"1\" /><label for=\"user_simple\">Hes so simple</label> </form>"
241
+ end
242
+
243
+ it "uses given :id" do
244
+ render('<% form_for :user, nil, :url=> "/xxx" do |f| %> <%= f.check_box_with_label :simple, "Hes so simple", :id => "xxx" %> <% end %>').
245
+ should == "<form action=\"/xxx\" method=\"post\"> <input name=\"user[simple]\" type=\"hidden\" value=\"0\" /><input id=\"xxx\" name=\"user[simple]\" type=\"checkbox\" value=\"1\" /><label for=\"xxx\">Hes so simple</label> </form>"
246
+ end
215
247
  end
216
248
 
217
- it "adds radio_button_with_label" do
218
- render('<% form_for :user, nil, :url=> "/xxx" do |f| %> <%= f.radio_button_with_label :simple, "yes", "Hes so simple" %> <% end %>').
219
- should == "<form action=\"/xxx\" method=\"post\"> <input id=\"user_simple_yes\" name=\"user[simple]\" type=\"radio\" value=\"yes\" /><label for=\"user_simple_yes\">Hes so simple</label> </form>"
249
+ describe :radio_button_with_label do
250
+ it "builds them" do
251
+ render('<% form_for :user, nil, :url=> "/xxx" do |f| %> <%= f.radio_button_with_label :simple, "yes", "Hes so simple" %> <% end %>').
252
+ should == "<form action=\"/xxx\" method=\"post\"> <input id=\"user_simple_yes\" name=\"user[simple]\" type=\"radio\" value=\"yes\" /><label for=\"user_simple_yes\">Hes so simple</label> </form>"
253
+ end
254
+
255
+ it "builds with weird values" do
256
+ render('<% form_for :user, nil, :url=> "/xxx" do |f| %> <%= f.radio_button_with_label :simple, "NO.;§", "Hes so simple" %> <% end %>').
257
+ should == "<form action=\"/xxx\" method=\"post\"> <input id=\"user_simple_NO.___\" name=\"user[simple]\" type=\"radio\" value=\"NO.;\302\247\" /><label for=\"user_simple_NO.___\">Hes so simple</label> </form>"
258
+ end
259
+
260
+ it "uses given :id" do
261
+ render('<% form_for :user, nil, :url=> "/xxx" do |f| %> <%= f.radio_button_with_label :simple, "yes", "Hes so simple", :id => "xxx" %> <% end %>').
262
+ should == "<form action=\"/xxx\" method=\"post\"> <input id=\"xxx\" name=\"user[simple]\" type=\"radio\" value=\"yes\" /><label for=\"xxx\">Hes so simple</label> </form>"
263
+ end
220
264
  end
221
265
  end
222
266
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helpful_fields
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-06 00:00:00 -07:00
18
+ date: 2011-11-09 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21