Skip to content

Commit

Permalink
Merge pull request #43 from Noah-Silvera/support-zeitwerk-loading
Browse files Browse the repository at this point in the history
Support zeitwerk loading (0.18.0)
  • Loading branch information
Noah-Silvera authored Mar 17, 2021
2 parents 6fe0283 + 68b258a commit 644ea96
Show file tree
Hide file tree
Showing 20 changed files with 117 additions and 90 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ dist: trusty
sudo: false
language: ruby
cache: bundler
addons:
postgresql: "9.3"
jobs:
include:
- rvm: 2.5.8
env: SOLIDUS_BRANCH=v2.9 DB=postgresql RAILS_VERSION="~> 5.1.0"
- rvm: 2.6.6
env: SOLIDUS_BRANCH=v2.10 DB=postgresql RAILS_VERSION="~> 5.2.0"
- rvm: 2.6.6
env: SOLIDUS_BRANCH=v2.10 DB=postgresql RAILS_VERSION="~> 6.0.0"

before_install:
- gem update --system
- gem install bundler
17 changes: 16 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
# Changelog

## master
## v0.18.0

- [#21](https://github.com/SuperGoodSoft/solidus_taxjar/pull/21) Migrated project to `solidus_dev_support`
- [#22](https://github.com/SuperGoodSoft/solidus_taxjar/pull/22) Added support for TaxJar address validation API through `SuperGood::SolidusTaxJar::Addresses` class
- [#34](https://github.com/SuperGoodSoft/solidus_taxjar/pull/34) Include API version in request headers
- [#38](https://github.com/SuperGoodSoft/solidus_taxjar/pull/38) Added a rails engine to support future solidus backend UI
- [#43](https://github.com/SuperGoodSoft/solidus_taxjar/pull/43) Support zeitwerk loading

**Breaking Changes**:

- Module name `SolidusTaxJar` renamed to `SolidusTaxjar`
- Class name `API` renamed to `Api`
- Class name `APIParams` renamed to `ApiParams`

### Upgrading from 0.17.X to 0.18.X

If you're currently using version 0.17.X and want to upgrade to 0.18.X, follow these steps:

- Rename any instances of the module `SolidusTaxJar` to `SolidusTaxjar`
- Rename any instances of the class `API` to `Api`
- Rename any instances of the class `APIParams` to `ApiParams`

## v0.17.1

Expand Down
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# `SuperGood::SolidusTaxJar` [![Build Status](https://travis-ci.com/SuperGoodSoft/solidus_taxjar.svg?token=rc5QTgHvLLF7cpqkmyfd&branch=master)](https://travis-ci.com/SuperGoodSoft/solidus_taxjar)
# `SuperGood::SolidusTaxjar` [![Build Status](https://travis-ci.com/SuperGoodSoft/solidus_taxjar.svg?token=rc5QTgHvLLF7cpqkmyfd&branch=master)](https://travis-ci.com/SuperGoodSoft/solidus_taxjar)

`SuperGood::SolidusTaxJar` is a [Solidus](https://github.com/solidusio/solidus) extension that allows Solidus stores to use [TaxJar](https://www.taxjar.com/) for tax calculations.
`SuperGood::SolidusTaxjar` is a [Solidus](https://github.com/solidusio/solidus) extension that allows Solidus stores to use [TaxJar](https://www.taxjar.com/) for tax calculations.

This is not a fork of [spree_taxjar](https://github.com/vinsol-spree-contrib/spree_taxjar), like [solidus_taxjar](https://github.com/boomerdigital/solidus_taxjar). Instead of using a custom calculator, `SuperGood::SolidusTaxJar` uses the new configurable tax system [by @adammathys](https://github.com/solidusio/solidus/pull/1892) introduced in Solidus v2.4. This maps better to how the TaxJar API itself works.
This is not a fork of [spree_taxjar](https://github.com/vinsol-spree-contrib/spree_taxjar), like [solidus_taxjar](https://github.com/boomerdigital/solidus_taxjar). Instead of using a custom calculator, `SuperGood::SolidusTaxjar` uses the new configurable tax system [by @adammathys](https://github.com/solidusio/solidus/pull/1892) introduced in Solidus v2.4. This maps better to how the TaxJar API itself works.

## Installation

Expand All @@ -16,29 +16,36 @@ This is not a fork of [spree_taxjar](https://github.com/vinsol-spree-contrib/spr

$ bundle

2. Next, configure Solidus to use this gem:
2. Install and run the necessary migrations:

```shell
bundle exec rails g super_good:solidus_taxjar:install
bundle exec rake db:migrate
```

3. Next, configure Solidus to use this gem:

```ruby
# Put this in config/initializers/solidus.rb
Spree.config do |config|
config.tax_calculator_class = SuperGood::SolidusTaxJar::TaxCalculator
config.tax_calculator_class = SuperGood::SolidusTaxjar::TaxCalculator
end
```

3. Also, configure your error handling:
4. Also, configure your error handling:

```ruby
# Put this in config/initializers/taxjar.rb
SuperGood::SolidusTaxJar.exception_handler = ->(e) {
SuperGood::SolidusTaxjar.exception_handler = ->(e) {
# Report exceptions in here. For example, if you were using the Sentry's
# raven-ruby gem to report errors, you might do this:
Raven.capture_exception(e)
}
```

4. Finally, make sure that the `TAXJAR_API_KEY` environment variable is set to a your TaxJar API key and make sure that you have a `Spree::TaxRate` with the name "Sales Tax". This will be used as the source for the tax adjustments that Solidus creates.
5. Finally, make sure that the `TAXJAR_API_KEY` environment variable is set to a your TaxJar API key and make sure that you have a `Spree::TaxRate` with the name "Sales Tax". This will be used as the source for the tax adjustments that Solidus creates.

## Project Status

Expand All @@ -57,11 +64,11 @@ The extension provides currently two high level `calculator` classes that wrap t

### TaxCalculator

`SuperGood::SolidusTaxJar::TaxCalculator` allows calculating the full tax breakdown for a given `Spree::Order`. The breakdown includes separate line items taxes and shipment taxes.
`SuperGood::SolidusTaxjar::TaxCalculator` allows calculating the full tax breakdown for a given `Spree::Order`. The breakdown includes separate line items taxes and shipment taxes.

### TaxRateCalculator

`SuperGood::SolidusTaxJar::TaxRateCalculator` allows calculating the tax rate for a given `Spree::Address`. It relies on the same low-level Ruby TaxJar API endpoint of the tax calculator in order to provide the most coherent and reliable results. TaxJar support recommends using this endpoint for live calculations.
`SuperGood::SolidusTaxjar::TaxRateCalculator` allows calculating the tax rate for a given `Spree::Address`. It relies on the same low-level Ruby TaxJar API endpoint of the tax calculator in order to provide the most coherent and reliable results. TaxJar support recommends using this endpoint for live calculations.

## Development

Expand Down
8 changes: 4 additions & 4 deletions lib/super_good/solidus_taxjar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require "super_good/solidus_taxjar/addresses"

module SuperGood
module SolidusTaxJar
module SolidusTaxjar
class << self
attr_accessor :cache_duration
attr_accessor :cache_key
Expand All @@ -28,17 +28,17 @@ class << self
attr_accessor :test_mode

def api
::SuperGood::SolidusTaxJar::API.new
::SuperGood::SolidusTaxjar::Api.new
end
end

self.cache_duration = 3.hours
self.cache_key = ->(record) {
record_type = record.class.name.demodulize.underscore
APIParams.send("#{record_type}_params", record).to_json
ApiParams.send("#{record_type}_params", record).to_json
}
self.custom_order_params = ->(order) { {} }
self.discount_calculator = ::SuperGood::SolidusTaxJar::DiscountCalculator
self.discount_calculator = ::SuperGood::SolidusTaxjar::DiscountCalculator
self.exception_handler = ->(e) {
Rails.logger.error "An error occurred while fetching TaxJar tax rates - #{e}: #{e.message}"
}
Expand Down
4 changes: 2 additions & 2 deletions lib/super_good/solidus_taxjar/addresses.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module SuperGood
module SolidusTaxJar
module SolidusTaxjar
class Addresses
class << self
def normalize(spree_address)
Expand All @@ -11,7 +11,7 @@ def possibilities(spree_address)
end
end

def initialize(api: ::SuperGood::SolidusTaxJar.api)
def initialize(api: ::SuperGood::SolidusTaxjar.api)
@api = api
end

Expand Down
20 changes: 10 additions & 10 deletions lib/super_good/solidus_taxjar/api.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module SuperGood
module SolidusTaxJar
class API
module SolidusTaxjar
class Api
def self.default_taxjar_client
::Taxjar::Client.new(
api_key: ENV.fetch("TAXJAR_API_KEY"),
Expand All @@ -16,8 +16,8 @@ def initialize(taxjar_client: self.class.default_taxjar_client)
end

def tax_for(order)
taxjar_client.tax_for_order(APIParams.order_params(order)).tap do |taxes|
next unless SuperGood::SolidusTaxJar.logging_enabled
taxjar_client.tax_for_order(ApiParams.order_params(order)).tap do |taxes|
next unless SuperGood::SolidusTaxjar.logging_enabled

Rails.logger.info(
"TaxJar response for #{order.number}: #{taxes.to_h.inspect}"
Expand All @@ -26,31 +26,31 @@ def tax_for(order)
end

def tax_rate_for(address)
taxjar_client.tax_for_order(APIParams.tax_rate_address_params(address)).rate
taxjar_client.tax_for_order(ApiParams.tax_rate_address_params(address)).rate
end

def tax_rates_for(address)
taxjar_client.rates_for_location(*APIParams.address_params(address))
taxjar_client.rates_for_location(*ApiParams.address_params(address))
end

def create_transaction_for(order)
taxjar_client.create_order APIParams.transaction_params(order)
taxjar_client.create_order ApiParams.transaction_params(order)
end

def update_transaction_for(order)
taxjar_client.update_order APIParams.transaction_params(order)
taxjar_client.update_order ApiParams.transaction_params(order)
end

def delete_transaction_for(order)
taxjar_client.delete_order order.number
end

def create_refund_for(reimbursement)
taxjar_client.create_refund APIParams.refund_params(reimbursement)
taxjar_client.create_refund ApiParams.refund_params(reimbursement)
end

def validate_spree_address(spree_address)
taxjar_client.validate_address APIParams.validate_address_params(spree_address)
taxjar_client.validate_address ApiParams.validate_address_params(spree_address)
end

private
Expand Down
12 changes: 6 additions & 6 deletions lib/super_good/solidus_taxjar/api_params.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module SuperGood
module SolidusTaxJar
module APIParams
module SolidusTaxjar
module ApiParams
class << self
def order_params(order)
{}
.merge(customer_params(order))
.merge(order_address_params(order.tax_address))
.merge(line_items_params(order.line_items))
.merge(shipping: shipping(order))
.merge(SuperGood::SolidusTaxJar.custom_order_params.call(order))
.merge(SuperGood::SolidusTaxjar.custom_order_params.call(order))
.tap do |params|
next unless SuperGood::SolidusTaxJar.logging_enabled
next unless SuperGood::SolidusTaxjar.logging_enabled

Rails.logger.info(
"TaxJar params for #{order.number}: #{params.inspect}"
Expand Down Expand Up @@ -133,11 +133,11 @@ def valid_line_items(line_items)
end

def discount(line_item)
::SuperGood::SolidusTaxJar.discount_calculator.new(line_item).discount
::SuperGood::SolidusTaxjar.discount_calculator.new(line_item).discount
end

def shipping(order)
SuperGood::SolidusTaxJar.shipping_calculator.call(order)
SuperGood::SolidusTaxjar.shipping_calculator.call(order)
end

def sales_tax(order)
Expand Down
8 changes: 4 additions & 4 deletions lib/super_good/solidus_taxjar/calculator_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module SuperGood
module SolidusTaxJar
module SolidusTaxjar
module CalculatorHelper
extend ActiveSupport::Concern

Expand All @@ -16,22 +16,22 @@ def incomplete_address?(address)
end

def taxable_address?(address)
SuperGood::SolidusTaxJar.taxable_address_check.call(address)
SuperGood::SolidusTaxjar.taxable_address_check.call(address)
end

def cache
if !Rails.env.test?
Rails.cache.fetch(
cache_key,
expires_in: SuperGood::SolidusTaxJar.cache_duration
expires_in: SuperGood::SolidusTaxjar.cache_duration
) { yield }
else
yield
end
end

def exception_handler
SuperGood::SolidusTaxJar.exception_handler
SuperGood::SolidusTaxjar.exception_handler
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/super_good/solidus_taxjar/discount_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module SuperGood
module SolidusTaxJar
module SolidusTaxjar
class DiscountCalculator
def initialize(line_item)
@line_item = line_item
Expand Down
14 changes: 7 additions & 7 deletions lib/super_good/solidus_taxjar/tax_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module SuperGood
module SolidusTaxJar
module SolidusTaxjar
class TaxCalculator
include CalculatorHelper

def initialize(order, api: SuperGood::SolidusTaxJar.api)
def initialize(order, api: SuperGood::SolidusTaxjar.api)
@order = order
@api = api
end

def calculate
return no_tax if SuperGood::SolidusTaxJar.test_mode
return no_tax if SuperGood::SolidusTaxjar.test_mode
return no_tax if incomplete_address?(order.tax_address) || order.line_items.none?
return no_tax unless taxable_order? order
return no_tax unless taxable_address? order.tax_address
Expand Down Expand Up @@ -114,22 +114,22 @@ def tax_rate
end

def cache_key
SuperGood::SolidusTaxJar.cache_key.call(order)
SuperGood::SolidusTaxjar.cache_key.call(order)
end

def taxable_order?(order)
SuperGood::SolidusTaxJar.taxable_order_check.call(order)
SuperGood::SolidusTaxjar.taxable_order_check.call(order)
end

def shipping_tax_label(shipment, shipping_tax)
SuperGood::SolidusTaxJar.shipping_tax_label_maker.call(
SuperGood::SolidusTaxjar.shipping_tax_label_maker.call(
shipment,
shipping_tax
)
end

def line_item_tax_label(taxjar_line_item, spree_line_item)
SuperGood::SolidusTaxJar.line_item_tax_label_maker.call(taxjar_line_item, spree_line_item)
SuperGood::SolidusTaxjar.line_item_tax_label_maker.call(taxjar_line_item, spree_line_item)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions lib/super_good/solidus_taxjar/tax_rate_calculator.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module SuperGood
module SolidusTaxJar
module SolidusTaxjar
class TaxRateCalculator
include CalculatorHelper
def initialize(address, api: SuperGood::SolidusTaxJar.api)
def initialize(address, api: SuperGood::SolidusTaxjar.api)
@address = address
@api = api
end

def calculate
return no_rate if SuperGood::SolidusTaxJar.test_mode
return no_rate if SuperGood::SolidusTaxjar.test_mode
return no_rate if incomplete_address?(address)
return no_rate unless taxable_address?(address)
cache do
Expand All @@ -28,7 +28,7 @@ def no_rate
end

def cache_key
SuperGood::SolidusTaxJar.cache_key.call(address)
SuperGood::SolidusTaxjar.cache_key.call(address)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/super_good/solidus_taxjar/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module SuperGood
module SolidusTaxJar
VERSION = "0.17.1"
module SolidusTaxjar
VERSION = "0.18.0"
end
end
6 changes: 3 additions & 3 deletions spec/super_good/solidus_taxjar/addresses_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "spec_helper"

RSpec.describe SuperGood::SolidusTaxJar::Addresses do
RSpec.describe SuperGood::SolidusTaxjar::Addresses do
describe "#normalize" do
subject { described_class.new(api: dummy_api).normalize(spree_address) }

Expand Down Expand Up @@ -38,7 +38,7 @@
}

let(:dummy_api) {
instance_double ::SuperGood::SolidusTaxJar::API
instance_double ::SuperGood::SolidusTaxjar::Api
}

context "when there are no possibilities for the address" do
Expand Down Expand Up @@ -171,7 +171,7 @@
}

let(:dummy_api) {
instance_double ::SuperGood::SolidusTaxJar::API
instance_double ::SuperGood::SolidusTaxjar::Api
}

context "when there are no possibilities for the address" do
Expand Down
Loading

0 comments on commit 644ea96

Please sign in to comment.