Sha256: 96932f311405d584964f002255b3f34fefd518a77980c5e8442478a6131fb6ff

Contents?: true

Size: 1.87 KB

Versions: 2

Compression:

Stored size: 1.87 KB

Contents

require 'carmen-rails'

module EffectiveAddressesHelper
  def effective_address_fields(form, method = 'billing', options = {})
    method = (method.to_s.include?('_address') ? method.to_s : "#{method}_address")

    required = (form.object._validators[method.to_sym].any? { |v| v.kind_of?(ActiveRecord::Validations::PresenceValidator) && (v.options[:if].blank? || (v.options[:if].respond_to?(:call) ? f.object.instance_exec(&v.options[:if]) : v.options[:if])) } rescue true)
    use_full_name = form.object._validators[method.to_sym].any? { |v| v.kind_of?(EffectiveAddressFullNamePresenceValidator) }

    opts = {:f => form, :method => method, :required => required, :use_full_name => use_full_name}.merge(options)

    if form.class.name == 'SimpleForm::FormBuilder'
      render :partial => 'effective/addresses/address_fields_simple_form', :locals => opts
    elsif form.class.name == 'Formtastic::FormBuilder'
      render :partial => 'effective/addresses/address_fields_formtastic', :locals => opts
    else
      raise 'Unsupported FormBuilder.  You must use formtastic or simpleform. Sorry.'
    end
  end

  def region_options_for_simple_form_select(regions = nil)
    if regions.present?
      countries = regions
    elsif EffectiveAddresses.country_codes == :all
      countries = Carmen::Country.all
    else
      countries = Carmen::Country.all.select { |c| (EffectiveAddresses.country_codes || []).include?(c.code) }
    end

    collection = countries.map { |c| [c.name, c.code] }.sort! { |a, b| a.first <=> b.first }

    if regions.blank? && EffectiveAddresses.country_codes_priority.present?
      collection.insert(0, ['---------------------', '', :disabled])

      EffectiveAddresses.country_codes_priority.reverse.each do |code|
        if (country = countries.coded(code))
          collection.insert(0, [country.name, country.code])
        end
      end
    end

    collection
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
effective_addresses-1.1.0 app/helpers/effective_addresses_helper.rb
effective_addresses-1.0.6 app/helpers/effective_addresses_helper.rb