effective_form_inputs 1.0.0 → 1.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 21eeb2410f7b3acf28a2f62bc982c1ac6e29855f
4
- data.tar.gz: ef450f694a8988d34bd376e84d09fa5d1ab4d779
3
+ metadata.gz: 70ab15ba6a6ca6aa19cbb4cb883fdbdd3e754b76
4
+ data.tar.gz: c3468e9539e7a91fd5d61a0a2393ab72718a49c2
5
5
  SHA512:
6
- metadata.gz: a78f89a52cadebf05008e99cd6febb2a1c2bdd163b2f8493ed58f28c3bb1aee06753f72f85c71e06c336331ce9b0ab25d132fbab40ff2d7ad34e313c0920993f
7
- data.tar.gz: 987c563c18c6c27d48bbb2a2858372255ff14149097711138f9a444aeea0d8cc55e98d0e3b516308acbeced3f0fdc5bafcdd998f184e6fbd2fbf0830d37e2084
6
+ metadata.gz: 57dba2ff14677bea5a9b8085865cf69a8810359b4f4b76d64a74d2cab5571646d172d3e22537ff0ed7feacb7404ae1b0a261329e809b4f35169cac112728bb6c
7
+ data.tar.gz: 0ed08a3df3d337401cd8e435d3358503513bf882c650f8d539682ac0f4e7a15a787d4f4e267482bb7509b8dea64d940267f4ad02cb7ec9d2900d397bdc53c6bf
data/README.md CHANGED
@@ -675,6 +675,39 @@ When `:bootstrap`, this loads a CDN hosted bootstrap 3.3.7 stylesheet.
675
675
  ```
676
676
 
677
677
 
678
+ ## Effective Radio Buttons
679
+
680
+ This custom form input adds image support to the SimpleForm radio buttons. It doesn't really work as a regular rails form helper.
681
+
682
+ ### Installation
683
+
684
+ If you've already installed the 'All Form Inputs' assets (see above), there are no additional installation steps.
685
+
686
+ To install this form input individually, add the following to your application.css:
687
+
688
+ ```ruby
689
+ *= require effective_radio_buttons/input
690
+ ```
691
+
692
+ There is no javascript.
693
+
694
+ ### Usage
695
+
696
+ As a SimpleForm input:
697
+
698
+ ```ruby
699
+ = simple_form_for @user do |f|
700
+ = f.input :breakfast,
701
+ :as => :effective_radio_buttons,
702
+ :collection => ['eggs', 'toast', 'bacon'],
703
+ :images => [asset_path('eggs.png'), asset_path('toast.png'), asset_path('bacon.png')]
704
+ ```
705
+
706
+ ### Options
707
+
708
+ The only additional option is `images: []`, which must be an array of strings the same length as the collection.
709
+
710
+
678
711
  ## License
679
712
 
680
713
  MIT License. Copyright [Code and Effect Inc.](http://www.codeandeffect.com/)
@@ -1,2 +1,3 @@
1
1
  @import 'effective_date_time_picker/input';
2
+ @import 'effective_radio_buttons/input';
2
3
  @import 'effective_select/input';
@@ -0,0 +1,25 @@
1
+ div.effective_radio_buttons.image_radio_buttons {
2
+ input { display: none; }
3
+
4
+ .radio label { padding-left: 0px; }
5
+
6
+ // Nested style
7
+ label > input + img {
8
+ cursor: pointer;
9
+ border: 3px solid transparent;
10
+ }
11
+
12
+ label > input:checked + img {
13
+ border: 3px solid #009bc9;
14
+ }
15
+
16
+ // Non-nested style
17
+ input + label > img {
18
+ cursor: pointer;
19
+ border: 3px solid transparent;
20
+ }
21
+
22
+ input:checked + label > img {
23
+ border: 3px solid #009bc9;
24
+ }
25
+ }
@@ -16,6 +16,10 @@ module Effective
16
16
  Inputs::EffectivePrice::Input.new(@object, @object_name, @template, method, options, options).to_html
17
17
  end
18
18
 
19
+ def effective_radio_buttons(method, options = {})
20
+ Inputs::EffectiveRadioButtons::Input.new(@object, @object_name, @template, method, options, options).to_html
21
+ end
22
+
19
23
  def effective_static_control(method, options = {})
20
24
  Inputs::EffectiveStaticControl::Input.new(@object, @object_name, @template, method, options, options).to_html
21
25
  end
@@ -63,7 +63,7 @@ module Effective
63
63
  # It serializes any js_options into JSON format
64
64
  # And turns html_options[:class] back into a string
65
65
  def tag_options
66
- html_options().tap do |html_options|
66
+ @tag_options ||= html_options().tap do |html_options|
67
67
  html_options['data-input-js-options'] = (JSON.generate(js_options) rescue {})
68
68
  html_options[:class] = html_options[:class].join(' ')
69
69
  html_options[:placeholder] = (html_options[:placeholder].presence || options[:placeholder])
@@ -0,0 +1,84 @@
1
+ module Inputs
2
+ module EffectiveRadioButtons
3
+ class Input < Effective::FormInput
4
+ delegate :collection_radio_buttons, :content_tag, :label_tag, :radio_button_tag, :image_tag, :to => :@template
5
+
6
+ BOOLEAN_COLLECTION = [['Yes', true], ['No', false]]
7
+
8
+ def default_options
9
+ { label_method: :first, value_method: :last }
10
+ end
11
+
12
+ def default_input_html
13
+ { class: 'effective_radio_buttons' }
14
+ end
15
+
16
+ def default_input_js
17
+ {}
18
+ end
19
+
20
+ def to_html
21
+ initialize_and_validate_images!
22
+ collection_radio_buttons(@object_name, @method, collection, options[:value_method], options[:label_method], {}, item_html_options, &proc { |builder| render_item(builder) })
23
+ end
24
+
25
+ def render_item(builder)
26
+ if options[:nested_boolean_style] == :nested
27
+ item = builder.label { builder.radio_button + item_image_or_text(builder) }
28
+ else
29
+ item = builder.radio_button + builder.label { item_image_or_text(builder) }
30
+ end
31
+
32
+ if options[:item_wrapper_tag]
33
+ content_tag(options[:item_wrapper_tag], item, class: options[:item_wrapper_class])
34
+ else
35
+ item
36
+ end
37
+ end
38
+
39
+ def item_image_or_text(builder)
40
+ @images_index += 1
41
+
42
+ if options[:images]
43
+ image_tag(options[:images][@images_index], alt: builder.text)
44
+ else
45
+ builder.text
46
+ end
47
+ end
48
+
49
+ def item_html_options
50
+ @item_html_options ||= { class: tag_options[:class] }
51
+ end
52
+
53
+ def html_options
54
+ super.tap do |html_options|
55
+ html_options[:class].delete('form-control')
56
+ end
57
+ end
58
+
59
+ def collection
60
+ @collection ||= begin
61
+ collection = options.delete(:collection) || BOOLEAN_COLLECTION
62
+ collection.respond_to?(:call) ? collection.call : collection.to_a
63
+ end
64
+ end
65
+
66
+ private
67
+
68
+ def initialize_and_validate_images!
69
+ @images_index = -1
70
+ return unless options[:images].present?
71
+
72
+ unless options[:images].kind_of?(Array) && (options[:images].first || '').kind_of?(String)
73
+ raise 'images must be an Array of Strings'
74
+ end
75
+
76
+ unless options[:images].length == collection.length
77
+ raise "images length must match collection length (#{collection.length})"
78
+ end
79
+ end
80
+
81
+ end
82
+ end
83
+ end
84
+
@@ -0,0 +1,25 @@
1
+ # = simple_form_for @thing do |f|
2
+ # = f.input :breakfast, :as => :effective_radio_buttons, :collection => ['Eggs', 'Bacon'], images => [asset_path('eggs.png'), asset_path('bacon.png')]
3
+
4
+ if defined?(SimpleForm)
5
+
6
+ class EffectiveRadioButtonsInput < SimpleForm::Inputs::CollectionRadioButtonsInput
7
+ def input(wrapper_options = nil)
8
+ label_method, value_method = (detect_collection_methods rescue [:first, :last])
9
+
10
+ options[:collection] = collection
11
+ options[:label_method] = label_method
12
+ options[:value_method] = value_method
13
+ options[:nested_boolean_style] = :nested if nested_boolean_style?
14
+
15
+ if options[:images]
16
+ options[:wrapper_html] ||= {}
17
+ options[:wrapper_html][:class] = "#{options[:wrapper_html][:class]} image_radio_buttons".strip
18
+ end
19
+
20
+ Inputs::EffectiveRadioButtons::Input.new(object, object_name, template, attribute_name, input_options, (merge_wrapper_options(input_html_options, wrapper_options) || {})).to_html
21
+ end
22
+
23
+ end
24
+
25
+ end
@@ -1,3 +1,3 @@
1
1
  module EffectiveFormInputs
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.0.1'.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.0
4
+ version: 1.0.1
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: 2016-12-07 00:00:00.000000000 Z
11
+ date: 2017-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -76,6 +76,7 @@ files:
76
76
  - app/assets/stylesheets/effective_date_time_picker/input.scss
77
77
  - app/assets/stylesheets/effective_date_time_picker/overrides.scss
78
78
  - app/assets/stylesheets/effective_form_inputs.scss
79
+ - app/assets/stylesheets/effective_radio_buttons/input.scss
79
80
  - app/assets/stylesheets/effective_select/bootstrap-theme.css
80
81
  - app/assets/stylesheets/effective_select/input.scss
81
82
  - app/assets/stylesheets/effective_select/overrides.scss
@@ -93,6 +94,8 @@ files:
93
94
  - app/models/inputs/effective_email_input.rb
94
95
  - app/models/inputs/effective_price/input.rb
95
96
  - app/models/inputs/effective_price_input.rb
97
+ - app/models/inputs/effective_radio_buttons/input.rb
98
+ - app/models/inputs/effective_radio_buttons_input.rb
96
99
  - app/models/inputs/effective_select/input.rb
97
100
  - app/models/inputs/effective_select_input.rb
98
101
  - app/models/inputs/effective_static_control/input.rb