twitter_bootstrap_combo 0.0.1

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.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in twitter_bootstrap_combo.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Romain Tartière
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,51 @@
1
+ # TwitterBootstrapCombo
2
+
3
+ Advanced combo-box using Twitter-Bootstrap
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'twitter_bootstrap_combo'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install twitter_bootstrap_combo
18
+
19
+ ## Usage
20
+
21
+ Add this line to `app/assets/javascripts/application.js`:
22
+
23
+ ~~~js
24
+ //= twitter_bootstrap_combo
25
+ ~~~
26
+
27
+ Then in your views, use:
28
+
29
+ ~~~erb
30
+ <%= combo_box(:calendar, :parent_id, Calendar.all.collect { |c| [ (content_tag(:i, "", :class => "icon-angle-right") * c.level + c.name).html_safe, c.id ] }, :include_blank => true) %>
31
+ ~~~
32
+
33
+ to produce:
34
+
35
+ ~~~html
36
+ <input id="calendar_parent_id" name="calendar[parent_id]" type="hidden" value="6" /><div class="btn-group"><a href="#" class="btn dropdown-toggle" data-toggle="dropdown"><span class="combo_box_text">Child 1</span> <i class="icon-caret-down"></i></a><ul class="dropdown-menu" data-for="calendar_parent_id"><li class=""><a href="#" class="combo_box_item">(None)</a></li><li class=""><a href="#" class="combo_box_item" data-value="5">Root</a></li><li class="active"><a href="#" class="combo_box_item" data-value="6"><i class="icon-angle-right"></i>Child 1</a></li><li class=""><a href="#" class="combo_box_item" data-value="8"><i class="icon-angle-right"></i><i class="icon-angle-right"></i>SubChild 1</a></li><li class=""><a href="#" class="combo_box_item" data-value="9"><i class="icon-angle-right"></i><i class="icon-angle-right"></i>SubChild 2</a></li><li class=""><a href="#" class="combo_box_item" data-value="7"><i class="icon-angle-right"></i>Child 2</a></li></ul></div>
37
+ ~~~
38
+
39
+ which render like this:
40
+
41
+ ![combo-closed](http://img11.hostingpics.net/pics/892421comboboxclosed.png)
42
+
43
+ ![combo-opened](http://img11.hostingpics.net/pics/781716comboboxopened.png)
44
+
45
+ ## Contributing
46
+
47
+ 1. Fork it
48
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
49
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
50
+ 4. Push to the branch (`git push origin my-new-feature`)
51
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ require "rails"
2
+
3
+ module TwitterBootstrapCombo
4
+ module Rails
5
+ if ::Rails.version < "3.1"
6
+ require 'twitter_bootstrap_combo/railties'
7
+ else
8
+ require 'twitter_bootstrap_combo/engine'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require "twitter_bootstrap_combo/view_helpers"
2
+
3
+ module TwitterBootstrapCombo
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ initializer "twitter_bootstrap_combo" do |app|
7
+ ActionView::Base.send :include, TwitterBootstrapCombo::ViewHelpers
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ require "twitter_bootstrap_combo/view_helpers"
2
+
3
+ module TwitterBootstrapCombo
4
+ class Railtie < Rails::Railtie
5
+ initializer "twitter_bootstrap_combo" do |app|
6
+ ActionView::Base.send :include, ViewHelpers
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module TwitterBootstrapCombo
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,89 @@
1
+ module TwitterBootstrapCombo
2
+ module ViewHelpers
3
+ # Accepts a container and return a string of combo_box items. If +selected+
4
+ # is specified, the corresponding item is marked as active.
5
+ def options_for_combo_box(container, selected = nil)
6
+ return container if String === container
7
+
8
+ container.map do |element|
9
+ if Array === element then
10
+ text, value = element
11
+ else
12
+ text, value = element, element
13
+ end
14
+ css_class = []
15
+ if selected == value then
16
+ css_class << "active"
17
+ end
18
+ content_tag(:li, :class => css_class.join(" ")) do
19
+ link_to(text, '#', :class => "combo_box_item", :data => { :value => value })
20
+ end
21
+ end.join().html_safe
22
+ end
23
+
24
+ # Returns a string of combo_box items that have been compiled by iterating
25
+ # over the +collection+ and assigning the result of the call to the
26
+ # +value_method+ as the item value and the +text_method+ as the item text.
27
+ # If +selected+ is specified, the item with the corresponding value is
28
+ # marked as selected.
29
+ def options_from_collection_for_combo_box(collection, value_method, text_method, selected = nil)
30
+ options = collection.map do |element|
31
+ [ element.send(text_method), element.send(value_method) ]
32
+ end
33
+
34
+ options_for_combo_box(options, selected)
35
+ end
36
+
37
+ # Returns a complete Twitter Bootstrap widget tailored for accessing a
38
+ # specified attribute (identified by +method+) on an object assigned to the
39
+ # template (identified by +object+) using a Twitter Bootstrap menu with
40
+ # +choices+. Additional options can be passed as a hash with +options+ and
41
+ # +html_options+.
42
+ #
43
+ # Supported +options+:
44
+ # - <tt>:include_blank</tt> - If set to true, an empty option will be created.
45
+ #
46
+ # Supported +html_options+:
47
+ # - None yet
48
+ #
49
+ # Examples:
50
+ # combo_box(:event, :calendar_id, [ [ "Calendar 1", 1 ], [ "Calendar 2", 2] ])
51
+ #
52
+ # container(:event, :calendar_id, options_from_collection_for_combo_box(Calendar.all, :id, :name, @event.calendar_id), :include_blank => true)
53
+ def combo_box(object, method, choices, options = {}, html_options = {})
54
+ current_value = self.instance_variable_get("@#{object}").instance_variable_get("@attributes")[method.to_s].to_i
55
+
56
+ current_text = "(None)"
57
+ if Array === choices then
58
+ if ! Array === choices[0] then
59
+ current_text = current_value
60
+ else
61
+ choices.each do |element|
62
+ if current_value == element[1] then
63
+ current_text = element[0]
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ hidden_field(object, method) +
70
+ content_tag(:div, :class => "btn-group") do
71
+ link_to(content_tag(:span, strip_tags(current_text), :class => "combo_box_text") + " " + content_tag(:i, "", :class => "icon-caret-down"), "#", :class => "btn dropdown-toggle", :data => { :toggle => "dropdown" }) +
72
+ content_tag(:ul, :class => "dropdown-menu", :data => { :for => "#{object}_#{method}" }) do
73
+ if options[:include_blank] then
74
+ css_class = []
75
+ if current_value == 0 then
76
+ css_class << "active"
77
+ end
78
+ content_tag(:li, :class => css_class.join(" ")) do
79
+ link_to("(None)", '#', :class => "combo_box_item")
80
+ end
81
+ else
82
+ "".html_safe
83
+ end +
84
+ options_for_combo_box(choices, current_value)
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'twitter_bootstrap_combo/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "twitter_bootstrap_combo"
8
+ gem.version = TwitterBootstrapCombo::VERSION
9
+ gem.authors = ["Romain Tartiere"]
10
+ gem.email = ["romain@blogreen.org"]
11
+ gem.description = %q{Advanced combo-box using Twitter-Bootstrap}
12
+ gem.summary = %q{twitter_bootstrap_combo workarounds limitation of SELECT tags in Rails application and provide a coherent look & feel through Twitter-Bootstraped applications.}
13
+ gem.homepage = "https://github.com/smortex/twitter_bootstrap_combo"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,11 @@
1
+ jQuery ->
2
+ $(".combo_box_text").each( ->
3
+ $(this).text($(this).closest("div.btn-group").find("ul").find("li.active").text())
4
+ )
5
+ $('a.combo_box_item').click( ->
6
+ $("#" + $(this).parent("li").parent("ul").attr("data-for")).val($(this).attr("data-value"))
7
+ $(this).closest(".btn-group").find(".combo_box_text").text($(this).text())
8
+ $('a.combo_box_item').parent("li").removeClass("active")
9
+ $(this).parent("li").addClass("active")
10
+ true
11
+ )
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_bootstrap_combo
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Romain Tartiere
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-02-02 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Advanced combo-box using Twitter-Bootstrap
22
+ email:
23
+ - romain@blogreen.org
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - Gemfile
33
+ - LICENSE.txt
34
+ - README.md
35
+ - Rakefile
36
+ - lib/twitter_bootstrap_combo.rb
37
+ - lib/twitter_bootstrap_combo/engine.rb
38
+ - lib/twitter_bootstrap_combo/railtie.rb
39
+ - lib/twitter_bootstrap_combo/version.rb
40
+ - lib/twitter_bootstrap_combo/view_helpers.rb
41
+ - twitter_bootstrap_combo.gemspec
42
+ - vendor/assets/javascripts/twitter_bootstrap_combo.js.coffee
43
+ homepage: https://github.com/smortex/twitter_bootstrap_combo
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.24
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: twitter_bootstrap_combo workarounds limitation of SELECT tags in Rails application and provide a coherent look & feel through Twitter-Bootstraped applications.
76
+ test_files: []
77
+