merb_helpers_monkey 0.1.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.
Files changed (4) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.txt +108 -0
  3. data/lib/merb_helpers_monkey.rb +189 -0
  4. metadata +55 -0
@@ -0,0 +1,20 @@
1
+ Copyright(c) 2008 kuwata-lab.com all rights reserverd.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,108 @@
1
+ ======
2
+ README
3
+ ======
4
+
5
+
6
+ About
7
+ =====
8
+
9
+ merb_helpers_monkey is a Merb plug-in to extend the following helper methods.
10
+
11
+ * radio_button() to take boolean value as :checked options.
12
+ * fields_for(), form_for(), and fieldset_for() to take :index_by option.
13
+
14
+
15
+ Install
16
+ =======
17
+
18
+ ### install gem
19
+ $ sudo gem install merb_helpers_monkey
20
+ ## add 'dependency "merb_helpers_monkey"
21
+ $ vi config/dependencies.rb
22
+
23
+
24
+ Usage
25
+ =====
26
+
27
+
28
+ radio_button()
29
+ --------------
30
+
31
+ In Merb, 'radio_button :checked=>true' and 'radio_button :checked=>false'
32
+ will genereate:
33
+
34
+ <input type="radio" checked="true" />
35
+ <input type="radio" checked="false" />
36
+
37
+ But the following output should be generated:
38
+
39
+ <input type="radio" checked="checked" />
40
+ <input type="radio" />
41
+
42
+ Merb_helpers_monkey extends radio_button() to generate the above <input> tag.
43
+ So you can write such as 'radio_button :checked=>(params[:foo]="y")'.
44
+
45
+
46
+ fields_for(), form_for(), fieldsset_for()
47
+ -----------------------------------------
48
+
49
+ These methods are extended to accept new ':index_by' option.
50
+
51
+ For example:
52
+
53
+ <% @users.each do |user| %>
54
+ <%= fields_for user, :index_by => :id do %>
55
+ <p>
56
+ <%= text_field :name, :label => 'Name' %>
57
+ <%= text_field :mail, :label => 'Mail' %>
58
+ </p>
59
+ <% end =%>
60
+ <% end %>
61
+
62
+ will generate:
63
+
64
+ <p>
65
+ <label for="user_name_101">Name</label>
66
+ <input type="text" name="user[101][name]" id="user_name_101" value="Foo" />
67
+ <label for="user_mail_101">Mail</label>
68
+ <input type="text" name="user[101][mail]" id="user_mail_101" value="foo@mail.com" />
69
+ </p>
70
+ <p>
71
+ <label for="user_name_102">Name</label>
72
+ <input type="text" name="user[102][name]" id="user_name_102" value="Bar" />
73
+ <label for="user_mail_102">Mail</label>
74
+ <input type="text" name="user[102][mail]" id="user_mail_102" value="bar@mail.com" />
75
+ </p>
76
+
77
+ and the following parameters will be sent:
78
+
79
+ params = {
80
+ :user => {
81
+ "101" => { :name => "Foo", :mail => "foo@mail.com" },
82
+ "102" => { :name => "Bar", :mail => "bar@mail.com" },
83
+ }
84
+ }
85
+
86
+ So you can define controller action like the following:
87
+
88
+ def update
89
+ params[:user].each do |user_id, values|
90
+ user = User.get(ur_id) or raise NotFound
91
+ user.update_attribute(values)
92
+ ...
93
+ end
94
+ end
95
+
96
+
97
+ Author
98
+ ------
99
+
100
+ makoto kuwata <kwa(at)kuwata-lab.com>
101
+ copyright(c) 2008 kuwata-lab.com all rights reserved.
102
+
103
+
104
+ License
105
+ -------
106
+
107
+ MIT License
108
+
@@ -0,0 +1,189 @@
1
+ ##
2
+ ## enhances some helper methods in merb_helpers.
3
+ ## see:
4
+ ## http://merb.lighthouseapp.com/projects/7435/tickets/10
5
+ ##
6
+ ## this monkey patch is based on merb-helpers 1.0.0.
7
+ ##
8
+
9
+
10
+ require 'merb-helpers'
11
+
12
+
13
+ ##
14
+ ## enhances radio_button() to take boolean value as :checked option.
15
+ ##
16
+
17
+ module Merb::Helpers::Form
18
+
19
+ alias _radio_button_orig radio_button
20
+
21
+ def radio_button(*args)
22
+ if (opts = args.last).is_a?(Hash)
23
+ opts[:checked] = "checked" if opts.delete(:checked)
24
+ end
25
+ _radio_button_orig(*args)
26
+ end
27
+
28
+ end
29
+
30
+
31
+ ##
32
+ ## enhances form_for(), fields_for(), and fieldset_for() to support :index_by option.
33
+ ## see:
34
+ ## http://merb.lighthouseapp.com/projects/7435/tickets/10
35
+ ##
36
+
37
+ ## --
38
+ ## merb-helpers-1.0/lib/merb-helpers/builder.rb
39
+ ## ++
40
+ module Merb::Helpers::Form::Builder
41
+
42
+
43
+ class Base
44
+
45
+ ##--
46
+ ##def bound_text_area(method, attrs = {})
47
+ ## name = "#{@name}[#{method}]"
48
+ ## update_bound_controls(method, attrs, "text_area")
49
+ ## unbound_text_area(control_value(method), {:name => name}.merge(attrs))
50
+ ##end
51
+ ##++
52
+ def bound_text_area(method, attrs = {})
53
+ name = control_name(method)
54
+ update_bound_controls(method, attrs, "text_area")
55
+ unbound_text_area(control_value(method), {:name => name}.merge(attrs))
56
+ end
57
+
58
+ private
59
+
60
+ ##--
61
+ ##def control_name(method)
62
+ ## @obj ? "#{@name}[#{method}]" : method
63
+ ##end
64
+ ##++
65
+ def control_name(method)
66
+ #!@obj ? method : !@index_by ? "#{@name}[#{method}]" : "#{@name}[#{@obj.__send__(@index_by)}][#{method}]"
67
+ !@obj ? method : !@index ? "#{@name}[#{method}]" : "#{@name}[#{@index}][#{method}]"
68
+ end
69
+
70
+ end
71
+
72
+
73
+ class Form
74
+
75
+ private
76
+
77
+ ##--
78
+ ##def update_bound_controls(method, attrs, type)
79
+ ## attrs.merge!(:id => "#{@name}_#{method}") unless attrs[:id]
80
+ ##end
81
+ ##++
82
+ def update_bound_controls(method, attrs, type)
83
+ #attrs.merge!(:id => "#{@name}_#{method}") unless attrs[:id]
84
+ unless attrs[:id]
85
+ id_attr = "#{@name}_#{method}"
86
+ #id_attr << "_#{@obj.__send__(@index_by)}" if @index_by
87
+ id_attr << "_#{@index}" if @index
88
+ attrs.merge!(:id => id_attr)
89
+ end
90
+ super
91
+ end
92
+
93
+ ##--
94
+ ##def radio_group_item(method, attrs)
95
+ ## unless attrs[:id]
96
+ ## attrs.merge!(:id => "#{@name}_#{method}_#{attrs[:value]}")
97
+ ## end
98
+ ##
99
+ ## attrs.merge!(:label => attrs[:label] || attrs[:value])
100
+ ## super
101
+ ##end
102
+ ##++
103
+ def radio_group_item(method, attrs)
104
+ unless attrs[:id]
105
+ #attrs.merge!(:id => "#{@name}_#{method}_#{attrs[:value]}")
106
+ id_attr = "#{@name}_#{method}_#{attrs[:value]}"
107
+ #id_attr << "_#{@obj.__send__(@index_by)}" if @index_by
108
+ id_attr << "_#{@index}" if @index
109
+ attrs.merge!(:id => id_attr)
110
+ end
111
+
112
+ attrs.merge!(:label => attrs[:label] || attrs[:value])
113
+ super
114
+ end
115
+
116
+ end
117
+
118
+
119
+ end
120
+
121
+
122
+ ## --
123
+ ## merb-helpers-1.0/lib/merb-helpers/helper.rb
124
+ ## ++
125
+ module Merb::Helpers::Form
126
+
127
+ ##--
128
+ ##def with_form_context(name, builder)
129
+ ## form_contexts.push(_new_form_context(name, builder))
130
+ ## ret = yield
131
+ ## form_contexts.pop
132
+ ## ret
133
+ ##end
134
+ ##++
135
+ def with_form_context(name, builder, index_by=nil)
136
+ form_contexts.push(c = _new_form_context(name, builder))
137
+ if index_by
138
+ c.instance_variable_set("@index_by", index_by) if index_by
139
+ index = c.instance_variable_get("@obj").__send__(index_by)
140
+ c.instance_variable_set("@index", index)
141
+ end
142
+ ret = yield
143
+ form_contexts.pop
144
+ ret
145
+ end
146
+
147
+ ##--
148
+ ##def form_for(name, attrs = {}, &blk)
149
+ ## with_form_context(name, attrs.delete(:builder)) do
150
+ ## current_form_context.form(attrs, &blk)
151
+ ## end
152
+ ##end
153
+ ##++
154
+ def form_for(name, attrs = {}, &blk)
155
+ with_form_context(name, attrs.delete(:builder), attrs.delete(:index_by)) do
156
+ current_form_context.form(attrs, &blk)
157
+ end
158
+ end
159
+
160
+ ##--
161
+ ##def fields_for(name, attrs = {}, &blk)
162
+ ## attrs ||= {}
163
+ ## with_form_context(name, attrs.delete(:builder)) do
164
+ ## capture(&blk)
165
+ ## end
166
+ ##end
167
+ ##++
168
+ def fields_for(name, attrs = {}, &blk)
169
+ attrs ||= {}
170
+ with_form_context(name, attrs.delete(:builder), attrs.delete(:index_by)) do
171
+ capture(&blk)
172
+ end
173
+ end
174
+
175
+ ##--
176
+ ##def fieldset_for(name, attrs = {}, &blk)
177
+ ## with_form_context(name, attrs.delete(:builder), attrs.delete(:index_by)) do
178
+ ## current_form_context.fieldset(attrs, &blk)
179
+ ## end
180
+ ##end
181
+ ##++
182
+ def fieldset_for(name, attrs = {}, &blk)
183
+ with_form_context(name, attrs.delete(:builder)) do
184
+ current_form_context.fieldset(attrs, &blk)
185
+ end
186
+ end
187
+
188
+
189
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: merb_helpers_monkey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - makoto kuwata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-12-21 00:00:00 +09:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "Merb_helpers_monkey is a Merb plug-in to extend the followng helper methods defined in merb-helpers: * radio_button() - to take boolean value as :checked option. * field_for(), form_for(), fieldset_for() - to take new :index_by option."
17
+ email: kwa(at)kuwata-lab.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - README.txt
26
+ - MIT-LICENSE
27
+ - lib/merb_helpers_monkey.rb
28
+ has_rdoc: false
29
+ homepage: http://github.com/kwatch/merb_helpers_monkey
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project: merb_helpers_monkey
50
+ rubygems_version: 1.3.1
51
+ signing_key:
52
+ specification_version: 2
53
+ summary: extends some helper methods defined in merb-helpers.
54
+ test_files: []
55
+