hanami-helpers 0.5.1 → 1.0.0.beta1

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: 81f2f927f612c01d961de1653cccea4c11e2ceeb
4
- data.tar.gz: e41a6a6995d4d04e7e11ac893843be76967c2555
3
+ metadata.gz: 9a4372126c3d3cc4666a281f370b709108ce7522
4
+ data.tar.gz: a906c5adc94f6e22df677efde17f28ce529fc35c
5
5
  SHA512:
6
- metadata.gz: 3eef23b1f56601b1977a15a754a6b1d54c120cbb4591f9e47b29b75dd7678e325f7fe953e978b8cfc9d926d98a87ce2548b4bdc8633d511cafe08d1dc75a9511
7
- data.tar.gz: 1c101a9b9f88da297a32b5ed9dd957c173bfca10746c0ab80b948dbee16e177d01bb3fb8fae8afcd5f115584b0a20afc200b3ad3ef88c6012fafc51c0cc45839
6
+ metadata.gz: 5f713e2872f9b0d40d2e51980e3563500b33f1ee8b54c278258cd37cad97361420bf56c35e5a5f61552c205075283e3a483fb28f84313f8de0e411a353b7ca6e
7
+ data.tar.gz: fa35b8534b30d4cd19698fe3b6cd7e82951bdf341e7831a196b190165f5476b382d4ced07d1099d11a9326a86ca14df1e19282b30f2cf87dda80e484cb9efac3
data/CHANGELOG.md CHANGED
@@ -1,6 +1,15 @@
1
1
  # Hanami::Helpers
2
2
  View helpers for Ruby web applications
3
3
 
4
+ ## v1.0.0.beta1 - 2017-02-14
5
+ ### Added
6
+ - [Luca Guidi] Official support for Ruby: MRI 2.4
7
+ - [Marion Duprey] Introduced Form helper `fields_for_collection` to support arrays of nested fields
8
+
9
+ ## Fixed
10
+ - [Ksenia Zalesnaya] Ensure radio buttons and selects to coerce the value to boolean before to decide if they should be checked or not.
11
+ - [Anton Davydov] Escape form values to prevent XSS attacks
12
+
4
13
  ## v0.5.1 - 2016-12-19
5
14
  ### Fixed
6
15
  - [Alex Coles] Ensure `#form_for`'s `values:` to accept `Hanami::Entity` instances
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ['lib']
21
21
  spec.required_ruby_version = '>= 2.3.0'
22
22
 
23
- spec.add_dependency 'hanami-utils', '~> 0.9'
23
+ spec.add_dependency 'hanami-utils', '~> 1.0.0.beta1'
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.6'
26
26
  spec.add_development_dependency 'rake', '~> 11'
@@ -198,7 +198,49 @@ module Hanami
198
198
  def fields_for(name)
199
199
  current_name = @name
200
200
  @name = _input_name(name)
201
- yield
201
+ yield(name)
202
+ ensure
203
+ @name = current_name
204
+ end
205
+
206
+ # Nested collections
207
+ #
208
+ # Supports nesting for collections, with infinite
209
+ # levels of nesting.
210
+ #
211
+ # @param name [Symbol] the nested name, it's used to generate input
212
+ # names, ids, and to lookup params to fill values.
213
+ #
214
+ # @example Basic usage
215
+ # <%=
216
+ # form_for :delivery, routes.deliveries_path do
217
+ # text_field :customer_name
218
+ #
219
+ # fields_for_collection :addresses do
220
+ # text_field :street
221
+ # end
222
+ #
223
+ # submit 'Create'
224
+ # end
225
+ # %>
226
+ #
227
+ # Output:
228
+ # # <form action="/deliveries" method="POST" accept-charset="utf-8" id="delivery-form">
229
+ # # <input type="text" name="delivery[customer_name]" id="delivery-customer-name" value="">
230
+ # # <input type="text" name="delivery[addresses][][street]" id="delivery-address-0-street" value="">
231
+ # # <input type="text" name="delivery[addresses][][street]" id="delivery-address-1-street" value="">
232
+ # #
233
+ # # <button type="submit">Create</button>
234
+ # # </form>
235
+ #
236
+ def fields_for_collection(name, &block)
237
+ current_name = @name
238
+ base_value = _value(name)
239
+ @name = _input_name(name)
240
+
241
+ base_value.count.times do |index|
242
+ fields_for(index, &block)
243
+ end
202
244
  ensure
203
245
  @name = current_name
204
246
  end
@@ -678,7 +720,7 @@ module Hanami
678
720
  # # <input type="radio" name="book[category]" value="Non-Fiction" checked="checked">
679
721
  def radio_button(name, value, attributes = {})
680
722
  attributes = { type: :radio, name: _input_name(name), value: value }.merge(attributes)
681
- attributes[:checked] = CHECKED if _value(name) == value
723
+ attributes[:checked] = CHECKED if _value(name).to_s == value.to_s
682
724
  input(attributes)
683
725
  end
684
726
 
@@ -957,10 +999,14 @@ module Hanami
957
999
  # @api private
958
1000
  # @since 0.2.0
959
1001
  def _attributes(type, name, attributes)
960
- { type: type, name: _input_name(name), id: _input_id(name), value: _value(name) }.merge(attributes)
1002
+ attrs = { type: type, name: _displayed_input_name(name), id: _input_id(name), value: _value(name) }
1003
+ attrs.merge!(attributes)
1004
+ attrs[:value] = Hanami::Utils::Escape.html(attrs[:value])
1005
+ attrs
961
1006
  end
962
1007
 
963
- # Input <tt>name</tt> HTML attribute
1008
+ # Full input name, used to construct the input
1009
+ # attributes.
964
1010
  #
965
1011
  # @api private
966
1012
  # @since 0.2.0
@@ -968,6 +1014,14 @@ module Hanami
968
1014
  "#{@name}[#{name}]"
969
1015
  end
970
1016
 
1017
+ # Input <tt>name</tt> HTML attribute
1018
+ #
1019
+ # @api private
1020
+ # @since 1.0.0.beta1
1021
+ def _displayed_input_name(name)
1022
+ _input_name(name).gsub(/\[\d+\]/, '[]')
1023
+ end
1024
+
971
1025
  # Input <tt>id</tt> HTML attribute
972
1026
  #
973
1027
  # @api private
@@ -1047,7 +1101,7 @@ module Hanami
1047
1101
  # rubocop:disable Metrics/PerceivedComplexity
1048
1102
  def _select_option_selected?(value, selected, input_value, multiple)
1049
1103
  value == selected || (multiple && (selected.is_a?(Array) && selected.include?(value))) ||
1050
- value == input_value || (multiple && (input_value.is_a?(Array) && input_value.include?(value)))
1104
+ value.to_s == input_value.to_s || (multiple && (input_value.is_a?(Array) && input_value.include?(value)))
1051
1105
  end
1052
1106
  # rubocop:enable Metrics/PerceivedComplexity
1053
1107
  # rubocop:enable Metrics/CyclomaticComplexity
@@ -36,6 +36,7 @@ module Hanami
36
36
  # @since 0.5.0
37
37
  # @api private
38
38
  def _get_from_params(*keys)
39
+ keys.map! { |key| key.to_s =~ /\A\d+\z/ ? key.to_s.to_i : key }
39
40
  @params.dig(*keys)
40
41
  end
41
42
 
@@ -47,15 +48,21 @@ module Hanami
47
48
 
48
49
  tail.each do |k|
49
50
  break if result.nil?
50
-
51
- result = case result
52
- when Utils::Hash, ::Hash then result[k]
53
- when ->(r) { r.respond_to?(k) } then result.public_send(k)
54
- end
51
+ result = _dig(result, k)
55
52
  end
56
53
 
57
54
  result
58
55
  end
56
+
57
+ # @since 1.0.0.beta1
58
+ # @api private
59
+ def _dig(base, key)
60
+ case base
61
+ when Utils::Hash, ::Hash then base[key]
62
+ when Array then base[key.to_s.to_i]
63
+ when ->(r) { r.respond_to?(key) } then base.public_send(key)
64
+ end
65
+ end
59
66
  end
60
67
  end
61
68
  end
@@ -3,6 +3,6 @@ module Hanami
3
3
  # Define version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.5.1'.freeze
6
+ VERSION = '1.0.0.beta1'.freeze
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 1.0.0.beta1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-12-19 00:00:00.000000000 Z
13
+ date: 2017-02-14 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: hanami-utils
@@ -18,14 +18,14 @@ dependencies:
18
18
  requirements:
19
19
  - - "~>"
20
20
  - !ruby/object:Gem::Version
21
- version: '0.9'
21
+ version: 1.0.0.beta1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
25
25
  requirements:
26
26
  - - "~>"
27
27
  - !ruby/object:Gem::Version
28
- version: '0.9'
28
+ version: 1.0.0.beta1
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -127,9 +127,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
127
  version: 2.3.0
128
128
  required_rubygems_version: !ruby/object:Gem::Requirement
129
129
  requirements:
130
- - - ">="
130
+ - - ">"
131
131
  - !ruby/object:Gem::Version
132
- version: '0'
132
+ version: 1.3.1
133
133
  requirements: []
134
134
  rubyforge_project:
135
135
  rubygems_version: 2.6.8