aeonscope-enhanced_select 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +3 -0
- data/LICENSE.rdoc +20 -0
- data/README.rdoc +90 -0
- data/Rakefile +58 -0
- data/VERSION.yml +4 -0
- data/lib/enhanced_select.rb +56 -0
- data/test/enhanced_select_test.rb +6 -0
- data/test/test_helper.rb +10 -0
- metadata +72 -0
data/CHANGELOG.rdoc
ADDED
data/LICENSE.rdoc
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Brooke Kuhlmann of {Berserk Technologies}[http://www.berserktech.com]
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
= Overview
|
2
|
+
|
3
|
+
An enhanced select helper for forms that allows you to use the full {HTML spec}[http://www.w3schools.com/tags/tag_option.asp] for select option elements. This is more than what you get with the default Rails select helper which only allows you to specify the value, text, and selected/disabled attributes.
|
4
|
+
|
5
|
+
= License
|
6
|
+
|
7
|
+
Copyright (c) 2009 Brooke Kuhlmann of {Berserk Technologies}[http://www.berserktech.com].
|
8
|
+
See the included LICENSE for more info.
|
9
|
+
|
10
|
+
= History
|
11
|
+
|
12
|
+
See the CHANGELOG file for more info.
|
13
|
+
|
14
|
+
= Requirements
|
15
|
+
|
16
|
+
1. {Ruby on Rails}[http://rubyonrails.org].
|
17
|
+
|
18
|
+
= Installation
|
19
|
+
|
20
|
+
Type the following from the command line to install:
|
21
|
+
|
22
|
+
* *UNIX*: sudo gem install aeonscope-enhanced_select
|
23
|
+
* *Windows*: gem install aeonscope-enhanced_select
|
24
|
+
|
25
|
+
Update your environment.rb file to include the new gem:
|
26
|
+
|
27
|
+
* config.gem "enhanced_select"
|
28
|
+
|
29
|
+
= Usage
|
30
|
+
|
31
|
+
Simply use "enhanced_select" instead of the Rails "select" helper method in your views:
|
32
|
+
|
33
|
+
<% form_for @city do |form| %>
|
34
|
+
<div><%= form.enhanced_select :id, @schools, :include_blank => "-select-" %></div>
|
35
|
+
<% end %>
|
36
|
+
|
37
|
+
This will then yield the following results:
|
38
|
+
|
39
|
+
<select id="<auto filled>" name="<auto filled>">
|
40
|
+
<option value="">-select-</option>
|
41
|
+
<option value="1">Goldbugs</option>
|
42
|
+
<option value="2">Falcons</option>
|
43
|
+
</select>
|
44
|
+
|
45
|
+
While the above is a simple example, lets look at a more interesting case. Assuming we are still dealing with cities, lets say
|
46
|
+
we want cities but we want each city to have CSS classes associated with so that we might be able to attach jQuery behaviors to
|
47
|
+
each option as it is selected (that and/or maybe we want to style each option). Here is how things might play out:
|
48
|
+
|
49
|
+
<b>Model Code</b>
|
50
|
+
|
51
|
+
class School < ActiveRecord::Base
|
52
|
+
def kind
|
53
|
+
[special? ? "special" : nil, restricted? ? "restricted" : nil].compact.join(' ')
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
<b>Controller Code</b>
|
58
|
+
|
59
|
+
class CityController < ApplicationController
|
60
|
+
before_filter :form_data, :only => [:new, :create, :edit, :update]
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
def form_data
|
65
|
+
@schools = School.all.collect {|school| {:value => school.id, :class => school.kind, :text => school.label}}
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
<b>View Code</b>
|
70
|
+
|
71
|
+
<% form_for @city do |form| %>
|
72
|
+
<div><%= form.enhanced_select :id, @schools, :include_blank => "-select-" %></div>
|
73
|
+
<% end %>
|
74
|
+
|
75
|
+
This would then yield the following results:
|
76
|
+
|
77
|
+
<select id="<auto filled>" name="<auto filled>">
|
78
|
+
<option value="">-select-</option>
|
79
|
+
<option value="1" class="restricted">Goldbugs</option>
|
80
|
+
<option value="2" class="special">Falcons</option>
|
81
|
+
</select>
|
82
|
+
|
83
|
+
The possibilities are up to you as you can definitely build a hash array of options with more attributes than value, class, and text
|
84
|
+
as shown above. The best part is that you have total control over how select options are built for your views.
|
85
|
+
|
86
|
+
= Contact/Feedback/Issues
|
87
|
+
|
88
|
+
* {Berserk Technologies}[http://www.berserktech.com] - Company web site.
|
89
|
+
* Aeonscope[http://www.aeonscope.net] - Personal web site.
|
90
|
+
* Twitter[http://www.twitter.com/Aeonscope] - Short bursts of insight and/or noise.
|
data/Rakefile
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "enhanced_select"
|
8
|
+
gem.summary = "An enhanced select helper for forms that allows you to use the full HTML spec for select option elements."
|
9
|
+
gem.description = "Enhances the default capabilities found with the Rails select helper. Instead of being able to supply just the value, text, and selected/disabled attributes, you can use all valid HTML attributes for select option elements."
|
10
|
+
gem.authors = ["Brooke Kuhlmann"]
|
11
|
+
gem.email = "aeonscope@gmail.com"
|
12
|
+
gem.homepage = "http://github.com/aeonscope/enhanced_select"
|
13
|
+
gem.required_ruby_version = ">= 1.8.6"
|
14
|
+
gem.add_dependency "rails", ">= 2.3.2"
|
15
|
+
gem.rdoc_options << "CHANGELOG.rdoc"
|
16
|
+
gem.files = FileList["[A-Z]*", "{bin,lib,generators,rails_generators,test,spec}/**/*"]
|
17
|
+
end
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'rake/testtask'
|
23
|
+
Rake::TestTask.new(:test) do |test|
|
24
|
+
test.libs << 'lib' << 'test'
|
25
|
+
test.pattern = 'test/**/*_test.rb'
|
26
|
+
test.verbose = true
|
27
|
+
end
|
28
|
+
|
29
|
+
begin
|
30
|
+
require 'rcov/rcovtask'
|
31
|
+
Rcov::RcovTask.new do |test|
|
32
|
+
test.libs << 'test'
|
33
|
+
test.pattern = 'test/**/*_test.rb'
|
34
|
+
test.verbose = true
|
35
|
+
end
|
36
|
+
rescue LoadError
|
37
|
+
task :rcov do
|
38
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
task :default => :test
|
43
|
+
|
44
|
+
require 'rake/rdoctask'
|
45
|
+
Rake::RDocTask.new do |rdoc|
|
46
|
+
if File.exist?('VERSION.yml')
|
47
|
+
config = YAML.load(File.read('VERSION.yml'))
|
48
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
49
|
+
else
|
50
|
+
version = ""
|
51
|
+
end
|
52
|
+
|
53
|
+
rdoc.rdoc_dir = 'rdoc'
|
54
|
+
rdoc.title = "enhanced_select #{version}"
|
55
|
+
rdoc.rdoc_files.include('README*')
|
56
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
57
|
+
end
|
58
|
+
|
data/VERSION.yml
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# Enhanced Select
|
2
|
+
module ActionView
|
3
|
+
module Helpers
|
4
|
+
module FormOptionsHelper
|
5
|
+
def enhanced_select object, method, choices, options = {}, html_options = {}
|
6
|
+
InstanceTag.new(object, method, self, options.delete(:object)).to_enhanced_select_tag(choices, options, html_options)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Accepts a hash array of options and returns a string of option tags. Each option hash is expected to be comprised of valid
|
10
|
+
# HTML option attributes (http://www.w3schools.com/tags/tag_option.asp) where the hash key is the HTML attribute name and the hash
|
11
|
+
# value is the attribute value. If +selected+ is specified, then the matching option will get the selected.
|
12
|
+
#
|
13
|
+
# Examples (call, result):
|
14
|
+
# enhanced_options_for_select [{:value => 1, :text => "New York"}, {:value => 2, :text => "Denver"}]
|
15
|
+
# <option value="1">New York</option>\n<option value="2">Denver</option>
|
16
|
+
#
|
17
|
+
# enhanced_options_for_select [{:value => "bmw", :text => "BMW"}, {:value => "tesla", :text => "Tesla Motors"}], "tesla"
|
18
|
+
# <option value="bmw">BMW</option>\n<option value="tesla" selected="selected">Tesla Motors</option>
|
19
|
+
#
|
20
|
+
# NOTE: Only the option tags are returned, you have to wrap this call in a regular HTML select tag.
|
21
|
+
def enhanced_options_for_select options = [], selected = nil
|
22
|
+
# Ensure option selections are unique.
|
23
|
+
options.each {|option| option.delete :selected}
|
24
|
+
# Generate the HTML code.
|
25
|
+
options.map do |option|
|
26
|
+
option[:selected] = "selected" if option[:value] == selected
|
27
|
+
html = "<option"
|
28
|
+
option.each_pair {|key, value| html << build_attribute(key, value)}
|
29
|
+
html << ">#{html_escape option[:text].to_s}</option>\n"
|
30
|
+
end.to_s
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# Convenience method for assembling attributes based on a key and value.
|
36
|
+
def build_attribute key, value
|
37
|
+
key == :text ? '' : " #{key.to_s}=\"#{html_escape value.to_s}\""
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class InstanceTag
|
42
|
+
def to_enhanced_select_tag choices, options, html_options
|
43
|
+
html_options = html_options.stringify_keys
|
44
|
+
add_default_name_and_id(html_options)
|
45
|
+
value = value(object)
|
46
|
+
content_tag "select", add_options(enhanced_options_for_select(choices, value), options, value), html_options
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
class FormBuilder
|
51
|
+
def enhanced_select method, choices, options = {}, html_options = {}
|
52
|
+
@template.enhanced_select @object_name, method, choices, objectify_options(options), @default_options.merge(html_options)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aeonscope-enhanced_select
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brooke Kuhlmann
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-03 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.2
|
24
|
+
version:
|
25
|
+
description: Enhances the default capabilities found with the Rails select helper. Instead of being able to supply just the value, text, and selected/disabled attributes, you can use all valid HTML attributes for select option elements.
|
26
|
+
email: aeonscope@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE.rdoc
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- CHANGELOG.rdoc
|
36
|
+
- LICENSE.rdoc
|
37
|
+
- README.rdoc
|
38
|
+
- Rakefile
|
39
|
+
- VERSION.yml
|
40
|
+
- lib/enhanced_select.rb
|
41
|
+
- test/enhanced_select_test.rb
|
42
|
+
- test/test_helper.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/aeonscope/enhanced_select
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --charset=UTF-8
|
48
|
+
- CHANGELOG.rdoc
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.8.6
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.2.0
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: An enhanced select helper for forms that allows you to use the full HTML spec for select option elements.
|
70
|
+
test_files:
|
71
|
+
- test/enhanced_select_test.rb
|
72
|
+
- test/test_helper.rb
|