Sha256: d03833db060de86092b0b33768d4fc6074683b1626afd838e48874af685c53af

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module SolidusZipZones
  module ZoneDecorator
    def self.prepended(base)
      base.scope :with_member_ids, ->(state_ids, country_ids, zipcode) do
        if state_ids.blank? && country_ids.blank? && zipcode.blank?
          none
        else
          if country_ids == 233
            zipcode = zipcode.split('-').first
          end
          spree_zone_members_table = Spree::ZoneMember.arel_table
          matching_state =
            spree_zone_members_table[:zoneable_type].eq("Spree::State").
            and(spree_zone_members_table[:zoneable_id].in(state_ids))
          matching_country =
            spree_zone_members_table[:zoneable_type].eq("Spree::Country").
            and(spree_zone_members_table[:zoneable_id].in(country_ids))
          spree_zones_table = Spree::Zone.arel_table
          matching_zipcode = spree_zones_table[:zipcodes].matches("%#{zipcode}%")
          left_joins(:zone_members).where(matching_zipcode.or(matching_state.or(matching_country))).distinct
        end
      end

      base.scope :for_address, ->(address) do
        if address
          with_member_ids(address.state_id, address.country_id, address.zipcode)
        else
          none
        end
      end
    end

    def kind
      return 'zip' if zipcodes.present?

      super
    end

    Spree::Zone.prepend self
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_zip_zones-0.1.0 app/models/solidus_zip_zones/zone_decorator.rb