merb_helpers 0.9.5 → 0.9.6
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/Rakefile +4 -4
- data/lib/merb_helpers/core_ext.rb +14 -1
- data/lib/merb_helpers/form/builder.rb +22 -20
- data/lib/merb_helpers/form/helpers.rb +15 -3
- data/lib/merb_helpers/text_helpers.rb +61 -0
- metadata +4 -3
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ GEM_EMAIL = "ykatz@engineyard.com"
|
|
18
18
|
|
19
19
|
GEM_NAME = "merb_helpers"
|
20
20
|
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
|
21
|
-
GEM_VERSION = (Merb::MORE_VERSION rescue "0.9.
|
21
|
+
GEM_VERSION = (Merb::MORE_VERSION rescue "0.9.6") + PKG_BUILD
|
22
22
|
|
23
23
|
RELEASE_NAME = "REL #{GEM_VERSION}"
|
24
24
|
|
@@ -36,7 +36,7 @@ spec = Gem::Specification.new do |s|
|
|
36
36
|
s.author = GEM_AUTHOR
|
37
37
|
s.email = GEM_EMAIL
|
38
38
|
s.homepage = PROJECT_URL
|
39
|
-
s.add_dependency('merb-core', '>= 0.9.
|
39
|
+
s.add_dependency('merb-core', '>= 0.9.6')
|
40
40
|
s.require_path = 'lib'
|
41
41
|
s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs}/**/*")
|
42
42
|
end
|
@@ -50,13 +50,13 @@ end
|
|
50
50
|
##############################################################################
|
51
51
|
desc "Install the gem"
|
52
52
|
task :install => [:package] do
|
53
|
-
sh
|
53
|
+
sh install_command(GEM_NAME, GEM_VERSION)
|
54
54
|
end
|
55
55
|
|
56
56
|
namespace :jruby do
|
57
57
|
desc "Run :package and install the resulting .gem with jruby"
|
58
58
|
task :install => :package do
|
59
|
-
sh
|
59
|
+
sh jinstall_command(GEM_NAME, GEM_VERSION)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
@@ -30,4 +30,17 @@ class Time
|
|
30
30
|
|
31
31
|
def to_time; self; end
|
32
32
|
public :to_date
|
33
|
-
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Truncates a string to the given length and appends the given suffix if the string is, in fact, truncated.
|
36
|
+
#
|
37
|
+
# ==== Examples:
|
38
|
+
# "This is a long string right here".truncate(10, "...") #=> "This is..."
|
39
|
+
|
40
|
+
class String
|
41
|
+
def truncate(length = 30, truncate_string = "...")
|
42
|
+
return self unless self.length > length
|
43
|
+
length = length - truncate_string.split(//).length
|
44
|
+
self[0...length] + truncate_string
|
45
|
+
end
|
46
|
+
end
|
@@ -33,8 +33,8 @@ module Merb::Helpers::Form::Builder
|
|
33
33
|
# Unless the method is :get, fake out the method using :post
|
34
34
|
attrs[:method] = :post unless attrs[:method] == :get
|
35
35
|
# Use a fake PUT if the object is not new, otherwise use the method
|
36
|
-
# passed in.
|
37
|
-
method ||= (@obj && !@obj.new_record? ? :put : :post
|
36
|
+
# passed in. Defaults to :post if no method is set.
|
37
|
+
method ||= (@obj.respond_to?(:new_record?) && !@obj.new_record?) || (@obj.respond_to?(:new?) && !@obj.new?) ? :put : :post
|
38
38
|
|
39
39
|
attrs[:enctype] = "multipart/form-data" if attrs.delete(:multipart) || @multipart
|
40
40
|
|
@@ -68,7 +68,7 @@ module Merb::Helpers::Form::Builder
|
|
68
68
|
def update_bound_check_box(method, attrs)
|
69
69
|
raise ArgumentError, ":value can't be used with a bound_check_box" if attrs.has_key?(:value)
|
70
70
|
|
71
|
-
attrs[:boolean]
|
71
|
+
attrs[:boolean] = attrs.fetch(:boolean, true)
|
72
72
|
|
73
73
|
val = @obj.send(method)
|
74
74
|
attrs[:checked] = attrs.key?(:on) ? val == attrs[:on] : considered_true?(val)
|
@@ -168,6 +168,7 @@ module Merb::Helpers::Form::Builder
|
|
168
168
|
|
169
169
|
def unbound_select(attrs = {})
|
170
170
|
update_unbound_controls(attrs, "select")
|
171
|
+
attrs[:name] << "[]" if attrs[:multiple] && !(attrs[:name] =~ /\[\]$/)
|
171
172
|
tag(:select, options_for(attrs), attrs)
|
172
173
|
end
|
173
174
|
|
@@ -180,6 +181,15 @@ module Merb::Helpers::Form::Builder
|
|
180
181
|
end.join
|
181
182
|
end
|
182
183
|
|
184
|
+
def unbound_radio_group(arr, attrs = {})
|
185
|
+
arr.map do |ind_attrs|
|
186
|
+
ind_attrs = {:value => ind_attrs} unless ind_attrs.is_a?(Hash)
|
187
|
+
joined = attrs.merge(ind_attrs)
|
188
|
+
joined.merge!(:label => joined[:label] || joined[:value])
|
189
|
+
unbound_radio_button(joined)
|
190
|
+
end.join
|
191
|
+
end
|
192
|
+
|
183
193
|
def unbound_text_area(contents, attrs)
|
184
194
|
update_unbound_controls(attrs, "text_area")
|
185
195
|
tag(:textarea, contents, attrs)
|
@@ -277,22 +287,14 @@ module Merb::Helpers::Form::Builder
|
|
277
287
|
super
|
278
288
|
end
|
279
289
|
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
# ==== Example
|
289
|
-
# <%= label :for => "name", :label => "Full Name" %>
|
290
|
-
# => <label for="name">Full Name</label>
|
291
|
-
def label(attrs)
|
292
|
-
attrs ||= {}
|
293
|
-
for_attr = attrs[:id] ? {:for => attrs[:id]} : {}
|
294
|
-
if label_text = attrs.delete(:label)
|
295
|
-
tag(:label, label_text, for_attr)
|
290
|
+
def label(contents, attrs = {})
|
291
|
+
if contents.is_a?(Hash)
|
292
|
+
attrs = contents
|
293
|
+
contents = attrs.delete(:label)
|
294
|
+
end
|
295
|
+
if contents
|
296
|
+
for_attr = attrs[:id] ? {:for => attrs[:id]} : {}
|
297
|
+
tag(:label, contents, for_attr)
|
296
298
|
else
|
297
299
|
""
|
298
300
|
end
|
@@ -377,7 +379,7 @@ module Merb::Helpers::Form::Builder
|
|
377
379
|
|
378
380
|
module Resourceful
|
379
381
|
def process_form_attrs(attrs)
|
380
|
-
attrs[:action] ||= url(@name, @obj) if @origin
|
382
|
+
attrs[:action] ||= @origin.url(@name, @obj) if @origin
|
381
383
|
super
|
382
384
|
end
|
383
385
|
end
|
@@ -1,8 +1,5 @@
|
|
1
1
|
# Form helpers provide a number of methods to simplify the creation of HTML forms.
|
2
2
|
# They can work directly with models (bound) or standalone (unbound).
|
3
|
-
#
|
4
|
-
# The core method of this helper, +form_for+, gives you the ability to create a form for a resource.
|
5
|
-
# For example, let's say that you have a model <tt>Person</tt> and want to create a new instance of it:
|
6
3
|
module Merb::Helpers::Form
|
7
4
|
|
8
5
|
def _singleton_form_context
|
@@ -152,6 +149,21 @@ module Merb::Helpers::Form
|
|
152
149
|
end
|
153
150
|
end
|
154
151
|
|
152
|
+
# Provides a generic HTML label.
|
153
|
+
#
|
154
|
+
# ==== Parameters
|
155
|
+
# attrs<Hash>:: HTML attributes
|
156
|
+
#
|
157
|
+
# ==== Returns
|
158
|
+
# String:: HTML
|
159
|
+
#
|
160
|
+
# ==== Example
|
161
|
+
# <%= label "Full Name", :for => "name" %>
|
162
|
+
# => <label for="name">Full Name</label>
|
163
|
+
def label(*args)
|
164
|
+
current_form_context.label(*args)
|
165
|
+
end
|
166
|
+
|
155
167
|
# Provides a HTML text input tag
|
156
168
|
#
|
157
169
|
# ==== Parameters
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Merb::Helpers::Text
|
2
|
+
# Allows you to cycle through elements in an array
|
3
|
+
#
|
4
|
+
# ==== Parameters
|
5
|
+
# values<Array>:: Array of objects to cycle through
|
6
|
+
# values<Hash>:: Last element of array can be a hash with the key of
|
7
|
+
# :name to specify the name of the cycle
|
8
|
+
#
|
9
|
+
# ==== Returns
|
10
|
+
# String
|
11
|
+
#
|
12
|
+
# ==== Notes
|
13
|
+
# * Default name is :default
|
14
|
+
#
|
15
|
+
# ==== Example
|
16
|
+
# <%= 5.times { cycle("odd! ","even! "} %>
|
17
|
+
#
|
18
|
+
# Generates:
|
19
|
+
#
|
20
|
+
# odd! even! odd! even! odd!
|
21
|
+
def cycle(*values)
|
22
|
+
options = extract_options_from_args!(values) || {}
|
23
|
+
key = (options[:name] || :default).to_sym
|
24
|
+
(@cycle_positions ||= {})[key] ||= {:position => -1, :values => values}
|
25
|
+
unless values == @cycle_positions[key][:values]
|
26
|
+
@cycle_positions[key] = {:position => -1, :values => values}
|
27
|
+
end
|
28
|
+
current = @cycle_positions[key][:position]
|
29
|
+
@cycle_positions[key][:position] = current + 1
|
30
|
+
values.at( (current + 1) % values.length).to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
# Allows you to reset a cycle
|
34
|
+
#
|
35
|
+
# ==== Parameters
|
36
|
+
# name<Symbol|String>:: Name of the cycle
|
37
|
+
#
|
38
|
+
# ==== Returns
|
39
|
+
# True if successful, otherwise nil
|
40
|
+
#
|
41
|
+
# ==== Notes
|
42
|
+
# * Default name is :default
|
43
|
+
#
|
44
|
+
# ==== Example
|
45
|
+
# <%= cycle("odd! ","even! ","what comes after even?") %>
|
46
|
+
# <%= cycle("odd! ","even! ","what comes after even?") %>
|
47
|
+
# <% reset_cycle %>
|
48
|
+
# <%= cycle("odd! ","even! ","what comes after even?") %>
|
49
|
+
#
|
50
|
+
# Generates:
|
51
|
+
#
|
52
|
+
# odd! even! odd!
|
53
|
+
def reset_cycle(name = :default)
|
54
|
+
(@cycle_positions[name.to_sym] = nil) &&
|
55
|
+
true if @cycle_positions && @cycle_positions[name.to_sym]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class Merb::Controller
|
60
|
+
include Merb::Helpers::Text
|
61
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yehuda Katz
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-08
|
12
|
+
date: 2008-09-08 00:00:00 +03:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 0.9.
|
23
|
+
version: 0.9.6
|
24
24
|
version:
|
25
25
|
description: Helper support for merb (similar to the Rails form helpers)
|
26
26
|
email: ykatz@engineyard.com
|
@@ -46,6 +46,7 @@ files:
|
|
46
46
|
- lib/merb_helpers/form_helpers.rb
|
47
47
|
- lib/merb_helpers/ordinalize.rb
|
48
48
|
- lib/merb_helpers/tag_helpers.rb
|
49
|
+
- lib/merb_helpers/text_helpers.rb
|
49
50
|
- lib/merb_helpers/time_dsl.rb
|
50
51
|
- lib/merb_helpers.rb
|
51
52
|
has_rdoc: true
|