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.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +35 -0
  3. data/.gem_release.yml +5 -0
  4. data/.github/stale.yml +17 -0
  5. data/.gitignore +21 -0
  6. data/.gitlab/ci.yml +63 -0
  7. data/.rspec +2 -0
  8. data/.rubocop.yml +17 -0
  9. data/Gemfile +33 -0
  10. data/LICENSE +26 -0
  11. data/README.md +396 -0
  12. data/Rakefile +6 -0
  13. data/app/decorators/solidus_identifiers/spree/api/api_helpers_decorator.rb +20 -0
  14. data/app/decorators/solidus_identifiers/spree/permission_sets/user_management_decorator.rb +15 -0
  15. data/app/decorators/solidus_identifiers/spree/permitted_attributes_decorator.rb +16 -0
  16. data/app/decorators/solidus_identifiers/spree/user_decorator.rb +11 -0
  17. data/app/models/spree/identifier.rb +19 -0
  18. data/app/models/spree/identifier_key.rb +17 -0
  19. data/app/views/spree/api/identifier_keys/_identifier_key.json.jbuilder +5 -0
  20. data/app/views/spree/api/identifier_keys/index.json.jbuilder +6 -0
  21. data/app/views/spree/api/identifier_keys/show.json.jbuilder +3 -0
  22. data/app/views/spree/api/identifiers/_identifier.json.jbuilder +14 -0
  23. data/app/views/spree/api/identifiers/index.json.jbuilder +6 -0
  24. data/app/views/spree/api/identifiers/show.json.jbuilder +3 -0
  25. data/app/views/spree/api/users/_user.json.jbuilder +31 -0
  26. data/bin/console +17 -0
  27. data/bin/r +15 -0
  28. data/bin/rake +7 -0
  29. data/bin/sandbox +84 -0
  30. data/bin/sandbox_rails +18 -0
  31. data/bin/setup +8 -0
  32. data/config/locales/en.yml +5 -0
  33. data/config/routes.rb +9 -0
  34. data/db/migrate/20200603191551_create_spree_identifier_keys.rb +10 -0
  35. data/db/migrate/20200603191555_create_spree_identifiers.rb +18 -0
  36. data/db/migrate/20200603203105_add_unique_index_to_spree_identifier_keys.rb +7 -0
  37. data/lib/controllers/api/spree/api/identifier_keys_controller.rb +15 -0
  38. data/lib/controllers/api/spree/api/identifiers_controller.rb +15 -0
  39. data/lib/generators/solidus_identifiers/install/install_generator.rb +26 -0
  40. data/lib/solidus_identifiers.rb +7 -0
  41. data/lib/solidus_identifiers/engine.rb +19 -0
  42. data/lib/solidus_identifiers/factories.rb +14 -0
  43. data/lib/solidus_identifiers/version.rb +5 -0
  44. data/solidus_identifiers.gemspec +38 -0
  45. data/spec/controllers/spree/api/identifier_keys_controller_spec.rb +151 -0
  46. data/spec/controllers/spree/api/identifiers_controller_spec.rb +161 -0
  47. data/spec/models/spree/identifier_key_spec.rb +21 -0
  48. data/spec/models/spree/identifier_spec.rb +33 -0
  49. data/spec/spec_helper.rb +32 -0
  50. metadata +174 -0
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'solidus_dev_support/rake_tasks'
4
+ SolidusDevSupport::RakeTasks.install
5
+
6
+ task default: 'extension:specs'
@@ -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,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Spree
4
+ module UserDecorator
5
+ def self.prepended(base)
6
+ base.has_many :identifiers, as: :attachable
7
+ end
8
+
9
+ ::Spree.user_class.prepend(self)
10
+ end
11
+ 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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ json.cache! [I18n.locale, identifier_key] do
4
+ json.call(identifier_key, *identifier_key_attributes)
5
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ json.partial! 'spree/api/shared/pagination', pagination: @identifier_keys
4
+ json.identifier_keys(@identifier_keys) do |identifier_key|
5
+ json.partial!("spree/api/identifier_keys/identifier_key", identifier_key: identifier_key)
6
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ json.partial!("spree/api/identifier_keys/identifier_key", identifier_key: @identifier_key)
@@ -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,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ json.partial! 'spree/api/shared/pagination', pagination: @identifiers
4
+ json.identifiers(@identifiers) do |identifier|
5
+ json.partial!("spree/api/identifiers/identifier", identifier: identifier)
6
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ json.partial!("spree/api/identifiers/identifier", identifier: @identifier)
@@ -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
@@ -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'
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "rubygems"
5
+ require "bundler/setup"
6
+
7
+ load Gem.bin_path("rake", "rake")
@@ -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."
@@ -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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ gem install bundler --conservative
7
+ bundle update
8
+ bin/rake clobber
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: Hello world
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ Spree::Core::Engine.routes.draw do
4
+ # Add your extension routes here
5
+ namespace :api do
6
+ resources :identifiers
7
+ resources :identifier_keys
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CreateSpreeIdentifierKeys < ActiveRecord::Migration[5.2]
4
+ def change
5
+ create_table :spree_identifier_keys do |t|
6
+ t.string :name, unique: true, null: false
7
+ t.timestamps
8
+ end
9
+ end
10
+ end
@@ -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,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddUniqueIndexToSpreeIdentifierKeys < ActiveRecord::Migration[5.2]
4
+ def change
5
+ add_index :spree_identifier_keys, :name, unique: true
6
+ end
7
+ 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