solidus_identifiers 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.circleci/config.yml +35 -0
- data/.gem_release.yml +5 -0
- data/.github/stale.yml +17 -0
- data/.gitignore +21 -0
- data/.gitlab/ci.yml +63 -0
- data/.rspec +2 -0
- data/.rubocop.yml +17 -0
- data/Gemfile +33 -0
- data/LICENSE +26 -0
- data/README.md +396 -0
- data/Rakefile +6 -0
- data/app/decorators/solidus_identifiers/spree/api/api_helpers_decorator.rb +20 -0
- data/app/decorators/solidus_identifiers/spree/permission_sets/user_management_decorator.rb +15 -0
- data/app/decorators/solidus_identifiers/spree/permitted_attributes_decorator.rb +16 -0
- data/app/decorators/solidus_identifiers/spree/user_decorator.rb +11 -0
- data/app/models/spree/identifier.rb +19 -0
- data/app/models/spree/identifier_key.rb +17 -0
- data/app/views/spree/api/identifier_keys/_identifier_key.json.jbuilder +5 -0
- data/app/views/spree/api/identifier_keys/index.json.jbuilder +6 -0
- data/app/views/spree/api/identifier_keys/show.json.jbuilder +3 -0
- data/app/views/spree/api/identifiers/_identifier.json.jbuilder +14 -0
- data/app/views/spree/api/identifiers/index.json.jbuilder +6 -0
- data/app/views/spree/api/identifiers/show.json.jbuilder +3 -0
- data/app/views/spree/api/users/_user.json.jbuilder +31 -0
- data/bin/console +17 -0
- data/bin/r +15 -0
- data/bin/rake +7 -0
- data/bin/sandbox +84 -0
- data/bin/sandbox_rails +18 -0
- data/bin/setup +8 -0
- data/config/locales/en.yml +5 -0
- data/config/routes.rb +9 -0
- data/db/migrate/20200603191551_create_spree_identifier_keys.rb +10 -0
- data/db/migrate/20200603191555_create_spree_identifiers.rb +18 -0
- data/db/migrate/20200603203105_add_unique_index_to_spree_identifier_keys.rb +7 -0
- data/lib/controllers/api/spree/api/identifier_keys_controller.rb +15 -0
- data/lib/controllers/api/spree/api/identifiers_controller.rb +15 -0
- data/lib/generators/solidus_identifiers/install/install_generator.rb +26 -0
- data/lib/solidus_identifiers.rb +7 -0
- data/lib/solidus_identifiers/engine.rb +19 -0
- data/lib/solidus_identifiers/factories.rb +14 -0
- data/lib/solidus_identifiers/version.rb +5 -0
- data/solidus_identifiers.gemspec +38 -0
- data/spec/controllers/spree/api/identifier_keys_controller_spec.rb +151 -0
- data/spec/controllers/spree/api/identifiers_controller_spec.rb +161 -0
- data/spec/models/spree/identifier_key_spec.rb +21 -0
- data/spec/models/spree/identifier_spec.rb +33 -0
- data/spec/spec_helper.rb +32 -0
- metadata +174 -0
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
module Api
|
5
|
+
module ApiHelpersDecorator
|
6
|
+
def self.prepended(base)
|
7
|
+
base::ATTRIBUTES << :identifier_attributes
|
8
|
+
base::ATTRIBUTES << :identifier_key_attributes
|
9
|
+
base.mattr_reader :identifier_attributes, :identifier_key_attributes
|
10
|
+
end
|
11
|
+
|
12
|
+
@@identifier_attributes = %i[id value]
|
13
|
+
@@identifier_key_attributes = %i[id name]
|
14
|
+
|
15
|
+
if SolidusSupport.api_available?
|
16
|
+
::Spree::Api::ApiHelpers.prepend self
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
module PermissionSets
|
5
|
+
module UserManagementDecorator
|
6
|
+
def activate!
|
7
|
+
super
|
8
|
+
|
9
|
+
can :manage, ::Spree::Identifier, attachable_type: Spree.user_class.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
::Spree::PermissionSets::DefaultCustomer.prepend(self)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
module PermittedAttributesDecorator
|
5
|
+
def self.prepended(base)
|
6
|
+
base::ATTRIBUTES << :identifier_attributes
|
7
|
+
base::ATTRIBUTES << :identifier_key_attributes
|
8
|
+
base.mattr_reader :identifier_attributes, :identifier_key_attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
@@identifier_attributes = %i[id value key_id attachable_id attachable_type]
|
12
|
+
@@identifier_key_attributes = %i[id name]
|
13
|
+
|
14
|
+
::Spree::PermittedAttributes.prepend(self)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
##
|
5
|
+
# Identifier to store 3rd party or external identifiers for records
|
6
|
+
# such as a user or product.
|
7
|
+
#
|
8
|
+
class Identifier < Spree::Base
|
9
|
+
scope :with_key, ->(name) { joins(:key).where(spree_identifier_keys: { name: name }) }
|
10
|
+
|
11
|
+
belongs_to :key, class_name: 'Spree::IdentifierKey', foreign_key: :key_id,
|
12
|
+
inverse_of: :identifiers
|
13
|
+
belongs_to :attachable, polymorphic: true
|
14
|
+
|
15
|
+
validates :value, presence: true
|
16
|
+
validates :key_id, presence: true
|
17
|
+
validates :key_id, uniqueness: { scope: [:attachable_id, :attachable_type] }
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
##
|
5
|
+
# A Key to for determining what the identifier represents.
|
6
|
+
# (facebook, google, twitter)
|
7
|
+
#
|
8
|
+
class IdentifierKey < Spree::Base
|
9
|
+
has_many :identifiers, class_name: 'Spree::Identifier',
|
10
|
+
foreign_key: :key_id,
|
11
|
+
dependent: :restrict_with_exception,
|
12
|
+
inverse_of: :key
|
13
|
+
|
14
|
+
validates :name, uniqueness: true
|
15
|
+
validates :name, presence: true
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
json.cache! [I18n.locale, identifier] do
|
4
|
+
json.call(identifier, *identifier_attributes)
|
5
|
+
|
6
|
+
json.key do
|
7
|
+
json.partial!('spree/api/identifier_keys/identifier_key', identifier_key: identifier.key)
|
8
|
+
end
|
9
|
+
|
10
|
+
json.attachable do
|
11
|
+
json.id identifier.attachable_id
|
12
|
+
json.type identifier.attachable_type
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
json.call(user, *user_attributes)
|
4
|
+
json.bill_address do
|
5
|
+
if user.bill_address
|
6
|
+
json.partial!("spree/api/addresses/address", address: user.bill_address)
|
7
|
+
else
|
8
|
+
json.nil!
|
9
|
+
end
|
10
|
+
end
|
11
|
+
json.ship_address do
|
12
|
+
if user.ship_address
|
13
|
+
json.partial!("spree/api/addresses/address", address: user.ship_address)
|
14
|
+
else
|
15
|
+
json.nil!
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Custom code for Identifiers
|
20
|
+
if can?(:admin, user)
|
21
|
+
json.identifiers user.identifiers do |identifier|
|
22
|
+
json.partial!('spree/api/identifiers/identifier', identifier: identifier)
|
23
|
+
end
|
24
|
+
|
25
|
+
json.flattened_identifiers do
|
26
|
+
user.identifiers.each do |identifier|
|
27
|
+
json.set! identifier.key.name, identifier.value
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
# End Custom
|
data/bin/console
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
require "bundler/setup"
|
6
|
+
require "solidus_identifiers"
|
7
|
+
|
8
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
9
|
+
# with your gem easier. You can also use a different console, if you like.
|
10
|
+
$LOAD_PATH.unshift(*Dir["#{__dir__}/../app/*"])
|
11
|
+
|
12
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
13
|
+
# require "pry"
|
14
|
+
# Pry.start
|
15
|
+
|
16
|
+
require "irb"
|
17
|
+
IRB.start(__FILE__)
|
data/bin/r
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
# This command will automatically be run when you run "rails" with Rails gems
|
5
|
+
# installed from the root of your application.
|
6
|
+
|
7
|
+
ENGINE_ROOT = File.expand_path('..', __dir__)
|
8
|
+
ENGINE_PATH = File.expand_path('../lib/solidus_identifiers/engine', __dir__)
|
9
|
+
|
10
|
+
# Set up gems listed in the Gemfile.
|
11
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
|
12
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
13
|
+
|
14
|
+
require 'rails/all'
|
15
|
+
require 'rails/engine/commands'
|
data/bin/rake
ADDED
data/bin/sandbox
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
#!/usr/bin/env bash
|
2
|
+
|
3
|
+
set -e
|
4
|
+
|
5
|
+
case "$DB" in
|
6
|
+
postgres|postgresql)
|
7
|
+
RAILSDB="postgresql"
|
8
|
+
;;
|
9
|
+
mysql)
|
10
|
+
RAILSDB="mysql"
|
11
|
+
;;
|
12
|
+
sqlite|'')
|
13
|
+
RAILSDB="sqlite3"
|
14
|
+
;;
|
15
|
+
*)
|
16
|
+
echo "Invalid DB specified: $DB"
|
17
|
+
exit 1
|
18
|
+
;;
|
19
|
+
esac
|
20
|
+
|
21
|
+
if [ ! -z $SOLIDUS_BRANCH ]
|
22
|
+
then
|
23
|
+
BRANCH=$SOLIDUS_BRANCH
|
24
|
+
else
|
25
|
+
BRANCH="master"
|
26
|
+
fi
|
27
|
+
|
28
|
+
extension_name="solidus_identifiers"
|
29
|
+
|
30
|
+
# Stay away from the bundler env of the containing extension.
|
31
|
+
function unbundled {
|
32
|
+
ruby -rbundler -e'b = proc {system *ARGV}; Bundler.respond_to?(:with_unbundled_env) ? Bundler.with_unbundled_env(&b) : Bundler.with_clean_env(&b)' -- $@
|
33
|
+
}
|
34
|
+
|
35
|
+
rm -rf ./sandbox
|
36
|
+
unbundled bundle exec rails new sandbox --database="$RAILSDB" \
|
37
|
+
--skip-bundle \
|
38
|
+
--skip-git \
|
39
|
+
--skip-keeps \
|
40
|
+
--skip-rc \
|
41
|
+
--skip-spring \
|
42
|
+
--skip-test \
|
43
|
+
--skip-javascript
|
44
|
+
|
45
|
+
if [ ! -d "sandbox" ]; then
|
46
|
+
echo 'sandbox rails application failed'
|
47
|
+
exit 1
|
48
|
+
fi
|
49
|
+
|
50
|
+
cd ./sandbox
|
51
|
+
cat <<RUBY >> Gemfile
|
52
|
+
gem 'solidus', github: 'solidusio/solidus', branch: '$BRANCH'
|
53
|
+
gem 'solidus_auth_devise', '>= 2.1.0'
|
54
|
+
gem 'rails-i18n'
|
55
|
+
gem 'solidus_i18n'
|
56
|
+
|
57
|
+
gem '$extension_name', path: '..'
|
58
|
+
|
59
|
+
group :test, :development do
|
60
|
+
platforms :mri do
|
61
|
+
gem 'pry-byebug'
|
62
|
+
end
|
63
|
+
end
|
64
|
+
RUBY
|
65
|
+
|
66
|
+
unbundled bundle install --gemfile Gemfile
|
67
|
+
|
68
|
+
unbundled bundle exec rake db:drop db:create
|
69
|
+
|
70
|
+
unbundled bundle exec rails generate spree:install \
|
71
|
+
--auto-accept \
|
72
|
+
--user_class=Spree::User \
|
73
|
+
--enforce_available_locales=true \
|
74
|
+
--with-authentication=false \
|
75
|
+
$@
|
76
|
+
|
77
|
+
unbundled bundle exec rails generate solidus:auth:install
|
78
|
+
|
79
|
+
echo
|
80
|
+
echo "🚀 Sandbox app successfully created for $extension_name!"
|
81
|
+
echo "🚀 Using $RAILSDB and Solidus $BRANCH"
|
82
|
+
echo "🚀 Use 'export DB=[postgres|mysql|sqlite]' to control the DB adapter"
|
83
|
+
echo "🚀 Use 'export SOLIDUS_BRANCH=<BRANCH-NAME>' to control the Solidus version"
|
84
|
+
echo "🚀 This app is intended for test purposes."
|
data/bin/sandbox_rails
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# frozen_string_literal: true
|
4
|
+
|
5
|
+
app_root = 'sandbox'
|
6
|
+
|
7
|
+
unless File.exist? "#{app_root}/bin/rails"
|
8
|
+
warn 'Creating the sandbox app...'
|
9
|
+
Dir.chdir "#{__dir__}/.." do
|
10
|
+
system "#{__dir__}/sandbox" or begin # rubocop:disable Style/AndOr
|
11
|
+
warn 'Automatic creation of the sandbox app failed'
|
12
|
+
exit 1
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Dir.chdir app_root
|
18
|
+
exec 'bin/rails', *ARGV
|
data/bin/setup
ADDED
data/config/routes.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class CreateSpreeIdentifiers < ActiveRecord::Migration[5.2]
|
4
|
+
def change
|
5
|
+
create_table :spree_identifiers do |t|
|
6
|
+
t.string :value, null: false
|
7
|
+
t.integer :key_id
|
8
|
+
t.integer :attachable_id
|
9
|
+
t.string :attachable_type
|
10
|
+
t.timestamps
|
11
|
+
end
|
12
|
+
|
13
|
+
add_index :spree_identifiers, [:attachable_id, :attachable_type],
|
14
|
+
name: 'ext_id_attachable_index'
|
15
|
+
add_index :spree_identifiers, [:attachable_id, :attachable_type, :key_id],
|
16
|
+
unique: true, name: 'ext_id_uniq_attachable_index'
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
module Api
|
5
|
+
class IdentifierKeysController < Spree::Api::ResourceController
|
6
|
+
helper ::Spree::Core::Engine.routes.url_helpers
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def permitted_identifier_key_attributes
|
11
|
+
Spree::PermittedAttributes.identifier_key_attributes
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
module Api
|
5
|
+
class IdentifiersController < Spree::Api::ResourceController
|
6
|
+
helper ::Spree::Core::Engine.routes.url_helpers
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def permitted_identifier_attributes
|
11
|
+
Spree::PermittedAttributes.identifier_attributes
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|