bootstrap-view-helpers 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
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,3 @@
1
+ = BootstrapViewHelpers
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'BootstrapViewHelpers'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
@@ -0,0 +1,63 @@
1
+ module Bootstrap::AccordionHelper
2
+
3
+ def accordion(options={})
4
+ options = ensure_accordion_id(options)
5
+ @accordion_id = options[:id]
6
+ options = ensure_class(options, 'accordion')
7
+
8
+ content_tag(:div, options) do
9
+ yield
10
+ end
11
+ end
12
+
13
+ def accordion_group(text, options={})
14
+ options = ensure_accordion_group_id(options)
15
+ @accordion_group_id = options[:id]
16
+ open = options.delete(:open)
17
+
18
+ content_tag(:div, class: 'accordion-group') do
19
+ accordion_group_heading(text) + accordion_group_body(open) { yield }
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def accordion_group_heading(text)
26
+ content_tag(:div, class: 'accordion-heading') do
27
+ content_tag(:a, text, class: %(accordion-toggle), href: "##{@accordion_group_id}", data: {toggle: 'collapse', parent: "##{@accordion_id}" })
28
+ end
29
+ end
30
+
31
+ def accordion_group_body(open)
32
+ classes = %w(accordion-body collapse)
33
+ classes << 'in' if open
34
+
35
+ content_tag(:div, id: @accordion_group_id, class: classes) do
36
+ content_tag(:div, class: 'accordion-inner') do
37
+ yield
38
+ end
39
+ end
40
+ end
41
+
42
+ def ensure_accordion_id(options)
43
+ if options.has_key?(:id)
44
+ options
45
+ else
46
+ @accordion_number = @accordion_number.to_i + 1
47
+ options.dup.tap do |h|
48
+ h[:id] = "accordion-#{@accordion_number}"
49
+ end
50
+ end
51
+ end
52
+
53
+ def ensure_accordion_group_id(options)
54
+ if options.has_key?(:id)
55
+ options
56
+ else
57
+ @accordion_group_number = @accordion_group_number.to_i + 1
58
+ options.dup.tap do |h|
59
+ h[:id] = "#{@accordion_id}-group-#{@accordion_group_number}"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,41 @@
1
+ # See: http://twitter.github.io/bootstrap/components.html#labels-badges
2
+ #
3
+ # Helper for producing Twitter Bootstrap badges
4
+ #
5
+ # Default badge:
6
+ #
7
+ # badge('Default')
8
+ #
9
+ # Other badges (see BADGE_TYPES):
10
+ #
11
+ # badge('Info', :info)
12
+ #
13
+ # Options passed through to <span> tag:
14
+ #
15
+ # badge('Warning', :warning, id: 'warn-id', class: 'more-class', my_key: 'my_value')
16
+ #
17
+ module Bootstrap::BadgeHelper
18
+ InvalidBadgeTypeError = Class.new(StandardError)
19
+
20
+ BADGE_TYPES = %w(default success warning important info inverse)
21
+
22
+ def badge(*args)
23
+ text = args.shift
24
+ options = add_badge_classes(*args)
25
+ content_tag(:span, text, options)
26
+ end
27
+
28
+ private
29
+
30
+ def add_badge_classes(*args)
31
+ options = args.extract_options!
32
+ validate_badge_types(args)
33
+ classes = ['badge'] + args.map { |arg| "badge-#{arg}" }
34
+ ensure_class(options, classes)
35
+ end
36
+
37
+ def validate_badge_types(badge_types)
38
+ badge_types.each { |e| raise(InvalidBadgeTypeError, e.inspect) unless BADGE_TYPES.include?(e.to_s) }
39
+ end
40
+
41
+ end
@@ -0,0 +1,87 @@
1
+ # Helper for producing Twitter Bootstrap buttons OR links that look like buttons.
2
+ # See: http://twitter.github.io/bootstrap/base-css.html#buttons
3
+ #
4
+ # Default button:
5
+ #
6
+ # button('Default') #=> <button class="btn">Default</button>
7
+ #
8
+ # Pass in a url to make a link that looks like a button:
9
+ #
10
+ # button('Home', url: '/home') #=> <a href="/home" class="btn">Home</a>
11
+ #
12
+ # Or make a <button> look like a link:
13
+ #
14
+ # button('Home', :link)
15
+ #
16
+ # Specify the type (see BUTTON_TYPES):
17
+ #
18
+ # button('Info', :info)
19
+ #
20
+ # Specify the size (see BUTTON_SIZES)
21
+ #
22
+ # button('Small', :small)
23
+ #
24
+ # Options passed through to <span> tag:
25
+ #
26
+ # button('Warning', :warning, :large id: 'warn-id', class: 'more-class', my_key: 'my_value')
27
+ #
28
+ # Button groups/toolbars: http://twitter.github.io/bootstrap/components.html#buttonGroups
29
+ #
30
+ # = button_group do
31
+ # = button("Left", "/left")
32
+ # = button("Right", "/right")
33
+ #
34
+ # = button_toolbar do
35
+ # = button('Single Button', '/single')
36
+ # = button_group
37
+ # = button('Group Button 1')
38
+ # = button('Group Button 2')
39
+ # = button('Another Single')
40
+ #
41
+ module Bootstrap::ButtonHelper
42
+ InvalidButtonModifierError = Class.new(StandardError)
43
+
44
+ BUTTON_TYPES = %w(default primary info success warning danger inverse link)
45
+ BUTTON_SIZES = %w(default large small mini)
46
+ BUTTON_OTHERS = %w(block)
47
+
48
+ BUTTON_ALL = BUTTON_TYPES + BUTTON_SIZES + BUTTON_OTHERS
49
+
50
+ def button(*args)
51
+ text = args.shift
52
+ options = args.extract_options!
53
+ href = options.delete(:url)
54
+ options = add_button_classes(options, args)
55
+
56
+ if href.present?
57
+ link_to(text, href, options)
58
+ else
59
+ content_tag(:button, text, options)
60
+ end
61
+ end
62
+
63
+ def button_toolbar
64
+ content_tag(:div, class: 'btn-toolbar') do
65
+ yield
66
+ end
67
+ end
68
+
69
+ def button_group
70
+ content_tag(:div, class: 'btn-group') do
71
+ yield
72
+ end
73
+ end
74
+
75
+ private
76
+
77
+ def add_button_classes(options, button_types_and_sizes)
78
+ validate_types_and_sizes(button_types_and_sizes)
79
+ classes = ['btn'] + button_types_and_sizes.map { |e| "btn-#{e}" }
80
+ ensure_class(options, classes)
81
+ end
82
+
83
+ def validate_types_and_sizes(types_and_sizes)
84
+ types_and_sizes.each { |e| raise(InvalidButtonModifierError, e.inspect) unless BUTTON_ALL.include?(e.to_s) }
85
+ end
86
+
87
+ end
@@ -0,0 +1,56 @@
1
+ module Bootstrap::CommonHelper
2
+ ArgumentError = Class.new(::ArgumentError)
3
+
4
+ # Returns a new hash with the :class key's value converted to an
5
+ # Array with each element converted to a String.
6
+ #
7
+ # arrayify_class({}) #=> {:class=>[]}
8
+ # arrayify_class(:class => 'foo') #=> {:class=>["foo"]}
9
+ # arrayify_class(:class => [:foo, 'bar']) #=> {:class=>["foo", "bar"]}
10
+ def arrayify_class(hash)
11
+ raise ArgumentError.new("expected a Hash, got #{hash.inspect}") unless hash.is_a?(Hash)
12
+
13
+ return hash if hash[:class] == false
14
+
15
+ hash.dup.tap do |h|
16
+ classes = h[:class]
17
+ h[:class] = case
18
+ when classes.blank? then []
19
+ when classes.is_a?(Array) then classes.dup
20
+ else classes.to_s.split(/\s/)
21
+ end
22
+ h[:class].map!(&:to_s)
23
+ end
24
+ end
25
+
26
+ # Returns down-caret character used in various dropdown menus.
27
+ def caret(options={})
28
+ options = ensure_class(options, 'caret')
29
+ content_tag(:span, nil, options)
30
+ end
31
+
32
+ # Ensures that _hash_ has key of :class that includes _klass_.
33
+ #
34
+ # ensure_class({}, 'foo') #=> {class: 'foo'}
35
+ # ensure_class({class: 'bar', id: 'my-id'}, 'foo') #=> {:class=>["bar", "foo"], :id=>"my-id"}
36
+ def ensure_class(hash, klass)
37
+ arrayify_class(hash).tap do |h|
38
+ klasses = Array(klass).map(&:to_s)
39
+ klasses.each do |k|
40
+ h[:class] << k unless h[:class].include?(k)
41
+ end
42
+ end
43
+ end
44
+
45
+ # Returns extra arguments that are Bootstrap modifiers. Basically 2nd argument
46
+ # up to (not including) the last (hash) argument.
47
+ #
48
+ # extract_extras('text') #=> []
49
+ # extract_extras('text', :small, :info, id: 'foo') #=> [:small, :info]
50
+ def extract_extras(*args)
51
+ args.extract_options!
52
+ args.shift
53
+ args
54
+ end
55
+
56
+ end
@@ -0,0 +1,100 @@
1
+ # Helper methods for various Bootstrap dropdown menus
2
+ #
3
+ # * http://twitter.github.io/bootstrap/components.html#buttonDropdowns
4
+ # * http://twitter.github.io/bootstrap/components.html#navbar
5
+ #
6
+ # All of the *_dropdown methods should have dropdown_item() or dropdown_divider()
7
+ # as children.
8
+ #
9
+ # = nav_dropdown('Admin') do
10
+ # = dropdown_item('Users', admin_users_path)
11
+ # = dropdown_item('Logs', admin_logs_path)
12
+ # = dropdown_divider
13
+ # = dropdown_item('Exceptions', admin_exceptions_path)
14
+ #
15
+ # = button_dropdown('Actions') do
16
+ # / dropdown_items
17
+ #
18
+ # = split_button_dropdown('Edit', edit_user_path(@user)) do
19
+ # / dropdown_items
20
+ #
21
+ module Bootstrap::DropdownHelper
22
+
23
+ # should be nested within a <ul class='nav'> tag
24
+ def nav_dropdown(text)
25
+ content_tag(:li, class: 'dropdown') do
26
+ nav_dropdown_link(text) + dropdown_ul { yield }
27
+ end
28
+ end
29
+
30
+ # likely nested within button_toolbar
31
+ def button_dropdown(*args)
32
+ content_tag(:div, class: 'btn-group') do
33
+ button_dropdown_link(*args) + dropdown_ul { yield }
34
+ end
35
+ end
36
+
37
+ # likely nested within button_toolbar
38
+ def split_button_dropdown(*args)
39
+ extras = extract_extras(*args)
40
+
41
+ content_tag(:div, class: 'btn-group') do
42
+ split_button_dropdown_default(*args) + split_button_dropdown_toggle(extras) + dropdown_ul { yield }
43
+ end
44
+ end
45
+
46
+ # Must be nested under one of the *_dropdown methods:
47
+ #
48
+ # dropdown_item('Action', '/action')
49
+ # dropdown_item('Action') # href set to 'javascript:void(0)'
50
+ # dropdown_item('Action', id: 'foo') # options passed to <a> tag
51
+ #
52
+ def dropdown_item(*args)
53
+ options = args.extract_options!
54
+ text = args.shift or raise "Need text to link to"
55
+ url = args.shift || 'javascript:void(0)'
56
+
57
+ content_tag(:li) do
58
+ link_to(text, url, options)
59
+ end
60
+ end
61
+
62
+ # Produces a line to divide sections of a dropdown menu
63
+ def dropdown_divider
64
+ content_tag(:li, nil, class: 'divider')
65
+ end
66
+
67
+ private
68
+
69
+ def nav_dropdown_link(text)
70
+ content_tag(:a, class: "dropdown-toggle", data: {toggle: "dropdown"}, href: "#") do
71
+ safe_join( [ h(text), caret ], ' ' )
72
+ end
73
+ end
74
+
75
+ def button_dropdown_link(*args)
76
+ text = args.shift
77
+ classes = %w(btn dropdown-toggle) + args.map { |e| "btn-#{e}" }
78
+ content_tag(:a, class: classes, data: {toggle: "dropdown"}, href: "#") do
79
+ safe_join( [ h(text), caret ], ' ' )
80
+ end
81
+ end
82
+
83
+ def split_button_dropdown_default(*args)
84
+ button(*args)
85
+ end
86
+
87
+ def split_button_dropdown_toggle(extras)
88
+ classes = %w(btn dropdown-toggle) + extras.map { |e| "btn-#{e}" }
89
+ content_tag(:button, class: classes, data: {toggle: 'dropdown'}) do
90
+ caret
91
+ end
92
+ end
93
+
94
+ def dropdown_ul
95
+ content_tag(:ul, class: "dropdown-menu") do
96
+ yield
97
+ end
98
+ end
99
+
100
+ end
@@ -0,0 +1,19 @@
1
+ module Bootstrap::FormHelper
2
+
3
+ def submit_tag(value = "Save changes", options = {})
4
+ options = arrayify_class(options.symbolize_keys)
5
+
6
+ class_arg = Array(options.delete(:class)).map(&:to_s)
7
+ classes = []
8
+ classes << 'btn-primary' unless options.delete(:bootstrap) == false ||
9
+ class_arg.detect { |e| e.starts_with?('btn-') }
10
+ class_arg.each { |e| classes << e }
11
+ classes << 'btn' if classes.detect { |e| e.starts_with?('btn-') }
12
+ options = ensure_class(options, classes)
13
+
14
+ options[:disable_with] = "Processing ..." unless options.has_key?(:disable_with)
15
+
16
+ super(value, options)
17
+ end
18
+
19
+ end
@@ -0,0 +1,45 @@
1
+ # See: http://twitter.github.io/bootstrap/components.html#labels-badges
2
+ #
3
+ # Helper for producing Twitter Bootstrap labels. We call them stamps because #label() is
4
+ # a Rails helper method.
5
+ #
6
+ # Default label:
7
+ #
8
+ # stamp('Default')
9
+ #
10
+ # Other labels (see LABEL_TYPES):
11
+ #
12
+ # stamp('Info', :info)
13
+ #
14
+ # Options passed through to <span> tag:
15
+ #
16
+ # stamp('Warning', :warning, id: 'warn-id', class: 'more-class', my_key: 'my_value')
17
+ #
18
+ module Bootstrap::StampHelper
19
+ InvalidStampTypeError = Class.new(StandardError)
20
+
21
+ LABEL_TYPES = %w(default success warning important info inverse)
22
+
23
+ # stamp('Text')
24
+ # stamp('Text', :success) # see LABEL_TYPES
25
+ # stamp('Text', :info, id: 'my-id') # options passed thru to <span>
26
+ def stamp(*args)
27
+ text = args.shift
28
+ options = add_label_classes(*args)
29
+ content_tag(:span, text, options)
30
+ end
31
+
32
+ private
33
+
34
+ def add_label_classes(*args)
35
+ options = args.extract_options!
36
+ validate_label_types(args)
37
+ classes = ['label'] + args.map { |arg| "label-#{arg}" }
38
+ ensure_class(options, classes)
39
+ end
40
+
41
+ def validate_label_types(label_types)
42
+ label_types.each { |e| raise(InvalidStampTypeError, e.inspect) unless LABEL_TYPES.include?(e.to_s) }
43
+ end
44
+
45
+ end
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,4 @@
1
+ require "bootstrap-view-helpers/engine"
2
+
3
+ module BootstrapViewHelpers
4
+ end
@@ -0,0 +1,6 @@
1
+ module BootstrapViewHelpers
2
+ class Engine < ::Rails::Engine
3
+ config.generators.integration_tool :rspec
4
+ config.generators.test_framework :rspec
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module BootstrapViewHelpers
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :bootstrap-view-helpers do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap-view-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Steve Downey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec-rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: capybara
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: guard-rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: guard-spork
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Rails view helpers for Bootstrap
111
+ email:
112
+ - steve.downtown@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - app/helpers/bootstrap/accordion_helper.rb
118
+ - app/helpers/bootstrap/badge_helper.rb
119
+ - app/helpers/bootstrap/button_helper.rb
120
+ - app/helpers/bootstrap/common_helper.rb
121
+ - app/helpers/bootstrap/dropdown_helper.rb
122
+ - app/helpers/bootstrap/form_helper.rb
123
+ - app/helpers/bootstrap/stamp_helper.rb
124
+ - config/routes.rb
125
+ - lib/bootstrap-view-helpers/engine.rb
126
+ - lib/bootstrap-view-helpers/version.rb
127
+ - lib/bootstrap-view-helpers.rb
128
+ - lib/tasks/bootstrap-view-helpers_tasks.rake
129
+ - MIT-LICENSE
130
+ - Rakefile
131
+ - README.rdoc
132
+ homepage: https://github.com/stevedowney/bootstrap_view_helpers
133
+ licenses: []
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ none: false
140
+ requirements:
141
+ - - ! '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ segments:
145
+ - 0
146
+ hash: -3576308479683783950
147
+ required_rubygems_version: !ruby/object:Gem::Requirement
148
+ none: false
149
+ requirements:
150
+ - - ! '>='
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ segments:
154
+ - 0
155
+ hash: -3576308479683783950
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 1.8.25
159
+ signing_key:
160
+ specification_version: 3
161
+ summary: Rails view helpers for Bootstrap
162
+ test_files: []