effective_bootstrap 0.3.3 → 0.3.4

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: a836e789cb8b0bee567ef732e5b368ca39f8c9ca
4
- data.tar.gz: 0f9450c9974758cb363a80324384a870936e5901
3
+ metadata.gz: 814a553c811fcf629ccae64c8b7600b3c17bd434
4
+ data.tar.gz: 7582182315089cd457742ec96afec30659ace1f7
5
5
  SHA512:
6
- metadata.gz: 2106312ad8bb61dd044bd50c63a72db6d7a6c1f05830e2610e3875d21f8de969467d6ca32dae6f5767d2dd3d7a5ef3fcded9f5892195d5070486c6091fd86db3
7
- data.tar.gz: f239fc4e6d5afcc5cb75bdf0470f6dfb1f9653b0b5ba6441afaef80bf45b7632ad69c2d3353a9775c36bfb63a50de50064cdaff8dbee234ad4fd99fb95c42a15
6
+ metadata.gz: a27f9df29e5f7e9912c276a139dd43900a73ad374193d3d47de6e7cf3c16be78f94c60eb169c903b7c9351e7e09ad31c231b848ba842b118ed91abfbd1aeba70
7
+ data.tar.gz: 68aa49cbba726414afba0c9465fa29f333ae1c88420ca5570bdd950b417515297275848d731ad5c2fc26c1cf38c2d1836c7cbb1889d6859acddaabcf587d3823
data/README.md CHANGED
@@ -437,6 +437,37 @@ $(document).on 'change', '.something', (event) ->
437
437
 
438
438
  There is currently no support for using AJAX to load remote data. This feature is supported by the underlying select2 library and will be implemented here at a future point.
439
439
 
440
+ ## Custom select_or_text_field
441
+
442
+ This custom form input is unique. It takes in two different field names, one of them a select, the other a text field.
443
+
444
+ It enforces an `XOR` between the two fields.
445
+
446
+ It's intended for selecting a `belongs_to` or using a freeform text field pattern.
447
+
448
+ This custom form input uses no 3rd party jQuery plugins.
449
+
450
+ ```haml
451
+ = f.select_or_text_field :post_id, :post_text, Post.all
452
+ = f.select_or_text_field :post_id, :post_text, Post.all, hint: 'Both select and text field will see this hint'
453
+ = f.select_or_text_field :post_id, :post_text, Post.all, select: { hint: 'select only options' }, text: { hint: 'text field only options'}
454
+ ```
455
+
456
+ The `f.object` should have two separate attributes, `post_id` and `post_text`.
457
+
458
+ The javascript form input will enforce XOR, but you can also apply your own validation to also have the same effect as `required: true`
459
+
460
+ ```
461
+ class PostSummary < ApplicationRecord
462
+ validate do
463
+ unless (post_id.present? ^ post_text.present?) # xor
464
+ self.errors.add(:post_id, 'please choose either post or post text')
465
+ self.errors.add(:post_text, 'please choose either post or post text')
466
+ end
467
+ end
468
+ end
469
+ ```
470
+
440
471
  ## Custom submit and save
441
472
 
442
473
  The `f.submit` puts in a wrapper and a default save button, and does the whole icon spin when submit thing.
@@ -14,3 +14,4 @@
14
14
  //= require ./effective_price/input
15
15
  //= require ./effective_radio/input
16
16
  //= require ./effective_select/input
17
+ //= require ./effective_select_or_text/input
@@ -0,0 +1,15 @@
1
+ $(document).on 'click', '[data-effective-select-or-text]', (event) ->
2
+ $obj = $(event.currentTarget).closest('.effective-select-or-text')
3
+
4
+ $visible = $obj.children('.form-group:visible').first()
5
+ $hidden = $obj.children('.form-group:not(:visible)').first()
6
+
7
+ $visible.fadeOut('fast', ->
8
+ $visible.find('select').val('').trigger('change.select2')
9
+ $visible.find('input').val('')
10
+
11
+ $hidden.slideDown('fast')
12
+ )
13
+
14
+ false
15
+
@@ -0,0 +1 @@
1
+ //= require ./initialize
@@ -8,4 +8,5 @@
8
8
  @import 'effective_editor/input';
9
9
  @import 'effective_file/input';
10
10
  @import 'effective_select/input';
11
+ @import 'effective_select_or_text/input';
11
12
  @import 'effective_radio/input';
@@ -0,0 +1,6 @@
1
+ .effective-select-or-text {
2
+ display: flex;
3
+
4
+ > .form-group { flex-grow: 1; }
5
+ > a { margin: 2rem 0 0 1rem; }
6
+ }
@@ -106,6 +106,11 @@ module Effective
106
106
  Effective::FormInputs::Select.new(name, options, builder: self).to_html
107
107
  end
108
108
 
109
+ def select_or_text(name, name_text, choices = nil, *args)
110
+ options = args.extract_options!.merge!(name_text: name_text, collection: choices)
111
+ Effective::FormInputs::SelectOrText.new(name, options, builder: self).to_html
112
+ end
113
+
109
114
  def submit(name = 'Save', options = {}, &block)
110
115
  (options = name; name = 'Save') if name.kind_of?(Hash)
111
116
  Effective::FormInputs::Submit.new(name, options, builder: self).to_html(&block)
@@ -0,0 +1,56 @@
1
+ module Effective
2
+ module FormInputs
3
+ class SelectOrText < Effective::FormInput
4
+ attr_accessor :name_text
5
+ attr_accessor :select_collection
6
+ attr_accessor :select_options
7
+ attr_accessor :text_options
8
+
9
+ VISIBLE = {}
10
+ HIDDEN = { wrapper: { style: 'display: none;' } }
11
+
12
+ def initialize(name, options, builder:)
13
+ @name_text = options.delete(:name_text) || raise('Please include a text method name')
14
+ @select_collection = options.delete(:collection) || raise('Please include a collection')
15
+
16
+ @select_options = { placeholder: 'Please choose, or...', required: false }
17
+ .merge(options[:select] || options.presence || {})
18
+
19
+ @text_options = { placeholder: 'Enter freeform', required: false }
20
+ .merge(options[:text] || options[:text_field] || options.presence || {})
21
+
22
+ super
23
+ end
24
+
25
+ def to_html(&block)
26
+ content_tag(:div, class: 'effective-select-or-text') do
27
+ @builder.select(name, select_collection, select_options) +
28
+ @builder.text_field(name_text, text_options) +
29
+ link_to(icon('rotate-ccw'), '#', class: 'effective-select-or-text-switch', title: 'Switch between choice and freeform', 'data-effective-select-or-text': true)
30
+ end
31
+ end
32
+
33
+ def select?
34
+ return true if object.errors[name].present?
35
+ return false if object.errors[name_text].present?
36
+
37
+ object.send(name).present? || (object.send(name).blank? && object.send(name_text).blank?)
38
+ end
39
+
40
+ def text?
41
+ return false if object.errors[name].present?
42
+
43
+ object.send(name_text).present? || object.errors[name_text].present?
44
+ end
45
+
46
+ def select_options
47
+ @select_options.merge(select? ? VISIBLE : HIDDEN)
48
+ end
49
+
50
+ def text_options
51
+ @text_options.merge(text? ? VISIBLE : HIDDEN)
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module EffectiveBootstrap
2
- VERSION = '0.3.3'.freeze
2
+ VERSION = '0.3.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_bootstrap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
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: 2018-12-02 00:00:00.000000000 Z
11
+ date: 2018-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -401,6 +401,8 @@ files:
401
401
  - app/assets/javascripts/effective_select/input.js
402
402
  - app/assets/javascripts/effective_select/overrides.js.coffee
403
403
  - app/assets/javascripts/effective_select/select2.js
404
+ - app/assets/javascripts/effective_select_or_text/initialize.js.coffee
405
+ - app/assets/javascripts/effective_select_or_text/input.js
404
406
  - app/assets/javascripts/effective_time/initialize.js.coffee
405
407
  - app/assets/javascripts/effective_time/input.js
406
408
  - app/assets/stylesheets/effective_bootstrap.scss
@@ -422,6 +424,7 @@ files:
422
424
  - app/assets/stylesheets/effective_select/input.scss
423
425
  - app/assets/stylesheets/effective_select/overrides.scss
424
426
  - app/assets/stylesheets/effective_select/select2.css
427
+ - app/assets/stylesheets/effective_select_or_text/input.scss
425
428
  - app/assets/stylesheets/effective_time/input.scss
426
429
  - app/helpers/effective_bootstrap_helper.rb
427
430
  - app/helpers/effective_editor_helper.rb
@@ -451,6 +454,7 @@ files:
451
454
  - app/models/effective/form_inputs/remote_link_to.rb
452
455
  - app/models/effective/form_inputs/save.rb
453
456
  - app/models/effective/form_inputs/select.rb
457
+ - app/models/effective/form_inputs/select_or_text.rb
454
458
  - app/models/effective/form_inputs/static_field.rb
455
459
  - app/models/effective/form_inputs/submit.rb
456
460
  - app/models/effective/form_inputs/text_area.rb