Sha256: 95559296e655668d5ce6fcabd7de5f37308f0538c49c737528d90377efdebcf7

Contents?: true

Size: 1.67 KB

Versions: 47

Compression:

Stored size: 1.67 KB

Contents

module Inputs
  module EffectivePrice
    class Input < Effective::FormInput
      delegate :content_tag, :number_to_currency, :text_field_tag, :hidden_field_tag, :to => :@template

      def default_options
        { include_blank: false }
      end

      def default_input_html
        { class: 'effective_price numeric', maxlength: 14, autocomplete: 'off' }
      end

      def to_html
        if options[:input_group] == false
          return text_field_tag(field_name, number_to_currency(value, unit: ''), tag_options) + hidden_field_tag(field_name, price, id: price_field)
        end

        content_tag(:div, class: 'input-group') do
          content_tag(:span, '$', class: 'input-group-addon') do
            content_tag(:i, '', class: 'glyphicon glyphicon-usd').html_safe
          end +
          text_field_tag(field_name, number_to_currency(value, unit: ''), tag_options) +
          hidden_field_tag(field_name, price, id: price_field)
        end
      end

      def value
        return nil if (@value == nil && options[:include_blank])

        val = (@value || 0) # This is 'super'
        val.kind_of?(Integer) ? ('%.2f' % (val / 100.0)) : ('%.2f' % val)
      end

      # These two are for the hidden input
      def price_field
        "#{field_name.parameterize.gsub('-', '_')}_value_as_integer"
      end

      def price
        return nil if (@value == nil && options[:include_blank])

        val = (@value || 0) # This is 'super'
        val.kind_of?(Integer) ? val : (val * 100.0).to_i
      end

      def html_options
        super().tap do |html_options|
          html_options['data-include-blank'] = options[:include_blank]
        end
      end

    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
effective_form_inputs-1.3.0 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.9 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.8 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.7 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.6 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.5 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.4 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.3 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.2 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.1 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.2.0 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.1.15 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.1.14 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.1.13 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.1.12 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.1.11 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.1.10 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.1.9 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.1.8 app/models/inputs/effective_price/input.rb
effective_form_inputs-1.1.7 app/models/inputs/effective_price/input.rb