effective_form_inputs 0.6.4 → 0.7.0

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: 3cea3031378c53e9054780a34f0829e2097ba2d7
4
- data.tar.gz: 5001cad0a253ce6d7dde3d7754e280b9209ca249
3
+ metadata.gz: 23128ed6bfc6abb1d0e7974489405d15958cab24
4
+ data.tar.gz: 633a4c5fd1831f215f5659ae2fcfca6ff10ee8de
5
5
  SHA512:
6
- metadata.gz: cf90a2c883b98b0bd2e3006f6ad0e68a5bf97f50454e019f9c4393a66101e79cf02be54b413792f1d93dca02b4f605b40303150e3755c634d866c2f44ab77b05
7
- data.tar.gz: 027fc8403328fb66737b7a3f16d3f658c7257e4277a20a386ee02f3c8ec6193c1dec3bad1bdde01b5db6a5b1804fcf7dd5e5262fada021c687363d36387c8649
6
+ metadata.gz: cdff9165217e98224a7f8c904cbe0dab4111bd8c81d4760a06b548d959affaa21a466e360caf7e76a7ff6bed77fd1a9668d588e2d0cfa4e8b93be3f63dec4a75
7
+ data.tar.gz: 54472a6f2f159ce33afe5962d06f157808576b5e65bae47623200cbbbe656c47cc4aaae51228ceedf5e96b1e2ca33eafcf97a907423a365118a59756c361e28e
data/README.md CHANGED
@@ -331,32 +331,60 @@ As a SimpleForm input without the input group (phone glyphicon)
331
331
  = f.input :phone, :as => :effective_tel, :input_group => false
332
332
  ```
333
333
 
334
- ### Options
334
+ ## Effective Price
335
+
336
+ This custom form input uses no 3rd party jQuery plugins.
337
+
338
+ It displays a currency formatted value `100.00` but posts the "price as integer" value of `10000` to the server.
339
+
340
+ Think about this value as "the number of cents".
341
+
342
+
343
+ ### Installation
335
344
 
336
- By default, this form input uses the `glyphicon-earphone` glyphicon and accepts a telephone number with optional 5-digit extension.
345
+ If you've already installed the 'All Form Inputs' assets (see above), there are no additional installation steps.
337
346
 
338
- It will set up a mask as follows:
347
+ To install this form input individually, add the following to your application.js:
339
348
 
340
349
  ```ruby
341
- :input_js => {:mask => '(999) 999-9999? x99999'}
350
+ //= require effective_price/input
342
351
  ```
343
352
 
344
- Calling the form input with `:cellphone => true` will use the `glyphicon-phone` glyphicon and accept only a telephone number.
353
+ ### Usage
345
354
 
346
- It will set up a mask as follows:
355
+ As a Rails Form Helper input:
347
356
 
348
357
  ```ruby
349
- :input_js => {:mask => '(999) 999-9999'}
358
+ = form_for @product do |f|
359
+ = f.effective_price :price
350
360
  ```
351
361
 
352
- The only available javascript options are `mask` and `placeholder`:
362
+ As a SimpleForm input:
353
363
 
354
364
  ```ruby
355
- :input_js => {:mask => '...', :placeholder => '_'}
365
+ = simple_form_for @product do |f|
366
+ = f.input :price, :as => :effective_price
356
367
  ```
357
368
 
369
+ As a SimpleForm input without the input group (glyphicon-usd glyphicon)
370
+
371
+ ```ruby
372
+ = simple_form_for @product do |f|
373
+ = f.input :price, :as => :effective_price, :input_group => false
374
+ ```
375
+
376
+ ### Options
377
+
378
+ There are no javascript options for this input.
379
+
380
+
381
+ ### Rails Helper
382
+
383
+ This input also installs a rails view helper `price_to_currency` that takes a value like `10000` and displays it as `$100.00`
384
+
385
+
358
386
 
359
- ## Passing Options to JavaScript
387
+ ## Passing Options to JavaScript Behaviour
360
388
 
361
389
  All `:input_js => options` passed to any effective_form_input will be used to initialize the Javascript library
362
390
 
@@ -1,4 +1,5 @@
1
1
  //= require ./effective_date_time_picker/input
2
2
  //= require ./effective_date_picker/input
3
+ //= require ./effective_price/input
3
4
  //= require ./effective_select/input
4
5
  //= require ./effective_tel/input
@@ -0,0 +1,37 @@
1
+ initialize = ->
2
+ $('input.effective_price:not(.initialized)').each (i, element) ->
3
+ element = $(element)
4
+ options = element.data('input-js-options') || {}
5
+
6
+ # We don't actually do anything
7
+ element.addClass('initialized')
8
+
9
+ $ -> initialize()
10
+ $(document).on 'page:change', -> initialize()
11
+ $(document).on 'cocoon:after-insert', -> initialize()
12
+
13
+
14
+ # Prevent non-currency buttons from being pressed
15
+ $(document).on 'keydown', "input[type='text'].effective_price", (event) ->
16
+ allowed = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', ',', '.']
17
+
18
+ if event.key.length == 1 && event.metaKey == false && allowed.indexOf(event.key) == -1
19
+ event.preventDefault()
20
+
21
+
22
+ # Format the value for display as currency (USD)
23
+ $(document).on 'change', "input[type='text'].effective_price", (event) ->
24
+ value = parseFloat($(event.target).val().replace(',', ''))
25
+
26
+ if isNaN(value) == false
27
+ value = value.toFixed(2).toString()
28
+ value = value.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2') while (/(\d+)(\d{3})/.test(value))
29
+ else
30
+ value = ''
31
+
32
+ $(event.target).val(value)
33
+
34
+ # Assign the hidden input a value of 100x value
35
+ $(document).on 'change keyup', "input[type='text'].effective_price", (event) ->
36
+ value = (parseFloat($(event.target).val().replace(',', '')) || 0.00) * 100.00
37
+ $(event.target).siblings("input[type='hidden']").first().val(value.toFixed(0))
@@ -0,0 +1 @@
1
+ //= require ./initialize
@@ -0,0 +1,7 @@
1
+ module EffectiveFormInputsHelper
2
+ def price_to_currency(price)
3
+ price = price || 0
4
+ raise 'price_to_currency expects an Integer representing the number of cents' unless price.kind_of?(Integer)
5
+ number_to_currency(price / 100.0)
6
+ end
7
+ end
@@ -8,6 +8,10 @@ module Effective
8
8
  Inputs::EffectiveDatePicker::Input.new(@object, @object_name, @template, method, options, options).to_html
9
9
  end
10
10
 
11
+ def effective_price(method, options = {})
12
+ Inputs::EffectivePrice::Input.new(@object, @object_name, @template, method, options, options).to_html
13
+ end
14
+
11
15
  def effective_static_control(method, options = {})
12
16
  Inputs::EffectiveStaticControl::Input.new(@object, @object_name, @template, method, options, options).to_html
13
17
  end
@@ -0,0 +1,42 @@
1
+ module Inputs
2
+ module EffectivePrice
3
+ class Input < Effective::FormInput
4
+ delegate :content_tag, :number_to_currency, :text_field_tag, :hidden_field_tag, :to => :@template
5
+
6
+ def default_input_html
7
+ {class: 'effective_price numeric', maxlength: 14}
8
+ end
9
+
10
+ def to_html
11
+ if options[:input_group] == false
12
+ return text_field_tag(field_name, value, tag_options) + hidden_field_tag(field_name, price, id: price_field)
13
+ end
14
+
15
+ content_tag(:div, class: 'input-group') do
16
+ content_tag(:span, '$', class: 'input-group-addon') do
17
+ content_tag(:i, '', class: 'glyphicon glyphicon-usd').html_safe
18
+ end +
19
+ text_field_tag(field_name, number_to_currency(value, unit: ''), tag_options) +
20
+ hidden_field_tag(field_name, price, id: price_field)
21
+ end
22
+ end
23
+
24
+ def value
25
+ val = (@value || 0) # This is 'super'
26
+ val.kind_of?(Integer) ? ('%.2f' % (val / 100.0)) : ('%.2f' % val)
27
+ end
28
+
29
+ # These two are for the hidden input
30
+ def price_field
31
+ "#{field_name.parameterize.gsub('-', '_')}_value_as_integer"
32
+ end
33
+
34
+ def price
35
+ val = (@value || 0) # This is 'super'
36
+ val.kind_of?(Integer) ? val : (val * 100.0).to_i
37
+ end
38
+
39
+ end
40
+ end
41
+ end
42
+
@@ -0,0 +1,12 @@
1
+ # = simple_form_for @thing do |f|
2
+ # = f.input :price, :as => :effective_price
3
+
4
+ if defined?(SimpleForm)
5
+
6
+ class EffectivePriceInput < SimpleForm::Inputs::StringInput
7
+ def input(wrapper_options = nil)
8
+ Inputs::EffectivePrice::Input.new(object, object_name, template, attribute_name, input_options, (merge_wrapper_options(input_html_options, wrapper_options) || {})).to_html
9
+ end
10
+ end
11
+
12
+ end
@@ -4,7 +4,7 @@ module EffectiveFormInputs
4
4
 
5
5
  config.autoload_paths += Dir["#{config.root}/app/models/**/"]
6
6
 
7
- initializer 'effective_orders.action_view' do |app|
7
+ initializer 'effective_form_inputs.action_view' do |app|
8
8
  Rails.application.config.to_prepare do
9
9
  ActiveSupport.on_load :action_view do
10
10
  ActionView::Helpers::FormBuilder.send(:include, Effective::FormBuilderInputs)
@@ -12,5 +12,15 @@ module EffectiveFormInputs
12
12
  end
13
13
  end
14
14
 
15
+ # Include Helpers to base application
16
+ initializer 'effective_form_inputs.action_controller' do |app|
17
+ Rails.application.config.to_prepare do
18
+ ActiveSupport.on_load :action_controller do
19
+ helper EffectiveFormInputsHelper
20
+ end
21
+ end
22
+ end
23
+
24
+
15
25
  end
16
26
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveFormInputs
2
- VERSION = '0.6.4'.freeze
2
+ VERSION = '0.7.0'.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: 0.6.4
4
+ version: 0.7.0
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: 2015-08-05 00:00:00.000000000 Z
11
+ date: 2015-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -56,6 +56,8 @@ files:
56
56
  - app/assets/javascripts/effective_date_time_picker/moment.js
57
57
  - app/assets/javascripts/effective_date_time_picker/overrides.js.coffee
58
58
  - app/assets/javascripts/effective_form_inputs.js
59
+ - app/assets/javascripts/effective_price/initialize.js.coffee
60
+ - app/assets/javascripts/effective_price/input.js
59
61
  - app/assets/javascripts/effective_select/initialize.js.coffee
60
62
  - app/assets/javascripts/effective_select/input.js
61
63
  - app/assets/javascripts/effective_select/overrides.js.coffee
@@ -72,12 +74,15 @@ files:
72
74
  - app/assets/stylesheets/effective_select/input.scss
73
75
  - app/assets/stylesheets/effective_select/overrides.scss
74
76
  - app/assets/stylesheets/effective_select/select2.css
77
+ - app/helpers/effective_form_inputs_helper.rb
75
78
  - app/models/effective/form_builder_inputs.rb
76
79
  - app/models/effective/form_input.rb
77
80
  - app/models/inputs/effective_date_picker/input.rb
78
81
  - app/models/inputs/effective_date_picker_input.rb
79
82
  - app/models/inputs/effective_date_time_picker/input.rb
80
83
  - app/models/inputs/effective_date_time_picker_input.rb
84
+ - app/models/inputs/effective_price/input.rb
85
+ - app/models/inputs/effective_price_input.rb
81
86
  - app/models/inputs/effective_select/input.rb
82
87
  - app/models/inputs/effective_select_input.rb
83
88
  - app/models/inputs/effective_static_control/input.rb