Sha256: 426eadd83fc21b67a6f0902853e6df8e19894da0e1b26b96edd63e5c2a85b88c

Contents?: true

Size: 1.69 KB

Versions: 3

Compression:

Stored size: 1.69 KB

Contents

module Effective
  module FormBuilderInputs
    class EffectivePrice < Effective::FormBuilderInput
      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

3 entries across 3 versions & 1 rubygems

Version Path
effective_form_inputs-1.5.0 app/models/effective/form_builder_inputs/effective_price.rb
effective_form_inputs-1.4.1 app/models/effective/form_builder_inputs/effective_price.rb
effective_form_inputs-1.4.0 app/models/effective/form_builder_inputs/effective_price.rb