Skip to content

Commit

Permalink
Update extension with new standards
Browse files Browse the repository at this point in the history
  • Loading branch information
aldesantis committed Jul 24, 2020
1 parent efd2fd0 commit 4def45b
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 83 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ pkg
*.swp
spec/dummy
spec/examples.txt
/sandbox
.rvmrc
.ruby-version
.ruby-gemset
7 changes: 7 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ else
end

gemspec

# Use a local Gemfile to include development dependencies that might not be
# relevant for the project or for other contributors, e.g. pry-byebug.
#
# We use `send` instead of calling `eval_gemfile` to work around an issue with
# how Dependabot parses projects: https://github.com/dependabot/dependabot-core/issues/1658.
send(:eval_gemfile, 'Gemfile-local') if File.exist? 'Gemfile-local'
97 changes: 78 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,103 @@

[![CircleCI](https://circleci.com/gh/solidusio-contrib/solidus_easypost.svg?style=svg)](https://circleci.com/gh/solidusio-contrib/solidus_easypost)

This is an extension to integrate EasyPost into Spree. Due to how it works, you will not be able to use any other extension than this for shipping methods. Your own shipping methods will not work, either. But the good thing is that you won't have to worry about that, because EasyPost handles it all for you.
This is an extension to integrate EasyPost into Spree.

Due to how it works, you will not be able to use any other extension for shipping methods. Your own
shipping methods will not work, either. But the good thing is that you won't have to worry about
that, because EasyPost handles it all for you.

You will need to [sign up for an account](https://www.easypost.com/) to use this extension.

## Installation

This goes in your `Gemfile`:
Add solidus_easypost to your Gemfile:

```ruby
gem 'solidus_easypost'
gem 'solidus_easypost'
```

This goes in your terminal:
Bundle your dependencies and run the installation generator:

```shell
bin/rails generate solidus_easypost:install
```

Create an initializer at `config/initializers/solidus_easypost.rb`:

```ruby
rake railties:install:migrations
rake db:migrate
EasyPost.api_key = 'YOUR_API_KEY_HERE'
```

This goes into a new file called `config/initializers/easy_post.rb`:
## Usage

This extension hijacks `Spree::Stock::Estimator#shipping_rates` to calculate shipping rates for your
orders. This call happens during the checkout process, once the order's address information has been
provided.

The extension also adds a callback to the `ship` event on the `Spree::Shipment` model, telling
EasyPost which rate was selected and "buying" that rate. This can be disabled by setting:

```ruby
EasyPost.api_key = 'YOUR_API_KEY_HERE'
# config/initializers/solidus_easypost.rb
SolidusEasypost.configure do |config|
config.purchase_labels = false
end
```

This gem will create shipping methods for each type of carrier/service for which it receives a rate
from the EasyPost API. These are set to `display_on: back_end` by default and must be set to
`front_end` or `both` before the rate will be visible on the delivery page of the checkout.

## Usage
## Development

This extension hijacks `Spree::Stock::Estimator#shipping_rates` to calculate shipping rates for your orders. This call happens during the checkout process, once the order's address information has been provided.
The extension also adds a callback to the "ship" event on the `Shipment` model, telling EasyPost which rate was selected and "buying" that rate. This can be disabled by setting:
### Testing the extension

First bundle your dependencies, then run `bin/rake`. `bin/rake` will default to building the dummy
app if it does not exist, then it will run specs. The dummy app can be regenerated by using
`bin/rake extension:test_app`.

```shell
bin/rake
```

To run [Rubocop](https://github.com/bbatsov/rubocop) static code analysis run

```shell
bundle exec rubocop
```

When testing your application's integration with this extension you may use its factories.
Simply add this require statement to your spec_helper:

```ruby
# config/initializers/easy_post.rb
SolidusEasypost.configure do |config|
config.purchase_labels = false
end
require 'solidus_easypost/factories'
```

This gem will create shipping methods for each type of carrier/service for which it receives a rate from the EasyPost API. These are set to `display_on: back_end` by default and must be set to `front_end`
or `both` before the rate will be visible on the delivery page of the checkout.
### Running the sandbox

To run this extension in a sandboxed Solidus application, you can run `bin/sandbox`. The path for
the sandbox app is `./sandbox` and `bin/rails` will forward any Rails commands to
`sandbox/bin/rails`.

Here's an example:

```
$ bin/rails server
=> Booting Puma
=> Rails 6.0.2.1 application starting in development
* Listening on tcp://127.0.0.1:3000
Use Ctrl-C to stop
```

### Releasing new versions

Your new extension version can be released using `gem-release` like this:

```shell
bundle exec gem bump -v VERSION --tag --push --remote upstream && gem release
```

## Issues
## License

Please let me know if you find any issues in [the usual places](https://github.com/solidusio-contrib/solidus_easypost/issues), with [the usual information](https://github.com/solidusio/solidus/blob/master/CONTRIBUTING.md).
Copyright (c) 2020 Brendan Deere, released under the New BSD License.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
require 'solidus_dev_support/rake_tasks'
SolidusDevSupport::RakeTasks.install

task default: %w[extension:test_app extension:specs]
task default: 'extension:specs'
16 changes: 4 additions & 12 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
#!/usr/bin/env ruby

# frozen_string_literal: true

app_root = 'spec/dummy'

unless File.exist? "#{app_root}/bin/rails"
system "bin/rake", app_root or begin # rubocop:disable Style/AndOr
warn "Automatic creation of the dummy app failed"
exit 1
end
if %w[g generate].include? ARGV.first
exec "#{__dir__}/rails-engine", *ARGV
else
exec "#{__dir__}/rails-sandbox", *ARGV
end

Dir.chdir app_root
exec 'bin/rails', *ARGV
13 changes: 13 additions & 0 deletions bin/rails-engine
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.

ENGINE_ROOT = File.expand_path('..', __dir__)
ENGINE_PATH = File.expand_path('../lib/solidus_easypost/engine', __dir__)

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])

require 'rails/all'
require 'rails/engine/commands'
16 changes: 16 additions & 0 deletions bin/rails-sandbox
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby

app_root = 'sandbox'

unless File.exist? "#{app_root}/bin/rails"
warn 'Creating the sandbox app...'
Dir.chdir "#{__dir__}/.." do
system "#{__dir__}/sandbox" or begin # rubocop:disable Style/AndOr
warn 'Automatic creation of the sandbox app failed'
exit 1
end
end
end

Dir.chdir app_root
exec 'bin/rails', *ARGV
7 changes: 7 additions & 0 deletions bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rake", "rake")
84 changes: 84 additions & 0 deletions bin/sandbox
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bash

set -e

case "$DB" in
postgres|postgresql)
RAILSDB="postgresql"
;;
mysql)
RAILSDB="mysql"
;;
sqlite|'')
RAILSDB="sqlite3"
;;
*)
echo "Invalid DB specified: $DB"
exit 1
;;
esac

if [ ! -z $SOLIDUS_BRANCH ]
then
BRANCH=$SOLIDUS_BRANCH
else
BRANCH="master"
fi

extension_name="solidus_easypost"

# Stay away from the bundler env of the containing extension.
function unbundled {
ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- $@
}

rm -rf ./sandbox
unbundled bundle exec rails new sandbox --database="$RAILSDB" \
--skip-bundle \
--skip-git \
--skip-keeps \
--skip-rc \
--skip-spring \
--skip-test \
--skip-javascript

if [ ! -d "sandbox" ]; then
echo 'sandbox rails application failed'
exit 1
fi

cd ./sandbox
cat <<RUBY >> Gemfile
gem 'solidus', github: 'solidusio/solidus', branch: '$BRANCH'
gem 'solidus_auth_devise', '>= 2.1.0'
gem 'rails-i18n'
gem 'solidus_i18n'
gem '$extension_name', path: '..'
group :test, :development do
platforms :mri do
gem 'pry-byebug'
end
end
RUBY

unbundled bundle install --gemfile Gemfile

unbundled bundle exec rake db:drop db:create

unbundled bundle exec rails generate spree:install \
--auto-accept \
--user_class=Spree::User \
--enforce_available_locales=true \
--with-authentication=false \
$@

unbundled bundle exec rails generate solidus:auth:install

echo
echo "🚀 Sandbox app successfully created for $extension_name!"
echo "🚀 Using $RAILSDB and Solidus $BRANCH"
echo "🚀 Use 'export DB=[postgres|mysql|sqlite]' to control the DB adapter"
echo "🚀 Use 'export SOLIDUS_BRANCH=<BRANCH-NAME>' to control the Solidus version"
echo "🚀 This app is intended for test purposes."
2 changes: 1 addition & 1 deletion bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ set -vx

gem install bundler --conservative
bundle update
bundle exec rake extension:test_app
bin/rake clobber
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Sample localization file for English. Add more files in this directory for other locales.
# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.

en:
hello: Hello world
8 changes: 4 additions & 4 deletions lib/generators/solidus_easypost/install/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class InstallGenerator < Rails::Generators::Base
class_option :auto_run_migrations, type: :boolean, default: false

def add_migrations
run 'bundle exec rake railties:install:migrations FROM=solidus_easypost'
run 'bin/rails railties:install:migrations FROM=solidus_easypost'
end

def run_migrations
run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]'))
run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]')) # rubocop:disable Metrics/LineLength
if run_migrations
run 'bundle exec rake db:migrate'
run 'bin/rails db:migrate'
else
puts 'Skipping rake db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output
puts 'Skipping bin/rails db:migrate, don\'t forget to run it!' # rubocop:disable Rails/Output
end
end
end
Expand Down
11 changes: 2 additions & 9 deletions lib/solidus_easypost/engine.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
# frozen_string_literal: true

require 'spree/core'
require 'solidus_easypost'

module SolidusEasypost
class Engine < Rails::Engine
include SolidusSupport::EngineExtensions

isolate_namespace Spree
isolate_namespace ::Spree

engine_name 'solidus_easypost'

# use rspec for tests
config.generators do |g|
g.test_framework :rspec
end

def self.activate
Dir.glob(File.join(File.dirname(__FILE__), '../../app/models/**/*.rb')) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end

config.to_prepare(&method(:activate).to_proc)
end
end
Loading

0 comments on commit 4def45b

Please sign in to comment.