hanami-helpers 0.4.0 → 0.5.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: e90f277e6ff4d461bd7ebcc45e48de6c03f91e70
4
- data.tar.gz: 8d97b52cf45b16454acf597a6fe198b3a549148a
3
+ metadata.gz: 8e1a90723c16139a6a3c5fdd84e0d85f732ed886
4
+ data.tar.gz: 1edccf5044d1185147cf0d0e51fe863d78467e03
5
5
  SHA512:
6
- metadata.gz: 21d4b2160f0d6af94d099ec8f765bbcce309b22623aff0ce3190f50b7fb2fec228fde3b6f330a47242455ab7989144e8c04b5a146170399d74f914ba0facc79f
7
- data.tar.gz: f898b3a411eb8d77e776a398ef2873bc6b6e8ef39fffd59602f8d0b7cd53a5662ea767665a95e7da228271bb3f16cf635f3df12d28feceed507177b2e621134c
6
+ metadata.gz: 98e536f514a1e3d152ab3f16b059592f0d8c1b7b2f83688cedf3641a397c8db506fbefd6855fc5acb9b0feeadf991501d9dc9f62e9c3e2f555837b9085f5aa6c
7
+ data.tar.gz: fac6a702eb642517b6e5c31d3a1bdbf784ebccf5ed7938665ed0dfb7234205074fae945f42ce2f343ebfc2b254ddb4e90fe5032f8472308378d688e303b8aefa
data/CHANGELOG.md CHANGED
@@ -1,6 +1,16 @@
1
1
  # Hanami::Helpers
2
2
  View helpers for Ruby web applications
3
3
 
4
+ ## v0.5.0 - 2016-11-15
5
+ ### Added
6
+ - [Marion Duprey] Allow `select` form helper to generate a multiple select (via `multiple: true` option)
7
+
8
+ ### Fixed
9
+ - [Luca Guidi] Ensure `form_for` to be compatible with params passed as `Hash`. This is useful for unit tests.
10
+
11
+ ### Changed
12
+ - [Luca Guidi] Official support for Ruby: MRI 2.3+ and JRuby 9.1.5.0+
13
+
4
14
  ## v0.4.0 - 2016-07-22
5
15
  ### Added
6
16
  - [Luca Guidi] Allow `link_to` to be concat with other helpers. Eg `link_to(...) + link_to(...)`, `span(...) + link_to(...)`.
data/README.md CHANGED
@@ -22,7 +22,7 @@ View helpers for Ruby applications
22
22
 
23
23
  ## Rubies
24
24
 
25
- __Hanami::Helpers__ supports Ruby (MRI) 2.2+
25
+ __Hanami::Helpers__ supports Ruby (MRI) 2.3+ and JRuby 9.1.5.0+
26
26
 
27
27
  ## Installation
28
28
 
@@ -18,9 +18,9 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ['lib']
21
- spec.required_ruby_version = '>= 2.2.0'
21
+ spec.required_ruby_version = '>= 2.3.0'
22
22
 
23
- spec.add_dependency 'hanami-utils', '~> 0.8'
23
+ spec.add_dependency 'hanami-utils', '~> 0.9'
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.6'
26
26
  spec.add_development_dependency 'rake', '~> 11'
@@ -56,14 +56,6 @@ module Hanami
56
56
  # @see Hanami::Helpers::FormHelper::FormBuilder#_input_id
57
57
  INPUT_ID_REPLACEMENT = '-\k<token>'.freeze
58
58
 
59
- # Replacement for input value interpolation
60
- #
61
- # @since 0.2.0
62
- # @api private
63
- #
64
- # @see Hanami::Helpers::FormHelper::FormBuilder#_value
65
- INPUT_VALUE_REPLACEMENT = '.\k<token>'.freeze
66
-
67
59
  # Default value for unchecked check box
68
60
  #
69
61
  # @since 0.2.0
@@ -780,9 +772,22 @@ module Hanami
780
772
  # # <option value="it" selected="selected">Italy</option>
781
773
  # # <option value="us">United States</option>
782
774
  # # </select>
775
+ #
776
+ # @example Multiple select
777
+ # <%=
778
+ # # ...
779
+ # values = Hash['it' => 'Italy', 'us' => 'United States']
780
+ # select :stores, values, multiple: true
781
+ # %>
782
+ #
783
+ # # Output:
784
+ # # <select name="book[store][]" id="book-store" multiple="multiple">
785
+ # # <option value="it">Italy</option>
786
+ # # <option value="us">United States</option>
787
+ # # </select>
783
788
  def select(name, values, attributes = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
784
789
  options = attributes.delete(:options) { {} }
785
- attributes = { name: _input_name(name), id: _input_id(name) }.merge(attributes)
790
+ attributes = { name: _select_input_name(name, attributes[:multiple]), id: _input_id(name) }.merge(attributes)
786
791
  prompt = options.delete(:prompt)
787
792
  selected = options.delete(:selected)
788
793
 
@@ -790,7 +795,7 @@ module Hanami
790
795
  option(prompt) unless prompt.nil?
791
796
 
792
797
  values.each do |content, value|
793
- if selected == value || _value(name) == value
798
+ if _select_option_selected?(value, selected, _value(name), attributes[:multiple])
794
799
  option(content, { value: value, selected: SELECTED }.merge(options))
795
800
  else
796
801
  option(content, { value: value }.merge(options))
@@ -977,8 +982,9 @@ module Hanami
977
982
  # @api private
978
983
  # @since 0.2.0
979
984
  def _value(name)
980
- name = _input_name(name).gsub(/\[(?<token>[[:word:]]*)\]/, INPUT_VALUE_REPLACEMENT)
981
- @values.get(name)
985
+ @values.get(
986
+ *_input_name(name).split(/[\[\]]+/).map(&:to_sym)
987
+ )
982
988
  end
983
989
 
984
990
  # Input <tt>for</tt> HTML attribute
@@ -1001,13 +1007,13 @@ module Hanami
1001
1007
  #
1002
1008
  # @see Hanami::Helpers::FormHelper::FormBuilder#check_box
1003
1009
  def _hidden_field_for_check_box(name, attributes)
1004
- if attributes[:value].nil? || !attributes[:unchecked_value].nil?
1005
- input(
1006
- type: :hidden,
1007
- name: attributes[:name] || _input_name(name),
1008
- value: attributes.delete(:unchecked_value) || DEFAULT_UNCHECKED_VALUE
1009
- )
1010
- end
1010
+ return unless attributes[:value].nil? || !attributes[:unchecked_value].nil?
1011
+
1012
+ input(
1013
+ type: :hidden,
1014
+ name: attributes[:name] || _input_name(name),
1015
+ value: attributes.delete(:unchecked_value) || DEFAULT_UNCHECKED_VALUE
1016
+ )
1011
1017
  end
1012
1018
 
1013
1019
  # HTML attributes for check box
@@ -1030,6 +1036,23 @@ module Hanami
1030
1036
 
1031
1037
  attributes
1032
1038
  end
1039
+
1040
+ def _select_input_name(name, multiple)
1041
+ select_name = _input_name(name)
1042
+ select_name = "#{select_name}[]" if multiple
1043
+ select_name
1044
+ end
1045
+
1046
+ # TODO: this has to be refactored
1047
+ #
1048
+ # rubocop:disable Metrics/CyclomaticComplexity
1049
+ # rubocop:disable Metrics/PerceivedComplexity
1050
+ def _select_option_selected?(value, selected, input_value, multiple)
1051
+ value == selected || (multiple && (selected.is_a?(Array) && selected.include?(value))) ||
1052
+ value == input_value || (multiple && (input_value.is_a?(Array) && input_value.include?(value)))
1053
+ end
1054
+ # rubocop:enable Metrics/PerceivedComplexity
1055
+ # rubocop:enable Metrics/CyclomaticComplexity
1033
1056
  end
1034
1057
  end
1035
1058
  end
@@ -18,27 +18,34 @@ module Hanami
18
18
  # @since 0.2.0
19
19
  # @api private
20
20
  def initialize(values, params)
21
- @values = Utils::Hash.new(values).stringify!
21
+ @values = Utils::Hash.new(values || {}).symbolize!
22
22
  @params = params
23
23
  end
24
24
 
25
- # Returns the value for the given key, if present
25
+ # Returns the value (if present) for the given key.
26
+ # Nested values are expressed with an array if symbols.
26
27
  #
27
28
  # @since 0.2.0
28
29
  # @api private
29
- def get(key)
30
- @params.get(key) || _get_from_values(key)
30
+ def get(*keys)
31
+ _get_from_params(*keys) || _get_from_values(*keys)
31
32
  end
32
33
 
33
34
  private
34
35
 
36
+ # @since 0.5.0
37
+ # @api private
38
+ def _get_from_params(*keys)
39
+ @params.dig(*keys)
40
+ end
41
+
35
42
  # @since 0.2.0
36
43
  # @api private
37
- def _get_from_values(key)
38
- initial_key, *keys = key.to_s.split(GET_SEPARATOR)
39
- result = @values[initial_key]
44
+ def _get_from_values(*keys)
45
+ head, *tail = *keys
46
+ result = @values[head]
40
47
 
41
- Array(keys).each do |k|
48
+ tail.each do |k|
42
49
  break if result.nil?
43
50
 
44
51
  result = if result.respond_to?(k) # rubocop:disable Style/IfUnlessModifier
@@ -110,7 +110,7 @@ module Hanami
110
110
 
111
111
  # Default precision
112
112
  #
113
- # @return [String] default precision
113
+ # @return [Integer] default precision
114
114
  #
115
115
  # @since 0.2.0
116
116
  # @api private
@@ -5,7 +5,7 @@ module Hanami
5
5
  # Routing helper for full stack Hanami web applications.
6
6
  #
7
7
  # For a given application called <tt>Web::Application</tt>, at runtime
8
- # Hanami creates a routes factory called <tt>Web::Routes</tt>.
8
+ # Hanami creates a routes factory called <tt>Web.routes</tt>.
9
9
  #
10
10
  # By including this module in a view, it makes that factory avaliable as
11
11
  # <tt>routes</tt>.
@@ -41,7 +41,7 @@ module Hanami
41
41
  # # <%= link_to_home %>
42
42
  module RoutingHelper
43
43
  def self.included(base)
44
- factory = "#{Utils::String.new(base).namespace}::Routes"
44
+ factory = "#{Utils::String.new(base).namespace}.routes"
45
45
 
46
46
  base.class_eval <<-END_EVAL, __FILE__, __LINE__
47
47
  def routes
@@ -3,6 +3,6 @@ module Hanami
3
3
  # Define version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '0.4.0'.freeze
6
+ VERSION = '0.5.0'.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.4.0
4
+ version: 0.5.0
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-07-22 00:00:00.000000000 Z
13
+ date: 2016-11-15 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.8'
21
+ version: '0.9'
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.8'
28
+ version: '0.9'
29
29
  - !ruby/object:Gem::Dependency
30
30
  name: bundler
31
31
  requirement: !ruby/object:Gem::Requirement
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
110
  requirements:
111
111
  - - ">="
112
112
  - !ruby/object:Gem::Version
113
- version: 2.2.0
113
+ version: 2.3.0
114
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  requirements:
116
116
  - - ">="