bootstrap_rails_helper 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.md ADDED
@@ -0,0 +1,4 @@
1
+ bootstrap_rails_helper
2
+ ======================
3
+
4
+ Simple rails helper to construct bootstrap components
data/Rakefile ADDED
@@ -0,0 +1,33 @@
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
+
8
+ begin
9
+ require 'rdoc/task'
10
+ rescue LoadError
11
+ require 'rdoc/rdoc'
12
+ require 'rake/rdoctask'
13
+ RDoc::Task = Rake::RDocTask
14
+ end
15
+
16
+ RDoc::Task.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'ActAsAdmin'
19
+ rdoc.options << '--line-numbers'
20
+ rdoc.rdoc_files.include('README.rdoc')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+ # install rspec rake task
25
+ require 'rspec/core/rake_task'
26
+ RSpec::Core::RakeTask.new(:spec) do |t|
27
+ t.rspec_opts = "--format d --color"
28
+ end
29
+ task :default => :spec
30
+
31
+
32
+ Bundler::GemHelper.install_tasks
33
+
@@ -0,0 +1,116 @@
1
+ module BootstrapFormHelper
2
+
3
+ def control_group form, field, opts={}, &block
4
+ errors = opts.delete(:errors)
5
+ label = opts.delete(:label) || field_name(field)
6
+ content_tag(:div, :class=>"control-group") do
7
+ concat content_tag(:label, label, :class=>"control-label")
8
+ concat(content_tag(:div, :class=>"controls"){
9
+ if(block_given?)
10
+ concat yield(form, field, opts)
11
+ else
12
+ concat form_field(form, field, opts)
13
+ end
14
+ concat content_tag(:span, errors.join(","), :class=>"help-inline error-help") if (errors && errors.count >0)
15
+ })
16
+ end
17
+ end
18
+
19
+ def form_action form, opts, &block
20
+ content_tag(:div, :class=>"form-actions") do
21
+ if block_given?
22
+ concat yield(form, opts)
23
+ else
24
+ submit_label = opts.delete(:submit)
25
+ concat form.submit(submit_label, :class=>'btn btn-primary')
26
+
27
+ cancel = opts.delete(:cancel)
28
+ if (cancel)
29
+ label = cancel[0]
30
+ url = cancel[1]
31
+ concat content_tag(:span, "OR")
32
+ concat link_to(label, url, :class=>"cancel")
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ def form_field form, field, opts
39
+ type = opts.delete :type
40
+ raise "No type defined for #{field} field" if type.nil?
41
+
42
+ cls = opts.delete(:class)
43
+ size = opts.delete(:size)
44
+ help = opts.delete(:help)
45
+
46
+ opts = opts.merge(:class=>[cls, size].compact.join(" "))
47
+ opts = opts.merge(:title=>help, :"data-toggle"=>"tooltip", :"data-trigger"=>"focus", :"data-placement"=>"bottom") if help.present?
48
+
49
+ field_of(form, field) do |f, field|
50
+ case type
51
+ when :select
52
+ f.select(field, selection_values(opts), opts)
53
+
54
+ when :radio_button
55
+ render_radio_button(f, field, selection_values(opts))
56
+
57
+ when :check_box
58
+ render_check_box(f, field, selection_values(opts))
59
+
60
+ else
61
+ f.send(type.to_sym, field, opts)
62
+ end
63
+ end
64
+ end
65
+
66
+
67
+ def field_of form, field
68
+
69
+ nested_fields = field.to_s.split(".")
70
+ if (nested_fields.size == 2)
71
+ form.fields_for(nested_fields[0].to_sym) {|fields|
72
+ yield(fields, nested_fields[1].to_sym)
73
+ }
74
+ else
75
+ yield(form, field)
76
+ end
77
+ end
78
+
79
+
80
+
81
+ def render_fileupload(file_field, extra=nil)
82
+ extra ||= ""
83
+ render :partial=>"components/bootstrap/fileupload", :locals=>{file_field: file_field, extra:extra}
84
+ end
85
+
86
+ def render_check_box form, field, selection
87
+ check_boxes = selection.collect do |item|
88
+ content_tag(:label, :class=>"checkbox") do
89
+ concat form.check_box(field, {}, item[1], nil)
90
+ concat item[0]
91
+ end
92
+ end
93
+ check_boxes.join("\n").html_safe
94
+ end
95
+
96
+ def render_radio_button form, field, selection
97
+ radio_buttons = selection.collect do |item|
98
+ content_tag(:label, :class=>"radio") do
99
+ concat form.radio_button(field, item[1])
100
+ concat item[0]
101
+ end
102
+ end
103
+ radio_buttons.join("\n").html_safe
104
+ end
105
+
106
+ private
107
+ def selection_values opts
108
+ selection = opts.delete(:selection)
109
+ if selection.respond_to? :keys
110
+ selection.collect{|k,v| [v,k]}
111
+ else
112
+ selection.collect{|i| [i,i]}
113
+ end
114
+ end
115
+
116
+ end
@@ -0,0 +1,197 @@
1
+ module BootstrapHelper
2
+
3
+ class TabBuilder
4
+ attr_accessor :tabs
5
+
6
+ def tab name, opts={}, &block
7
+ @tabs ||= []
8
+ @tabs << {:name=>name, :opts=>opts, :proc=>block}
9
+ end
10
+
11
+ def tabs
12
+ return @tabs || []
13
+ end
14
+ end
15
+
16
+ def bootstrap_form_fields form, model, &block
17
+ BootstrapFormBuilder.render_items(form, model, self, &block)
18
+ end
19
+
20
+ # Render a standard bootstrap sidebar panel with nav-stacked, nav-pills content
21
+ #
22
+ # opts:: Options that will be passed to the yieled block
23
+ def bootstrap_sidebar opts={}
24
+ content_tag(:div, :class=>"section sidebar-nav") do
25
+ nav_class = opts.delete(:nav_class) || "nav-pills nav-stacked"
26
+ content_tag(:ul, :class=>"nav #{nav_class}") do
27
+ header = opts.delete(:header)
28
+ concat(content_tag :li, header.html_safe, :class=>"nav-header") if header.present?
29
+ yield(opts) if block_given?
30
+ end
31
+ end
32
+ end
33
+
34
+
35
+ # Render a standard fixed-top bootstrap navbar
36
+ # It will yield to a block that will output nav menus as <li> markup.
37
+ # #bootstrap_dropdown_menu will output a menu markup.
38
+ #
39
+ # opts[:brand]:: The html markups for brand section
40
+ # opts[:right]:: The html markups for the pull-right section
41
+ def bootstrap_navbar opts={}, &block
42
+ brand = opts.delete :brand || ""
43
+ right = opts.delete :right || ""
44
+
45
+ content_tag(:div, :class=>"navbar navbar-fixed-top") do
46
+ content_tag(:div, :class=>"navbar-inner") do
47
+ content_tag(:div, :class=>"container-fluid") do
48
+ concat(nav_collapse.html_safe)
49
+ concat(link_to brand.html_safe, "#", :class=>"brand")
50
+ nav = content_tag(:div, :class=>"container-fluid nav-collapse") do
51
+ concat(content_tag(:ul, :class=>"nav", &block))
52
+ concat(content_tag(:ul, right, :class=>"nav pull-right"))
53
+ end
54
+ concat(nav)
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ # Render a dropdown menu <li> markup in the bootstrap nav bar
61
+ # It will yield to a block that will output the menu items as html <li> tag
62
+ #
63
+ # menu:: The text on the menu
64
+ def bootstrap_dropdown_menu menu, cls=nil
65
+ opts = {:class=>["dropdown",cls].compact.join(" ")}
66
+ content_tag(:li, opts) do
67
+ concat link_to(%{#{menu} #{content_tag :b, "", :class=>"caret"}}.html_safe, "#", :class=>"dropdow-toggle", :"data-toggle"=>"dropdown")
68
+ concat content_tag(:ul, :class=>"dropdown-menu"){yield}
69
+ end
70
+ end
71
+
72
+ # Render a bootstrap dropdown button.
73
+ # The yielded block should render each dropdown item as a <li>
74
+ #
75
+ # content:: The html content on the button
76
+ # opts[:class]:: The dropdown button class, default is "btn"
77
+ def bootstrap_dropdown_button content, opts={}
78
+ btn_class = (opts||{}).delete(:class) || "btn"
79
+ content_tag(:div, :class=>"btn-group") do
80
+ concat link_to(%{#{content} #{content_tag :span, "", :class=>"caret"}}.html_safe, "#", :class=>"#{btn_class} dropdown-toggle", :"data-toggle"=>"dropdown")
81
+ concat content_tag(:ul, :class=>"dropdown-menu"){yield if block_given?}
82
+ end
83
+ end
84
+
85
+
86
+ # Render a bootstrap button group
87
+ # data:: The data content in the button group. If it is an emuratable object, this method
88
+ # will yield the block with each item in the collection.
89
+ def bootstrap_btn_group data=nil, &block
90
+ content_tag(:div, :class=>"btn-group") do
91
+ if data.respond_to? :each
92
+ data.each(&block)
93
+ else
94
+ yield data
95
+ end
96
+ end
97
+ end
98
+
99
+
100
+ # Render standard fav link icons for different devices.
101
+ # It normally embeded in the <header>
102
+ def bootstrap_fav_links
103
+ render "components/bootstrap/fav_links"
104
+ end
105
+
106
+ # Render a bootstrap modal panel.
107
+ # It will yield to a block that should ouput the modal-body content
108
+ #
109
+ # options[:id]:: Dialog id
110
+ # options[:title]:: Dialog title, default "Title"
111
+ # options[:actions]:: Html markups for modal-footer section, default is a Cancel button
112
+ # options[:remote_path]:: If presented, the modal div will have 'data-remote-path' attribute set to this value
113
+ def bootstrap_modal options
114
+ id = options.delete(:id)
115
+ remote_path = options.delete(:remote_path)
116
+ title=options.delete(:title) || "Title"
117
+ actions = options.delete(:actions) || [link_to("Cancel","#", :class=>"btn ", :"data-dismiss"=>"modal", :"aria-hidden"=>"true")]
118
+ cls = options.delete(:class) || ""
119
+
120
+ modal_options = {:class=>"modal hide fade #{cls}"}
121
+ modal_options[:id] = id if id.present?
122
+ modal_options[:"data-remote-path"] = remote_path if remote_path.present?
123
+
124
+ content_tag(:div, modal_options) do
125
+ concat(content_tag(:div, :class=>"modal-header"){
126
+ concat content_tag(:button,"&times;".html_safe, :type=>"button", :class=>"close", :"data-dismiss"=>"modal", :"aria-hidden"=>"true")
127
+ concat content_tag(:h3, title)
128
+ })
129
+ concat(content_tag(:div, :class=>"modal-body"){yield if block_given?})
130
+ concat content_tag(:div, actions.join("\n").html_safe, :class=>"modal-footer")
131
+ end
132
+ end
133
+
134
+
135
+ # Render a bootstrap tab pane
136
+ # Example code:
137
+ # <tt>
138
+ # bootstrap_tabs(tab_pos: "tabs-top") do |t|
139
+ # t.tab "Tab1", :id=>"id_of_tab1" do
140
+ # ...
141
+ # end
142
+ # t.tab "Tab2", :id=>"id_of_tab2" do
143
+ # ...
144
+ # end
145
+ # end
146
+ # </tt>
147
+ #
148
+ # Parameters for the tab panel
149
+ # opts[:tab_pos]:: The bootstrap class name for the tab position, like "tab-top".
150
+ # Default value is "tab-top"
151
+ #
152
+ # Parameters for each tab
153
+ # name:: The tab name
154
+ # opts[:id]:: The tab id, default value is the tab name
155
+ #
156
+ def bootstrap_tab_pane opts={}
157
+ tab_builder = TabBuilder.new
158
+ yield(tab_builder) if block_given?
159
+
160
+ tab_pos = opts.delete(:tab_pos) || "tabs-top"
161
+ tab_options = tab_builder.tabs.collect do |item|
162
+ opts = item[:opts] || {}
163
+ name = item[:name]
164
+ id = opts.delete(:id) || name
165
+ {:id=>id, :link=>link_to(name, "##{id}", :"data-toggle"=>"tab"), :content_proc=>item[:proc], :opts=>opts}
166
+ end
167
+
168
+ content_tag :div, :class=>"tabbable #{tab_pos}" do
169
+ concat(content_tag(:ul, :class=>"nav nav-tabs"){
170
+ tab_options.each_with_index do |item, index|
171
+ cls = "active" if index==0
172
+ concat content_tag(:li, item[:link], :class=>cls)
173
+ end
174
+ })
175
+
176
+ concat(content_tag(:div, :class=>"tab-content"){
177
+ tab_options.each_with_index do |item, index|
178
+ opts = {:id=>item[:id], :class=>"tab-pane #{'active' if index==0}"}.merge(item[:opts])
179
+ concat(content_tag(:div, opts){
180
+ proc = item[:content_proc]
181
+ proc.call if proc
182
+ })
183
+ end
184
+ })
185
+
186
+ end
187
+ end
188
+
189
+
190
+ private
191
+ def nav_collapse
192
+ content_tag(:a, :class=>"btn btn-navbar", :"data-target"=>".nav-collapse", :"data-toggle"=>"collapse") do
193
+ (1..3).each{|i| concat(content_tag :span,"", :class=>"icon-bar")}
194
+ end
195
+ end
196
+
197
+ end
@@ -0,0 +1,3 @@
1
+ module BoostrapRailsHelper
2
+ require 'bootstrap_rails_helper/engine' if defined?(Rails)
3
+ end
@@ -0,0 +1,4 @@
1
+ module BootstrapRailsHelper
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module BootstrapRailsHelper
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap_rails_helper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hu Hao
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-15 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: 3.2.3
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: 3.2.3
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
+ description: Simple rails helper to construct bootstrap components
47
+ email:
48
+ - huhao98@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - app/helpers/bootstrap_form_helper.rb
54
+ - app/helpers/bootstrap_helper.rb
55
+ - app/views/components/bootstrap/_fileupload.html.erb
56
+ - lib/bootstrap_rails_helper/engine.rb
57
+ - lib/bootstrap_rails_helper/version.rb
58
+ - lib/bootstrap_rails_helper.rb
59
+ - MIT-LICENSE
60
+ - Rakefile
61
+ - README.md
62
+ homepage: http://brainet.github.com
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ segments:
75
+ - 0
76
+ hash: -3744927139214705583
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ segments:
84
+ - 0
85
+ hash: -3744927139214705583
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 1.8.25
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Simple rails helper to construct bootstrap components
92
+ test_files: []