effective_form_inputs 1.0.4 → 1.0.5

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: 268cef64a10eb121ee7ed9cbc56e80b37e6072fe
4
- data.tar.gz: 217dcdf851e1d1ef92ec40d85b80beec818e413e
3
+ metadata.gz: 1f902d1b47f8f42634665e3f866708573532a4eb
4
+ data.tar.gz: 447661ba85bf6b67274d762b4f135a3bfa90a22e
5
5
  SHA512:
6
- metadata.gz: d8bafe99402fc8d137cc1177417c077041a592ca477870e5ed2e49d7c380756b493f54d1689ec8be2c8611aae66192f5745c9006e0586c784061a66302515059
7
- data.tar.gz: 8d97d9ac3fdd8011164fc022a5742b3ddcf44f88ee0b0e90cb6c29d89b4b5dad03a37fb7767eff5ba9496540f6bdd20be3e8c6e3548b9862bd7ec9008af093d1
6
+ metadata.gz: dcf849517c895cf279b678db02583ed25c27589e2f8ca51ad91f1be63e989fa61cf2326ee9209ccc1d01ff29e5262831b4e2ebb5908afe2c64a9667c8f7c1115
7
+ data.tar.gz: 95a8268916ad69957724cea6f3779328d81ef1a7d7f9a737e8a1245a72549f3229d9bcc3725e5b3927bacd45013151b81bb7848f4cac47154804dad404000c2c
data/README.md CHANGED
@@ -396,6 +396,12 @@ To add a css class to the select2 container or dropdown:
396
396
  :dropdownClass => 'custom-dropdown-class'
397
397
  ```
398
398
 
399
+ ### Additional
400
+
401
+ Call with `single_selected: true` to ensure only the first selected option tag will be `<option selected="selected">`.
402
+
403
+ This can be useful when displaying multiple options with an identical value.
404
+
399
405
  ### Working with dynamic options
400
406
 
401
407
  The following information applies to `effective_select` only, and is not part of the standard select2 API.
@@ -705,6 +711,20 @@ As a SimpleForm input:
705
711
  :buttons => true
706
712
  ```
707
713
 
714
+ ### Inline
715
+
716
+ Pass `inline: true` to the input options to render the radio buttons inline.
717
+
718
+ As a SimpleForm input:
719
+
720
+ ```ruby
721
+ = simple_form_for @user do |f|
722
+ = f.input :breakfast,
723
+ :as => :effective_radio_buttons,
724
+ :collection => ['eggs', 'toast', 'bacon'],
725
+ :inline => true
726
+ ```
727
+
708
728
  ### Images
709
729
 
710
730
  Pass `images: []` as an array of strings with the same length as the collection to render image buttons.
@@ -23,3 +23,27 @@ div.effective_radio_buttons.image_radio_buttons {
23
23
  border: 3px solid #009bc9;
24
24
  }
25
25
  }
26
+
27
+ div.effective_radio_buttons.inline_radio_buttons {
28
+ label.control-label { display: block; }
29
+
30
+ label.radio-inline {
31
+ padding-top: 7px;
32
+ padding-bottom: 7px;
33
+ }
34
+ }
35
+
36
+ div.effective_radio_buttons.button_radio_buttons {
37
+ input { display: none; }
38
+ }
39
+
40
+ .btn-group.effective_radio_buttons > .btn.radio-first:not(:first-child):not(:last-child):not(.dropdown-toggle) {
41
+ border-bottom-left-radius: 4px;
42
+ border-top-left-radius: 4px;
43
+ }
44
+
45
+ form.form-horizontal {
46
+ .btn-group.effective_radio_buttons {
47
+ display: block;
48
+ }
49
+ }
@@ -23,17 +23,28 @@ module Inputs
23
23
  end
24
24
 
25
25
  def render_item(builder)
26
- if options[:nested_boolean_style] == :nested || options[:buttons]
26
+ if options[:inline] || options[:buttons]
27
+ item = builder.radio_button + item_image_or_text(builder)
28
+ elsif options[:nested_boolean_style] == :nested
27
29
  item = builder.label { builder.radio_button + item_image_or_text(builder) }
28
30
  else
29
31
  item = builder.radio_button + builder.label { item_image_or_text(builder) }
30
32
  end
31
33
 
34
+ if options[:buttons]
35
+ @button_index ||= -1; @button_index += 1
36
+ end
37
+
32
38
  if options[:item_wrapper_tag]
33
39
  active = (builder.object.send(options[:value_method]).to_s == value.to_s)
34
40
 
35
41
  content_tag(options[:item_wrapper_tag], item,
36
- class: ['btn', 'btn-default', options[:item_wrapper_class], ('active' if active)].flatten.uniq.join(' ')
42
+ class: [
43
+ options[:item_wrapper_class],
44
+ ('btn btn-default' if options[:buttons]),
45
+ ('radio-first' if options[:buttons] && @button_index == 0),
46
+ ('active' if active)
47
+ ].flatten.compact.uniq.join(' ')
37
48
  )
38
49
  else
39
50
  item
@@ -71,6 +82,18 @@ module Inputs
71
82
  options[:checked] || super
72
83
  end
73
84
 
85
+ def options
86
+ @effective_radio_buttons_options ||= super().tap do |options|
87
+ options[:item_wrapper_class] = Array(options[:item_wrapper_class]).flatten.uniq
88
+
89
+ if options[:inline] || options[:buttons]
90
+ options[:item_wrapper_tag] = :label unless options[:buttons]
91
+ options[:item_wrapper_class] = 'radio-inline'
92
+ end
93
+
94
+ end
95
+ end
96
+
74
97
  private
75
98
 
76
99
  def initialize_and_validate_images!
@@ -12,15 +12,25 @@ if defined?(SimpleForm)
12
12
  options[:value_method] = value_method
13
13
  options[:nested_boolean_style] = :nested if nested_boolean_style?
14
14
 
15
+ # Wrapper
16
+ options[:wrapper_html] ||= {}
17
+ options[:wrapper_html][:class] = Array(options[:wrapper_html][:class])
18
+
15
19
  if options[:images]
16
- options[:wrapper_html] ||= {}
17
- options[:wrapper_html][:class] = "#{options[:wrapper_html][:class]} image_radio_buttons".strip
18
- elsif options[:buttons]
19
- options[:wrapper_html] ||= {}
20
- options[:wrapper_html][:class] = "#{options[:wrapper_html][:class]} button_radio_buttons btn-group".strip
20
+ options[:wrapper_html][:class] << 'image_radio_buttons'
21
+ end
22
+
23
+ if options[:buttons]
24
+ options[:wrapper_html][:class] << 'button_radio_buttons btn-group inline_radio_buttons'
21
25
  options[:wrapper_html][:data] = { toggle: 'buttons' }
22
26
  end
23
27
 
28
+ if options[:inline] && !options[:buttons]
29
+ options[:wrapper_html][:class] << 'inline_radio_buttons'
30
+ end
31
+
32
+ options[:wrapper_html][:class] = options[:wrapper_html][:class].join(' ')
33
+
24
34
  Inputs::EffectiveRadioButtons::Input.new(object, object_name, template, attribute_name, input_options, (merge_wrapper_options(input_html_options, wrapper_options) || {})).to_html
25
35
  end
26
36
 
@@ -31,6 +31,12 @@ module Inputs
31
31
  html += hidden_field(@object_name, polymorphic_id_method, value: polymorphic_id_value)
32
32
  end
33
33
 
34
+ if options[:single_selected]
35
+ if html.sub!('selected="selected"', "selected='selected'")
36
+ html.gsub!('selected="selected"', '')
37
+ end
38
+ end
39
+
34
40
  html
35
41
  end
36
42
 
@@ -112,6 +118,8 @@ module Inputs
112
118
  elsif value.first.respond_to?(options[:value_method]) # This is probably a belongs_to ActiveRecord object
113
119
  options[:selected] = value.map { |value| (value.public_send(options[:value_method]) rescue value) }
114
120
  end
121
+ when Integer
122
+ options[:selected] = value
115
123
  else # Value is not an Array
116
124
  if options[:polymorphic]
117
125
  options[:selected] = polymorphic_value(value)
@@ -1,3 +1,3 @@
1
1
  module EffectiveFormInputs
2
- VERSION = '1.0.4'.freeze
2
+ VERSION = '1.0.5'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_form_inputs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-18 00:00:00.000000000 Z
11
+ date: 2017-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails