helpful_fields 0.0.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/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source :rubygems
2
+
3
+ group :dev do # not development <-> would add unneeded development dependencies in gemspec
4
+ gem 'rake'
5
+ gem 'actionpack', '2.3.12'
6
+ gem 'rspec', '~>2'
7
+ gem 'jeweler'
8
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionpack (2.3.12)
5
+ activesupport (= 2.3.12)
6
+ rack (~> 1.1.0)
7
+ activesupport (2.3.12)
8
+ diff-lcs (1.1.2)
9
+ git (1.2.5)
10
+ jeweler (1.6.4)
11
+ bundler (~> 1.0)
12
+ git (>= 1.2.5)
13
+ rake
14
+ rack (1.1.2)
15
+ rake (0.9.2)
16
+ rspec (2.6.0)
17
+ rspec-core (~> 2.6.0)
18
+ rspec-expectations (~> 2.6.0)
19
+ rspec-mocks (~> 2.6.0)
20
+ rspec-core (2.6.4)
21
+ rspec-expectations (2.6.0)
22
+ diff-lcs (~> 1.1.2)
23
+ rspec-mocks (2.6.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ actionpack (= 2.3.12)
30
+ jeweler
31
+ rake
32
+ rspec (~> 2)
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ task :default do
2
+ sh "rspec spec/"
3
+ end
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = 'helpful_fields'
9
+ gem.summary = "Many helpful field helpers e.g. check_box_with_label"
10
+ gem.email = "michael@grosser.it"
11
+ gem.homepage = "http://github.com/grosser/#{gem.name}"
12
+ gem.authors = ["Michael Grosser"]
13
+ end
14
+
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
18
+ end
data/Readme.md ADDED
@@ -0,0 +1,43 @@
1
+ Many helpful field helpers for Rails(need ActionView)
2
+
3
+ - prefilled form elements with values from params (even deep nesting like 'search[foo][bar][]')
4
+ - check boxes/radio buttos with matching labels
5
+ - selects directly filled with values
6
+
7
+ Install
8
+ =======
9
+
10
+ sudo gem install helpful_fields
11
+ Or
12
+
13
+ rails plugin install git://github.com/grosser/helpful_fields.git
14
+
15
+
16
+ Usage
17
+ =====
18
+
19
+ # text field filled from params
20
+ <%= params_text_field_tag 'search[category]' %>
21
+
22
+ # check box with label
23
+ # selected if params[:search][:user] is 1 or is an array that includes 1
24
+ <%= params_check_box_with_label 'search[with_user]', 1, 'Search with users' %>
25
+
26
+ # radio button with label, checked when in params
27
+ <%= params_radio_button_with_label 'search[type]', 'product', 'by Product' %>
28
+ <%= params_radio_button_with_label 'search[type]', 'shop', 'by Shop' %>
29
+
30
+ # select tag with options, preselected from params(or :value => xxx)
31
+ <%= params_select_options_tag :type, ['', 'none'] %>
32
+ <%= params_select_options_tag :type, [[0,'none'], [1, 'all']] %>
33
+ <%= params_select_options_tag :type, {'none' => 0, 'all' => 1} %>
34
+
35
+ # check box/radio with label for forms
36
+ <% f.check_box_with_label :is_admin, 'Can destroy stuff?' %>
37
+ <% f.radio_button_with_label :type, 'evil', 'No so nice' %>
38
+
39
+ Author
40
+ ======
41
+ [Michael Grosser](http://grosser.it)<br/>
42
+ michael@grosser.it<br/>
43
+ Hereby placed under public domain, do what you want, just do not hold me accountable...
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,40 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{helpful_fields}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Grosser"]
12
+ s.date = %q{2011-07-17}
13
+ s.email = %q{michael@grosser.it}
14
+ s.files = [
15
+ "Gemfile",
16
+ "Gemfile.lock",
17
+ "Rakefile",
18
+ "Readme.md",
19
+ "VERSION",
20
+ "helpful_fields.gemspec",
21
+ "lib/helpful_fields.rb",
22
+ "lib/helpful_fields/core_ext/hash.rb",
23
+ "spec/helpful_fields_spec.rb",
24
+ "spec/spec_helper.rb"
25
+ ]
26
+ s.homepage = %q{http://github.com/grosser/helpful_fields}
27
+ s.require_paths = ["lib"]
28
+ s.rubygems_version = %q{1.6.2}
29
+ s.summary = %q{Many helpful field helpers e.g. check_box_with_label}
30
+
31
+ if s.respond_to? :specification_version then
32
+ s.specification_version = 3
33
+
34
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
35
+ else
36
+ end
37
+ else
38
+ end
39
+ end
40
+
@@ -0,0 +1,87 @@
1
+ require 'helpful_fields/core_ext/hash'
2
+
3
+ class HelpfulFields
4
+ VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
5
+
6
+ def self.check_box_checked?(params, name, value)
7
+ in_params = params.value_from_nested_key(name).presence
8
+ if in_params.is_a?(Array)
9
+ in_params.map(&:to_s).include?(value.to_s)
10
+ else
11
+ in_params.to_s == value.to_s
12
+ end
13
+ end
14
+
15
+ module TagHelper
16
+ # --- misc
17
+ def params_text_field_tag(name, options={})
18
+ text_field_tag name, params.value_from_nested_key(name), options
19
+ end
20
+
21
+ def params_text_area_tag(name, options={})
22
+ text_area_tag name, params.value_from_nested_key(name), options
23
+ end
24
+
25
+ def params_hidden_field_tag (name, options={})
26
+ hidden_field_tag name, params.value_from_nested_key(name), options
27
+ end
28
+
29
+ def params_select_options_tag(name, list, options={})
30
+ list = list.map{|x| x.is_a?(Array) ? [x[0],h(x[1])] : h(x) } # stringify values from lists
31
+ selected = h(options[:value] || params.value_from_nested_key(name))
32
+ select_tag(name, options_for_select(list,selected), options.except(:value))
33
+ end
34
+
35
+ # --- check_box
36
+ def params_check_box_tag(name, value, options={})
37
+ check_box_tag(name, value, HelpfulFields.check_box_checked?(params, name, value), options)
38
+ end
39
+
40
+ def check_box_with_label(name, value, checked, label, options={})
41
+ label_for = options[:id] || name
42
+ check_box_tag(name, value, checked, options) + label_tag(label_for, label)
43
+ end
44
+
45
+ def params_check_box_with_label(name, value, label, options={})
46
+ check_box_with_label(name, value, HelpfulFields.check_box_checked?(params, name, value), label, options)
47
+ end
48
+
49
+ # --- radio_button
50
+ def radio_button_with_label(name, value, checked, label, options={})
51
+ label_for = if options[:id]
52
+ options[:id] # when id was changed, label has to be for this id
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
60
+ end
61
+ radio_button_tag(name, value, checked) + label_tag(label_for, label)
62
+ end
63
+
64
+ def params_radio_button_tag(name, value, options={})
65
+ radio_button_tag(name, value, HelpfulFields.check_box_checked?(params, name, value), options)
66
+ end
67
+
68
+ def params_radio_button_with_label(name, value, label, options={})
69
+ radio_button_with_label(name, value, HelpfulFields.check_box_checked?(params, name, value), label, options)
70
+ end
71
+ end
72
+
73
+ module FormBuilder
74
+ def check_box_with_label(field, label, options={})
75
+ check_box(field, options) + label(field, label, options)
76
+ end
77
+
78
+ def radio_button_with_label(field, value, label, options={})
79
+ radio_button(field, value, options) + label("#{field}_#{value}", label, options)
80
+ end
81
+ end
82
+ end
83
+
84
+ if defined?(ActionView)
85
+ ActionView::Base.send(:include, HelpfulFields::TagHelper)
86
+ ActionView::Helpers::FormBuilder.send(:include, HelpfulFields::FormBuilder)
87
+ end
@@ -0,0 +1,16 @@
1
+ class Hash
2
+ # {'x'=>{'y'=>{'z'=>1}}.value_from_nested_key('x[y][z]') => 1
3
+ def value_from_nested_key(key)
4
+ if key.to_s.include?('[')
5
+ match, first, nesting = key.to_s.match(/(.+?)\[(.*)\]/).to_a
6
+ value = self[first]
7
+ nesting.split('][').each do |part|
8
+ return nil unless value.is_a?(Hash)
9
+ value = value[part]
10
+ end
11
+ value
12
+ else
13
+ self[key]
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,214 @@
1
+ require File.expand_path('spec/spec_helper')
2
+
3
+ describe HelpfulFields do
4
+ def render(text, params={})
5
+ view = ActionView::Base.new
6
+ view.stub!(:params).and_return params.with_indifferent_access
7
+ view.stub!(:protect_against_forgery?).and_return false
8
+ view.render(:inline => text)
9
+ end
10
+
11
+ it "has a VERSION" do
12
+ HelpfulFields::VERSION.should =~ /^\d+\.\d+\.\d+$/
13
+ end
14
+
15
+ describe 'core ext' do
16
+ describe :value_from_nested_key do
17
+ it "accesses normal keys" do
18
+ {:x => 1}.value_from_nested_key(:x).should == 1
19
+ {'x' => 1}.value_from_nested_key('x').should == 1
20
+ end
21
+
22
+ it "can access deep values" do
23
+ {'x' => {'y' => {'z' => 1}}}.value_from_nested_key('x[y][z]').should == 1
24
+ end
25
+
26
+ it "returns nil for unfound keys" do
27
+ {'x' => 1}.value_from_nested_key('x[y][z]').should == nil
28
+ end
29
+
30
+ it "can get values from xxx[]" do
31
+ {'x' => {'y' => [1,2,3]}}.value_from_nested_key('x[y][]').should == [1,2,3]
32
+ {'x' => [1,2,3]}.value_from_nested_key('x[]').should == [1,2,3]
33
+ end
34
+
35
+ it "does not mess up with escaped =" do
36
+ {'x=y' => 1}['x=y'].should == 1
37
+ end
38
+ end
39
+ end
40
+
41
+ describe 'TagHelper' do
42
+ describe :params_text_field_tag do
43
+ it "renders empty" do
44
+ render('<%= params_text_field_tag :xxx %>').
45
+ should == "<input id=\"xxx\" name=\"xxx\" type=\"text\" />"
46
+ end
47
+
48
+ it "grabs the value from params" do
49
+ render('<%= params_text_field_tag :xxx %>', 'xxx' => 1).
50
+ should == "<input id=\"xxx\" name=\"xxx\" type=\"text\" value=\"1\" />"
51
+ end
52
+
53
+ it "renders with nested params" do
54
+ render('<%= params_text_field_tag "foo[bar]" %>').
55
+ should == "<input id=\"foo_bar\" name=\"foo[bar]\" type=\"text\" />"
56
+ end
57
+
58
+ it "grabs the value from nested params" do
59
+ render('<%= params_text_field_tag "foo[bar]" %>', :foo => {:bar => 2}).
60
+ should == "<input id=\"foo_bar\" name=\"foo[bar]\" type=\"text\" value=\"2\" />"
61
+ end
62
+ end
63
+
64
+ describe :params_text_area_tag do
65
+ it "renders with value" do
66
+ render('<%= params_text_area_tag "foo[bar]" %>', :foo => {:bar => 2}).
67
+ should == "<textarea id=\"foo_bar\" name=\"foo[bar]\">2</textarea>"
68
+ end
69
+ end
70
+
71
+ describe :params_hidden_field_tag do
72
+ it "renders with value" do
73
+ render('<%= params_hidden_field_tag "foo[bar]" %>', :foo => {:bar => 2}).
74
+ should == "<input id=\"foo_bar\" name=\"foo[bar]\" type=\"hidden\" value=\"2\" />"
75
+ end
76
+ end
77
+
78
+ describe :params_check_box_tag do
79
+ it "renders with value" do
80
+ render('<%= params_check_box_tag "foo[bar]", "1" %>', :foo => {:bar => 1}).
81
+ should == "<input checked=\"checked\" id=\"foo_bar\" name=\"foo[bar]\" type=\"checkbox\" value=\"1\" />"
82
+ end
83
+
84
+ it "is not checked when value does not match" do
85
+ render('<%= params_check_box_tag "foo[bar]", "2" %>', :foo => {:bar => 1}).
86
+ should == "<input id=\"foo_bar\" name=\"foo[bar]\" type=\"checkbox\" value=\"2\" />"
87
+ end
88
+
89
+ it "is checked when value is in array" do
90
+ render('<%= params_check_box_tag "foo[bar][]", "1" %>', :foo => {:bar => [1,2,3]}).
91
+ should == "<input checked=\"checked\" id=\"foo_bar_\" name=\"foo[bar][]\" type=\"checkbox\" value=\"1\" />"
92
+ end
93
+
94
+ it "is not checked when value is not in array" do
95
+ render('<%= params_check_box_tag "foo[bar][]", "1" %>', :foo => {:bar => [2,3]}).
96
+ should == "<input id=\"foo_bar_\" name=\"foo[bar][]\" type=\"checkbox\" value=\"1\" />"
97
+ end
98
+ end
99
+
100
+ describe :check_box_with_label do
101
+ it "adds a label to the checkbox" do
102
+ render('<%= check_box_with_label "foo[bar]", "1", true, "Click it" %>').
103
+ should == "<input checked=\"checked\" id=\"foo_bar\" name=\"foo[bar]\" type=\"checkbox\" value=\"1\" /><label for=\"foo_bar\">Click it</label>"
104
+ end
105
+
106
+ it "uses a given id" do
107
+ render('<%= check_box_with_label "foo[bar]", "1", true, "Click it", :id => "xxx" %>').
108
+ should == "<input checked=\"checked\" id=\"xxx\" name=\"foo[bar]\" type=\"checkbox\" value=\"1\" /><label for=\"xxx\">Click it</label>"
109
+ end
110
+ end
111
+
112
+ describe :params_check_box_with_label do
113
+ it "its not surprise" do
114
+ render('<%= params_check_box_with_label "foo[bar]", "1", "Click it" %>', :foo => {:bar => 1}).
115
+ should == "<input checked=\"checked\" id=\"foo_bar\" name=\"foo[bar]\" type=\"checkbox\" value=\"1\" /><label for=\"foo_bar\">Click it</label>"
116
+ end
117
+ end
118
+
119
+ describe :radio_button_with_label do
120
+ it "adds a simple label to a radio button" do
121
+ render('<%= radio_button_with_label "foo", "1", true, "Click it" %>').
122
+ should == "<input checked=\"checked\" id=\"foo_1\" name=\"foo\" type=\"radio\" value=\"1\" /><label for=\"foo_1\">Click it</label>"
123
+ end
124
+
125
+ it "generates labels for nested attributes" do
126
+ render('<%= radio_button_with_label "foo[bar][baz]", "1", true, "Click it" %>').
127
+ 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>"
128
+ end
129
+
130
+ it "generates labels for arrays" do
131
+ render('<%= radio_button_with_label "foo[]", "1", true, "Click it" %>').
132
+ should == "<input checked=\"checked\" id=\"foo__1\" name=\"foo[]\" type=\"radio\" value=\"1\" /><label for=\"foo_1\">Click it</label>"
133
+ end
134
+
135
+ it "generates labels for nested attribute-arrays" do
136
+ render('<%= radio_button_with_label "foo[bar][baz][]", "1", true, "Click it" %>').
137
+ 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>"
138
+ end
139
+ end
140
+
141
+ describe :params_radio_button_tag do
142
+ it "renders normally" do
143
+ render('<%= params_radio_button_tag "foo", "1" %>').
144
+ should == "<input id=\"foo_1\" name=\"foo\" type=\"radio\" value=\"1\" />"
145
+ end
146
+
147
+ it "fetches value from params" do
148
+ render('<%= params_radio_button_tag "foo", "1" %>', :foo => 1).
149
+ should == "<input checked=\"checked\" id=\"foo_1\" name=\"foo\" type=\"radio\" value=\"1\" />"
150
+ end
151
+
152
+ it "fetches value from nested params" do
153
+ render('<%= params_radio_button_tag "foo[bar]", "1" %>', :foo => {:bar => 1}).
154
+ should == "<input checked=\"checked\" id=\"foo_bar_1\" name=\"foo[bar]\" type=\"radio\" value=\"1\" />"
155
+ end
156
+
157
+ it "fetches value from nested params-array" do
158
+ render('<%= params_radio_button_tag "foo[bar][]", "1" %>', :foo => {:bar => [1]}).
159
+ should == "<input checked=\"checked\" id=\"foo_bar__1\" name=\"foo[bar][]\" type=\"radio\" value=\"1\" />"
160
+ end
161
+ end
162
+
163
+ describe :params_radio_button_with_label do
164
+ it "its not surprise" do
165
+ render('<%= params_radio_button_with_label "foo", "1", "Click it" %>', :foo => 1).
166
+ should == "<input checked=\"checked\" id=\"foo_1\" name=\"foo\" type=\"radio\" value=\"1\" /><label for=\"foo_1\">Click it</label>"
167
+ end
168
+ end
169
+
170
+ describe :params_select_options_tag do
171
+ it "builds from simple list" do
172
+ render('<%= params_select_options_tag "foo", [1] %>').
173
+ should == "<select id=\"foo\" name=\"foo\"><option value=\"1\">1</option></select>"
174
+ end
175
+
176
+ it "builds from nested array" do
177
+ render('<%= params_select_options_tag "foo", [[1,2]] %>').
178
+ should == "<select id=\"foo\" name=\"foo\"><option value=\"2\">1</option></select>"
179
+ end
180
+
181
+ it "builds from simple list and nested array" do
182
+ render('<%= params_select_options_tag "foo", [1, [2,3]] %>').
183
+ should == "<select id=\"foo\" name=\"foo\"><option value=\"1\">1</option>\n<option value=\"3\">2</option></select>"
184
+ end
185
+
186
+ it "builds from hash" do
187
+ render('<%= params_select_options_tag "foo", {1 => 2} %>').
188
+ should == "<select id=\"foo\" name=\"foo\"><option value=\"2\">1</option></select>"
189
+ end
190
+
191
+ it "is prefilled" do
192
+ render('<%= params_select_options_tag "foo", [1,2,3] %>', :foo => '3').
193
+ should == "<select id=\"foo\" name=\"foo\"><option value=\"1\">1</option>\n<option value=\"2\">2</option>\n<option value=\"3\" selected=\"selected\">3</option></select>"
194
+ end
195
+
196
+ it "is prefilled from :value" do
197
+ render('<%= params_select_options_tag "foo", [1,2,3], :value => 2 %>', :foo => '3').
198
+ should == "<select id=\"foo\" name=\"foo\"><option value=\"1\">1</option>\n<option value=\"2\" selected=\"selected\">2</option>\n<option value=\"3\">3</option></select>"
199
+ end
200
+ end
201
+ end
202
+
203
+ describe 'FormBuilder' do
204
+ it "adds check_box_with_label" do
205
+ render('<% form_for :user, nil, :url=> "/xxx" do |f| %> <%= f.check_box_with_label :simple, "Hes so simple" %> <% end %>').
206
+ 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>"
207
+ end
208
+
209
+ it "adds radio_button_with_label" do
210
+ render('<% form_for :user, nil, :url=> "/xxx" do |f| %> <%= f.radio_button_with_label :simple, "yes", "Hes so simple" %> <% end %>').
211
+ 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>"
212
+ end
213
+ end
214
+ end
@@ -0,0 +1,4 @@
1
+ require 'active_support/all'
2
+ require 'action_view'
3
+ $LOAD_PATH.unshift 'lib'
4
+ require 'helpful_fields'
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: helpful_fields
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 0
10
+ version: 0.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Michael Grosser
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-07-17 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description:
23
+ email: michael@grosser.it
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - Gemfile
32
+ - Gemfile.lock
33
+ - Rakefile
34
+ - Readme.md
35
+ - VERSION
36
+ - helpful_fields.gemspec
37
+ - lib/helpful_fields.rb
38
+ - lib/helpful_fields/core_ext/hash.rb
39
+ - spec/helpful_fields_spec.rb
40
+ - spec/spec_helper.rb
41
+ has_rdoc: true
42
+ homepage: http://github.com/grosser/helpful_fields
43
+ licenses: []
44
+
45
+ post_install_message:
46
+ rdoc_options: []
47
+
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ hash: 3
56
+ segments:
57
+ - 0
58
+ version: "0"
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ hash: 3
65
+ segments:
66
+ - 0
67
+ version: "0"
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.6.2
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Many helpful field helpers e.g. check_box_with_label
75
+ test_files: []
76
+