padrino-helpers 0.9.15 → 0.9.16
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/padrino-helpers/dom_helpers.rb +46 -0
- data/lib/padrino-helpers/form_helpers.rb +16 -2
- data/lib/padrino-helpers.rb +2 -1
- data/test/test_dom_helpers.rb +37 -0
- data/test/test_form_builder.rb +21 -1
- metadata +9 -7
@@ -0,0 +1,46 @@
|
|
1
|
+
module Padrino
|
2
|
+
module Helpers
|
3
|
+
module DomHelpers
|
4
|
+
##
|
5
|
+
# Create DOM id from given object. You can also specify optional prefix.
|
6
|
+
#
|
7
|
+
# ==== Examples
|
8
|
+
#
|
9
|
+
# @user = User.new
|
10
|
+
# dom_id(@user, "new") # => "new_user"
|
11
|
+
#
|
12
|
+
# @user.save
|
13
|
+
# @user.id # => 10
|
14
|
+
# dom_id(@user) # => user_10
|
15
|
+
# dom_id(@user, "edit") # => edit_user_10
|
16
|
+
#
|
17
|
+
def dom_id(object, prefix=nil)
|
18
|
+
chain = []
|
19
|
+
chain << prefix if prefix
|
20
|
+
chain << object.class.to_s.underscore.gsub("/", "_")
|
21
|
+
chain << object.id if object.respond_to?('id') && object.id
|
22
|
+
chain.join('_')
|
23
|
+
end
|
24
|
+
|
25
|
+
##
|
26
|
+
# Create DOM class name from given object. You can also specify optional
|
27
|
+
# prefix.
|
28
|
+
#
|
29
|
+
# ==== Examples
|
30
|
+
#
|
31
|
+
# @user = User.new
|
32
|
+
# dom_class(@user, "new") # => new_user
|
33
|
+
# dom_class(@user) # => user
|
34
|
+
#
|
35
|
+
# @user.save
|
36
|
+
# @user.id # => 11
|
37
|
+
# dom_class(@user) # => user
|
38
|
+
def dom_class(object, prefix=nil)
|
39
|
+
chain = []
|
40
|
+
chain << prefix if prefix
|
41
|
+
chain << object.class.to_s.underscore.gsub("/", "_")
|
42
|
+
chain.join('_')
|
43
|
+
end
|
44
|
+
end # DomHelpers
|
45
|
+
end # Helpers
|
46
|
+
end # Padrino
|
@@ -357,7 +357,7 @@ module Padrino
|
|
357
357
|
return '' if option_items.blank?
|
358
358
|
option_items.collect do |caption, value|
|
359
359
|
value ||= caption
|
360
|
-
content_tag(:option, caption, :value => value, :selected =>
|
360
|
+
content_tag(:option, caption, :value => value, :selected => option_is_selected?(value, caption, selected_value))
|
361
361
|
end
|
362
362
|
end
|
363
363
|
|
@@ -374,6 +374,20 @@ module Padrino
|
|
374
374
|
configured_builder = "Padrino::Helpers::FormBuilder::#{configured_builder}".constantize if configured_builder.is_a?(String)
|
375
375
|
configured_builder
|
376
376
|
end
|
377
|
+
|
378
|
+
|
379
|
+
#
|
380
|
+
# Returns whether the option should be selected or not
|
381
|
+
#
|
382
|
+
def option_is_selected?(value, caption, selected_value)
|
383
|
+
if selected_value.is_a? Array
|
384
|
+
selected_value.any? do |selected|
|
385
|
+
selected.to_s =~ /^(#{value}|#{caption})$/
|
386
|
+
end
|
387
|
+
else
|
388
|
+
selected_value.to_s =~ /^(#{value}|#{caption})$/
|
389
|
+
end
|
390
|
+
end
|
377
391
|
end # FormHelpers
|
378
392
|
end # Helpers
|
379
|
-
end # Padrino
|
393
|
+
end # Padrino
|
data/lib/padrino-helpers.rb
CHANGED
@@ -30,6 +30,7 @@ module Padrino
|
|
30
30
|
class << self
|
31
31
|
def registered(app)
|
32
32
|
app.set :default_builder, 'StandardFormBuilder'
|
33
|
+
app.helpers Padrino::Helpers::DomHelpers
|
33
34
|
app.helpers Padrino::Helpers::OutputHelpers
|
34
35
|
app.helpers Padrino::Helpers::TagHelpers
|
35
36
|
app.helpers Padrino::Helpers::AssetTagHelpers
|
@@ -42,4 +43,4 @@ module Padrino
|
|
42
43
|
alias :included :registered
|
43
44
|
end
|
44
45
|
end # Helpers
|
45
|
-
end # Padrino
|
46
|
+
end # Padrino
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/helper')
|
2
|
+
|
3
|
+
class TestObject
|
4
|
+
attr_accessor :id
|
5
|
+
end
|
6
|
+
|
7
|
+
class TestDomHelpers < Test::Unit::TestCase
|
8
|
+
include Padrino::Helpers::DomHelpers
|
9
|
+
|
10
|
+
class SecondTestObject
|
11
|
+
attr_accessor :id
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'for #dom_id method' do
|
15
|
+
should "return DOM id based on name of given object" do
|
16
|
+
assert_equal dom_id(TestObject.new), "test_object"
|
17
|
+
assert_equal dom_id(TestDomHelpers::SecondTestObject.new), "test_dom_helpers_second_test_object"
|
18
|
+
end
|
19
|
+
should "prepend given prefix to generated id" do
|
20
|
+
assert_equal dom_id(TestObject.new, "new"), "new_test_object"
|
21
|
+
end
|
22
|
+
should "append object #id to generated id if it's not empty" do
|
23
|
+
test_obj = TestObject.new
|
24
|
+
test_obj.id = 10
|
25
|
+
assert_equal dom_id(test_obj), "test_object_10"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'for #dom_class method' do
|
30
|
+
should "return DOM class name based on name of given object" do
|
31
|
+
assert_equal dom_class(TestObject.new), "test_object"
|
32
|
+
end
|
33
|
+
should "prepend given prefix to generated class name" do
|
34
|
+
assert_equal dom_class(TestObject.new, "new"), "new_test_object"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/test/test_form_builder.rb
CHANGED
@@ -413,6 +413,26 @@ class TestFormBuilder < Test::Unit::TestCase
|
|
413
413
|
assert_has_tag('select option', :value => 'California', :selected => 'selected') { actual_html }
|
414
414
|
end
|
415
415
|
|
416
|
+
should "display correct select html with selected item if it matches full value" do
|
417
|
+
@user.stubs(:state => 'Cali')
|
418
|
+
actual_html = standard_builder.select(:state, :options => ['Cali', 'California', 'Texas', 'Wyoming'])
|
419
|
+
assert_has_tag('select', :id => 'user_state', :name => 'user[state]') { actual_html }
|
420
|
+
assert_has_tag('select option', :selected => 'selected', :count => 1) { actual_html }
|
421
|
+
assert_has_tag('select option', :value => 'Cali', :selected => 'selected') { actual_html }
|
422
|
+
assert_has_tag('select option', :value => 'California') { actual_html }
|
423
|
+
end
|
424
|
+
|
425
|
+
should "display correct select html with multiple selected items" do
|
426
|
+
@user.stubs(:pickles => ['foo', 'bar'])
|
427
|
+
actual_html = standard_builder.select(
|
428
|
+
:pickles, :options => [ ['Foo', 'foo'], ['Bar', 'bar'], ['Baz', 'baz'], ['Bar Buz', 'bar buz'] ]
|
429
|
+
)
|
430
|
+
assert_has_tag('option', :value => 'foo', :content => 'Foo', :selected => 'selected') { actual_html }
|
431
|
+
assert_has_tag('option', :value => 'bar', :content => 'Bar', :selected => 'selected') { actual_html }
|
432
|
+
assert_has_tag('option', :value => 'baz', :content => 'Baz') { actual_html }
|
433
|
+
assert_has_tag('option', :value => 'bar buz', :content => 'Bar Buz') { actual_html }
|
434
|
+
end
|
435
|
+
|
416
436
|
should "display correct select html with include_blank" do
|
417
437
|
actual_html = standard_builder.select(:state, :options => ['California', 'Texas', 'Wyoming'], :include_blank => true)
|
418
438
|
assert_has_tag('select', :id => 'user_state', :name => 'user[state]') { actual_html }
|
@@ -665,4 +685,4 @@ class TestFormBuilder < Test::Unit::TestCase
|
|
665
685
|
assert_have_selector '#demo2 p input', :type => 'image', :class => 'image', :src => "/images/buttons/ok.png?#{@stamp}"
|
666
686
|
end
|
667
687
|
end
|
668
|
-
end
|
688
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: padrino-helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 9
|
9
|
-
-
|
10
|
-
version: 0.9.
|
9
|
+
- 16
|
10
|
+
version: 0.9.16
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Padrino Team
|
@@ -18,7 +18,7 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2010-
|
21
|
+
date: 2010-09-24 00:00:00 +02:00
|
22
22
|
default_executable:
|
23
23
|
dependencies:
|
24
24
|
- !ruby/object:Gem::Dependency
|
@@ -29,12 +29,12 @@ dependencies:
|
|
29
29
|
requirements:
|
30
30
|
- - "="
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
hash:
|
32
|
+
hash: 27
|
33
33
|
segments:
|
34
34
|
- 0
|
35
35
|
- 9
|
36
|
-
-
|
37
|
-
version: 0.9.
|
36
|
+
- 16
|
37
|
+
version: 0.9.16
|
38
38
|
type: :runtime
|
39
39
|
version_requirements: *id001
|
40
40
|
- !ruby/object:Gem::Dependency
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- Rakefile
|
70
70
|
- padrino-helpers.gemspec
|
71
71
|
- lib/padrino-helpers/asset_tag_helpers.rb
|
72
|
+
- lib/padrino-helpers/dom_helpers.rb
|
72
73
|
- lib/padrino-helpers/form_builder/abstract_form_builder.rb
|
73
74
|
- lib/padrino-helpers/form_builder/standard_form_builder.rb
|
74
75
|
- lib/padrino-helpers/form_helpers.rb
|
@@ -119,6 +120,7 @@ files:
|
|
119
120
|
- test/fixtures/render_app/views/template/some_template.haml
|
120
121
|
- test/helper.rb
|
121
122
|
- test/test_asset_tag_helpers.rb
|
123
|
+
- test/test_dom_helpers.rb
|
122
124
|
- test/test_form_builder.rb
|
123
125
|
- test/test_form_helpers.rb
|
124
126
|
- test/test_format_helpers.rb
|