bootstrap-view-helpers 0.0.5 → 0.0.6

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.
data/README.md CHANGED
@@ -7,6 +7,8 @@ Includes support for:
7
7
  * navigation
8
8
  * nav bar
9
9
  * nav list
10
+ * icons
11
+ * icons with text
10
12
  * buttons
11
13
  * `<button>`
12
14
  * `<a>` styled as button
@@ -19,7 +21,8 @@ Includes support for:
19
21
  * split-button dropdowns
20
22
  * accordion
21
23
  * form helpers
22
- * submit_tag_button
24
+ * submit_tag_button
25
+ * cancel_tag_button
23
26
 
24
27
  ## Note
25
28
 
@@ -79,6 +82,30 @@ Complete [API documentation](http://rubydoc.info/gems/bootstrap-view-helpers/fra
79
82
  <% end %>
80
83
  ```
81
84
 
85
+ ### Icons
86
+
87
+ See: http://twitter.github.io/bootstrap/base-css.html#icons
88
+
89
+ Icons
90
+ ```ruby
91
+ icon(:search)
92
+ icon(:search, :white)
93
+ icon(:search, :white, id: 'my-id')
94
+ ```
95
+
96
+ Icons with Text
97
+ ```ruby
98
+ icon(:search, 'Search')
99
+ icon(:remove, :white, 'Delete')
100
+ icon(:ok, 'Save', id: 'my-id')
101
+ ```
102
+
103
+ Line up text when some have icons and some don't
104
+ ```ruby
105
+ icon(:search, 'With icon') # shows search icon
106
+ icon(:blank, 'No icon') # no icon, text will aligin if icons are stacked
107
+ ```
108
+
82
109
  ### Buttons
83
110
 
84
111
  ```ruby
@@ -194,4 +221,14 @@ submit_button_tag('Big', :large)
194
221
  submit_button_tag('With Options', :small, :info, id: 'my-id')
195
222
  ```
196
223
 
224
+ #### cancel_tag_button
197
225
 
226
+ Convenience method for standard "Cancel" button. `<a>` tag styled as Bootstrap button.
227
+
228
+ ```ruby
229
+ cancel_button_tag(url: '/') # => <a href="/" class="btn">Cancel</a>
230
+ cancel_button_tag('Go Back', url: '/')
231
+ cancel_button_tag(:info, url: '/')
232
+ cancel_button_tag(:large, url: '/')
233
+ cancel_button_tag('Return', :small, :warning, url: '/', id: 'my-id')
234
+ ```
@@ -1,3 +1,4 @@
1
+ # @private
1
2
  class BootstrapViewHelpersController < ApplicationController
2
3
 
3
4
  def index
@@ -1,33 +1,40 @@
1
- # Utililty methods used by Bootstrap::*Heler classes
1
+ # Utililty methods used by Bootstrap::*Helper classes
2
2
  module Bootstrap::CommonHelper
3
3
  ArgumentError = Class.new(::ArgumentError)
4
4
 
5
5
  # Returns a new Hash with:
6
6
  # * keys converted to Symbols
7
- # * the +:class+ key has it value converted to an Array of String
7
+ # * the +:class+ key has its value converted to an Array of String
8
+ # @example
9
+ # canonicalize_options("id" => "ID", "class" => "CLASS") # => {:id=>"ID", :class=>["CLASS"]}
10
+ # canonicalize_options(:class => 'one two') # => {:class=>["one", "two"]}
11
+ # canonicalize_options("class" => [:one, 2]) # => {:class=>["one", "2"]}
12
+ # @param [Hash] hash typically an +options+ param to a method call
8
13
  # @raise [ArgumentError] if _hash_ is not a Hash
9
14
  # @return [Hash]
10
15
  def canonicalize_options(hash)
11
16
  raise ArgumentError.new("expected a Hash, got #{hash.inspect}") unless hash.is_a?(Hash)
12
17
 
13
18
  hash.symbolize_keys.tap do |h|
14
- h[:class] = arrayify_class_and_stringify_elements(h[:class])
19
+ h[:class] = arrayify_and_stringify_elements(h[:class])
15
20
  end
16
21
  end
17
22
 
18
- # Returns a new Array of String
23
+ # Returns a new Array of String from _arg_.
19
24
  # @example
20
- # arrayify_class_and_stringify_elements(nil) #=> []
21
- # arrayify_class_and_stringify_elements('foo') #=> ["foo"]
22
- # arrayify_class_and_stringify_elements([:foo, 'bar']) #=> ["foo", "bar"]
25
+ # arrayify_and_stringify_elements(nil) #=> []
26
+ # arrayify_and_stringify_elements('foo') #=> ["foo"]
27
+ # arrayify_and_stringify_elements('foo bar') #=> ["foo", "bar"]
28
+ # arrayify_and_stringify_elements([:foo, 'bar']) #=> ["foo", "bar"]
29
+ # @param [String, Array] arg
23
30
  # @return [Array of String]
24
- def arrayify_class_and_stringify_elements(klass)
25
- return false if klass == false
31
+ def arrayify_and_stringify_elements(arg)
32
+ return false if arg == false
26
33
 
27
34
  case
28
- when klass.blank? then []
29
- when klass.is_a?(Array) then klass
30
- else klass.to_s.strip.split(/\s/)
35
+ when arg.blank? then []
36
+ when arg.is_a?(Array) then arg
37
+ else arg.to_s.strip.split(/\s/)
31
38
  end.map(&:to_s)
32
39
  end
33
40
 
@@ -42,11 +49,8 @@ module Bootstrap::CommonHelper
42
49
  content_tag(:span, nil, options)
43
50
  end
44
51
 
45
- # Returns new Hash where :class value includes _klasses_.
52
+ # Returns new (canonicalized) Hash where :class value includes _klasses_.
46
53
  #
47
- # Assumes _hash_ has a key :class (Symbol) whose value is an Array of String.
48
- # {Bootstrap::CommonHelper#canonicalize_options} will return such a Hash.
49
- #
50
54
  # @example
51
55
  # ensure_class({class: []}, 'foo') #=> {class: 'foo'}
52
56
  # ensure_class({class: ['bar'], id: 'my-id'}, ['foo', 'foo2']) #=> {:class=>["bar", "foo", "foo2"], :id=>"my-id"}
@@ -54,6 +58,8 @@ module Bootstrap::CommonHelper
54
58
  # @param [String, Array] klasses one or more classes to add to the +:class+ key of _hash_
55
59
  # @return [Hash]
56
60
  def ensure_class(hash, klasses)
61
+ canonicalize_options(hash)
62
+
57
63
  hash.dup.tap do |h|
58
64
  Array(klasses).map(&:to_s).each do |k|
59
65
  h[:class] << k unless h[:class].include?(k)
@@ -1,37 +1,60 @@
1
1
  # Rails helper methods associated with forms for Bootstrap.
2
2
  #
3
- # @example +#submit_button_tag()+ instead of +#submit_button()+
4
- # submit_button_tag
5
- # submit_button_tag('Save')
6
- # submit_button_tag('Delete', :danger)
7
- # submit_button_tag('Big', :large)
8
- # submit_button_tag('With Options', :small, :info, id: 'my-id')
3
+ # @example Bootstrap form-actions <div>
4
+ # <div class='form-actions'>
5
+ # <%= submit_button_tag %>
6
+ # <%= cancel_button_tag %>
7
+ # </div>
8
+ #
9
9
  module Bootstrap::FormHelper
10
+ ArgumentError = Class.new(StandardError)
10
11
  InvalidButtonModifierError = Class.new(StandardError)
11
12
 
12
- # Returns <input> similar to +#submit_tag()+ but:
13
+ # Convenience method for standard "Cancel" button.
14
+ #
15
+ # The +text, type,+ and +size+ arguments are all optional.
16
+ #
17
+ # Has same semantics as calls to {Bootstrap::ButtonHelper#button} except:
18
+ # * text defaults to "Cancel"
19
+ # * +:url+ option is required
20
+ # @example
21
+ # cancel_button_tag(url: '/')
22
+ # cancel_button_tag('Go Back', url: '/')
23
+ # cancel_button_tag(:info, url: '/')
24
+ # cancel_button_tag(:large, url: '/')
25
+ # cancel_button_tag('Return', :small, :warning, url: '/', id: 'my-id')
26
+ # @overload cancel_button_tag(text, type, size, options={})
27
+ # @param [String] text text of link
28
+ # @param [Symbol] type type of button
29
+ # @param [Symbol] size size of button
30
+ # @param [Hash] options All keys except +:url+ become html attributes of the <a> tag
31
+ # @option options [String] :url required
32
+ # @return [String] <a> tag styled as Bootstrap button
33
+ def cancel_button_tag(*args)
34
+ options = canonicalize_options(args.extract_options!)
35
+ raise(ArgumentError, "must pass a :url option") unless options.has_key?(:url)
36
+ options = ensure_class(options, 'btn')
37
+
38
+ args.map!(&:to_s)
39
+ args.unshift("Cancel") if args.all? { |e| ::Bootstrap::ButtonHelper::BUTTON_ALL.include?(e) }
40
+
41
+ button(*args, options)
42
+ end
43
+
44
+ # Returns <input> similar to +#submit_tag()+ but: x
13
45
  # * styled like a Bootstrap button, type :primary
14
46
  # * has +:disable_with+ set to "Processing ..."
15
47
  # See {Bootstrap::ButtonHelper} for documentation on button type and size
16
- # @overload submit_button_tag(options={})
17
- # @param [Hash] options all options except +:disable_with+ become html attributes for <input> tag
18
- # @option options [String, false] :disable_with either override or turn off the disabling of the button
19
- # @overload submit_button_tag(text, options={})
20
- # @param [String] text value of <input>
21
- # @param [Hash] options see +options+ param in first method signature
22
- # @overload submit_button_tag(text, type, options={})
23
- # @param [String] text value of <input>
24
- # @param [String, Symbol] type type of button
25
- # @param [Hash] options see +options+ param in first method signature
26
- # @overload submit_button_tag(text, size, options={})
27
- # @param [String] text value of <input>
28
- # @param [String, Symbol] size size of button
29
- # @param [Hash] options see +options+ param in first method signature
48
+ # submit_button_tag('Save')
49
+ # submit_button_tag('Delete', :danger)
50
+ # submit_button_tag('Big', :large)
51
+ # submit_button_tag('With Options', :small, :info, id: 'my-id')
30
52
  # @overload submit_button_tag(text, type, size, options={})
31
53
  # @param [String] text value of <input>
32
54
  # @param [String, Symbol] type type of button
33
55
  # @param [String, Symbol] size size of button
34
- # @param [Hash] options see +options+ param in first method signature
56
+ # @param [Hash] options all options except +:disable_with+ become html attributes for <input> tag
57
+ # @option options [String, false] :disable_with either override or turn off the disabling of the button
35
58
  # @return [String]
36
59
  def submit_button_tag(*args)
37
60
  options = canonicalize_options(args.extract_options!)
@@ -0,0 +1,30 @@
1
+ # Helper methods for Bootstrap icons.
2
+ #
3
+ # See: http://twitter.github.io/bootstrap/base-css.html#icons
4
+ #
5
+ # @example Icons
6
+ # icon(:search)
7
+ # icon(:search, :white)
8
+ # icon(:search, :white, id: 'my-id')
9
+ #
10
+ # @example Icons with text
11
+ # icon(:search, 'Search')
12
+ # icon(:remove, :white, 'Delete')
13
+ # icon(:ok, 'Save', id: 'my-id')
14
+ #
15
+ # @example Text without icon
16
+ # # so text lines up when you have text but no icon
17
+ # icon(:blank, 'No icon')
18
+ module Bootstrap::IconHelper
19
+
20
+ # Returns a Bootstrap icon glyph.
21
+ #
22
+ # Optionally returns text with icon.
23
+ #
24
+ # See class documentation (above) for examples.
25
+ # @return [String]
26
+ def icon(*args)
27
+ ::Bootstrap::IconRenderer.new(self, *args).html
28
+ end
29
+
30
+ end
@@ -0,0 +1,60 @@
1
+ # @private
2
+ class Bootstrap::IconRenderer
3
+ ArgumentError = Class.new(StandardError)
4
+
5
+ delegate :canonicalize_options, :content_tag, :ensure_class, :h, to: :template
6
+ attr_accessor :template, :options, :args, :text
7
+
8
+ def initialize(template, *args)
9
+ self.template = template
10
+ process_arguments(args)
11
+ end
12
+
13
+ def html
14
+ icon_tag.tap do |tag|
15
+ tag << text if text.present?
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def icon_tag
22
+ content_tag(:i, nil, options)
23
+ end
24
+
25
+ def process_arguments(args)
26
+ self.options = canonicalize_options(args.extract_options!)
27
+ self.args = args.map { |e| e.to_s }
28
+
29
+ process_icon_type
30
+ process_icon_white
31
+ process_text
32
+ end
33
+
34
+ def process_icon_type
35
+ icon_type = args.shift.presence or raise(ArgumentError, "must pass an icon type")
36
+ icon_class = icon_type == 'blank' ? 'icon-search' : "icon-#{icon_type}"
37
+ self.options = ensure_class(self.options, icon_class)
38
+
39
+ if icon_type == 'blank'
40
+ options[:style] = [options[:style], 'opacity: 0'].map(&:presence).compact.join('; ')
41
+ end
42
+ end
43
+
44
+ def process_icon_white
45
+ if args.delete('white')
46
+ self.options = ensure_class(options, "icon-white")
47
+ end
48
+ end
49
+
50
+ def process_text
51
+ self.text = if args.size == 0
52
+ nil
53
+ elsif args.size == 1
54
+ h(" #{args.first}")
55
+ else
56
+ raise ArgumentError, 'too many arguments'
57
+ end
58
+ end
59
+
60
+ end
@@ -1,5 +1,20 @@
1
1
  <h1>Examples</h1>
2
2
 
3
+ <p>
4
+ <%= icon('search') %>
5
+ <%= stamp(icon('search', :white))%>
6
+ <%= stamp(icon('search', 'white', 'Search'))%>
7
+ </p>
8
+ <p>
9
+ <%= stamp(icon(:remove, :white, 'Delete'), :important)%>
10
+ <br />
11
+ <%= stamp(icon(:spacer, :white, 'Spacer'), :important)%>
12
+ </p>
13
+ <p>
14
+ <%= stamp(icon(:remove, 'Delete'), :important)%>
15
+ <br />
16
+ <%= stamp(icon(:spacer, 'Spacer'), :important)%>
17
+ </p>
3
18
  <%= split_button_dropdown('Default', url: '/default') do %>
4
19
  <%= dropdown_item('Action 1', '/action1') %>
5
20
  <%= dropdown_item('Action 2', '/action2') %>
@@ -1,4 +1,5 @@
1
1
  require "bootstrap-view-helpers/engine"
2
2
 
3
+ # @private
3
4
  module BootstrapViewHelpers
4
5
  end
@@ -1,3 +1,3 @@
1
1
  module BootstrapViewHelpers
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bootstrap-view-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-18 00:00:00.000000000 Z
12
+ date: 2013-05-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -201,6 +201,8 @@ files:
201
201
  - app/helpers/bootstrap/common_helper.rb
202
202
  - app/helpers/bootstrap/dropdown_helper.rb
203
203
  - app/helpers/bootstrap/form_helper.rb
204
+ - app/helpers/bootstrap/icon_helper.rb
205
+ - app/helpers/bootstrap/icon_renderer.rb
204
206
  - app/helpers/bootstrap/nav_helper.rb
205
207
  - app/helpers/bootstrap/stamp_helper.rb
206
208
  - app/views/bootstrap_view_helpers/index.html.erb