foundation_rails_helper 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8778cbf569b219f1dc900b76ccc9dbce91f14352
4
- data.tar.gz: 2f04eeda4780f7ea097fc6489b522089c3828799
3
+ metadata.gz: dbac7ada4277e720024750bf07c703d5d1404dbb
4
+ data.tar.gz: 040f0a887dbd697f63400aa20584eac8f16c67c9
5
5
  SHA512:
6
- metadata.gz: 95f6960132fbfc01eba7fc1490897c3d67bbf9ea6fb678ef8ac478bc9a75421be821bb98d1f4e224ac5d3ec8bda5ae912d7bc596033c23b67126d37f7279d150
7
- data.tar.gz: 44739788edf92dd99efdf494081aee4a5930d367e79750a987d9d336b8552351de84d464ea11470eb9fb1a620d0d95f7b65c7f0b2a74ed2cc78c0258403100c8
6
+ metadata.gz: eddef716126110058f5164799bcb4c3e8e3bc1818b08f02f17d5922fbd4ed4083361aa2d1b2659d675031cd817d28dea47ffff768ba5d208510c693c1053e073
7
+ data.tar.gz: 0e189759a3e85f514d065b2c05655198b34e45d6e42f8e416bead9fb010e4d21d4880025cc15b220c0a92f71dcc0d42ae5366a6228f61ed04410d0d1dbb2e906
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ test/version_tmp
17
17
  tmp
18
18
  .rbenv-gemsets
19
19
  vendor/bundle/*
20
+ .ruby-version
data/.travis.yml CHANGED
@@ -2,3 +2,4 @@ rvm:
2
2
  - 1.9.3
3
3
  - 2.1.0
4
4
  - 2.2.0
5
+ sudo: false
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## Version 1.1.0
2
+ * Form Helper: [Prefix and
3
+ Postfix](http://foundation.zurb.com/sites/docs/v/5.5.3/components/forms.html#pre-postfix-labels-amp-actions) are now supported (PR #104 thanks djkz)
4
+ * Flash Helper: a list of keys to be ignored can be specified - e.g. keys which are only used internally to pass data, not user-viewable messages(fix for #98)
5
+ * FIX: Hints are added as span elements (PR #96 thanks collimarco)
6
+ * FIX: Labels and fields don't have empty class attributes or repeated error classes
7
+ (thanks collimarco)
8
+ * FIX: Radio buttons don't have the `label="false"` on them when `label:
9
+ false` is set (PR #107 thanks frenkel)
10
+
1
11
  ## Version 1.0.0
2
12
 
3
13
  * Released Feb 03rd 2015
data/README.md CHANGED
@@ -110,11 +110,11 @@ generates:
110
110
  <input id="user_name" name="user[name]" type="text">
111
111
  ```
112
112
 
113
- Prevent the generation of a label:
114
-
113
+ Preventing the generation of labels can be accomplished two ways. To disable on a form element:
115
114
  ```ruby
116
115
  f.text_field :name, label: false
117
116
  ```
117
+ For all form elements, add the option: `auto_labels: false` to the form helper.
118
118
 
119
119
  Change the label text and add a class on the label:
120
120
 
@@ -170,18 +170,55 @@ The class attribute of the 'small' element will mirror the class attribute of th
170
170
 
171
171
  If the `html_safe_errors: true` option is specified on a field, then any HTML you may have embedded in a custom error string will be displayed with the html_safe option.
172
172
 
173
+ ### Prefix and Postfix
174
+ Simple prefix and postfix span elements can be added beside inputs.
175
+ ```ruby
176
+ f.text_field :name, prefix { value: 'foo' small: 2, large: 3 }
177
+ ```
178
+ generates
179
+ ```html
180
+ <div class="row collapse">
181
+ <div class="small-2 large-3 columns">
182
+ <span class="prefix">foo</span>
183
+ </div>
184
+ <div class="small-10 large-9 columns">
185
+ <input type="text" name="user[name]" id="user_name">
186
+ </div>
187
+ </div>
188
+ ```
189
+
190
+
173
191
  ## Configuration
174
- Add an initializer file to your Rails app: *config/initializers/foundation_rails_helper.rb*. See below for current options.
192
+ Add an initializer file to your Rails app: *config/initializers/foundation_rails_helper.rb*
193
+ containing the following block:
175
194
 
176
- ### Submit Button Class
177
- To use a different class for the [submit button](https://github.com/sgruhier/foundation_rails_helper#submit-button) used in `form_for`, add a config named **button_class**. Please note, the button class can still be overridden by an options hash.
178
195
  ```ruby
179
196
  FoundationRailsHelper.configure do |config|
180
- # Default: 'small radius success button'
181
- config.button_class = 'large secondary button'
197
+ # your options here
182
198
  end
183
199
  ```
184
200
 
201
+ Currently supported options:
202
+
203
+ ### Submit Button Class
204
+ To use a different class for the [submit button](https://github.com/sgruhier/foundation_rails_helper#submit-button) used in `form_for`, add a config named **button_class**:
205
+ ```ruby
206
+ # Default: 'small radius success button'
207
+ config.button_class = 'large secondary button'
208
+ ```
209
+
210
+ Please note, the button class can still be overridden by an options hash.
211
+
212
+ ### Ignored Flash Keys
213
+ The flash helper assumes all flash entries are user-viewable messages.
214
+ To exclude flash entries which are used for storing state
215
+ (e.g. [Devise's `:timedout` flash](https://github.com/plataformatec/devise/issues/1777))
216
+ you can specify a blacklist of keys to ignore with the **ignored_flash_keys** config option:
217
+ ```ruby
218
+ # Default: []
219
+ config.ignored_flash_keys = [:timedout]
220
+ ```
221
+
185
222
  ## Contributing
186
223
 
187
224
  See the [CONTRIBUTING](CONTRIBUTING.md) file.
@@ -16,12 +16,13 @@ Gem::Specification.new do |gem|
16
16
  gem.version = FoundationRailsHelper::VERSION
17
17
  gem.license = 'MIT'
18
18
 
19
- gem.add_dependency 'railties', '~> 4.1', '>= 4.1.1'
20
- gem.add_dependency 'actionpack', '~> 4.1', '>= 4.1.1'
21
- gem.add_dependency 'activemodel', '~> 4.1', '>= 4.1.1'
22
- gem.add_dependency 'activesupport', '~> 4.1', '>= 4.1.1'
19
+ gem.add_dependency 'railties', '~> 4.1'
20
+ gem.add_dependency 'actionpack', '~> 4.1'
21
+ gem.add_dependency 'activemodel', '~> 4.1'
22
+ gem.add_dependency 'activesupport', '~> 4.1'
23
23
  gem.add_dependency 'tzinfo', '~> 1.2', '>= 1.2.2'
24
24
 
25
25
  gem.add_development_dependency 'rspec-rails', '~> 3.1', '>= 3.1.0'
26
+ gem.add_development_dependency 'mime-types', '~> 2'
26
27
  gem.add_development_dependency 'capybara', '~> 2.4', '>= 2.4.3'
27
28
  end
@@ -17,9 +17,11 @@ module FoundationRailsHelper
17
17
 
18
18
  class Configuration
19
19
  attr_accessor :button_class
20
+ attr_accessor :ignored_flash_keys
20
21
 
21
22
  def initialize
22
23
  @button_class = 'small radius success button'
24
+ @ignored_flash_keys = []
23
25
  end
24
26
  end
25
27
  end
@@ -17,12 +17,29 @@ module FoundationRailsHelper
17
17
  }
18
18
  def display_flash_messages(key_matching = {})
19
19
  key_matching = DEFAULT_KEY_MATCHING.merge(key_matching)
20
+ key_matching.default = :standard
20
21
 
21
- flash.inject "" do |message, (key, value)|
22
- message += content_tag :div, :data => { :alert => "" }, :class => "alert-box #{key_matching[key.to_sym] || :standard}" do
23
- (value + link_to("&times;".html_safe, "#", :class => :close)).html_safe
22
+ capture do
23
+ flash.each do |key, value|
24
+ next if FoundationRailsHelper.configuration.ignored_flash_keys.include? key.to_sym
25
+ alert_class = key_matching[key.to_sym]
26
+ concat alert_box(value, alert_class)
24
27
  end
25
- end.html_safe
28
+ end
26
29
  end
30
+
31
+ private
32
+
33
+ def alert_box(value, alert_class)
34
+ content_tag :div, :data => { :alert => "" }, :class => "alert-box #{alert_class}" do
35
+ concat value
36
+ concat close_link
37
+ end
38
+ end
39
+
40
+ def close_link
41
+ link_to("&times;".html_safe, "#", :class => :close)
42
+ end
43
+
27
44
  end
28
45
  end
@@ -5,23 +5,27 @@ module FoundationRailsHelper
5
5
  include ActionView::Helpers::TagHelper
6
6
  %w(file_field email_field text_field text_area telephone_field phone_field
7
7
  url_field number_field date_field datetime_field datetime_local_field
8
- month_field week_field time_field range_field search_field color_field ).each do |method_name|
8
+ month_field week_field time_field range_field search_field color_field)
9
+ .each do |method_name|
9
10
  define_method(method_name) do |*args|
10
11
  attribute = args[0]
11
12
  options = args[1] || {}
12
- field(attribute, options) do |options|
13
- super(attribute, options)
13
+ field(attribute, options) do |opts|
14
+ super(attribute, opts)
14
15
  end
15
16
  end
16
17
  end
17
18
 
18
19
  def label(attribute, text = nil, options = {})
19
- options[:class] ||= ""
20
- options[:class] += " error" if has_error?(attribute)
21
- super(attribute, (text || "").html_safe, options)
20
+ if has_error?(attribute)
21
+ options[:class] ||= ''
22
+ options[:class] += ' error'
23
+ end
24
+
25
+ super(attribute, (text || '').html_safe, options)
22
26
  end
23
27
 
24
- def check_box(attribute, options = {}, checked_value = "1", unchecked_value = "0")
28
+ def check_box(attribute, options = {}, checked_value = '1', unchecked_value = '0')
25
29
  custom_label(attribute, options[:label], options[:label_options]) do
26
30
  options.delete(:label)
27
31
  options.delete(:label_options)
@@ -32,120 +36,204 @@ module FoundationRailsHelper
32
36
  def radio_button(attribute, tag_value, options = {})
33
37
  options[:label_options] ||= {}
34
38
  label_options = options.delete(:label_options).merge!(value: tag_value)
35
- unless options[:label] == false
36
- l = label(attribute, options.delete(:label), label_options)
39
+ label_text = options.delete(:label)
40
+ unless label_text == false
41
+ l = label(attribute, label_text, label_options)
37
42
  end
38
- r = @template.radio_button(@object_name, attribute, tag_value, objectify_options(options))
43
+ r = @template.radio_button(@object_name, attribute, tag_value,
44
+ objectify_options(options))
39
45
 
40
46
  "#{r}#{l}".html_safe
41
47
  end
42
48
 
43
49
  def password_field(attribute, options = {})
44
- field attribute, options do |options|
45
- super(attribute, options.merge(:autocomplete => :off))
50
+ field attribute, options do |opts|
51
+ super(attribute, opts.merge(:autocomplete => :off))
46
52
  end
47
53
  end
48
54
 
49
55
  def datetime_select(attribute, options = {}, html_options = {})
50
- field attribute, options, html_options do |html_options|
51
- super(attribute, options, html_options.merge(:autocomplete => :off))
56
+ field attribute, options, html_options do |html_opts|
57
+ super(attribute, options, html_opts.merge(:autocomplete => :off))
52
58
  end
53
59
  end
54
60
 
55
61
  def date_select(attribute, options = {}, html_options = {})
56
- field attribute, options, html_options do |html_options|
57
- super(attribute, options, html_options.merge(:autocomplete => :off))
62
+ field attribute, options, html_options do |html_opts|
63
+ super(attribute, options, html_opts.merge(:autocomplete => :off))
58
64
  end
59
65
  end
60
66
 
61
67
  def time_zone_select(attribute, priorities = nil, options = {}, html_options = {})
62
- field attribute, options, html_options do |html_options|
63
- super(attribute, priorities, options, html_options.merge(:autocomplete => :off))
68
+ field attribute, options, html_options do |html_opts|
69
+ super(attribute, priorities, options,
70
+ html_opts.merge(:autocomplete => :off))
64
71
  end
65
72
  end
66
73
 
67
74
  def select(attribute, choices, options = {}, html_options = {})
68
- field attribute, options, html_options do |html_options|
75
+ field attribute, options, html_options do |html_opts|
69
76
  html_options[:autocomplete] ||= :off
70
- super(attribute, choices, options, html_options)
77
+ super(attribute, choices, options, html_opts)
71
78
  end
72
79
  end
73
80
 
74
81
  def collection_select(attribute, collection, value_method, text_method, options = {}, html_options = {})
75
- field attribute, options, html_options do |html_options|
82
+ field attribute, options, html_options do |html_opts|
76
83
  html_options[:autocomplete] ||= :off
77
- super(attribute, collection, value_method, text_method, options, html_options)
84
+ super(attribute, collection, value_method, text_method, options,
85
+ html_opts)
78
86
  end
79
87
  end
80
88
 
81
89
  def grouped_collection_select(attribute, collection, group_method, group_label_method, option_key_method, option_value_method, options = {}, html_options = {})
82
- field attribute, options, html_options do |html_options|
90
+ field attribute, options, html_options do |html_opts|
83
91
  html_options[:autocomplete] ||= :off
84
- super(attribute, collection, group_method, group_label_method, option_key_method, option_value_method, options, html_options)
92
+ super(attribute, collection, group_method, group_label_method,
93
+ option_key_method, option_value_method, options, html_opts)
85
94
  end
86
95
  end
87
96
 
88
97
  def autocomplete(attribute, url, options = {})
89
- field attribute, options do |options|
90
- autocomplete_field(attribute, url, options.merge(:update_elements => options[:update_elements],
91
- :min_length => 0,
92
- :value => object.send(attribute)))
98
+ field attribute, options do |opts|
99
+ opts.merge!(:update_elements => opts[:update_elements],
100
+ :min_length => 0, :value => object.send(attribute))
101
+ autocomplete_field(attribute, url, opts)
93
102
  end
94
103
  end
95
104
 
96
- def submit(value=nil, options={})
105
+ def submit(value = nil, options = {})
97
106
  options[:class] ||= FoundationRailsHelper.configuration.button_class
98
107
  super(value, options)
99
108
  end
100
109
 
101
110
  private
111
+
102
112
  def has_error?(attribute)
103
113
  object.respond_to?(:errors) && !object.errors[attribute].blank?
104
114
  end
105
115
 
106
116
  def error_for(attribute, options = {})
107
- class_name = "error"
117
+ class_name = 'error'
108
118
  class_name += " #{options[:class]}" if options[:class]
109
- if has_error?(attribute)
110
- error_messages = object.errors[attribute].join(', ')
111
- error_messages = error_messages.html_safe if options[:html_safe_errors]
112
- content_tag(:small, error_messages, :class => class_name)
113
- end
119
+
120
+ return unless has_error?(attribute)
121
+
122
+ error_messages = object.errors[attribute].join(', ')
123
+ error_messages = error_messages.html_safe if options[:html_safe_errors]
124
+ content_tag(:small, error_messages, :class => class_name)
114
125
  end
115
126
 
116
127
  def custom_label(attribute, text, options, &block)
117
- return block_given? ? block.call.html_safe : "".html_safe if text == false
128
+ return block_given? ? block.call.html_safe : ''.html_safe if text == false
118
129
  if text.nil? || text == true
119
- text = if object.class.respond_to?(:human_attribute_name)
120
- object.class.human_attribute_name(attribute)
121
- else
122
- attribute.to_s.humanize
123
- end
130
+ text =
131
+ if object.class.respond_to?(:human_attribute_name)
132
+ object.class.human_attribute_name(attribute)
133
+ else
134
+ attribute.to_s.humanize
135
+ end
124
136
  end
125
137
  text = block.call.html_safe + " #{text}" if block_given?
126
138
  options ||= {}
127
- options[:class] ||= ""
128
- options[:class] += " error" if has_error?(attribute)
129
139
  label(attribute, text, options)
130
140
  end
131
141
 
142
+ def column_classes(options)
143
+ classes = ''
144
+ if options[:small].present? && options[:small].to_i < 12
145
+ classes += "small-#{options[:small]} "
146
+ end
147
+ if options[:medium].present? && options[:medium].to_i < 12
148
+ classes += "medium-#{options[:medium]} "
149
+ end
150
+ if options[:large].present? && options[:large].to_i < 12
151
+ classes += "large-#{options[:large]} "
152
+ end
153
+ classes + 'columns'
154
+ end
155
+
156
+ def tag_from_options(name, options)
157
+ return ''.html_safe unless options && options[:value].present?
158
+
159
+ content_tag(:div,
160
+ content_tag(:span, options[:value], :class => name),
161
+ :class => "#{ column_classes( options ) }")
162
+ end
163
+
164
+ def decrement_input_size(input, column, options)
165
+ return unless options.key?(column)
166
+
167
+ input.send("#{column}=",
168
+ (input.send(column) - options.fetch(column).to_i))
169
+ input.send('changed?=', true)
170
+ end
171
+
172
+ def calculate_input_size(prefix_options, postfix_options)
173
+ input_size =
174
+ OpenStruct.new(changed?: false, small: 12, medium: 12, large: 12)
175
+ if prefix_options.present?
176
+ %w(small medium large).each do |size|
177
+ decrement_input_size(input_size, size.to_sym, prefix_options)
178
+ end
179
+ end
180
+ if postfix_options.present?
181
+ %w(small medium large).each do |size|
182
+ decrement_input_size(input_size, size.to_sym, postfix_options)
183
+ end
184
+ end
185
+
186
+ input_size
187
+ end
188
+
189
+ def wrap_prefix_and_postfix(block, prefix_options, postfix_options)
190
+ prefix = tag_from_options('prefix', prefix_options)
191
+ postfix = tag_from_options('postfix', postfix_options)
192
+
193
+ input_size = calculate_input_size(prefix_options, postfix_options)
194
+ klass = "#{column_classes(input_size.marshal_dump)}"
195
+ input = content_tag(:div, block, :class => klass)
196
+
197
+ html =
198
+ if input_size.changed?
199
+ content_tag(:div, prefix + input + postfix, :class => "row collapse")
200
+ else
201
+ block
202
+ end
203
+
204
+ html.html_safe
205
+ end
206
+
132
207
  def error_and_hint(attribute, options = {})
133
- html = ""
134
- html += content_tag(:span, options[:hint], :class => :hint) if options[:hint]
135
- html += error_for(attribute, options) || ""
208
+ html = ''
209
+ if options[:hint]
210
+ html += content_tag(:span, options[:hint], :class => :hint)
211
+ end
212
+ html += error_for(attribute, options) || ''
136
213
  html.html_safe
137
214
  end
138
215
 
139
- def field(attribute, options, html_options=nil, &block)
140
- html = ''.html_safe
141
- html = custom_label(attribute, options[:label], options[:label_options]) if @options[:auto_labels] || options[:label]
216
+ def field(attribute, options, html_options = nil, &block)
217
+ if @options[:auto_labels] || options[:label]
218
+ html = custom_label(attribute, options[:label], options[:label_options])
219
+ else
220
+ html = ''.html_safe
221
+ end
142
222
  class_options = html_options || options
143
- class_options[:class] = class_options[:class].to_s
144
- class_options[:class] += " error" if has_error?(attribute)
223
+
224
+ if has_error?(attribute)
225
+ class_options[:class] = class_options[:class].to_s
226
+ class_options[:class] += ' error'
227
+ end
228
+
145
229
  options.delete(:label)
146
230
  options.delete(:label_options)
147
- html += yield(class_options)
148
- html += error_and_hint(attribute, options)
231
+ hint = options.delete(:hint)
232
+ prefix = options.delete(:prefix)
233
+ postfix = options.delete(:postfix)
234
+
235
+ html += wrap_prefix_and_postfix(yield(class_options), prefix, postfix)
236
+ html + error_and_hint(attribute, options.merge(hint: hint))
149
237
  end
150
238
  end
151
239
  end
@@ -1,3 +1,3 @@
1
1
  module FoundationRailsHelper
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -18,19 +18,43 @@ describe FoundationRailsHelper do
18
18
  end
19
19
  end
20
20
 
21
+ describe "#ignored_flash_keys" do
22
+ it "defaults to empty" do
23
+ config = FoundationRailsHelper::Configuration.new
24
+ expect(config.ignored_flash_keys).to eq([])
25
+ end
26
+ end
27
+
28
+ describe "#ignored_flash_keys=" do
29
+ it "can set the value" do
30
+ config = FoundationRailsHelper::Configuration.new
31
+ config.ignored_flash_keys = [:foo]
32
+ expect(config.ignored_flash_keys).to eq([:foo])
33
+ end
34
+ end
35
+
21
36
  describe ".reset" do
22
- before :each do
37
+ it "resets the configured button class" do
23
38
  FoundationRailsHelper.configure do |config|
24
39
  config.button_class = 'new-class'
25
40
  end
26
- end
27
41
 
28
- it "resets the configuration" do
29
42
  FoundationRailsHelper.reset
30
43
 
31
44
  config = FoundationRailsHelper.configuration
32
45
  expect(config.button_class).to eq('small radius success button')
33
46
  end
47
+
48
+ it "resets the configured ignored flash keys" do
49
+ FoundationRailsHelper.configure do |config|
50
+ config.ignored_flash_keys = [:new_key]
51
+ end
52
+
53
+ FoundationRailsHelper.reset
54
+
55
+ config = FoundationRailsHelper.configuration
56
+ expect(config.ignored_flash_keys).to eq([])
57
+ end
34
58
  end
35
59
  end
36
60
 
@@ -6,32 +6,80 @@ describe FoundationRailsHelper::FlashHelper do
6
6
  include ActionView::Context if defined?(ActionView::Context)
7
7
  include ActionView::Helpers::UrlHelper
8
8
  include ActionView::Helpers::TagHelper
9
+ include ActionView::Helpers::TextHelper
9
10
  include FoundationRailsHelper::FlashHelper
10
11
 
11
12
  FoundationRailsHelper::FlashHelper::DEFAULT_KEY_MATCHING.each do |message_type, foundation_type|
12
13
  it "displays flash message with #{foundation_type} class for #{message_type} message" do
13
- allow(self).to receive(:flash).and_return({message_type => "Flash message"})
14
+ allow(self).to receive(:flash).and_return({message_type.to_s => "Flash message"})
14
15
  node = Capybara.string display_flash_messages
15
- expect(node).to have_css("div.alert-box.#{foundation_type}", :text => "Flash message")
16
- expect(node).to have_css("div.alert-box a.close", :text => "×")
16
+ expect(node).
17
+ to have_css("div.alert-box.#{foundation_type}", :text => "Flash message").
18
+ and have_css("div.alert-box a.close", :text => "×")
17
19
  end
18
20
  end
19
21
 
22
+ it "handles symbol keys" do
23
+ allow(self).to receive(:flash).and_return({ :success => "Flash message" })
24
+ node = Capybara.string display_flash_messages
25
+ expect(node).to have_css("div.alert-box.success", :text => "Flash message")
26
+ end
27
+
28
+ it "handles string keys" do
29
+ allow(self).to receive(:flash).and_return({ "success" => "Flash message" })
30
+ node = Capybara.string display_flash_messages
31
+ expect(node).to have_css("div.alert-box.success", :text => "Flash message")
32
+ end
33
+
34
+ it "displays multiple flash messages" do
35
+ allow(self).to receive(:flash).and_return({ "success" => "Yay it worked", "error" => "But this other thing failed" })
36
+ node = Capybara.string display_flash_messages
37
+ expect(node).
38
+ to have_css("div.alert-box.success", :text => "Yay it worked").
39
+ and have_css("div.alert-box.alert", :text => "But this other thing failed")
40
+ end
41
+
20
42
  it "displays flash message with overridden key matching" do
21
- allow(self).to receive(:flash).and_return({:notice => "Flash message"})
43
+ allow(self).to receive(:flash).and_return({ "notice" => "Flash message" })
22
44
  node = Capybara.string display_flash_messages({:notice => :alert})
23
- expect(node).to have_css("div.alert-box.alert")
45
+ expect(node).to have_css("div.alert-box.alert", :text => "Flash message")
24
46
  end
25
47
 
26
48
  it "displays flash message with custom key matching" do
27
- allow(self).to receive(:flash).and_return({:custom_type => "Flash message"})
49
+ allow(self).to receive(:flash).and_return({ "custom_type" => "Flash message" })
28
50
  node = Capybara.string display_flash_messages({:custom_type => :custom_class})
29
- expect(node).to have_css("div.alert-box.custom_class")
51
+ expect(node).to have_css("div.alert-box.custom_class", :text => "Flash message")
30
52
  end
31
53
 
32
54
  it "displays flash message with standard class if key doesn't match" do
33
- allow(self).to receive(:flash).and_return({:custom_type => "Flash message"})
55
+ allow(self).to receive(:flash).and_return({ "custom_type" => "Flash message" })
34
56
  node = Capybara.string display_flash_messages
35
- expect(node).to have_css("div.alert-box.standard")
57
+ expect(node).to have_css("div.alert-box.standard", :text => "Flash message")
58
+ end
59
+
60
+ context "when the flash hash contains devise internal data" do
61
+ before do
62
+ FoundationRailsHelper.configure do |config|
63
+ config.ignored_flash_keys += [:timedout]
64
+ end
65
+ end
66
+
67
+ it "doesn't raise an error (e.g. NoMethodError)" do
68
+ allow(self).to receive(:flash).and_return({ "timedout" => true })
69
+ expect{ Capybara.string display_flash_messages }.not_to raise_error
70
+ end
71
+
72
+ it "doesn't display an alert for that data" do
73
+ allow(self).to receive(:flash).and_return({ "timedout" => true })
74
+ expect(display_flash_messages).to be_nil
75
+
76
+ # Ideally we'd create a node using Capybara.string, as in the other examples
77
+ # and set the following expectation:
78
+ # expect(node).to_not have_css("div.alert-box")
79
+ # but Capybara.string doesn't behave nicely with nil input:
80
+ # the input gets assigned to the @native instance variable,
81
+ # which is used by the css matcher, so we get the following error:
82
+ # undefined method `css' for nil:NilClass
83
+ end
36
84
  end
37
85
  end
@@ -34,6 +34,153 @@ describe "FoundationRailsHelper::FormHelper" do
34
34
  end
35
35
  end
36
36
 
37
+ describe "label" do
38
+ context "when there aren't any errors and no class option is passed" do
39
+ it "should not have a class attribute" do
40
+ form_for(@author) do |builder|
41
+ node = Capybara.string builder.text_field(:login)
42
+ expect(node).to have_css('label:not([class=""])')
43
+ end
44
+ end
45
+ end
46
+
47
+ it "should not have error class multiple times" do
48
+ form_for(@author) do |builder|
49
+ allow(@author).to receive(:errors).and_return({:login => ['required']})
50
+ node = Capybara.string builder.text_field(:login)
51
+ error_class = node.find('label')['class'].split(/\s+/).keep_if { |v| v == 'error' }
52
+ expect(error_class.size).to eq 1
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "prefix" do
58
+ context "when input field has a prefix" do
59
+ before do
60
+ form_for(@author) do |builder|
61
+ @node = Capybara.string builder.text_field(:login, :prefix => {small: 2, medium:4, large: 6, value: "Prefix"})
62
+ end
63
+ end
64
+
65
+ it "wraps input in the div with class 'row collapse'" do
66
+ expect(@node.find('.row.collapse')).to_not be nil
67
+ end
68
+
69
+ it "wraps prefix in the div with the right column size" do
70
+ expect(@node.find('.row.collapse')).to have_css('div.small-2.medium-4.large-6.columns')
71
+ end
72
+
73
+ it "creates prefix span with right value" do
74
+ expect(@node.find('.row.collapse').find('div.small-2.medium-4.large-6.columns').find('span').text).to eq "Prefix"
75
+ end
76
+
77
+ it "creates prefix span with right class" do
78
+ expect(@node.find('.row.collapse')).to have_css('span.prefix')
79
+ end
80
+
81
+ it "wraps input in the div with the right column size" do
82
+ expect(@node.find('.row.collapse')).to have_css('div.small-10.medium-8.large-6.columns')
83
+ end
84
+
85
+ it "has right value for the input" do
86
+ expect(@node.find('.row.collapse').find('div.small-10.medium-8.large-6.columns')).to have_css('input[type="text"][name="author[login]"]')
87
+ end
88
+ end
89
+
90
+ context "without prefix" do
91
+ it "will not wrap input into a div" do
92
+ form_for(@author) do |builder|
93
+ node = Capybara.string builder.text_field(:login)
94
+ expect(node).to_not have_css('div.row.collapse')
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ describe "postfix" do
101
+ context "when input field has a postfix" do
102
+ before do
103
+ form_for(@author) do |builder|
104
+ @node = Capybara.string builder.text_field(:login, :postfix => {small: 2, medium: 4, large: 6, value: "Postfix"})
105
+ end
106
+ end
107
+
108
+ it "wraps input in the div with class 'row collapse'" do
109
+ expect(@node.find('.row.collapse')).to_not be nil
110
+ end
111
+
112
+ it "wraps postfix in the div with the right column size" do
113
+ expect(@node.find('.row.collapse')).to have_css('div.small-2.medium-4.large-6.columns')
114
+ end
115
+
116
+ it "creates postfix span with right value" do
117
+ expect(@node.find('.row.collapse').find('div.small-2.medium-4.large-6.columns').find('span').text).to eq "Postfix"
118
+ end
119
+
120
+ it "creates postfix span with right class" do
121
+ expect(@node.find('.row.collapse')).to have_css('span.postfix')
122
+ end
123
+
124
+ it "wraps input in the div with the right column size" do
125
+ expect(@node.find('.row.collapse')).to have_css('div.small-10.medium-8.large-6.columns')
126
+ end
127
+
128
+ it "has right value for the input" do
129
+ expect(@node.find('.row.collapse').find('div.small-10.medium-8.large-6.columns')).to have_css('input[type="text"][name="author[login]"]')
130
+ end
131
+ end
132
+
133
+ context "with only one column size" do
134
+ before do
135
+ form_for(@author) do |builder|
136
+ @small_node = Capybara.string builder.text_field(:login, :postfix => {small: 2, value: "Postfix"})
137
+ @medium_node = Capybara.string builder.text_field(:login, :postfix => {medium: 2, value: "Postfix"})
138
+ @large_node = Capybara.string builder.text_field(:login, :postfix => {large: 2, value: "Postfix"})
139
+ end
140
+ end
141
+
142
+ it "wraps postfix in the div with the right column size" do
143
+ expect(@small_node.find('.row.collapse')).to have_css('div.small-2.columns')
144
+ expect(@medium_node.find('.row.collapse')).to have_css('div.medium-2.columns')
145
+ expect(@large_node.find('.row.collapse')).to have_css('div.large-2.columns')
146
+ end
147
+
148
+ it "wraps input in the div with the right column size" do
149
+ expect(@small_node.find('.row.collapse')).to have_css('div.small-10.columns')
150
+ expect(@medium_node.find('.row.collapse')).to have_css('div.medium-10.columns')
151
+ expect(@large_node.find('.row.collapse')).to have_css('div.large-10.columns')
152
+ end
153
+
154
+ it "excludes other classes from the prefix" do
155
+ expect(@small_node.find('.row.collapse')).to_not have_css('div.medium-2.columns')
156
+ expect(@small_node.find('.row.collapse')).to_not have_css('div.large-2.columns')
157
+ end
158
+
159
+ it "excludes other classes from the input" do
160
+ expect(@small_node.find('.row.collapse')).to have_css('div.small-10.columns')
161
+ expect(@small_node.find('.row.collapse')).to_not have_css('div.medium-12.columns')
162
+ expect(@small_node.find('.row.collapse')).to_not have_css('div.large-12.columns')
163
+ end
164
+ end
165
+ end
166
+
167
+ describe "with both prefix and postfix" do
168
+ context "when input field has a prefix" do
169
+ before do
170
+ form_for(@author) do |builder|
171
+ @node = Capybara.string builder.text_field(:login,
172
+ :prefix => {small: 2, medium: 3, large: 4, value:"Prefix"},
173
+ :postfix => {small: 2, medium: 3, large: 4, value: "Postfix"})
174
+ end
175
+ end
176
+
177
+ it "wraps input in the div with the right column size" do
178
+ expect(@node.find('.row.collapse')).to have_css('div.small-8.medium-6.large-4.columns')
179
+ end
180
+
181
+ end
182
+ end
183
+
37
184
  describe "input generators" do
38
185
  it "should generate text_field input" do
39
186
  form_for(@author) do |builder|
@@ -168,6 +315,7 @@ describe "FoundationRailsHelper::FormHelper" do
168
315
  form_for(@author) do |builder|
169
316
  node = Capybara.string builder.radio_button(:active, "ok", label: false)
170
317
  expect(node).to_not have_css('label[for="author_active_ok"]')
318
+ expect(node).to_not have_css('input[label="false"]')
171
319
  expect(node).to have_css('input[type="radio"][name="author[active]"]')
172
320
  end
173
321
  end
@@ -340,6 +488,32 @@ describe "FoundationRailsHelper::FormHelper" do
340
488
  expect(node).to have_css('select[name="author[favorite_book]"] optgroup[label="Pirate Exploits"] option[value="133"]', :text => "Treasure Island")
341
489
  end
342
490
  end
491
+
492
+ describe "hint" do
493
+ it "should add a span element" do
494
+ form_for(@author) do |builder|
495
+ hint = 'Enter login'
496
+ node = Capybara.string builder.text_field(:login, :hint => hint)
497
+ expect(node.find("span").text).to eq hint
498
+ end
499
+ end
500
+
501
+ it "should not add hint attribute" do
502
+ form_for(@author) do |builder|
503
+ node = Capybara.string builder.text_field(:login, :hint => 'Enter login')
504
+ expect(node.find_field("author_login")['hint']).to be_nil
505
+ end
506
+ end
507
+ end
508
+
509
+ context "when there aren't any errors and no class option is passed" do
510
+ it "should not have a class attribute" do
511
+ form_for(@author) do |builder|
512
+ node = Capybara.string builder.text_field(:login)
513
+ expect(node).to have_css('input:not([class=""])')
514
+ end
515
+ end
516
+ end
343
517
  end
344
518
 
345
519
  describe "errors generator" do
@@ -518,82 +692,4 @@ describe "FoundationRailsHelper::FormHelper" do
518
692
  end
519
693
  end
520
694
  end
521
-
522
- describe 'submit button generator' do
523
- after :each do
524
- FoundationRailsHelper.reset
525
- end
526
-
527
- context 'when button_class config is not set' do
528
- it "should display form button with default class" do
529
- form_for(@author) do |builder|
530
- node = Capybara.string builder.submit('Save')
531
- expect(node).to have_css('input[type="submit"][class="small radius success button"]')
532
- end
533
- end
534
- end
535
-
536
- context 'when button_class config is "superduper"' do
537
- before do
538
- FoundationRailsHelper.configure do |config|
539
- config.button_class = 'superduper'
540
- end
541
- end
542
-
543
- it "should display form button with 'superduper' class" do
544
- form_for(@author) do |builder|
545
- node = Capybara.string builder.submit('Save')
546
- expect(node).to have_css('input[type="submit"][class="superduper"]')
547
- end
548
- end
549
- end
550
-
551
- context 'when option value is "superduper"' do
552
- it "should display form button with 'superduper' class" do
553
- form_for(@author) do |builder|
554
- node = Capybara.string builder.submit('Save', class: 'superduper')
555
- expect(node).to have_css('input[type="submit"][class="superduper"]')
556
- end
557
- end
558
- end
559
- end
560
-
561
- describe 'submit button generator' do
562
- after :each do
563
- FoundationRailsHelper.reset
564
- end
565
-
566
- context 'when button_class config is not set' do
567
- it "should display form button with default class" do
568
- form_for(@author) do |builder|
569
- node = Capybara.string builder.submit('Save')
570
- expect(node).to have_css('input[type="submit"][class="small radius success button"]')
571
- end
572
- end
573
- end
574
-
575
- context 'when button_class config is "superduper"' do
576
- before do
577
- FoundationRailsHelper.configure do |config|
578
- config.button_class = 'superduper'
579
- end
580
- end
581
-
582
- it "should display form button with 'superduper' class" do
583
- form_for(@author) do |builder|
584
- node = Capybara.string builder.submit('Save')
585
- expect(node).to have_css('input[type="submit"][class="superduper"]')
586
- end
587
- end
588
- end
589
-
590
- context 'when option value is "superduper"' do
591
- it "should display form button with 'superduper' class" do
592
- form_for(@author) do |builder|
593
- node = Capybara.string builder.submit('Save', class: 'superduper')
594
- expect(node).to have_css('input[type="submit"][class="superduper"]')
595
- end
596
- end
597
- end
598
- end
599
695
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: foundation_rails_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastien Gruhier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2015-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -17,9 +17,6 @@ dependencies:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '4.1'
20
- - - ">="
21
- - !ruby/object:Gem::Version
22
- version: 4.1.1
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
@@ -27,9 +24,6 @@ dependencies:
27
24
  - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '4.1'
30
- - - ">="
31
- - !ruby/object:Gem::Version
32
- version: 4.1.1
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: actionpack
35
29
  requirement: !ruby/object:Gem::Requirement
@@ -37,9 +31,6 @@ dependencies:
37
31
  - - "~>"
38
32
  - !ruby/object:Gem::Version
39
33
  version: '4.1'
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 4.1.1
43
34
  type: :runtime
44
35
  prerelease: false
45
36
  version_requirements: !ruby/object:Gem::Requirement
@@ -47,9 +38,6 @@ dependencies:
47
38
  - - "~>"
48
39
  - !ruby/object:Gem::Version
49
40
  version: '4.1'
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 4.1.1
53
41
  - !ruby/object:Gem::Dependency
54
42
  name: activemodel
55
43
  requirement: !ruby/object:Gem::Requirement
@@ -57,9 +45,6 @@ dependencies:
57
45
  - - "~>"
58
46
  - !ruby/object:Gem::Version
59
47
  version: '4.1'
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: 4.1.1
63
48
  type: :runtime
64
49
  prerelease: false
65
50
  version_requirements: !ruby/object:Gem::Requirement
@@ -67,9 +52,6 @@ dependencies:
67
52
  - - "~>"
68
53
  - !ruby/object:Gem::Version
69
54
  version: '4.1'
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: 4.1.1
73
55
  - !ruby/object:Gem::Dependency
74
56
  name: activesupport
75
57
  requirement: !ruby/object:Gem::Requirement
@@ -77,9 +59,6 @@ dependencies:
77
59
  - - "~>"
78
60
  - !ruby/object:Gem::Version
79
61
  version: '4.1'
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: 4.1.1
83
62
  type: :runtime
84
63
  prerelease: false
85
64
  version_requirements: !ruby/object:Gem::Requirement
@@ -87,9 +66,6 @@ dependencies:
87
66
  - - "~>"
88
67
  - !ruby/object:Gem::Version
89
68
  version: '4.1'
90
- - - ">="
91
- - !ruby/object:Gem::Version
92
- version: 4.1.1
93
69
  - !ruby/object:Gem::Dependency
94
70
  name: tzinfo
95
71
  requirement: !ruby/object:Gem::Requirement
@@ -130,6 +106,20 @@ dependencies:
130
106
  - - ">="
131
107
  - !ruby/object:Gem::Version
132
108
  version: 3.1.0
109
+ - !ruby/object:Gem::Dependency
110
+ name: mime-types
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '2'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '2'
133
123
  - !ruby/object:Gem::Dependency
134
124
  name: capybara
135
125
  requirement: !ruby/object:Gem::Requirement
@@ -200,7 +190,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
200
190
  version: '0'
201
191
  requirements: []
202
192
  rubyforge_project:
203
- rubygems_version: 2.2.2
193
+ rubygems_version: 2.4.5.1
204
194
  signing_key:
205
195
  specification_version: 4
206
196
  summary: Rails helpers for zurb foundation CSS framework