From 6a0d1b01c8932bcb7980261223384681513e0468 Mon Sep 17 00:00:00 2001 From: Jonathan Tapia Date: Thu, 11 Jul 2019 13:38:44 -0500 Subject: [PATCH 01/32] Refacting code to be compatible with Zeitwerk Renamed some files and used new approach to decorate solidus code Refactored some admin views to use bootstrap --- .rspec | 3 +- .travis.yml | 28 +-- Gemfile | 19 ++- README.md | 126 +++++--------- Rakefile | 14 +- Versionfile | 6 - .../spree/backend/suppliers_autocomplete.js | 80 ++++----- .../admin/products_controller_decorator.rb | 88 ++++++++++ .../admin/reports_controller_decorator.rb | 84 +++++++++ .../admin/stock_items_controller_decorator.rb | 21 +++ .../stock_locations_controller_decorator.rb | 32 ++++ .../admin/users_controller_decorator.rb | 56 ++++++ .../stock_locations_controller_decorator.rb | 27 +++ .../base_controller_decorator.rb | 20 +++ .../users_controller_decorator.rb | 53 ++++++ .../admin/marketplace_settings_controller.rb | 28 +-- .../admin/products_controller_decorator.rb | 80 +-------- .../admin/reports_controller_decorator.rb | 71 +------- .../spree/admin/shipments_controller.rb | 2 - .../admin/stock_items_controller_decorator.rb | 11 +- .../stock_locations_controller_decorator.rb | 24 +-- .../spree/admin/suppliers_controller.rb | 74 +++----- .../spree/admin/users_controller_decorator.rb | 104 +----------- .../stock_locations_controller_decorator.rb | 1 + .../spree/api/suppliers_controller.rb | 6 +- .../stock_locations_controller_decorator.rb | 18 -- .../spree/base_controller_decorator.rb | 14 +- app/mailers/spree/marketplace_order_mailer.rb | 10 +- app/mailers/spree/supplier_mailer.rb | 7 +- .../solidus_marketplace/order_decorator.rb | 50 ++++++ .../solidus_marketplace/payment_decorator.rb | 9 + .../solidus_marketplace/product_decorator.rb | 58 +++++++ .../solidus_marketplace/shipment_decorator.rb | 43 +++++ .../stock_location_decorator.rb | 29 ++++ .../solidus_marketplace/user_decorator.rb | 29 ++++ .../solidus_marketplace/variant_decorator.rb | 28 +++ app/models/spree/mail_method_decorator.rb | 5 - app/models/spree/order_decorator.rb | 45 +---- app/models/spree/payment_decorator.rb | 8 +- app/models/spree/product_decorator.rb | 53 +----- app/models/spree/shipment_decorator.rb | 41 +---- .../spree/stock/splitter/marketplace.rb | 58 +++---- app/models/spree/stock_location_decorator.rb | 24 +-- app/models/spree/supplier.rb | 119 +++++++------ app/models/spree/user_decorator.rb | 24 +-- app/models/spree/variant_decorator.rb | 25 +-- app/overrides/add_supplier_admin_tab.rb | 6 + .../add_supplier_cost_to_order_total.rb | 7 + .../add_supplier_to_admin_product_form.rb | 7 + .../add_supplier_to_configuration_menu.rb | 7 + .../add_supplier_to_stock_locations.rb | 7 + .../admin/add_payment_data_to_admin_users.rb | 8 - app/overrides/spree/admin/menus.rb | 15 -- ...d_admin_product_form_right.html.erb.deface | 5 - .../edit/override_rows.html.erb.deface | 22 --- .../_form/add_supplier.html.erb.deface | 12 -- .../admin/marketplace_settings/edit.html.erb | 38 ++--- .../spree/admin/reports/earnings.html.erb | 5 +- .../shared/_marketplace_settings.html.erb | 4 +- .../admin/shared/_payment_data_user.html.erb | 5 - .../shared/_supplier_order_total.html.erb | 5 + .../shared/_supplier_product_form.html.erb | 8 + .../_supplier_stock_location_form.html.erb | 14 ++ .../spree/admin/suppliers/_form.html.erb | 158 +++++++++-------- app/views/spree/admin/suppliers/edit.html.erb | 29 ++-- .../spree/admin/suppliers/index.html.erb | 69 ++++---- app/views/spree/admin/suppliers/new.html.erb | 17 +- .../spree/admin/users/_wallet_form.html.erb | 53 +++--- .../spree/admin/users/addwallet.html.erb | 4 +- app/views/spree/admin/users/wallets.html.erb | 2 - .../api/shared/_pagination.json.jbuilder | 8 +- .../supplier_order.html.erb | 5 +- app/views/spree/shared/unauthorized.erb | 2 +- .../spree/supplier_mailer/welcome.html.erb | 18 +- config/locales/en.yml | 5 + config/routes.rb | 17 +- db/default/spree/marketplace_roles.rb | 20 ++- db/migrate/20121006073854_create_suppliers.rb | 20 ++- ...30216070944_product_belongs_to_supplier.rb | 4 +- ...502_stock_locations_belongs_to_supplier.rb | 4 +- ...20130405011127_user_belongs_to_supplier.rb | 4 +- ...8063053_add_balanced_token_to_suppliers.rb | 4 +- ...1443_add_supplier_id_to_ckeditor_assets.rb | 4 +- ...130606220913_add_permalink_to_suppliers.rb | 4 +- ...38_add_supplier_commission_to_shipments.rb | 4 +- ...16184616_migrate_payment_and_commission.rb | 4 +- ...29041325_create_spree_supplier_variants.rb | 6 +- ...027180043_add_paypal_email_to_suppliers.rb | 4 +- ...80827180043_add_admin_user_to_suppliers.rb | 4 +- ...0130043_add_admin_user_ids_to_suppliers.rb | 8 + db/seeds.rb | 1 + .../install/install_generator.rb | 20 +-- lib/solidus_marketplace.rb | 8 +- .../solidus_marketplace/configuration.rb | 15 +- lib/solidus_marketplace/engine.rb | 13 +- lib/solidus_marketplace/factories.rb | 37 ++-- .../permitted_attributes.rb | 24 +++ lib/solidus_marketplace/version.rb | 4 +- lib/spree/permitted_attributes_decorator.rb | 18 -- lib/tasks/spree_sample.rake | 62 +++---- solidus_marketplace.gemspec | 50 +++--- spec/features/admin/orders_spec.rb | 11 -- spec/features/admin/stock_management_spec.rb | 156 ----------------- spec/features/spree/admin/orders_spec.rb | 17 ++ .../{ => spree}/admin/products_spec.rb | 21 ++- .../admin/return_authorizations_spec.rb | 6 +- .../{ => spree}/admin/settings_spec.rb | 8 +- .../{ => spree}/admin/shipments_spec.rb | 64 +++---- .../spree/admin/stock_management_spec.rb | 160 ++++++++++++++++++ spec/features/{ => spree}/admin/stock_spec.rb | 36 ++-- .../{ => spree}/admin/suppliers_spec.rb | 45 +++-- spec/models/spree/order_decorator_spec.rb | 58 +++---- spec/models/spree/payment_decorator_spec.rb | 8 +- spec/models/spree/product_decorator_spec.rb | 21 +-- spec/models/spree/shipment_decorator_spec.rb | 54 +++--- .../spree/stock/splitter/marketplace_spec.rb | 80 +++------ .../spree/stock_location_decorator_spec.rb | 18 +- spec/models/spree/supplier_ability_spec.rb | 51 ++++-- spec/models/spree/supplier_spec.rb | 139 +++++++-------- spec/models/spree/supplier_variant_spec.rb | 4 +- spec/models/spree/user_decorator_spec.rb | 27 ++- spec/models/spree/variant_decorator_spec.rb | 4 +- spec/spec_helper.rb | 82 +-------- spec/support/ability_helpers.rb | 11 -- spec/support/integration_helpers.rb | 15 -- 125 files changed, 1857 insertions(+), 1902 deletions(-) delete mode 100644 Versionfile create mode 100644 app/controllers/solidus_marketplace/admin/products_controller_decorator.rb create mode 100644 app/controllers/solidus_marketplace/admin/reports_controller_decorator.rb create mode 100644 app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb create mode 100644 app/controllers/solidus_marketplace/admin/stock_locations_controller_decorator.rb create mode 100644 app/controllers/solidus_marketplace/admin/users_controller_decorator.rb create mode 100644 app/controllers/solidus_marketplace/api/stock_locations_controller_decorator.rb create mode 100644 app/controllers/solidus_marketplace/base_controller_decorator.rb create mode 100644 app/controllers/solidus_marketplace/users_controller_decorator.rb create mode 100644 app/controllers/spree/api/stock_locations_controller_decorator.rb delete mode 100644 app/controllers/spree/api/v1/stock_locations_controller_decorator.rb create mode 100644 app/models/solidus_marketplace/order_decorator.rb create mode 100644 app/models/solidus_marketplace/payment_decorator.rb create mode 100644 app/models/solidus_marketplace/product_decorator.rb create mode 100644 app/models/solidus_marketplace/shipment_decorator.rb create mode 100644 app/models/solidus_marketplace/stock_location_decorator.rb create mode 100644 app/models/solidus_marketplace/user_decorator.rb create mode 100644 app/models/solidus_marketplace/variant_decorator.rb delete mode 100644 app/models/spree/mail_method_decorator.rb create mode 100644 app/overrides/add_supplier_admin_tab.rb create mode 100644 app/overrides/add_supplier_cost_to_order_total.rb create mode 100644 app/overrides/add_supplier_to_admin_product_form.rb create mode 100644 app/overrides/add_supplier_to_configuration_menu.rb create mode 100644 app/overrides/add_supplier_to_stock_locations.rb delete mode 100644 app/overrides/spree/admin/add_payment_data_to_admin_users.rb delete mode 100644 app/overrides/spree/admin/menus.rb delete mode 100644 app/overrides/spree/admin/products/_form/converted_admin_product_form_right.html.erb.deface delete mode 100644 app/overrides/spree/admin/products/edit/override_rows.html.erb.deface delete mode 100644 app/overrides/spree/admin/stock_locations/_form/add_supplier.html.erb.deface delete mode 100644 app/views/spree/admin/shared/_payment_data_user.html.erb create mode 100644 app/views/spree/admin/shared/_supplier_order_total.html.erb create mode 100644 app/views/spree/admin/shared/_supplier_product_form.html.erb create mode 100644 app/views/spree/admin/shared/_supplier_stock_location_form.html.erb create mode 100644 db/migrate/20190710130043_add_admin_user_ids_to_suppliers.rb rename app/models/spree/marketplace_configuration.rb => lib/solidus_marketplace/configuration.rb (62%) create mode 100644 lib/solidus_marketplace/permitted_attributes.rb delete mode 100644 lib/spree/permitted_attributes_decorator.rb delete mode 100644 spec/features/admin/orders_spec.rb delete mode 100644 spec/features/admin/stock_management_spec.rb create mode 100644 spec/features/spree/admin/orders_spec.rb rename spec/features/{ => spree}/admin/products_spec.rb (59%) rename spec/features/{ => spree}/admin/return_authorizations_spec.rb (58%) rename spec/features/{ => spree}/admin/settings_spec.rb (87%) rename spec/features/{ => spree}/admin/shipments_spec.rb (50%) create mode 100644 spec/features/spree/admin/stock_management_spec.rb rename spec/features/{ => spree}/admin/stock_spec.rb (77%) rename spec/features/{ => spree}/admin/suppliers_spec.rb (80%) delete mode 100644 spec/support/ability_helpers.rb delete mode 100644 spec/support/integration_helpers.rb diff --git a/.rspec b/.rspec index e4d136b..b8f148f 100644 --- a/.rspec +++ b/.rspec @@ -1 +1,2 @@ ---format progress +--color +-r spec_helper diff --git a/.travis.yml b/.travis.yml index 1f17d99..9a20a26 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,19 @@ -before_script: - - "sh -e /etc/init.d/xvfb start" - - "bundle exec rake test_app" -script: - - "export DISPLAY=:99.0" - - "bundle exec rspec spec" -rvm: - - 2.0.0 - - 2.1.2 - - 2.1.5 +dist: trusty +cache: bundler language: ruby +rvm: + - 2.4.6 +env: + matrix: + - SOLIDUS_BRANCH=v2.4 DB=postgres + - SOLIDUS_BRANCH=v2.5 DB=postgres + - SOLIDUS_BRANCH=v2.6 DB=postgres + - SOLIDUS_BRANCH=v2.7 DB=postgres + - SOLIDUS_BRANCH=v2.8 DB=postgres + - SOLIDUS_BRANCH=master DB=postgres + - SOLIDUS_BRANCH=v2.4 DB=mysql + - SOLIDUS_BRANCH=v2.5 DB=mysql + - SOLIDUS_BRANCH=v2.6 DB=mysql + - SOLIDUS_BRANCH=v2.7 DB=mysql + - SOLIDUS_BRANCH=v2.8 DB=mysql + - SOLIDUS_BRANCH=master DB=mysql diff --git a/Gemfile b/Gemfile index d3f9af4..14af998 100644 --- a/Gemfile +++ b/Gemfile @@ -1,8 +1,21 @@ -source 'https://rubygems.org' +# frozen_string_literal: true +source 'https://rubygems.org' branch = ENV.fetch('SOLIDUS_BRANCH', 'master') -gem "solidus", github: "solidusio/solidus", branch: branch -gem "solidus_auth_devise", github: "solidusio/solidus_auth_devise", branch: branch +gem 'solidus', github: 'solidusio/solidus', branch: branch +gem 'solidus_auth_devise', github: 'solidusio/solidus_auth_devise' +# gem 'solidus_reports', github: 'solidusio-contrib/solidus_reports' + +if ENV['DB'] == 'mysql' + gem 'mysql2', '~> 0.4.10' +else + gem 'pg', '~> 0.21' +end + +group :development, :test do + gem 'factory_bot', (branch < 'v2.5' ? '4.10.0' : '> 4.10.0') + gem 'pry-rails' +end gemspec diff --git a/README.md b/README.md index fadc64a..1e4f55d 100644 --- a/README.md +++ b/README.md @@ -1,54 +1,50 @@ -# Solidus Marketplace +# SolidusMarketplace -# NOTE: This gem is currently a work-in-progress. -Contributors are welcome to help us get this gem to a viable MVP. -We suggest installing [ZenHub](http://zenhub.com) in order to view/manage open issues. -This will give you a new tab in Github called "Boards", which provides a Kanban-style view of the project's issues. - -# Overview +[![Build Status](https://travis-ci.org/jtapia/solidus_marketplace.svg?branch=master)](https://travis-ci.org/jtapia/solidus_marketplace) This is marketplace implementation for solidus. Basic functionality: -* Links products to one or more suppliers -* Once an order is placed: - * A shipment is created for the product's supplier - * The shipment is then sent to the store owner for fulfillment and to the supplier for visibility (via Email by default). - * The store owner fulfills orders. The supplier can view their shipments (read-only for now). - -Installation ------------- -Here's how to install solidus_marketplace into your existing spree site AFTER you've installed Spree: - -Add the following to your Gemfile: +- Links products to one or more suppliers +- Once an order is placed: + - A shipment is created for the product's supplier + - The shipment is then sent to the store owner for fulfillment and to the supplier for visibility (via Email by default). + - The store owner fulfills orders. The supplier can view their shipments (read-only for now). - gem 'solidus_marketplace' +## Installation -Make your bundle happy: +Add this line to your application's Gemfile: - bundle install - -Now run the generator: +```ruby +gem 'solidus_marketplace', github: 'jtapia/solidus_marketplace' +``` - rails g solidus_marketplace:install +Then run the following: -(Optional) Run the generator for solidus_gateway to enable the use of Stripe or other payment providers -included with that extension: +```console +$ bundle install +$ bundle exec rails g solidus_marketplace:install +``` - rails g solidus_gateway:install +(Optional) If you want to be able to see the reports, please add this line to your `Gemfile`: -Then migrate your database if you did not run during installation generator: +```ruby +gem 'solidus_reports', github: 'solidus-contrib/solidus_reports' +``` - bundle exec rake db:migrate +(Optional) If you want to use Stripe or other payment providers, please add this line to your `Gemfile`: -And reboot your server: +```ruby +gem 'solidus_gateway' +``` - rails s +and run -You should be up and running now! +```ruby +rails g solidus_gateway:install +``` -Sample Data ------------ +## Sample Data If you'd like to generate sample data, use the included rake tasks: @@ -59,65 +55,25 @@ rake spree_sample:suppliers # Create sample suppliers and randomly link rake spree_sample:marketplace_orders # Create sample marketplace orders ``` -This will include a new role (supplier_admin) and 2 new users in addition to the default 'admin@example.com' user provided by solidus. +This will include a new role (supplier_admin) and 2 new users in addition to the default `admin@example.com` user provided by solidus. Those users have the following email/password/roles -* marketmaker@example.com / test123 / admin -* supplier_admin@example.com / test123 / supplier_admin - -Demo ----- +- marketmaker@example.com / test123 / admin +- supplier_admin@example.com / test123 / supplier_admin -You can easily use the spec/dummy app as a demo of solidus_marketplace. Just `cd` to where you develop and run: +## Testing -```shell -git clone git://github.com/boomerdigital/solidus_marketplace.git -cd solidus_marketplace -bundle install -bundle exec rake test_app -cd spec/dummy -rake db:migrate db:seed spree_sample:load spree_sample:suppliers spree_sample:marketplace_orders -rails s -``` - -Testing -------- - -Be sure to bundle your dependencies and then create a dummy test app for the specs to run against. - -```shell -brew install geckodriver +Run the following to automatically build a dummy app and run the tests: -bundle -bundle exec rake test_app -bundle exec rspec spec +```console +$ bundle exec rake ``` -Todo ----- - -* See open issues here: [open issues](https://github.com/boomerdigital/solidus_marketplace/issues) - -Contributing ------------- - -In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project. - -Here are some ways *you* can contribute: +## Contributing -* by using prerelease versions -* by reporting [bugs](https://github.com/boomerdigital/solidus_marketplace/issues) -* by suggesting new features -* by [translating to a new language](https://github.com/boomerdigital/solidus_marketplace/tree/master/config/locales) -* by writing or editing documentation -* by writing specifications -* by writing code (*no patch is too small*: fix typos, add comments, clean up inconsistent whitespace) -* by refactoring code -* by resolving [issues](https://github.com/boomerdigital/solidus_marketplace/issues) -* by reviewing patches +Bug reports and pull requests are welcome on GitHub at https://github.com/solidusio-contrib/solidus_abandoned_carts. -Donating --------- +## License -Copyright (c) 2016-2017 Boomer Digital, released under the [New BSD License](https://github.com/boomerdigital/solidus_marketplace/tree/master/LICENSE). +The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT). diff --git a/Rakefile b/Rakefile index 8883da6..0d11893 100644 --- a/Rakefile +++ b/Rakefile @@ -1,15 +1,23 @@ +# frozen_string_literal: true + require 'bundler' Bundler::GemHelper.install_tasks require 'rspec/core/rake_task' -require 'spree/testing_support/common_rake' +require 'spree/testing_support/extension_rake' RSpec::Core::RakeTask.new -task :default => [:spec] +task :default do + if Dir["spec/dummy"].empty? + Rake::Task[:test_app].invoke + Dir.chdir("../../") + end + Rake::Task[:spec].invoke +end desc 'Generates a dummy app for testing' task :test_app do ENV['LIB_NAME'] = 'solidus_marketplace' - Rake::Task['common:test_app'].invoke 'Spree::User' + Rake::Task['extension:test_app'].invoke end diff --git a/Versionfile b/Versionfile deleted file mode 100644 index dba6f93..0000000 --- a/Versionfile +++ /dev/null @@ -1,6 +0,0 @@ -# This file is used to designate compatibilty with different versions of Spree -# Please see http://spreecommerce.com/documentation/extensions.html#versionfile for details -# '1.3.x' => { :ref => 'e5624f7' } # Not going to be maintained but most of the works there if your looking for a starting point to branch off of. - -'2.4.x' => { :branch => 'master' } -'2.3.x' => { :branch => '2-3-stable' } diff --git a/app/assets/javascripts/spree/backend/suppliers_autocomplete.js b/app/assets/javascripts/spree/backend/suppliers_autocomplete.js index 0eecafc..4d9bdbb 100644 --- a/app/assets/javascripts/spree/backend/suppliers_autocomplete.js +++ b/app/assets/javascripts/spree/backend/suppliers_autocomplete.js @@ -2,52 +2,52 @@ $.fn.supplierAutocomplete = function () { 'use strict'; this.select2({ - placeholder: Spree.translations.supplier_placeholder, - multiple: true, - initSelection: function (element, callback) { - var ids = element.val(), - count = ids.split(",").length; + placeholder: Spree.translations.supplier_placeholder, + multiple: true, + initSelection: function (element, callback) { + var ids = element.val(), + count = ids.split(",").length; - Spree.ajax({ - type: "GET", - url: Spree.routes.suppliers_search, - data: { - ids: ids, - per_page: count - }, - success: function (data) { - callback(data['suppliers']); - } - }); - }, - ajax: { + Spree.ajax({ + type: "GET", url: Spree.routes.suppliers_search, - datatype: 'json', - data: function (term, page) { - return { - per_page: 50, - page: page, - q: { - name_cont: term - }, - token: Spree.api_key - }; + data: { + ids: ids, + per_page: count }, - results: function (data, page) { - var more = page < data.pages; - return { - results: data['suppliers'], - more: more - }; + success: function (data) { + callback(data['suppliers']); } + }); + }, + ajax: { + url: Spree.routes.suppliers_search, + datatype: 'json', + data: function (term, page) { + return { + per_page: 50, + page: page, + q: { + name_cont: term + }, + token: Spree.api_key + }; }, - formatResult: function (supplier, container, query, escapeMarkup) { - return escapeMarkup(supplier.name); - }, - formatSelection: function (supplier, container, escapeMarkup) { - return escapeMarkup(supplier.name); + results: function (data, page) { + var more = page < data.pages; + return { + results: data['suppliers'], + more: more + }; } - }); + }, + formatResult: function (supplier, container, query, escapeMarkup) { + return escapeMarkup(supplier.name); + }, + formatSelection: function (supplier, container, escapeMarkup) { + return escapeMarkup(supplier.name); + } + }); }; $(document).ready(function () { diff --git a/app/controllers/solidus_marketplace/admin/products_controller_decorator.rb b/app/controllers/solidus_marketplace/admin/products_controller_decorator.rb new file mode 100644 index 0000000..e8b2887 --- /dev/null +++ b/app/controllers/solidus_marketplace/admin/products_controller_decorator.rb @@ -0,0 +1,88 @@ +module SolidusMarketplace + module Admin + module ProductsControllerDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + before_action :get_suppliers, only: [:edit] #, :update] + before_action :supplier_collection, only: [:index] + after_action :update_product_suppliers, only: [:update], unless: -> { params['product']['supplier_ids'].nil? } + create.after :add_product_to_supplier + end + + module InstanceMethods + private + + def permitted_resource_params + params[object_name].present? ? params.require(object_name).permit! : ActionController::Parameters.new.permit! + params[object_name].except(:supplier_ids) + end + + def update_product_suppliers + if adding_suppliers? + supplier_ids = new_supplier_ids - current_supplier_ids + @product.add_suppliers!(supplier_ids) + elsif removing_suppliers? + supplier_ids = current_supplier_ids - new_supplier_ids + @product.remove_suppliers!(supplier_ids) + elsif same_number_of_suppliers? && different_suppliers? + @product.remove_suppliers!(current_suppliers) + @product.add_suppliers!(new_suppliers) + elsif same_suppliers? + #noop + end + end + + def adding_suppliers? + new_supplier_ids.count > current_supplier_ids.count + end + + def removing_suppliers? + new_supplier_ids.count < current_supplier_ids.count + end + + def same_suppliers? + new_supplier_ids == current_supplier_ids + end + + def same_number_of_suppliers? + new_supplier_ids.count == current_supplier_ids.count + end + + def different_suppliers? + new_supplier_ids.sort != current_supplier_ids.sort + end + + def new_supplier_ids + return [] unless params["product"].present? && params["product"]["supplier_ids"].present? + params["product"]["supplier_ids"].split(",").map(&:to_i) + end + + def current_supplier_ids + @product.supplier_ids + end + + def get_suppliers + @suppliers = Spree::Supplier.order(:name) + end + + # Scopes the collection to what the user should have access to, based on the user's role + def supplier_collection + return unless try_spree_current_user + + if try_spree_current_user.supplier? + @collection = @collection.joins(:suppliers).where('spree_suppliers.id = ?', try_spree_current_user.supplier_id) + end + end + + # Newly added products by a Supplier are associated with it. + def add_product_to_supplier + if try_spree_current_user && try_spree_current_user.supplier? + @product.add_supplier!(try_spree_current_user.supplier_id) + end + end + end + end + end +end diff --git a/app/controllers/solidus_marketplace/admin/reports_controller_decorator.rb b/app/controllers/solidus_marketplace/admin/reports_controller_decorator.rb new file mode 100644 index 0000000..be908a2 --- /dev/null +++ b/app/controllers/solidus_marketplace/admin/reports_controller_decorator.rb @@ -0,0 +1,84 @@ +require 'csv' + +module SolidusMarketplace + module Admin + module ReportsControllerDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + before_action :add_marketplace_reports, only: [:index] + end + + module InstanceMethods + def earnings + @supplier_earnings = get_supplier_earnings + + respond_to do |format| + format.html + format.csv { send_data(earnings_csv) } + end + end + + def earnings_csv + header1 = ['Supplier Earnings'] + header2 = ['Supplier', 'Earnings', 'Paypal Email'] + + CSV.generate do |csv| + csv << header1 + csv << header2 + @supplier_earnings.each do |se| + csv << ["#{se[:name]}", + "#{se[:earnings].to_html}", + "#{se[:paypal_email]}"] + end + end + end + + private + + def add_marketplace_reports + marketplace_reports.each do |report| + Spree::Admin::ReportsController.add_available_report!(report) + end + end + + def marketplace_reports + [:earnings] + end + + def get_supplier_earnings + grouped_supplier_earnings.each do |se| + se[:earnings] = se[:earnings].inject(Spree::Money.new(0)) do |e, c| + c + e + end + end + end + + def grouped_supplier_earnings + params[:q] = search_params + + @search = Spree::Order.complete.not_canceled.ransack(params[:q]) + @orders = @search.result + + supplier_earnings_map = @orders.map(&:supplier_earnings_map) + grouped_suppliers_map = supplier_earnings_map.flatten.group_by(&:name).values + grouped_earnings = grouped_suppliers_map.map do |gs| + h = {} + h[:name] = nil + h[:paypal_email] = nil + h[:earnings] = [] + gs.each do |s| + h[:name] = s[:name] if h[:name].nil? + h[:paypal_email] = s[:paypal_email] if h[:paypal_email].nil? + h[:earnings] << s[:earnings] + end + h + end + + grouped_earnings + end + end + end + end +end diff --git a/app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb b/app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb new file mode 100644 index 0000000..c787e52 --- /dev/null +++ b/app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb @@ -0,0 +1,21 @@ +module SolidusMarketplace + module Admin + module StockItemsControllerDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + before_action :load_supplier_stock_location, only: :index + end + + module InstanceMethods + def load_supplier_stock_location + if try_spree_current_user.supplier + @stock_locations = Spree::StockLocation.by_supplier(try_spree_current_user.supplier).accessible_by(current_ability, :read) + @stock_item_stock_locations = params[:stock_location_id].present? ? @stock_locations.where(id: params[:stock_location_id]) : @stock_locations + end + end + end + end + end +end diff --git a/app/controllers/solidus_marketplace/admin/stock_locations_controller_decorator.rb b/app/controllers/solidus_marketplace/admin/stock_locations_controller_decorator.rb new file mode 100644 index 0000000..8f45e49 --- /dev/null +++ b/app/controllers/solidus_marketplace/admin/stock_locations_controller_decorator.rb @@ -0,0 +1,32 @@ +module SolidusMarketplace + module Admin + module StockLocationsControllerDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + create.after :set_supplier + + def index + @stock_locations = Spree::StockLocation.accessible_by(current_ability, :read) + .order('name ASC') + .ransack(params[:q]) + .result + .page(params[:page]) + .per(params[:per_page]) + end + end + + module InstanceMethods + private + + def set_supplier + if try_spree_current_user.supplier? + @object.supplier = try_spree_current_user.supplier + @object.save + end + end + end + end + end +end diff --git a/app/controllers/solidus_marketplace/admin/users_controller_decorator.rb b/app/controllers/solidus_marketplace/admin/users_controller_decorator.rb new file mode 100644 index 0000000..73e313c --- /dev/null +++ b/app/controllers/solidus_marketplace/admin/users_controller_decorator.rb @@ -0,0 +1,56 @@ +module SolidusMarketplace + module Admin + module UsersControllerDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + end + + + module InstanceMethods + def create + current_supplier = try_spree_current_user.supplier + @user = Spree.user_class.new(user_params) + @user.supplier = current_supplier + + if @user.save + set_roles + set_stock_locations + + flash[:success] = t('spree.created_successfully') + redirect_to edit_admin_user_url(@user) + else + load_roles + load_stock_locations + + flash.now[:error] = @user.errors.full_messages.join(", ") + render :new, status: :unprocessable_entity + end + end + + def wallet + @payment_method = Spree::PaymentMethod.find_by(type: 'Spree::Gateway::StripeGateway', + deleted_at: nil) + end + + def removewallet + @payment_method = Spree::PaymentMethod.find_by(type: 'Spree::Gateway::StripeGateway', + deleted_at: nil) + end + + private + + def build_resource + current_supplier = try_spree_current_user.supplier + + if parent? + parent.send(controller_name).build + else + model_class.new(supplier: current_supplier) + end + end + end + end + end +end diff --git a/app/controllers/solidus_marketplace/api/stock_locations_controller_decorator.rb b/app/controllers/solidus_marketplace/api/stock_locations_controller_decorator.rb new file mode 100644 index 0000000..3cfdecf --- /dev/null +++ b/app/controllers/solidus_marketplace/api/stock_locations_controller_decorator.rb @@ -0,0 +1,27 @@ +module SolidusMarketplace + module Api + module StockLocationsControllerDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + before_action :supplier_locations, only: [:index] + before_action :supplier_transfers, only: [:index] + end + + module InstanceMethods + private + + def supplier_locations + params[:q] ||= {} + params[:q][:supplier_id_eq] = spree_current_user.supplier_id + end + + def supplier_transfers + params[:q] ||= {} + params[:q][:supplier_id_eq] = spree_current_user.supplier_id + end + end + end + end +end diff --git a/app/controllers/solidus_marketplace/base_controller_decorator.rb b/app/controllers/solidus_marketplace/base_controller_decorator.rb new file mode 100644 index 0000000..6f5d67f --- /dev/null +++ b/app/controllers/solidus_marketplace/base_controller_decorator.rb @@ -0,0 +1,20 @@ +module SolidusMarketplace + module BaseControllerDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + prepend_before_action :redirect_supplier + end + + module InstanceMethods + private + + def redirect_supplier + if ['/admin', '/admin/authorization_failure'].include?(request.path) && try_spree_current_user.try(:supplier) + redirect_to '/admin/shipments' and return false + end + end + end + end +end diff --git a/app/controllers/solidus_marketplace/users_controller_decorator.rb b/app/controllers/solidus_marketplace/users_controller_decorator.rb new file mode 100644 index 0000000..b5cca7d --- /dev/null +++ b/app/controllers/solidus_marketplace/users_controller_decorator.rb @@ -0,0 +1,53 @@ +module SolidusMarketplace + module UsersControllerDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + end + + module InstanceMethods + def create + current_supplier = try_spree_current_user.supplier + @user = Spree.user_class.new(user_params) + @user.supplier = current_supplier + + if @user.save + set_roles + set_stock_locations + + flash[:success] = t('spree.created_successfully') + redirect_to edit_admin_user_url(@user) + else + load_roles + load_stock_locations + + flash.now[:error] = @user.errors.full_messages.join(", ") + render :new, status: :unprocessable_entity + end + end + + def wallet + @payment_method = Spree::PaymentMethod.find_by(type: 'Spree::Gateway::StripeGateway', + deleted_at: nil) + end + + def removewallet + @payment_method = Spree::PaymentMethod.find_by(type: 'Spree::Gateway::StripeGateway', + deleted_at: nil) + end + + private + + def build_resource + current_supplier = try_spree_current_user.supplier + + if parent? + parent.send(controller_name).build + else + model_class.new(supplier: current_supplier) + end + end + end + end +end diff --git a/app/controllers/spree/admin/marketplace_settings_controller.rb b/app/controllers/spree/admin/marketplace_settings_controller.rb index c775555..2b20969 100644 --- a/app/controllers/spree/admin/marketplace_settings_controller.rb +++ b/app/controllers/spree/admin/marketplace_settings_controller.rb @@ -1,19 +1,21 @@ -class Spree::Admin::MarketplaceSettingsController < Spree::Admin::BaseController +module Spree + module Admin + class MarketplaceSettingsController < Spree::Admin::BaseController + def edit + @config = SolidusMarketplace::Config + end - def edit - @config = Spree::MarketplaceConfiguration.new - end + def update + config = SolidusMarketplace::Config - def update - config = Spree::MarketplaceConfiguration.new + params.each do |name, value| + next unless config.has_preference? name + config[name] = value + end - params.each do |name, value| - next unless config.has_preference? name - config[name] = value + flash[:success] = t('spree.admin.marketplace_settings.update.success') + redirect_to spree.edit_admin_marketplace_settings_path + end end - - flash[:success] = Spree.t('admin.marketplace_settings.update.success') - redirect_to spree.edit_admin_marketplace_settings_path end - end diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb index 32464be..e779683 100644 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ b/app/controllers/spree/admin/products_controller_decorator.rb @@ -1,79 +1 @@ -Spree::Admin::ProductsController.class_eval do - - before_action :get_suppliers, only: [:edit] #, :update] - before_action :supplier_collection, only: [:index] - after_action :update_product_suppliers, only: [:update], unless: -> { params['product']['supplier_ids'].nil? } - create.after :add_product_to_supplier - - private - - def permitted_resource_params - params[object_name].present? ? params.require(object_name).permit! : ActionController::Parameters.new.permit! - params[object_name].except(:supplier_ids) - end - - def update_product_suppliers - if adding_suppliers? - supplier_ids = new_supplier_ids - current_supplier_ids - @product.add_suppliers!(supplier_ids) - elsif removing_suppliers? - supplier_ids = current_supplier_ids - new_supplier_ids - @product.remove_suppliers!(supplier_ids) - elsif same_number_of_suppliers? && different_suppliers? - @product.remove_suppliers!(current_suppliers) - @product.add_suppliers!(new_suppliers) - elsif same_suppliers? - #noop - end - end - - def adding_suppliers? - new_supplier_ids.count > current_supplier_ids.count - end - - def removing_suppliers? - new_supplier_ids.count < current_supplier_ids.count - end - - def same_suppliers? - new_supplier_ids == current_supplier_ids - end - - def same_number_of_suppliers? - new_supplier_ids.count == current_supplier_ids.count - end - - def different_suppliers? - new_supplier_ids.sort != current_supplier_ids.sort - end - - def new_supplier_ids - return [] unless params["product"].present? && params["product"]["supplier_ids"].present? - params["product"]["supplier_ids"].split(",").map(&:to_i) - end - - def current_supplier_ids - @product.supplier_ids - end - - def get_suppliers - @suppliers = Spree::Supplier.order(:name) - end - - # Scopes the collection to what the user should have access to, based on the user's role - def supplier_collection - return unless try_spree_current_user - - if try_spree_current_user.supplier? - @collection = @collection.joins(:suppliers).where('spree_suppliers.id = ?', try_spree_current_user.supplier_id) - end - end - - # Newly added products by a Supplier are associated with it. - def add_product_to_supplier - if try_spree_current_user && try_spree_current_user.supplier? - @product.add_supplier!(try_spree_current_user.supplier_id) - end - end - -end +Spree::Admin::ProductsController.include(SolidusMarketplace::Admin::ProductsControllerDecorator) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb index 9cf3b6a..67532ce 100644 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ b/app/controllers/spree/admin/reports_controller_decorator.rb @@ -1,70 +1,3 @@ -require 'csv' - -Spree::Admin::ReportsController.class_eval do - before_action :add_marketplace_reports, only: [:index] - - def earnings - @supplier_earnings = get_supplier_earnings - respond_to do |format| - format.html - format.csv { send_data earnings_csv } - end - end - - - - def earnings_csv - header1 = ["Supplier Earnings"] - header2 = ["Supplier", "Earnings", "Paypal Email"] - - CSV.generate do |csv| - csv << header1 - csv << [] - csv << header2 - @supplier_earnings.each do |se| - csv << ["#{se[:name]}", "#{se[:earnings].to_html}", "#{se[:paypal_email]}"] - end - end - end - - private - - def add_marketplace_reports - marketplace_reports.each do |report| - Spree::Admin::ReportsController.add_available_report! report - end - end - - def marketplace_reports - [:earnings] - end - - def get_supplier_earnings - grouped_supplier_earnings.each do |se| - se[:earnings] = se[:earnings].inject(Spree::Money.new(0)){ |e, c| c + e } - end - end - - def grouped_supplier_earnings - params[:q] = search_params - @search = Spree::Order.complete.ransack(params[:q]) - @orders = @search.result - - supplier_earnings_map = @orders.map { |o| o.supplier_earnings_map } - grouped_suppliers_map = supplier_earnings_map.flatten.group_by { |e| e[:name] }.values - grouped_earnings = grouped_suppliers_map.map do |gs| - h = {} - h[:name] = nil - h[:paypal_email] = nil - h[:earnings] = [] - gs.each do |s| - h[:name] = s[:name] if h[:name].nil? - h[:paypal_email] = s[:paypal_email] if h[:paypal_email].nil? - h[:earnings] << s[:earnings] - end - h - end - grouped_earnings - end - +if defined?(SolidusReports::Engine) + Spree::Admin::ReportsController.include(SolidusMarketplace::Admin::ReportsControllerDecorator) end diff --git a/app/controllers/spree/admin/shipments_controller.rb b/app/controllers/spree/admin/shipments_controller.rb index 0a70e44..6874ace 100644 --- a/app/controllers/spree/admin/shipments_controller.rb +++ b/app/controllers/spree/admin/shipments_controller.rb @@ -1,7 +1,6 @@ module Spree module Admin class ShipmentsController < Spree::Admin::ResourceController - def index params[:q] ||= {} # params[:q][:completed_at_null] ||= '1' @@ -41,7 +40,6 @@ def find_resource model_class.find_by!(number: params[:id]) end end - end end end diff --git a/app/controllers/spree/admin/stock_items_controller_decorator.rb b/app/controllers/spree/admin/stock_items_controller_decorator.rb index a24727d..e2e729b 100644 --- a/app/controllers/spree/admin/stock_items_controller_decorator.rb +++ b/app/controllers/spree/admin/stock_items_controller_decorator.rb @@ -1,10 +1 @@ -Spree::Admin::StockItemsController.class_eval do - before_action :load_supplier_stock_location, only: :index - - def load_supplier_stock_location - if try_spree_current_user.supplier - @stock_locations = Spree::StockLocation.by_supplier(try_spree_current_user.supplier).accessible_by(current_ability, :read) - @stock_item_stock_locations = params[:stock_location_id].present? ? @stock_locations.where(id: params[:stock_location_id]) : @stock_locations - end - end -end +Spree::Admin::StockItemsController.include(SolidusMarketplace::Admin::StockItemsControllerDecorator) diff --git a/app/controllers/spree/admin/stock_locations_controller_decorator.rb b/app/controllers/spree/admin/stock_locations_controller_decorator.rb index 4022961..b42af02 100644 --- a/app/controllers/spree/admin/stock_locations_controller_decorator.rb +++ b/app/controllers/spree/admin/stock_locations_controller_decorator.rb @@ -1,23 +1 @@ -Spree::Admin::StockLocationsController.class_eval do - - create.after :set_supplier - - def index - @stock_locations = Spree::StockLocation.accessible_by(current_ability, :read) - .order('name ASC') - .ransack(params[:q]) - .result - .page(params[:page]) - .per(params[:per_page]) - end - - private - - def set_supplier - if try_spree_current_user.supplier? - @object.supplier = try_spree_current_user.supplier - @object.save - end - end - -end +Spree::Admin::StockLocationsController.include(SolidusMarketplace::Admin::StockLocationsControllerDecorator) diff --git a/app/controllers/spree/admin/suppliers_controller.rb b/app/controllers/spree/admin/suppliers_controller.rb index 4744134..362b207 100644 --- a/app/controllers/spree/admin/suppliers_controller.rb +++ b/app/controllers/spree/admin/suppliers_controller.rb @@ -1,54 +1,36 @@ -class Spree::Admin::SuppliersController < Spree::Admin::ResourceController +module Spree + module Admin + class SuppliersController < Spree::Admin::ResourceController + before_action :set_address, only: [:update] + before_action :build_address, only: [:edit, :new] + + private + + def set_address + address_attributes = permitted_resource_params.delete(:address_attributes) + @object.address = Spree::Address.immutable_merge(@object.address, + permitted_resource_params.delete(:address_attributes)) + end - def update - @object.address = Spree::Address.immutable_merge(@object.address, permitted_resource_params[:address_attributes]) + def build_address + @object.address = Spree::Address.build_default unless @object.address.present? + end - if @object.update_attributes(permitted_resource_params.except(:address_attributes)) - respond_with(@object) do |format| - format.html do - flash[:success] = flash_message_for(@object, :successfully_updated) - redirect_to location_after_save - end - format.js { render layout: false } + def collection + params[:q] ||= {} + params[:q][:meta_sort] ||= 'name.asc' + @search = Spree::Supplier.search(params[:q]) + @collection = @search.result.page(params[:page]). + per(Spree::Config[:orders_per_page]) end - else - respond_with(@object) do |format| - format.html do - flash.now[:error] = @object.errors.full_messages.join(", ") - render_after_update_error - end - format.js { render layout: false } + + def find_resource + Spree::Supplier.friendly.find(params[:id]) end - end - end - def edit - @object.address = Spree::Address.build_default unless @object.address.present? - respond_with(@object) do |format| - format.html { render :layout => !request.xhr? } - format.js { render :layout => false } + def location_after_save + spree.edit_admin_supplier_path(@object) + end end end - - def new - @object.address = Spree::Address.build_default - end - - private - - def collection - params[:q] ||= {} - params[:q][:meta_sort] ||= "name.asc" - @search = Spree::Supplier.search(params[:q]) - @collection = @search.result.page(params[:page]).per(Spree::Config[:orders_per_page]) - end - - def find_resource - Spree::Supplier.friendly.find(params[:id]) - end - - def location_after_save - spree.edit_admin_supplier_path(@object) - end - end diff --git a/app/controllers/spree/admin/users_controller_decorator.rb b/app/controllers/spree/admin/users_controller_decorator.rb index a9d4fe4..80dc38b 100644 --- a/app/controllers/spree/admin/users_controller_decorator.rb +++ b/app/controllers/spree/admin/users_controller_decorator.rb @@ -1,103 +1 @@ -Spree::Admin::UsersController.class_eval do - - def create - current_supplier = try_spree_current_user.supplier - @user = Spree.user_class.new(user_params) - @user.supplier = current_supplier - if @user.save - set_roles - set_stock_locations - - flash[:success] = Spree.t(:created_successfully) - redirect_to edit_admin_user_url(@user) - else - load_roles - load_stock_locations - - flash.now[:error] = @user.errors.full_messages.join(", ") - render :new, status: :unprocessable_entity - end - end - - def wallet - @payment_method = Spree::PaymentMethod.find_by(type: 'Spree::Gateway::StripeGateway', deleted_at: nil) - end - - def wallets_actions - @user = Spree::User.find(params[:id]) - wallet_payment_source = Spree::WalletPaymentSource.find(params[:order][:wallet_payment_source_id]) - - if params[:submit] == 'change_default_payment' - @user.wallet.default_wallet_payment_source = wallet_payment_source - if @user.save - flash[:success] = I18n.t('spree.admin.user.payment_wallet.actions.successfully_change_default_payment') - redirect_to spree.wallets_admin_user_path(@user) and return - else - flash[:error] = I18n.t('spree.admin.user.payment_wallet.actions.error_change_default_payment') - redirect_to spree.wallets_admin_user_path(@user) and return - end - elsif params[:submit] == 'remove_card' - if wallet_payment_source.destroy - flash[:success] = I18n.t('spree.admin.user.payment_wallet.actions.successfully_card_removed') - redirect_to spree.wallets_admin_user_path(@user) and return - else - flash[:error] = I18n.t('spree.admin.user.payment_wallet.actions.error_card_removed') - redirect_to spree.wallets_admin_user_path(@user) and return - end - end - end - - def addcard - - use_existing_card = params[:use_existing_card].present? ? params[:use_existing_card]: 'not' - wallet_payment_source_id = params[:order].present? ? params[:order][:wallet_payment_source_id]: nil - payment_source = params[:payment_source].present? ? params[:payment_source]["6"]: nil - - token = params[:card].present? ? params[:card][:token]: nil - - if token != '0000' - user_wallet = @user.wallet - begin - customer = Stripe::Customer.create(description: @user.email, email: @user.email) - card = customer.sources.create(source: token) - method = Spree::PaymentMethod.find_by(type: 'Spree::PaymentMethod::StripeCreditCard', deleted_at: nil) - credit_card = Spree::CreditCard.new(month: card.exp_month, year: card.exp_year, cc_type: card.brand.downcase, - last_digits: card.last4, gateway_customer_profile_id: customer.id, gateway_payment_profile_id: card.id, - name: params.require(:payment_source)["6"][:name], user_id: @user.id, payment_method_id: method.id) - - credit_card.save! - user_wallet.add(credit_card) - # Create wallet record - wallet_payment_source = user_wallet.add(credit_card) - user_wallet.default_wallet_payment_source = wallet_payment_source - if @user.save - flash[:success] = I18n.t("spree.admin.user.payment_wallet.actions.success_add_card") - redirect_to spree.wallets_admin_user_path(@user) - else - flash[:error] = @user.errors.full_messages.to_sentence - redirect_to spree.wallets_admin_user_path(@user) - end - - rescue Stripe::CardError => e - flash[:error] = I18n.t('spree.admin.user.payment_wallet.actions.error_add_card') - redirect_to spree.wallets_admin_user_path(@user) - end - else - flash[:error] = I18n.t('spree.admin.user.payment_wallet.actions.error_add_card') - redirect_to spree.wallets_admin_user_path(@user) - end - end - - private - - def build_resource - current_supplier = try_spree_current_user.supplier - - if parent? - parent.send(controller_name).build - else - model_class.new(supplier: current_supplier) - end - end - -end +Spree::Admin::UsersController.include(SolidusMarketplace::Admin::UsersControllerDecorator) diff --git a/app/controllers/spree/api/stock_locations_controller_decorator.rb b/app/controllers/spree/api/stock_locations_controller_decorator.rb new file mode 100644 index 0000000..c95dc05 --- /dev/null +++ b/app/controllers/spree/api/stock_locations_controller_decorator.rb @@ -0,0 +1 @@ +Spree::Api::StockLocationsController.include(SolidusMarketplace::Api::StockLocationsControllerDecorator) diff --git a/app/controllers/spree/api/suppliers_controller.rb b/app/controllers/spree/api/suppliers_controller.rb index 010cb9c..73e2b8a 100644 --- a/app/controllers/spree/api/suppliers_controller.rb +++ b/app/controllers/spree/api/suppliers_controller.rb @@ -3,9 +3,11 @@ module Api class SuppliersController < Spree::Api::BaseController def index if params[:ids] - @suppliers = Spree::Supplier.accessible_by(current_ability, :read).where(id: params[:ids].split(',')).order(:name) + @suppliers = Spree::Supplier.accessible_by(current_ability, :read). + where(id: params[:ids].split(',')).order(:name) else - @suppliers = Spree::Supplier.accessible_by(current_ability, :read).order(:name).ransack(params[:q]).result + @suppliers = Spree::Supplier.accessible_by(current_ability, :read). + order(:name).ransack(params[:q]).result end @suppliers = paginate(@suppliers) diff --git a/app/controllers/spree/api/v1/stock_locations_controller_decorator.rb b/app/controllers/spree/api/v1/stock_locations_controller_decorator.rb deleted file mode 100644 index 223f49f..0000000 --- a/app/controllers/spree/api/v1/stock_locations_controller_decorator.rb +++ /dev/null @@ -1,18 +0,0 @@ -Spree::Api::StockLocationsController.class_eval do - - before_action :supplier_locations, only: [:index] - before_action :supplier_transfers, only: [:index] - - private - - def supplier_locations - params[:q] ||= {} - params[:q][:supplier_id_eq] = spree_current_user.supplier_id - end - - def supplier_transfers - params[:q] ||= {} - params[:q][:supplier_id_eq] = spree_current_user.supplier_id - end - -end diff --git a/app/controllers/spree/base_controller_decorator.rb b/app/controllers/spree/base_controller_decorator.rb index 28976b9..64be38d 100644 --- a/app/controllers/spree/base_controller_decorator.rb +++ b/app/controllers/spree/base_controller_decorator.rb @@ -1,13 +1 @@ -Spree::BaseController.class_eval do - - prepend_before_action :redirect_supplier - - private - - def redirect_supplier - if ['/admin', '/admin/authorization_failure'].include?(request.path) && try_spree_current_user.try(:supplier) - redirect_to '/admin/shipments' and return false - end - end - -end +Spree::BaseController.include(SolidusMarketplace::BaseControllerDecorator) diff --git a/app/mailers/spree/marketplace_order_mailer.rb b/app/mailers/spree/marketplace_order_mailer.rb index 9525025..9c51490 100644 --- a/app/mailers/spree/marketplace_order_mailer.rb +++ b/app/mailers/spree/marketplace_order_mailer.rb @@ -1,13 +1,15 @@ module Spree class MarketplaceOrderMailer < Spree::BaseMailer - default from: Spree::Store.default.mail_from_address def supplier_order(shipment_id) - @shipment = Shipment.find shipment_id + @shipment = Spree::Shipment.find(shipment_id) @supplier = @shipment.supplier - mail to: @supplier.user.email, subject: Spree.t('marketplace_order_mailer.supplier_order.subject', name: Spree::Store.current.name, number: @shipment.number) - end + subject = t('spree.marketplace_order_mailer.supplier_order.subject', + name: Spree::Store.current.name, number: @shipment.number) + mail to: @supplier.user.email, + subject: subject + end end end diff --git a/app/mailers/spree/supplier_mailer.rb b/app/mailers/spree/supplier_mailer.rb index 0f35de2..f41aea8 100644 --- a/app/mailers/spree/supplier_mailer.rb +++ b/app/mailers/spree/supplier_mailer.rb @@ -1,12 +1,11 @@ module Spree class SupplierMailer < Spree::BaseMailer - default from: Spree::Store.default.mail_from_address def welcome(supplier_id) - @supplier = Supplier.find supplier_id - mail to: @supplier.user.email, subject: Spree.t('supplier_mailer.welcome.subject') + @supplier = Spree::Supplier.find(supplier_id) + mail to: @supplier.user.email, + subject: t('spree.supplier_mailer.welcome.subject') end - end end diff --git a/app/models/solidus_marketplace/order_decorator.rb b/app/models/solidus_marketplace/order_decorator.rb new file mode 100644 index 0000000..a8a6a3f --- /dev/null +++ b/app/models/solidus_marketplace/order_decorator.rb @@ -0,0 +1,50 @@ +module SolidusMarketplace + module OrderDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + has_many :stock_locations, through: :shipments + has_many :suppliers, through: :stock_locations + end + + module InstanceMethods + def supplier_total(user_or_supplier) + supplier = user_or_supplier.is_a?(Spree::Supplier) ? user_or_supplier : user_or_supplier.supplier + shipments = self.shipments.by_supplier(supplier) + commissions = shipments.map(&:supplier_commission_total) + Spree::Money.new(commissions.sum) + end + + def supplier_earnings_map + suppliers.map do |s| + { + name: s.name, + earnings: self.supplier_total(s), + paypal_email: s.paypal_email + } + end + end + + # Once order is finalized we want to notify the suppliers of their drop ship orders. + # Here we are handling notification by emailing the suppliers. + # If you want to customize this you could override it as a hook for notifying a supplier with a API request instead. + def finalize_with_supplier! + finalize_without_supplier! + shipments.each do |shipment| + if SolidusMarketplace::Config.send_supplier_email && shipment.supplier.present? + begin + Spree::MarketplaceOrderMailer.supplier_order(shipment.id).deliver! + rescue => ex #Errno::ECONNREFUSED => ex + puts ex.message + puts ex.backtrace.join("\n") + Rails.logger.error ex.message + Rails.logger.error ex.backtrace.join("\n") + return true # always return true so that failed email doesn't crash app. + end + end + end + end + end + end +end diff --git a/app/models/solidus_marketplace/payment_decorator.rb b/app/models/solidus_marketplace/payment_decorator.rb new file mode 100644 index 0000000..8743877 --- /dev/null +++ b/app/models/solidus_marketplace/payment_decorator.rb @@ -0,0 +1,9 @@ +module SolidusMarketplace + module PaymentDecorator + extend ActiveSupport::Concern + + included do + belongs_to :payable, polymorphic: true + end + end +end diff --git a/app/models/solidus_marketplace/product_decorator.rb b/app/models/solidus_marketplace/product_decorator.rb new file mode 100644 index 0000000..54b961f --- /dev/null +++ b/app/models/solidus_marketplace/product_decorator.rb @@ -0,0 +1,58 @@ +module SolidusMarketplace + module ProductDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + has_many :suppliers, -> { readonly }, through: :master + scope :of_supplier, -> (supplier_id) { joins(:suppliers).where('spree_suppliers.id = ?', supplier_id) } + end + + module InstanceMethods + def add_supplier!(supplier_or_id) + supplier = supplier_or_id.is_a?(Spree::Supplier) ? supplier_or_id : Spree::Supplier.find(supplier_or_id) + populate_for_supplier! supplier if supplier + end + + def add_suppliers!(supplier_ids) + Spree::Supplier.where(id: supplier_ids).each do |supplier| + populate_for_supplier! supplier + end + end + + def remove_suppliers!(supplier_ids) + Spree::Supplier.where(id: supplier_ids).each do |supplier| + unpopulate_for_supplier! supplier + end + end + + # Returns true if the product has one or more suppliers. + def supplier? + suppliers.present? + end + + private + + def populate_for_supplier!(supplier) + variants_including_master.each do |variant| + unless variant.suppliers.pluck(:id).include?(supplier.id) + variant.suppliers << supplier + supplier.stock_locations.each { |location| location.propagate_variant(variant) unless location.stock_item(variant) } + end + end + end + + def unpopulate_for_supplier!(supplier) + variants_including_master.each do |variant| + if variant.suppliers.pluck(:id).include?(supplier.id) + variant.suppliers.delete(supplier) + + supplier.stock_locations.each do |location| + location.unpropagate_variant(variant) + end + end + end + end + end + end +end diff --git a/app/models/solidus_marketplace/shipment_decorator.rb b/app/models/solidus_marketplace/shipment_decorator.rb new file mode 100644 index 0000000..a5d6325 --- /dev/null +++ b/app/models/solidus_marketplace/shipment_decorator.rb @@ -0,0 +1,43 @@ +module SolidusMarketplace + module ShipmentDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + belongs_to :order, class_name: 'Spree::Order', touch: true, inverse_of: :shipments + has_many :payments, as: :payable + scope :by_supplier, -> (supplier_id) { joins(:stock_location).where(spree_stock_locations: { supplier_id: supplier_id }) } + delegate :supplier, to: :stock_location + self.whitelisted_ransackable_attributes = ["number", "state"] + end + + module InstanceMethods + def display_final_price_with_items + Spree::Money.new final_price_with_items + end + + def final_price_with_items + self.item_cost + self.final_price + end + + # TODO move commission to spree_marketplace? + def supplier_commission_total + ((self.final_price_with_items * self.supplier.commission_percentage / 100) + self.supplier.commission_flat_rate) + end + + private + + def after_ship + super + + if supplier.present? + update_commission + end + end + + def update_commission + update_column(:supplier_commission, self.supplier_commission_total) + end + end + end +end diff --git a/app/models/solidus_marketplace/stock_location_decorator.rb b/app/models/solidus_marketplace/stock_location_decorator.rb new file mode 100644 index 0000000..098c1b0 --- /dev/null +++ b/app/models/solidus_marketplace/stock_location_decorator.rb @@ -0,0 +1,29 @@ +module SolidusMarketplace + module StockLocationDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + belongs_to :supplier, class_name: 'Spree::Supplier' + scope :by_supplier, -> (supplier_id) { where(supplier_id: supplier_id) } + end + + module InstanceMethods + # Wrapper for creating a new stock item respecting the backorderable config and supplier + def propagate_variant(variant) + if self.supplier_id.blank? || variant.suppliers.pluck(:id).include?(self.supplier_id) + self.stock_items.create!(variant: variant, backorderable: self.backorderable_default) + end + end + + def unpropagate_variant(variant) + stock_items = self.stock_items.where(variant: variant) + stock_items.map(&:destroy) + end + + def available?(variant) + stock_item(variant).try(:available?) + end + end + end +end diff --git a/app/models/solidus_marketplace/user_decorator.rb b/app/models/solidus_marketplace/user_decorator.rb new file mode 100644 index 0000000..4b01695 --- /dev/null +++ b/app/models/solidus_marketplace/user_decorator.rb @@ -0,0 +1,29 @@ +module SolidusMarketplace + module UserDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + belongs_to :supplier, class_name: 'Spree::Supplier' + has_many :variants, through: :supplier + end + + module InstanceMethods + def supplier? + supplier.present? + end + + def supplier_admin? + spree_roles.map(&:name).include?("supplier_admin") + end + + def market_maker? + has_admin_role? + end + + def has_admin_role? + spree_roles.map(&:name).include?("admin") + end + end + end +end diff --git a/app/models/solidus_marketplace/variant_decorator.rb b/app/models/solidus_marketplace/variant_decorator.rb new file mode 100644 index 0000000..13c556a --- /dev/null +++ b/app/models/solidus_marketplace/variant_decorator.rb @@ -0,0 +1,28 @@ +module SolidusMarketplace + module VariantDecorator + extend ActiveSupport::Concern + + included do + prepend(InstanceMethods) + has_many :supplier_variants + has_many :suppliers, through: :supplier_variants + before_create :populate_for_suppliers + end + + module InstanceMethods + private + + def create_stock_items + Spree::StockLocation.all.each do |stock_location| + if stock_location.supplier_id.blank? || self.suppliers.pluck(:id).include?(stock_location.supplier_id) + stock_location.propagate_variant(self) if stock_location.propagate_all_variants? + end + end + end + + def populate_for_suppliers + self.suppliers = self.product.suppliers + end + end + end +end diff --git a/app/models/spree/mail_method_decorator.rb b/app/models/spree/mail_method_decorator.rb deleted file mode 100644 index 8c042ba..0000000 --- a/app/models/spree/mail_method_decorator.rb +++ /dev/null @@ -1,5 +0,0 @@ -Spree::Core::MailMethod.class_eval do - def settings - mail_server_settings - end -end \ No newline at end of file diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb index 32b46e2..cb149ed 100644 --- a/app/models/spree/order_decorator.rb +++ b/app/models/spree/order_decorator.rb @@ -1,44 +1 @@ -Spree::Order.class_eval do - - has_many :stock_locations, through: :shipments - has_many :suppliers, through: :stock_locations - - def supplier_total(user_or_supplier) - supplier = user_or_supplier.is_a?(Spree::Supplier) ? user_or_supplier : user_or_supplier.supplier - shipments = self.shipments.by_supplier(supplier) - commissions = shipments.map(&:supplier_commission_total) - Spree::Money.new(commissions.sum) - end - - def supplier_earnings_map - suppliers.map do |s| - { - name: s.name, - earnings: self.supplier_total(s), - paypal_email: s.paypal_email - } - end - end - - # Once order is finalized we want to notify the suppliers of their drop ship orders. - # Here we are handling notification by emailing the suppliers. - # If you want to customize this you could override it as a hook for notifying a supplier with a API request instead. - def finalize_with_supplier! - finalize_without_supplier! - shipments.each do |shipment| - if SolidusMarketplace::Config[:send_supplier_email] && shipment.supplier.present? - begin - Spree::MarketplaceOrderMailer.supplier_order(shipment.id).deliver! - rescue => ex #Errno::ECONNREFUSED => ex - puts ex.message - puts ex.backtrace.join("\n") - Rails.logger.error ex.message - Rails.logger.error ex.backtrace.join("\n") - return true # always return true so that failed email doesn't crash app. - end - end - end - end - # alias_method :finalize!, :drop_ship - -end +Spree::Order.include(SolidusMarketplace::OrderDecorator) diff --git a/app/models/spree/payment_decorator.rb b/app/models/spree/payment_decorator.rb index a8003e4..33b320e 100644 --- a/app/models/spree/payment_decorator.rb +++ b/app/models/spree/payment_decorator.rb @@ -1,7 +1 @@ -module Spree - Payment.class_eval do - - belongs_to :payable, polymorphic: true, optional: true - - end -end +Spree::Payment.include(SolidusMarketplace::PaymentDecorator) diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb index 5972410..8075f08 100644 --- a/app/models/spree/product_decorator.rb +++ b/app/models/spree/product_decorator.rb @@ -1,52 +1 @@ -Spree::Product.class_eval do - - has_many :suppliers, -> { readonly }, through: :master - - scope :of_supplier, -> (supplier_id) { joins(:suppliers).where('spree_suppliers.id = ?', supplier_id) } - - def add_supplier!(supplier_or_id) - supplier = supplier_or_id.is_a?(Spree::Supplier) ? supplier_or_id : Spree::Supplier.find(supplier_or_id) - populate_for_supplier! supplier if supplier - end - - def add_suppliers!(supplier_ids) - Spree::Supplier.where(id: supplier_ids).each do |supplier| - populate_for_supplier! supplier - end - end - - def remove_suppliers!(supplier_ids) - Spree::Supplier.where(id: supplier_ids).each do |supplier| - unpopulate_for_supplier! supplier - end - end - - # Returns true if the product has one or more suppliers. - def supplier? - suppliers.present? - end - - private - - def populate_for_supplier!(supplier) - variants_including_master.each do |variant| - unless variant.suppliers.pluck(:id).include?(supplier.id) - variant.suppliers << supplier - supplier.stock_locations.each { |location| location.propagate_variant(variant) unless location.stock_item(variant) } - end - end - end - - def unpopulate_for_supplier!(supplier) - variants_including_master.each do |variant| - if variant.suppliers.pluck(:id).include?(supplier.id) - variant.suppliers.delete(supplier) - - supplier.stock_locations.each do |location| - location.unpropagate_variant(variant) - end - end - end - end - -end +Spree::Product.include(SolidusMarketplace::ProductDecorator) diff --git a/app/models/spree/shipment_decorator.rb b/app/models/spree/shipment_decorator.rb index 56cd6ba..08dc1e9 100644 --- a/app/models/spree/shipment_decorator.rb +++ b/app/models/spree/shipment_decorator.rb @@ -1,40 +1 @@ -Spree::Shipment.class_eval do - # TODO here to fix cancan issue thinking its just Order - belongs_to :order, class_name: 'Spree::Order', touch: true, inverse_of: :shipments - - has_many :payments, as: :payable - - scope :by_supplier, -> (supplier_id) { joins(:stock_location).where(spree_stock_locations: { supplier_id: supplier_id }) } - - delegate :supplier, to: :stock_location - - self.whitelisted_ransackable_attributes = ["number", "state"] - - def display_final_price_with_items - Spree::Money.new final_price_with_items - end - - def final_price_with_items - self.item_cost + self.final_price - end - - # TODO move commission to spree_marketplace? - def supplier_commission_total - ((self.final_price_with_items * self.supplier.commission_percentage / 100) + self.supplier.commission_flat_rate) - end - - private - - durably_decorate :after_ship, mode: 'soft', sha: '5401c76850108aba74c87a87ff634379bdc844ce' do - original_after_ship - - if supplier.present? - update_commission - end - end - - def update_commission - update_column :supplier_commission, self.supplier_commission_total - end - -end +Spree::Shipment.include(SolidusMarketplace::ShipmentDecorator) diff --git a/app/models/spree/stock/splitter/marketplace.rb b/app/models/spree/stock/splitter/marketplace.rb index d13d969..0d261fb 100644 --- a/app/models/spree/stock/splitter/marketplace.rb +++ b/app/models/spree/stock/splitter/marketplace.rb @@ -5,37 +5,37 @@ class Marketplace < Spree::Stock::Splitter::Base def split(packages) begin - split_packages = [] - packages.each do |package| - # Package fulfilled items together. - fulfilled = package.contents.select { |content| - begin - content.variant.suppliers.count == 0 - rescue => e - end - } - split_packages << build_package(fulfilled) - # Determine which supplier to package shipped items. - supplier_contents = package.contents.select { |content| content.variant.suppliers.count > 0 } - supplier_contents.each do |content| - # Select the related variant - variant = content.variant - # Select suppliers ordering ascending according to cost. - suppliers = variant.supplier_variants.order("spree_supplier_variants.cost ASC").map(&:supplier) - # Select first supplier that has stock location with avialable stock item. - available_supplier = suppliers.detect do |supplier| - supplier.stock_locations_with_available_stock_items(variant).any? - end - # Select the first available stock location with in the available_supplier stock locations. - stock_location = available_supplier.stock_locations_with_available_stock_items(variant).first - # Add to any existing packages or create a new one. - if existing_package = split_packages.detect { |p| p.stock_location == stock_location } - existing_package.contents << content - else - split_packages << Spree::Stock::Package.new(stock_location, [content]) + split_packages = [] + packages.each do |package| + # Package fulfilled items together. + fulfilled = package.contents.select { |content| + begin + content.variant.suppliers.count == 0 + rescue => e + end + } + split_packages << build_package(fulfilled) + # Determine which supplier to package shipped items. + supplier_contents = package.contents.select { |content| content.variant.suppliers.count > 0 } + supplier_contents.each do |content| + # Select the related variant + variant = content.variant + # Select suppliers ordering ascending according to cost. + suppliers = variant.supplier_variants.order("spree_supplier_variants.cost ASC").map(&:supplier) + # Select first supplier that has stock location with avialable stock item. + available_supplier = suppliers.detect do |supplier| + supplier.stock_locations_with_available_stock_items(variant).any? + end + # Select the first available stock location with in the available_supplier stock locations. + stock_location = available_supplier.stock_locations_with_available_stock_items(variant).first + # Add to any existing packages or create a new one. + if existing_package = split_packages.detect { |p| p.stock_location == stock_location } + existing_package.contents << content + else + split_packages << Spree::Stock::Package.new(stock_location, [content]) + end end end - end rescue => e end return_next split_packages diff --git a/app/models/spree/stock_location_decorator.rb b/app/models/spree/stock_location_decorator.rb index d8a42b0..100191a 100644 --- a/app/models/spree/stock_location_decorator.rb +++ b/app/models/spree/stock_location_decorator.rb @@ -1,23 +1 @@ -Spree::StockLocation.class_eval do - - belongs_to :supplier, class_name: 'Spree::Supplier' - - scope :by_supplier, -> (supplier_id) { where(supplier_id: supplier_id) } - - # Wrapper for creating a new stock item respecting the backorderable config and supplier - durably_decorate :propagate_variant, mode: 'soft', sha: '68fd322da48facbdbf567a2391c2495d04c3c8bc' do |variant| - if self.supplier_id.blank? || variant.suppliers.pluck(:id).include?(self.supplier_id) - self.stock_items.create!(variant: variant, backorderable: self.backorderable_default) - end - end - - def unpropagate_variant(variant) - stock_items = self.stock_items.where(variant: variant) - stock_items.map(&:destroy) - end - - def available?(variant) - stock_item(variant).try(:available?) - end - -end +Spree::StockLocation.include(SolidusMarketplace::StockLocationDecorator) diff --git a/app/models/spree/supplier.rb b/app/models/spree/supplier.rb index 5bdee58..af99902 100644 --- a/app/models/spree/supplier.rb +++ b/app/models/spree/supplier.rb @@ -1,77 +1,74 @@ -class Spree::Supplier < Spree::Base - extend FriendlyId - # include ActiveModel::ForbiddenAttributesProtection - friendly_id :name, use: :slugged - - attr_accessor :password, :password_confirmation - - #========================================== - # Associations - belongs_to :user, :class_name => Spree.user_class.to_s - belongs_to :address, class_name: 'Spree::Address' - accepts_nested_attributes_for :address - - if defined?(Ckeditor::Asset) - has_many :ckeditor_pictures - has_many :ckeditor_attachment_files - end - has_many :supplier_variants - has_many :variants, through: :supplier_variants - has_many :products, through: :variants +module Spree + class Supplier < Spree::Base + extend FriendlyId + friendly_id :name, use: :slugged - has_many :stock_locations - has_many :shipments, through: :stock_locations + attr_accessor :password, :password_confirmation - has_many :users, class_name: Spree.user_class.to_s + belongs_to :user, class_name: Spree.user_class.to_s + belongs_to :admin, class_name: Spree.user_class.to_s + belongs_to :address, class_name: 'Spree::Address' + accepts_nested_attributes_for :address - #========================================== - # Validations + if defined?(Ckeditor::Asset) + has_many :ckeditor_pictures + has_many :ckeditor_attachment_files + end - validates :commission_flat_rate, presence: true - validates :commission_percentage, presence: true - validates :name, presence: true, uniqueness: true - validates :url, format: { with: URI::regexp(%w(http https)), allow_blank: true } + has_many :supplier_variants + has_many :variants, through: :supplier_variants + has_many :products, through: :variants + has_many :stock_locations + has_many :shipments, through: :stock_locations + has_many :users, class_name: Spree.user_class.to_s + has_many :admins, class_name: Spree.user_class.to_s - #========================================== - # Callbacks + validates :commission_flat_rate, presence: true + validates :commission_percentage, presence: true + validates :name, presence: true, uniqueness: true + validates :url, format: { with: URI::regexp(%w(http https)), allow_blank: true } - after_create :assign_user - after_create :create_stock_location - after_create :send_welcome, if: -> { SolidusMarketplace::Config[:send_supplier_email] } - before_create :set_commission - before_validation :check_url + before_validation :check_url - #========================================== - # Instance Methods - scope :active, -> { where(active: true) } + before_create :set_commission + after_create :assign_user + after_create :create_stock_location + after_create :send_welcome, if: -> { SolidusMarketplace::Config[:send_supplier_email] } - def deleted? - deleted_at.present? - end + scope :active, -> { where(active: true) } - def user_ids_string - user_ids.join(',') - end + def deleted? + deleted_at.present? + end - def user_ids_string=(s) - self.user_ids = s.to_s.split(',').map(&:strip) - end + def user_ids_string + user_ids.join(',') + end - # Retreive the stock locations that has available - # stock items of the given variant - def stock_locations_with_available_stock_items(variant) - stock_locations.select { |sl| sl.available?(variant) } - end + def admin_ids_string + admin_ids.join(',') + end - #========================================== - # Protected Methods + def user_ids_string=(user_ids) + self.user_ids = user_ids.to_s.split(',').map(&:strip) + end + + def admin_ids_string=(admin_ids) + self.admin_ids = admin_ids.to_s.split(',').map(&:strip) + end + + # Retreive the stock locations that has available + # stock items of the given variant + def stock_locations_with_available_stock_items(variant) + stock_locations.select { |sl| sl.available?(variant) } + end - protected + protected def assign_user if self.users.empty? - self.users << self.user - self.save + self.users << self.user + self.save end end @@ -94,9 +91,7 @@ def create_stock_location end end - def send_welcome - - end + def send_welcome; end def set_commission unless changes.has_key?(:commission_flat_rate) @@ -106,5 +101,5 @@ def set_commission self.commission_percentage = SolidusMarketplace::Config[:default_commission_percentage] end end - + end end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index e05b438..38262ea 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -1,23 +1 @@ -Spree.user_class.class_eval do - - belongs_to :supplier, class_name: 'Spree::Supplier', optional: true - - has_many :variants, through: :supplier - - def supplier? - supplier.present? - end - - def supplier_admin? - spree_roles.map(&:name).include?("supplier_admin") - end - - def market_maker? - has_admin_role? - end - - def has_admin_role? - spree_roles.map(&:name).include?("admin") - end - -end +Spree.user_class.include(SolidusMarketplace::UserDecorator) diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb index e80414f..5cc155f 100644 --- a/app/models/spree/variant_decorator.rb +++ b/app/models/spree/variant_decorator.rb @@ -1,24 +1 @@ -module Spree - Variant.class_eval do - - has_many :supplier_variants - has_many :suppliers, through: :supplier_variants - - before_create :populate_for_suppliers - - private - - durably_decorate :create_stock_items, mode: 'soft', sha: '98704433ac5c66ba46e02699f3cf03d13d4f1281' do - StockLocation.all.each do |stock_location| - if stock_location.supplier_id.blank? || self.suppliers.pluck(:id).include?(stock_location.supplier_id) - stock_location.propagate_variant(self) if stock_location.propagate_all_variants? - end - end - end - - def populate_for_suppliers - self.suppliers = self.product.suppliers - end - - end -end +Spree::Variant.include(SolidusMarketplace::VariantDecorator) diff --git a/app/overrides/add_supplier_admin_tab.rb b/app/overrides/add_supplier_admin_tab.rb new file mode 100644 index 0000000..9be1237 --- /dev/null +++ b/app/overrides/add_supplier_admin_tab.rb @@ -0,0 +1,6 @@ +Deface::Override.new( + virtual_path: 'spree/admin/shared/_menu', + name: 'marketplace_menus', + insert_bottom: "[data-hook='admin_tabs']", + partial: 'spree/admin/shared/marketplace_tabs' +) diff --git a/app/overrides/add_supplier_cost_to_order_total.rb b/app/overrides/add_supplier_cost_to_order_total.rb new file mode 100644 index 0000000..0a4a951 --- /dev/null +++ b/app/overrides/add_supplier_cost_to_order_total.rb @@ -0,0 +1,7 @@ +Deface::Override.new( + virtual_path: 'spree/admin/orders/index', + name: 'add_supplier_input_to_product_form', + replace_contents: "[data-hook='admin_orders_index_rows'] td:nth-child(9)", + partial: 'spree/admin/shared/supplier_order_total', + disabled: false, +) diff --git a/app/overrides/add_supplier_to_admin_product_form.rb b/app/overrides/add_supplier_to_admin_product_form.rb new file mode 100644 index 0000000..9ac4635 --- /dev/null +++ b/app/overrides/add_supplier_to_admin_product_form.rb @@ -0,0 +1,7 @@ +Deface::Override.new( + virtual_path: 'spree/admin/products/_form', + name: 'add_supplier_input_to_product_form', + insert_before: "[data-hook='admin_product_form_taxons']", + partial: 'spree/admin/shared/supplier_product_form', + disabled: false, +) diff --git a/app/overrides/add_supplier_to_configuration_menu.rb b/app/overrides/add_supplier_to_configuration_menu.rb new file mode 100644 index 0000000..01266e7 --- /dev/null +++ b/app/overrides/add_supplier_to_configuration_menu.rb @@ -0,0 +1,7 @@ +Deface::Override.new( + virtual_path: 'spree/admin/shared/_configuration_menu', + name: 'add_supplier_to_admin_configurations_menu', + insert_bottom: "[data-hook='admin_configurations_sidebar_menu']", + disabled: false, + partial: 'spree/admin/shared/marketplace_settings' +) diff --git a/app/overrides/add_supplier_to_stock_locations.rb b/app/overrides/add_supplier_to_stock_locations.rb new file mode 100644 index 0000000..c75d433 --- /dev/null +++ b/app/overrides/add_supplier_to_stock_locations.rb @@ -0,0 +1,7 @@ +Deface::Override.new( + virtual_path: 'spree/admin/stock_locations/_form', + name: 'add_supplier_to_stock_locations_form', + insert_bottom: '[data-hook="admin_stock_locations_form_fields"]', + partial: 'spree/admin/shared/supplier_stock_location_form', + disabled: false, +) diff --git a/app/overrides/spree/admin/add_payment_data_to_admin_users.rb b/app/overrides/spree/admin/add_payment_data_to_admin_users.rb deleted file mode 100644 index ab36122..0000000 --- a/app/overrides/spree/admin/add_payment_data_to_admin_users.rb +++ /dev/null @@ -1,8 +0,0 @@ -Deface::Override.new( - virtual_path: "spree/admin/users/_tabs", - name: "add_payment_data_to_admin_user", - insert_bottom: '[data-hook="admin_user_tab_options"]', - partial: "spree/admin/shared/payment_data_user", - disabled: false - -) diff --git a/app/overrides/spree/admin/menus.rb b/app/overrides/spree/admin/menus.rb deleted file mode 100644 index 2e52541..0000000 --- a/app/overrides/spree/admin/menus.rb +++ /dev/null @@ -1,15 +0,0 @@ -Deface::Override.new( - virtual_path: "spree/admin/shared/_menu", - name: "marketplace_menus", - insert_bottom: '[data-hook="admin_tabs"]', - partial: "spree/admin/shared/marketplace_tabs" -) - - -Deface::Override.new( - virtual_path: "spree/admin/shared/_configuration_menu", - name: "marketplace_admin_configurations_menu", - insert_bottom: "[data-hook='admin_configurations_sidebar_menu']", - disabled: false, - partial: "spree/admin/shared/marketplace_settings" -) diff --git a/app/overrides/spree/admin/products/_form/converted_admin_product_form_right.html.erb.deface b/app/overrides/spree/admin/products/_form/converted_admin_product_form_right.html.erb.deface deleted file mode 100644 index 7891dce..0000000 --- a/app/overrides/spree/admin/products/_form/converted_admin_product_form_right.html.erb.deface +++ /dev/null @@ -1,5 +0,0 @@ - - -<% if can? :manage, Spree::Supplier %> - -<% end %> diff --git a/app/overrides/spree/admin/products/edit/override_rows.html.erb.deface b/app/overrides/spree/admin/products/edit/override_rows.html.erb.deface deleted file mode 100644 index 3616895..0000000 --- a/app/overrides/spree/admin/products/edit/override_rows.html.erb.deface +++ /dev/null @@ -1,22 +0,0 @@ - - -<%# Changed classes to new like https://github.com/spree/spree/blob/master/backend/app/views/spree/admin/stock_locations/_form.html.erb %> -<%= product.sku rescue '' %> -<%= mini_image product, class: "thumbnail" %> - - <% if try_spree_current_user && try_spree_current_user.supplier? %> - <%= link_to product.try(:name), spree.admin_product_stock_path(product)%> - <% else %> - <%= link_to product.try(:name), edit_admin_product_path(product) %> - <% end %> - -<%= product.display_price.to_html rescue '' %> - - <% if try_spree_current_user && try_spree_current_user.supplier? %> - <%= link_to 'Stock Management', spree.admin_product_stock_path(product), title:'Stock Management', class: 'btn btn-default btn-sm with-tip without-textdecor' if can?(:stock, product) && !product.deleted? %> - <% else %> - <%= link_to_edit product, :no_text => true, :class => 'edit' if can?(:edit, product) && !product.deleted? %> - <% end %> - <%= link_to_clone product, :no_text => true, :class => 'clone' if can?(:clone, product) %> - <%= link_to_delete product, :no_text => true if can?(:delete, product) && !product.deleted? %> - diff --git a/app/overrides/spree/admin/stock_locations/_form/add_supplier.html.erb.deface b/app/overrides/spree/admin/stock_locations/_form/add_supplier.html.erb.deface deleted file mode 100644 index bf267e8..0000000 --- a/app/overrides/spree/admin/stock_locations/_form/add_supplier.html.erb.deface +++ /dev/null @@ -1,12 +0,0 @@ - - -<%# Remove extra div and change classes %> -<% if spree_current_user.admin? %> -
- <%= f.field_container :supplier do %> - <%= f.label :supplier_id, Spree.t(:supplier) %> - <%= f.collection_select(:supplier_id, Spree::Supplier.all, :id, :name, { :include_blank => 'None' }, { :class => 'select2' }) %> - <%= f.error_message_on :supplier %> - <% end %> -
-<% end %> diff --git a/app/views/spree/admin/marketplace_settings/edit.html.erb b/app/views/spree/admin/marketplace_settings/edit.html.erb index a772ec7..16dae81 100644 --- a/app/views/spree/admin/marketplace_settings/edit.html.erb +++ b/app/views/spree/admin/marketplace_settings/edit.html.erb @@ -1,26 +1,24 @@ -<%= render 'spree/admin/shared/configuration_menu' %> +<%= render 'spree/admin/shared/general_tabs' %> -<% content_for :page_title do %> - <%= Spree.t("marketplace_settings") %> -<% end %> +<% admin_breadcrumb(link_to(plural_resource_name(Spree::Store), admin_stores_path)) %> +<% admin_breadcrumb(t('spree.marketplace_settings')) %> -<%= form_tag(spree.admin_marketplace_settings_path, :method => :put) do %> -
- <% %w( default_commission_flat_rate default_commission_percentage automatically_deliver_orders_to_supplier send_supplier_email).each do |key| - type = SolidusMarketplace::Config.preference_type(key) %> +<% content_for :page_actions do %> +<% end %> -
- <%= render "spree/admin/shared/preference_fields/#{type}", - label: Spree.t(key) , name: key, value: SolidusMarketplace::Config[key] %> +<%= form_tag(spree.admin_marketplace_settings_path, method: :put) do %> +
+ <% SolidusMarketplace::Config.preferences.keys.each do |key| + type = SolidusMarketplace::Config.preference_type(key) %> -
+ <%= render "spree/admin/shared/preference_fields/#{type}", + label: t("spree.#{key}") , + name: key, + value: SolidusMarketplace::Config[key] %> + <% end %> + <%= render partial: 'spree/admin/shared/edit_resource_links', + locals: { collection_url: spree.admin_stores_path } %> +
+<% end %> - <% end %> -
- <%= button Spree.t(:update), 'update' %> - <%= Spree.t(:or) %> - <%= button_link_to Spree.t(:cancel), spree.edit_admin_marketplace_settings_url, :icon => 'cancel' %> -
- -<% end %> diff --git a/app/views/spree/admin/reports/earnings.html.erb b/app/views/spree/admin/reports/earnings.html.erb index ab31dc1..2335042 100644 --- a/app/views/spree/admin/reports/earnings.html.erb +++ b/app/views/spree/admin/reports/earnings.html.erb @@ -2,6 +2,9 @@ <% admin_breadcrumb(t('spree.earnings')) %> <% content_for :page_actions do %> +
  • + <%= link_to t('spree.export_supplier_csv'), spree.earnings_admin_reports_url(format: 'csv'), class: 'btn btn-primary' %> +
  • <% end %> <% content_for :table_filter_title do %> @@ -30,5 +33,3 @@ <% end %> - -<%= link_to "Export Earnings CSV", earnings_admin_reports_url(format: "csv") %> diff --git a/app/views/spree/admin/shared/_marketplace_settings.html.erb b/app/views/spree/admin/shared/_marketplace_settings.html.erb index 472bc98..c31e1b3 100644 --- a/app/views/spree/admin/shared/_marketplace_settings.html.erb +++ b/app/views/spree/admin/shared/_marketplace_settings.html.erb @@ -1 +1,3 @@ -<%= configurations_sidebar_menu_item Spree.t(:marketplace_settings), spree.edit_admin_marketplace_settings_path %> +<% if can?(:edit, Spree::Store) %> + <%= configurations_sidebar_menu_item t('spree.marketplace_settings'), spree.edit_admin_marketplace_settings_path %> +<% end %> diff --git a/app/views/spree/admin/shared/_payment_data_user.html.erb b/app/views/spree/admin/shared/_payment_data_user.html.erb deleted file mode 100644 index fb6e419..0000000 --- a/app/views/spree/admin/shared/_payment_data_user.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -<% if can?(:wallets, @user) %> - > - <%= link_to t("spree.admin.user.wallets"), spree.wallets_admin_user_path(@user) %> - -<% end %> \ No newline at end of file diff --git a/app/views/spree/admin/shared/_supplier_order_total.html.erb b/app/views/spree/admin/shared/_supplier_order_total.html.erb new file mode 100644 index 0000000..96fa77c --- /dev/null +++ b/app/views/spree/admin/shared/_supplier_order_total.html.erb @@ -0,0 +1,5 @@ +<% if try_spree_current_user.supplier? %> + <%= order.supplier_total(try_spree_current_user).to_html %> +<% else %> + <%= order.display_total.to_html %> +<% end %> diff --git a/app/views/spree/admin/shared/_supplier_product_form.html.erb b/app/views/spree/admin/shared/_supplier_product_form.html.erb new file mode 100644 index 0000000..337d995 --- /dev/null +++ b/app/views/spree/admin/shared/_supplier_product_form.html.erb @@ -0,0 +1,8 @@ +<% if try_spree_current_user.market_maker? %> +
    + <%= f.field_container :suppliers do %> + <%= f.label :supplier_ids, plural_resource_name(Spree::Supplier) %>
    + <%= f.hidden_field :supplier_ids, value: @product.supplier_ids.join(',') %> + <% end %> +
    +<% end %> diff --git a/app/views/spree/admin/shared/_supplier_stock_location_form.html.erb b/app/views/spree/admin/shared/_supplier_stock_location_form.html.erb new file mode 100644 index 0000000..f943ee0 --- /dev/null +++ b/app/views/spree/admin/shared/_supplier_stock_location_form.html.erb @@ -0,0 +1,14 @@ +<% if try_spree_current_user.has_admin_role? %> +
    +
    + Supplier + +
    + <%= f.field_container :supplier do %> + <%= f.label :supplier_id %> + <%= f.collection_select(:supplier_id, Spree::Supplier.all, :id, :name, { include_blank: 'None' }, { class: 'select2 fullwidth' }) %> + <% end %> +
    +
    +
    +<% end %> diff --git a/app/views/spree/admin/suppliers/_form.html.erb b/app/views/spree/admin/suppliers/_form.html.erb index d651184..6f0a8a9 100644 --- a/app/views/spree/admin/suppliers/_form.html.erb +++ b/app/views/spree/admin/suppliers/_form.html.erb @@ -1,75 +1,93 @@ -
    -
    - <%= Spree.t('contact_address') %> - <%= form.fields_for :address do |contact_form| %> - <%= render :partial => 'spree/admin/shared/address_form', :locals => { :f => contact_form, :name => Spree.t(:contact_address), :type => "shipping" } %> - <% end %> -
    -
    +
    +
    +
    +
    + <%= t('spree.address')%> +
    + <%= f.fields_for :address do |address_form| %> + <%= render partial: 'spree/admin/shared/address_form', + locals: { f: address_form, type: 'shipping' } %> + <% end %> +
    +
    +
    -<%# added bootstrap classes for 'field_container' and '' %> -
    -
    - <%= Spree.t('supplier_details') %> - <%= form.field_container :name, class: ['form-group'] do %> - <%= form.label :name, Spree::Supplier.human_attribute_name(:name) %>:
    - <%= form.text_field :name, :class => 'form-control' %> - <% end %> +
    +
    + <%= t('spree.details')%> +
    + <%= f.field_container :name do %> + <%= f.label Spree::Supplier.human_attribute_name(:name) %> + <%= f.text_field :name, class: 'fullwidth' %> + <% end %> +
    - <%= form.field_container :user, class: ['form-group'] do %> - <%= form.label :user_admin %>
    - <%= hidden_field_tag 'supplier[user_id]', form.object.user_ids.join(','), class: 'user_picker fullwidth' %> - <% end %> +
    + <%= f.field_container :user do %> + <%= f.label :user_admin %> + <%= hidden_field_tag 'supplier[user_id]', + f.object.user_ids.join(','), + class: 'user_picker fullwidth' %> + <% end %> +
    - <%= form.field_container :paypal_email, class: ['form-group'] do %> - <%= form.label :paypal_email, Spree::Supplier.human_attribute_name(:paypal_email) %>:
    - <%= form.email_field :paypal_email, :class => 'form-control' %> - <%# email validation %> - <%= error_message_on :user, :paypal_email %> - <% end %> - <%= form.field_container :url, class: ['form-group'] do %> - <%= form.label :url, Spree::Supplier.human_attribute_name(:url) %>:
    - <%= form.text_field :url, :class => 'form-control' %> - <% end %> - <%= form.field_container :tax_id, class: ['form-group'] do %> - <%= form.label :tax_id, Spree::Supplier.human_attribute_name(:tax_id) %>:
    - <%= form.text_field :tax_id, :class => 'form-control' %> - <% end %> - <% if spree_current_user.admin? %> - <%= form.field_container :commission_flat_rate, class: ['form-group'] do %> - <%= form.label :commission_flat_rate, Spree::Supplier.human_attribute_name(:commission_flat_rate) %>:
    - <%= form.text_field :commission_flat_rate, :class => 'form-control' %> - <% end %> - <%= form.field_container :commission_percentage , class: ['form-group'] do %> - <%= form.label :commission_percentage, Spree::Supplier.human_attribute_name(:commission_percentage) %>:
    - <%= form.text_field :commission_percentage, :class => 'form-control' %> - <% end %> - <%= form.field_container :users, class: ['form-group'] do %> - <%= form.label :users %>
    - <%= hidden_field_tag 'supplier[user_ids_string]', form.object.user_ids.join(','), class: 'user_picker fullwidth' %> - <% end %> - <%= form.field_container :active, class: ['form-group'] do %> -
    - <%= label_tag :active do %> - <%= form.check_box :active %> - <%= Spree::Supplier.human_attribute_name(:active) %> - <% end %> +
    + <%= f.field_container :paypal_email do %> + <%= f.label Spree::Supplier.human_attribute_name(:paypal_email) %> + <%= f.email_field :paypal_email, class: 'fullwidth' %> + <% end %> +
    + +
    + <%= f.field_container :url do %> + <%= f.label Spree::Supplier.human_attribute_name(:url) %> + <%= f.text_field :url, class: 'fullwidth' %> + <% end %>
    - <% end %> - <% end %> - <%# This moved to _edit(new)_resource_links.html.erb %> - - <%# if form.object.new_record? %> - <%#= button Spree.t('create') %> - <%# else %> - <%#= button Spree.t('update') %> - <%# end %> - <%#= Spree.t('or') %> - <%# if can? :index, Spree::Supplier %> - <%#= link_to Spree.t('cancel'), collection_url %> - <%# else %> - <%#= link_to Spree.t('cancel'), spree.account_path %> - <%# end %> - -
    + +
    + <%= f.field_container :tax_id do %> + <%= f.label Spree::Supplier.human_attribute_name(:tax_id) %> + <%= f.text_field :tax_id, class: 'fullwidth' %> + <% end %> +
    + + <% if try_spree_current_user.has_admin_role? %> +
    + <%= f.field_container :commission_flat_rate do %> + <%= f.label Spree::Supplier.human_attribute_name(:commission_flat_rate) %> + <%= f.text_field :commission_flat_rate, class: 'fullwidth' %> + <% end %> +
    + +
    + <%= f.field_container :commission_percentage do %> + <%= f.label Spree::Supplier.human_attribute_name(:commission_percentage) %> + <%= f.text_field :commission_percentage, class: 'fullwidth' %> + <% end %> +
    + +
    + <%= f.field_container :users do %> + <%= f.label :users %> + <%= hidden_field_tag 'supplier[user_ids_string]', + f.object.user_ids.join(','), + class: 'user_picker fullwidth' %> + <% end %> +
    + +
    + <%= f.field_container :active do %> +
    + <%= label_tag :active do %> + <%= f.check_box :active, class: 'fullwidth' %> + <%= Spree::Supplier.human_attribute_name(:active) %> + <% end %> +
    + <% end %> +
    + <% end %> +
    +
    +
    diff --git a/app/views/spree/admin/suppliers/edit.html.erb b/app/views/spree/admin/suppliers/edit.html.erb index a6c2d2e..388f88a 100644 --- a/app/views/spree/admin/suppliers/edit.html.erb +++ b/app/views/spree/admin/suppliers/edit.html.erb @@ -1,24 +1,17 @@ -<% if can? :index, Spree::Supplier %> - <% content_for :page_actions do %> - <%= button_link_to Spree.t('back_to_suppliers'), spree.admin_suppliers_path, icon: 'arrow-left' %> - <% end %> -<% end %> +<% admin_breadcrumb(link_to(plural_resource_name(Spree::Supplier), admin_suppliers_path)) %> +<% admin_breadcrumb(@object.name) %> -<% content_for :page_title do %> - <%== Spree.t(:editing_supplier) + " “#{@object.name}”".html_safe %> +<% content_for :page_actions do %> + <% end %> -<%= render 'spree/shared/error_messages', target: @object %> - -<%= link_to Spree.t(:user), edit_admin_user_path(@object.user) , class:"button primary"%> +<%= render partial: 'spree/shared/error_messages', locals: { target: @object } %> <%= form_for [:admin, @object] do |f| %> - <%= render "form", form: f %> -
    - <%# render 'update' and 'cancel' buttons %> -
    - <%= render :partial => 'spree/admin/shared/edit_resource_links' %> -
    +
    + <%= render 'form', f: f %> + <%= render partial: 'spree/admin/shared/edit_resource_links' %> +
    <% end %> - - diff --git a/app/views/spree/admin/suppliers/index.html.erb b/app/views/spree/admin/suppliers/index.html.erb index 97325b6..86a8956 100644 --- a/app/views/spree/admin/suppliers/index.html.erb +++ b/app/views/spree/admin/suppliers/index.html.erb @@ -1,55 +1,65 @@ -<%# Updated icon %> <% content_for :page_actions do %> - <%= button_link_to Spree.t('new_supplier'), new_object_url, icon: 'add', class: 'btn-success' %> +
  • + <%= link_to t('spree.new_supplier'), new_object_url, class: 'btn btn-primary' %> +
  • <% end %> <% admin_breadcrumb(plural_resource_name(Spree::Supplier)) %> <% content_for :table_filter_title do %> - <%= Spree.t(:search) %> + <%= t('spree.search') %> <% end %> <% content_for :table_filter do %>
    <%= search_form_for [:admin, @search] do |f| %> -
    - <%= f.label :name_cont, Spree.t(:name) %> - <%= f.text_field :name_cont, :size => 15, class: "form-control" %> +
    +
    +
    + <%= f.label :name_cont, Spree::Product.human_attribute_name(:name) %> + <%= f.text_field :name_cont, size: 15 %> +
    -
    -
    - <%= button Spree.t(:search), 'search' %> +
    + +
    + <%= button_tag t('spree.search'), class: 'btn btn-primary' %>
    <% end %>
    <% end %> -<%= paginate @collection, theme: "solidus_admin" %> +
    + +<%= paginate @collection, theme: 'solidus_admin' %> <% if @collection.any? %> - +
    - - - - - + + + + + + - <%# Updated classes! %> - <%- @collection.each do |supplier| %> + <% @collection.each do |supplier| %> id="<%= spree_dom_id supplier %>" data-hook="admin_suppliers_index_rows" class="<%= cycle('odd', 'even') %>"> - - + - - + @@ -57,12 +67,11 @@
    <%= sort_link @search, :name, Spree::Supplier.human_attribute_name(:name) %><%= sort_link @search, :email, Spree::Supplier.human_attribute_name(:email) %><%= Spree.t(:users) %><%= sort_link @search, :active, Spree::Supplier.human_attribute_name(:active) %><%= sort_link(@search, :name, Spree::Supplier.human_attribute_name(:name)) %><%= t('spree.admins') %><%= t('spree.users') %><%= sort_link(@search, :active, t('spree.status')) %>
    <%= link_to supplier.name, edit_object_url(supplier) %><%= mail_to supplier.user.email %><%= supplier.admins.pluck(:email).join(', ') %> <%= supplier.users.pluck(:email).join(', ') %><%= supplier.active %> + + + <%= t("spree.#{supplier.active? ? 'active' : 'inactive'}") %> + + <% unless supplier.deleted? %> - <%= link_to_edit supplier, :no_text => true, :class => 'edit' %> - <%= link_to_delete supplier, :no_text => true %> + <%= link_to_edit supplier, no_text: true, class: 'edit' if can?(:edit, supplier) && !supplier.deleted? %> + <%= link_to_delete supplier, no_text: true if can?(:delete, supplier) && !supplier.deleted? %> <% end %>
    <% else %> - <%# New alert with link to add new supplier %> -
    - <%= Spree.t(:no_resource_found, resource: Spree::Supplier.model_name.human(count: :many)) %>, - <%= link_to Spree.t(:add_one), spree.new_admin_supplier_path %>! +
    + <%= render 'spree/admin/shared/no_objects_found', + resource: Spree::Supplier, + new_resource_url: new_object_url %>
    <% end %> -<%# This is a new paginate! %> -<%= paginate @collection, theme: "solidus_admin" %> +<%= paginate @collection, theme: 'solidus_admin' %> diff --git a/app/views/spree/admin/suppliers/new.html.erb b/app/views/spree/admin/suppliers/new.html.erb index fafdf20..01f5cd8 100644 --- a/app/views/spree/admin/suppliers/new.html.erb +++ b/app/views/spree/admin/suppliers/new.html.erb @@ -1,17 +1,18 @@ <% content_for :page_actions do %> - <%= button_link_to Spree.t('back_to_suppliers'), spree.admin_suppliers_path, icon: 'arrow-left' %> + <% end %> <% content_for :page_title do %> - <%= Spree.t('new_supplier') %> + <%= t('spree.new_supplier') %> <% end %> -<%= render 'spree/shared/error_messages', target: @object%> +<%= render partial: 'spree/shared/error_messages', locals: { target: @object } %> <%= form_for [:admin, @object] do |f| %> - <%= render "form", form: f %> - <%# render 'create' and 'cancel' buttons %> -
    - <%= render :partial => 'spree/admin/shared/new_resource_links' %> -
    +
    + <%= render 'form', f: f %> + <%= render partial: 'spree/admin/shared/new_resource_links' %> +
    <% end %> diff --git a/app/views/spree/admin/users/_wallet_form.html.erb b/app/views/spree/admin/users/_wallet_form.html.erb index 6fe5fe2..b801d2c 100644 --- a/app/views/spree/admin/users/_wallet_form.html.erb +++ b/app/views/spree/admin/users/_wallet_form.html.erb @@ -1,38 +1,33 @@
    -
    - <%= t('spree.payment_methods') %> - - <% wallet_payment_sources = @user.wallet.wallet_payment_sources %> + <%= t('spree.payment_methods') %> + <% wallet_payment_sources = @user.wallet.wallet_payment_sources %> - <% if wallet_payment_sources.present? %> - <% default_wallet_payment_source = wallet_payment_sources.detect(&:default) || - wallet_payment_sources.first %> + <% if wallet_payment_sources.present? %> + <% default_wallet_payment_source = wallet_payment_sources.detect(&:default) || wallet_payment_sources.first %> - - - <% wallet_payment_sources.each do |wallet_payment_source| %> +
    + + <% wallet_payment_sources.each do |wallet_payment_source| %> + <%= + render( + partial: "spree/checkout/existing_payment/#{wallet_payment_source.payment_source.payment_method.partial_name}", + locals: { + wallet_payment_source: wallet_payment_source, + default: wallet_payment_source == default_wallet_payment_source, + } + ) + %> + <% end %> - <%= - render( - partial: "spree/checkout/existing_payment/#{wallet_payment_source.payment_source.payment_method.partial_name}", - locals: { - wallet_payment_source: wallet_payment_source, - default: wallet_payment_source == default_wallet_payment_source, - } - ) - %> - <% end %> - - -
    - <% else %> -
    - <%= Spree.t(:no_payment_methods) %> -
    - <% end %> + + + <% else %> +
    + <%= t('spree.no_payment_methods') %> +
    + <% end %>
    -
    diff --git a/app/views/spree/admin/users/addwallet.html.erb b/app/views/spree/admin/users/addwallet.html.erb index dd6e2e4..da6c537 100644 --- a/app/views/spree/admin/users/addwallet.html.erb +++ b/app/views/spree/admin/users/addwallet.html.erb @@ -2,13 +2,11 @@ <% admin_breadcrumb(link_to @user.email, edit_admin_user_url(@user)) %> <% admin_breadcrumb(plural_resource_name(Spree::Address)) %> - <%= render 'spree/admin/users/sidebar' %> <%= render 'spree/admin/users/tabs', current: :wallet %> <%= render partial: 'spree/admin/users/user_page_actions' %>
    -
    <%= render partial: 'spree/shared/error_messages', locals: { target: @user } %>
    @@ -24,4 +22,4 @@ <% end %> <% end %>
    - \ No newline at end of file + diff --git a/app/views/spree/admin/users/wallets.html.erb b/app/views/spree/admin/users/wallets.html.erb index ddce438..560e3a5 100644 --- a/app/views/spree/admin/users/wallets.html.erb +++ b/app/views/spree/admin/users/wallets.html.erb @@ -2,13 +2,11 @@ <% admin_breadcrumb(link_to @user.email, edit_admin_user_url(@user)) %> <% admin_breadcrumb(plural_resource_name(Spree::PaymentMethod)) %> - <%= render 'spree/admin/users/sidebar' %> <%= render 'spree/admin/users/tabs', current: :wallet %> <%= render partial: 'spree/admin/users/user_page_actions' %>
    -
    <%= render partial: 'spree/shared/error_messages', locals: { target: @user } %>
    diff --git a/app/views/spree/api/shared/_pagination.json.jbuilder b/app/views/spree/api/shared/_pagination.json.jbuilder index 69205e3..28d16c9 100644 --- a/app/views/spree/api/shared/_pagination.json.jbuilder +++ b/app/views/spree/api/shared/_pagination.json.jbuilder @@ -1,5 +1,5 @@ -json.count pagination.count -json.total_count pagination.total_count +json.count pagination.count +json.total_count pagination.total_count json.current_page pagination.current_page -json.pages pagination.total_pages -json.per_page pagination.limit_value \ No newline at end of file +json.pages pagination.total_pages +json.per_page pagination.limit_value diff --git a/app/views/spree/marketplace_order_mailer/supplier_order.html.erb b/app/views/spree/marketplace_order_mailer/supplier_order.html.erb index 8d27c43..e3f6953 100644 --- a/app/views/spree/marketplace_order_mailer/supplier_order.html.erb +++ b/app/views/spree/marketplace_order_mailer/supplier_order.html.erb @@ -11,8 +11,7 @@ -

    <%= Spree.t('marketplace_order_mailer. - supplier_order.hello', name: @supplier.name ) %>

    +

    <%= t('spree.marketplace_order_mailer.supplier_order.hello', name: @supplier.name ) %>

    An order has been placed. Please confirm the shipment details, and <%= link_to 'update the shipment', spree.edit_admin_shipment_url(@shipment) %> once it has shipped.

    @@ -107,7 +106,7 @@
    -

    <%= raw Spree.t('marketplace_order_mailer.supplier_order.thank_you_again', name: Spree::Store.current.name) %>

    +

    <%= raw t('spree.marketplace_order_mailer.supplier_order.thank_you_again', name: Spree::Store.current.name) %>

    diff --git a/app/views/spree/shared/unauthorized.erb b/app/views/spree/shared/unauthorized.erb index b0d8072..3d86378 100644 --- a/app/views/spree/shared/unauthorized.erb +++ b/app/views/spree/shared/unauthorized.erb @@ -1 +1 @@ -<%= Spree.t(:authorization_failure) %> +<%= t('spree.authorization_failure') %> diff --git a/app/views/spree/supplier_mailer/welcome.html.erb b/app/views/spree/supplier_mailer/welcome.html.erb index b2384cb..8c69a77 100644 --- a/app/views/spree/supplier_mailer/welcome.html.erb +++ b/app/views/spree/supplier_mailer/welcome.html.erb @@ -6,32 +6,32 @@ -

    <%= link_to Spree::Store.default.name, spree.root_url %>

    +

    <%= link_to(Spree::Store.default.name, spree.root_url) %>

    -

    <%= Spree.t('supplier_mailer.welcome.hello', name: @supplier.name ) %>

    +

    <%= t('spree.supplier_mailer.welcome.hello', name: @supplier.name ) %>

    - <%= Spree.t('supplier_mailer.welcome.manage_your_account' ) %> - <%= link_to Spree.t('supplier_mailer.welcome.logging_into_your_account'), spree.login_url, target: '_blank' %> + <%= t('spree.supplier_mailer.welcome.manage_your_account' ) %> + <%= link_to t('spree.supplier_mailer.welcome.logging_into_your_account'), spree.login_url, target: '_blank' %>

    -

    <%= Spree.t('supplier_mailer.welcome.please_verify_your_information') %>

    +

    <%= t('spree.supplier_mailer.welcome.please_verify_your_information') %>

    - + @@ -41,7 +41,7 @@ diff --git a/config/locales/en.yml b/config/locales/en.yml index 8817c9d..1f6f3d7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -53,6 +53,9 @@ en: approve_marketplace_orders: Approve Marketplace Orders resend_marketplace_orders: Resend Marketplace Orders marketplace_order_approval_confirmation: This will send an email notification to each supplier. Continue? + active: Active + address: Address + admins: Admins back_to_orders_list: Back To Orders List back_to_suppliers: Back To Suppliers business: Business @@ -67,6 +70,7 @@ en: drop_ship: Drop Ship earnings: Earnings earnings_description: Earnings for Suppliers over the specified time period + export_supplier_csv: Export Earnings CSV marketplace_order_mailer: supplier_order: hello: "Hello %{name}," @@ -76,6 +80,7 @@ en: marketplace_orders: Marketplace Orders marketplace_settings: Marketplace Settings editing_supplier: Editing Supplier + inactive: Inactive individual: Individual manage: Manage must_be_logged_in: 'Must Be Logged In' diff --git a/config/routes.rb b/config/routes.rb index cfebbf5..6db4d2a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,20 +1,8 @@ Spree::Core::Engine.routes.draw do - namespace :admin do resource :marketplace_settings resources :shipments resources :suppliers - end - - namespace :api do - resources :suppliers, only: :index - end - -end - -Spree::Core::Engine.routes.prepend do - namespace :admin do - resources :reports, only: [:index] do collection do get :earnings @@ -28,9 +16,10 @@ put :wallets_actions put :addcard end - end + end - + namespace :api do + resources :suppliers, only: :index end end diff --git a/db/default/spree/marketplace_roles.rb b/db/default/spree/marketplace_roles.rb index 718aa68..d19382e 100644 --- a/db/default/spree/marketplace_roles.rb +++ b/db/default/spree/marketplace_roles.rb @@ -1,14 +1,18 @@ +# frozen_string_literal: true + # create supplier_admin role, new supplier and supplier_admin user -supplier_admin_role = Spree::Role.where(name: "supplier_admin").first_or_create -new_supplier = Spree::Supplier.new(name: "Kevin's Guitars", email: "kevinsguitars@example.com") -supplier_admin = Spree::User.create(email: "supplier_admin@example.com", - password: "test123", - password_confirmation: "test123", - supplier: new_supplier) +supplier_admin_role = Spree::Role.where(name: 'supplier_admin').first_or_create +supplier_admin = Spree::User.create(email: 'supplier_admin@example.com', + password: 'test123', + password_confirmation: 'test123') supplier_admin.spree_roles << supplier_admin_role +new_supplier = Spree::Supplier.create(name: "Kevin's Guitars", + user: supplier_admin) # create marketmaker user -admin_role = Spree::Role.find_by(name: "admin") -marketmaker = Spree::User.create(email: "marketmaker@example.com", password: "test123", password_confirmation: "test123") +admin_role = Spree::Role.find_by(name: 'admin') +marketmaker = Spree::User.create(email: 'marketmaker@example.com', + password: 'test123', + password_confirmation: 'test123') marketmaker.spree_roles << admin_role diff --git a/db/migrate/20121006073854_create_suppliers.rb b/db/migrate/20121006073854_create_suppliers.rb index 5299870..1b16015 100644 --- a/db/migrate/20121006073854_create_suppliers.rb +++ b/db/migrate/20121006073854_create_suppliers.rb @@ -1,20 +1,22 @@ -class CreateSuppliers < ActiveRecord::Migration[4.2] +# frozen_string_literal: true +class CreateSuppliers < SolidusSupport::Migration[4.2] def change create_table :spree_suppliers do |t| - t.boolean :active, default: false, null: false + t.boolean :active, default: false, null: false t.references :address - t.decimal :commission_flat_rate, :precision => 8, :scale => 2, :default => 0.0, :null => false - t.float :commission_percentage, default: 0.0, null: false - t.string :email - t.string :name - t.string :url - t.datetime :deleted_at + t.decimal :commission_flat_rate, precision: 8, scale: 2, default: 0.0, null: false + t.float :commission_percentage, default: 0.0, null: false + t.string :email + t.string :name + t.string :url + t.datetime :deleted_at + t.timestamps end + add_index :spree_suppliers, :address_id add_index :spree_suppliers, :deleted_at add_index :spree_suppliers, :active end - end diff --git a/db/migrate/20130216070944_product_belongs_to_supplier.rb b/db/migrate/20130216070944_product_belongs_to_supplier.rb index 3386725..2a19f3f 100644 --- a/db/migrate/20130216070944_product_belongs_to_supplier.rb +++ b/db/migrate/20130216070944_product_belongs_to_supplier.rb @@ -1,4 +1,6 @@ -class ProductBelongsToSupplier < ActiveRecord::Migration[4.2] +# frozen_string_literal: true + +class ProductBelongsToSupplier < SolidusSupport::Migration[4.2] def change add_column :spree_products, :supplier_id, :integer add_index :spree_products, :supplier_id diff --git a/db/migrate/20130405005502_stock_locations_belongs_to_supplier.rb b/db/migrate/20130405005502_stock_locations_belongs_to_supplier.rb index 2d21982..8a02035 100644 --- a/db/migrate/20130405005502_stock_locations_belongs_to_supplier.rb +++ b/db/migrate/20130405005502_stock_locations_belongs_to_supplier.rb @@ -1,4 +1,6 @@ -class StockLocationsBelongsToSupplier < ActiveRecord::Migration[4.2] +# frozen_string_literal: true + +class StockLocationsBelongsToSupplier < SolidusSupport::Migration[4.2] def change add_column :spree_stock_locations, :supplier_id, :integer add_index :spree_stock_locations, :supplier_id diff --git a/db/migrate/20130405011127_user_belongs_to_supplier.rb b/db/migrate/20130405011127_user_belongs_to_supplier.rb index 0def714..92ac45d 100644 --- a/db/migrate/20130405011127_user_belongs_to_supplier.rb +++ b/db/migrate/20130405011127_user_belongs_to_supplier.rb @@ -1,4 +1,6 @@ -class UserBelongsToSupplier < ActiveRecord::Migration[4.2] +# frozen_string_literal: true + +class UserBelongsToSupplier < SolidusSupport::Migration[4.2] def change add_column Spree.user_class.table_name, :supplier_id, :integer add_index Spree.user_class.table_name, :supplier_id diff --git a/db/migrate/20130428063053_add_balanced_token_to_suppliers.rb b/db/migrate/20130428063053_add_balanced_token_to_suppliers.rb index a3b226a..959d5c7 100644 --- a/db/migrate/20130428063053_add_balanced_token_to_suppliers.rb +++ b/db/migrate/20130428063053_add_balanced_token_to_suppliers.rb @@ -1,4 +1,6 @@ -class AddBalancedTokenToSuppliers < ActiveRecord::Migration[4.2] +# frozen_string_literal: true + +class AddBalancedTokenToSuppliers < SolidusSupport::Migration[4.2] def change add_column :spree_suppliers, :tax_id, :string add_column :spree_suppliers, :token, :string diff --git a/db/migrate/20130510181443_add_supplier_id_to_ckeditor_assets.rb b/db/migrate/20130510181443_add_supplier_id_to_ckeditor_assets.rb index 708ea00..90c2ba4 100644 --- a/db/migrate/20130510181443_add_supplier_id_to_ckeditor_assets.rb +++ b/db/migrate/20130510181443_add_supplier_id_to_ckeditor_assets.rb @@ -1,4 +1,6 @@ -class AddSupplierIdToCkeditorAssets < ActiveRecord::Migration[4.2] +# frozen_string_literal: true + +class AddSupplierIdToCkeditorAssets < SolidusSupport::Migration[4.2] if table_exists?(:ckeditor_assets) def change add_column :ckeditor_assets, :supplier_id, :integer diff --git a/db/migrate/20130606220913_add_permalink_to_suppliers.rb b/db/migrate/20130606220913_add_permalink_to_suppliers.rb index 3405991..7b8f646 100644 --- a/db/migrate/20130606220913_add_permalink_to_suppliers.rb +++ b/db/migrate/20130606220913_add_permalink_to_suppliers.rb @@ -1,4 +1,6 @@ -class AddPermalinkToSuppliers < ActiveRecord::Migration[4.2] +# frozen_string_literal: true + +class AddPermalinkToSuppliers < SolidusSupport::Migration[4.2] def change add_column :spree_suppliers, :slug, :string add_index :spree_suppliers, :slug, unique: true diff --git a/db/migrate/20140323170638_add_supplier_commission_to_shipments.rb b/db/migrate/20140323170638_add_supplier_commission_to_shipments.rb index 0bd9fbe..0d1e04a 100644 --- a/db/migrate/20140323170638_add_supplier_commission_to_shipments.rb +++ b/db/migrate/20140323170638_add_supplier_commission_to_shipments.rb @@ -1,4 +1,6 @@ -class AddSupplierCommissionToShipments < ActiveRecord::Migration[4.2] +# frozen_string_literal: true + +class AddSupplierCommissionToShipments < SolidusSupport::Migration[4.2] def change add_column :spree_shipments, :supplier_commission, :decimal, precision: 8, scale: 2, default: 0.0, null: false end diff --git a/db/migrate/20140416184616_migrate_payment_and_commission.rb b/db/migrate/20140416184616_migrate_payment_and_commission.rb index c526e4e..63bc57f 100644 --- a/db/migrate/20140416184616_migrate_payment_and_commission.rb +++ b/db/migrate/20140416184616_migrate_payment_and_commission.rb @@ -1,4 +1,6 @@ -class MigratePaymentAndCommission < ActiveRecord::Migration[4.2] +# frozen_string_literal: true + +class MigratePaymentAndCommission < SolidusSupport::Migration[4.2] def change add_column :spree_payments, :payable_id, :integer add_column :spree_payments, :payable_type, :string diff --git a/db/migrate/20140529041325_create_spree_supplier_variants.rb b/db/migrate/20140529041325_create_spree_supplier_variants.rb index 9820416..e0e08e7 100644 --- a/db/migrate/20140529041325_create_spree_supplier_variants.rb +++ b/db/migrate/20140529041325_create_spree_supplier_variants.rb @@ -1,4 +1,6 @@ -class CreateSpreeSupplierVariants < ActiveRecord::Migration[4.2] +# frozen_string_literal: true + +class CreateSpreeSupplierVariants < SolidusSupport::Migration[4.2] def change create_table :spree_supplier_variants do |t| t.belongs_to :supplier, index: true @@ -7,9 +9,11 @@ def change t.timestamps end + Spree::Product.where.not(supplier_id: nil).each do |product| product.add_supplier! product.supplier_id end + remove_column :spree_products, :supplier_id end end diff --git a/db/migrate/20171027180043_add_paypal_email_to_suppliers.rb b/db/migrate/20171027180043_add_paypal_email_to_suppliers.rb index 4de7868..7295e5c 100644 --- a/db/migrate/20171027180043_add_paypal_email_to_suppliers.rb +++ b/db/migrate/20171027180043_add_paypal_email_to_suppliers.rb @@ -1,4 +1,6 @@ -class AddPaypalEmailToSuppliers < ActiveRecord::Migration[5.1] +# frozen_string_literal: true + +class AddPaypalEmailToSuppliers < SolidusSupport::Migration[5.1] def change add_column :spree_suppliers, :paypal_email, :string end diff --git a/db/migrate/20180827180043_add_admin_user_to_suppliers.rb b/db/migrate/20180827180043_add_admin_user_to_suppliers.rb index 323aea7..26db402 100644 --- a/db/migrate/20180827180043_add_admin_user_to_suppliers.rb +++ b/db/migrate/20180827180043_add_admin_user_to_suppliers.rb @@ -1,4 +1,6 @@ -class AddAdminUserToSuppliers < ActiveRecord::Migration[5.1] +# frozen_string_literal: true + +class AddAdminUserToSuppliers < SolidusSupport::Migration[5.1] def change add_column :spree_suppliers, :user_id, :integer add_index :spree_suppliers, :user_id diff --git a/db/migrate/20190710130043_add_admin_user_ids_to_suppliers.rb b/db/migrate/20190710130043_add_admin_user_ids_to_suppliers.rb new file mode 100644 index 0000000..941d0cc --- /dev/null +++ b/db/migrate/20190710130043_add_admin_user_ids_to_suppliers.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class AddAdminUserIdsToSuppliers < SolidusSupport::Migration[5.1] + def change + add_column :spree_suppliers, :admin_id, :integer + add_index :spree_suppliers, :admin_id + end +end diff --git a/db/seeds.rb b/db/seeds.rb index df9419a..6cfe9d7 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,5 +1,6 @@ # Loads seed data out of default dir default_path = File.join(File.dirname(__FILE__), 'default') + %w( marketplace_roles ).each do |seed| diff --git a/lib/generators/solidus_marketplace/install/install_generator.rb b/lib/generators/solidus_marketplace/install/install_generator.rb index 6f36127..3c8179e 100644 --- a/lib/generators/solidus_marketplace/install/install_generator.rb +++ b/lib/generators/solidus_marketplace/install/install_generator.rb @@ -1,7 +1,7 @@ module SolidusMarketplace module Generators class InstallGenerator < Rails::Generators::Base - class_option :auto_run_migrations, :type => :boolean, :default => false + class_option :auto_run_migrations, type: :boolean, default: false def add_javascripts append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/solidus_marketplace\n" @@ -9,13 +9,13 @@ def add_javascripts end def add_stylesheets - inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/solidus_marketplace\n", :before => /\*\//, :verbose => true - inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/solidus_marketplace\n", :before => /\*\//, :verbose => true + inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/solidus_marketplace\n", before: /\*\//, verbose: true + inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/solidus_marketplace\n", before: /\*\//, verbose: true end def include_seed_data - seed_file = "db/seeds.rb" - content = "SolidusMarketplace::Engine.load_seed if defined?(SolidusMarketplace)" + seed_file = 'db/seeds.rb' + content = 'SolidusMarketplace::Engine.load_seed if defined?(SolidusMarketplace)' append_file(seed_file, content) unless File.readlines(seed_file).last == content end @@ -23,12 +23,10 @@ def add_migrations run 'bundle exec rake railties:install:migrations FROM=solidus_marketplace' end - def run_migrations - run_migrations = options[:auto_run_migrations] || ENV['AUTO_RUN_MIGRATIONS'] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]') - if run_migrations - run 'bundle exec rake db:migrate' - else - puts 'Skipping rake db:migrate, don\'t forget to run it!' + def running_migrations? + options.auto_run_migrations? || begin + response = ask 'Would you like to run the migrations now? [Y/n]' + ['', 'y'].include? response.downcase end end end diff --git a/lib/solidus_marketplace.rb b/lib/solidus_marketplace.rb index 5d50cbd..c265321 100644 --- a/lib/solidus_marketplace.rb +++ b/lib/solidus_marketplace.rb @@ -1,6 +1,4 @@ -require 'durable_decorator' -require 'sass/rails' -require 'spree_api' -require 'spree_backend' -require 'spree_core' +require 'solidus_core' +require 'solidus_support' +require 'deface' require 'solidus_marketplace/engine' diff --git a/app/models/spree/marketplace_configuration.rb b/lib/solidus_marketplace/configuration.rb similarity index 62% rename from app/models/spree/marketplace_configuration.rb rename to lib/solidus_marketplace/configuration.rb index 96d9834..851e586 100644 --- a/app/models/spree/marketplace_configuration.rb +++ b/lib/solidus_marketplace/configuration.rb @@ -1,17 +1,14 @@ -module Spree - class MarketplaceConfiguration < Preferences::Configuration - +# frozen_string_literal: true + +module SolidusMarketplace + class Configuration < Spree::Preferences::Configuration # Determines if send orders directly to supplier. preference :automatically_deliver_orders_to_supplier, :boolean, default: true - # Default flat rate to charge suppliers per order for commission. - preference :default_commission_flat_rate, :float, default: 0.0 - + preference :default_commission_flat_rate, :decimal, default: 0.0 # Default percentage to charge suppliers per order for commission. - preference :default_commission_percentage, :float, default: 0.0 - + preference :default_commission_percentage, :decimal, default: 0.0 # Determines whether or not to email a new supplier their welcome email. preference :send_supplier_email, :boolean, default: true - end end diff --git a/lib/solidus_marketplace/engine.rb b/lib/solidus_marketplace/engine.rb index 0365f88..85f3a12 100644 --- a/lib/solidus_marketplace/engine.rb +++ b/lib/solidus_marketplace/engine.rb @@ -1,6 +1,7 @@ +# frozen_string_literal: true + module SolidusMarketplace class Engine < Rails::Engine - require 'spree/core' isolate_namespace Spree engine_name 'solidus_marketplace' @@ -15,11 +16,12 @@ class Engine < Rails::Engine app.config.spree.stock_splitters << Spree::Stock::Splitter::Marketplace end - initializer "solidus_marketplace.preferences", before: :load_config_initializers do |app| - SolidusMarketplace::Config = Spree::MarketplaceConfiguration.new + initializer 'solidus_marketplace.preferences', before: :load_config_initializers do |app| + SolidusMarketplace::Config = SolidusMarketplace::Configuration.new + Spree::PermittedAttributes.singleton_class.prepend(SolidusMarketplace::PermittedAttributes) end - initializer "solidus_marketplace.menu", before: :load_config_initializers do |app| + initializer 'solidus_marketplace.menu', before: :load_config_initializers do |app| Spree::Backend::Config.configure do |config| config.menu_items << Spree::BackendConfiguration::MenuItem.new( [:stock_locations], @@ -45,9 +47,10 @@ def self.activate Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c| Rails.configuration.cache_classes ? require(c) : load(c) end + Spree::Ability.register_ability(Spree::SupplierAbility) end - config.to_prepare &method(:activate).to_proc + config.to_prepare(&method(:activate).to_proc) end end diff --git a/lib/solidus_marketplace/factories.rb b/lib/solidus_marketplace/factories.rb index 15248b0..11f8314 100644 --- a/lib/solidus_marketplace/factories.rb +++ b/lib/solidus_marketplace/factories.rb @@ -1,19 +1,18 @@ -FactoryGirl.define do +# frozen_string_literal: true +FactoryBot.define do factory :order_from_supplier, parent: :order do bill_address ship_address transient do - line_items_count 5 + line_items_count { 5 } end after(:create) do |order, evaluator| supplier = create(:supplier) product = create(:product) product.add_supplier! supplier - # product.stock_items.where(variant_id: product.master.id).first.adjust_count_on_hand(10) - product_2 = create(:product) product_2.add_supplier! create(:supplier) @@ -21,16 +20,15 @@ order: order, variant: product_2.master ) - order.line_items.reload + order.line_items.reload create(:shipment, order: order, stock_location: supplier.stock_locations.first) order.shipments.reload - order.recalculate end factory :completed_order_from_supplier_with_totals do - state 'complete' + state { 'complete' } after(:create) do |order| order.refresh_shipment_rates @@ -38,8 +36,8 @@ end factory :order_from_supplier_ready_to_ship do - payment_state 'paid' - shipment_state 'ready' + payment_state { 'paid' } + shipment_state { 'ready' } after(:create) do |order| create(:payment, amount: order.total, order: order, state: 'completed') @@ -63,22 +61,24 @@ end end - factory :supplier, :class => Spree::Supplier do + factory :supplier, class: Spree::Supplier do sequence(:name) { |i| "Big Store #{i}" } - user_id - url "http://example.com" + user + url { 'http://example.com' } address - commission_flat_rate 0.0 - commission_percentage 10.0 + commission_flat_rate { 0.0 } + commission_percentage { 10.0 } # Creating a stock location with a factory instead of letting the model handle it # so that we can run tests with backorderable defaulting to true. before :create do |supplier| - supplier.stock_locations << build(:stock_location, name: supplier.name, supplier: supplier) + supplier.stock_locations << build(:stock_location, + name: supplier.name, + supplier: supplier) end factory :supplier_with_commission do - commission_flat_rate 0.5 - commission_percentage 10 + commission_flat_rate { 0.5 } + commission_percentage { 10 } end end @@ -87,7 +87,7 @@ end factory :supplier_admin_role, parent: :role do - name "supplier_admin" + name { 'supplier_admin' } end factory :supplier_admin, parent: :user do @@ -103,5 +103,4 @@ variant.product.add_supplier! create(:supplier) end end - end diff --git a/lib/solidus_marketplace/permitted_attributes.rb b/lib/solidus_marketplace/permitted_attributes.rb new file mode 100644 index 0000000..35126c3 --- /dev/null +++ b/lib/solidus_marketplace/permitted_attributes.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module SolidusMarketplace + module PermittedAttributes + class << self + @@supplier_attributes = [ + :id, + :address_id, + :commission_flat_rate, + :commission_percentage, + :user_id, + :name, + :url, + :deleted_at, + :tax_id, + :token, + :slug, + :paypal_email + ] + + mattr_reader(:supplier_attributes) + end + end +end diff --git a/lib/solidus_marketplace/version.rb b/lib/solidus_marketplace/version.rb index 1445a43..1a27c4e 100644 --- a/lib/solidus_marketplace/version.rb +++ b/lib/solidus_marketplace/version.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module SolidusMarketplace - VERSION = "0.1.0" + VERSION = '1.0.0' end diff --git a/lib/spree/permitted_attributes_decorator.rb b/lib/spree/permitted_attributes_decorator.rb deleted file mode 100644 index 61d7ec9..0000000 --- a/lib/spree/permitted_attributes_decorator.rb +++ /dev/null @@ -1,18 +0,0 @@ -Spree::PermittedAttributes.class_eval do - @@supplier_attributes = [ - :id, - :address_id, - :commission_flat_rate, - :commission_percentage, - :user_id, - :name, - :url, - :deleted_at, - :tax_id, - :token, - :slug, - :paypal_email - ] - - mattr_reader(:supplier_attributes) -end diff --git a/lib/tasks/spree_sample.rake b/lib/tasks/spree_sample.rake index 157aa0b..3c37d89 100644 --- a/lib/tasks/spree_sample.rake +++ b/lib/tasks/spree_sample.rake @@ -1,63 +1,66 @@ namespace :spree_sample do - desc "Create sample marketplace orders" - task :marketplace_orders => :environment do + desc 'Create sample marketplace orders' + task marketplace_orders: :environment do if Spree::Order.count == 0 - puts "Please run `rake spree_sample:load` first to create products and orders" + puts 'Please run `rake spree_sample:load` first to create products and orders' exit end if Spree::Supplier.count == 0 - puts "Please run `rake spree_sample:suppliers` first to create suppliers" + puts 'Please run `rake spree_sample:suppliers` first to create suppliers' exit end - count = 0 - @orders = Spree::Order.complete.includes(:line_items).all + count = 0 + @orders = Spree::Order.complete.includes(:line_items).all @suppliers = Spree::Supplier.all - puts "Linking existing line items to suppliers" + puts 'Linking existing line items to suppliers' Spree::LineItem.all.each do |li| print "*" if li.product.add_supplier! @suppliers.shuffle.first.id and li.save end puts - puts "Creating marketplace orders for existing orders" + puts 'Creating marketplace orders for existing orders' Spree::Order.all.each do |order| - print "*" if order.finalize! + print '*' if order.finalize! end puts end - desc "Create sample suppliers and randomly link to products" - task :suppliers => :environment do - old_send_value = SolidusMarketplace::Config[:send_supplier_email] - SolidusMarketplace::Config[:send_supplier_email] = false + desc 'Create sample suppliers and randomly link to products' + task suppliers: :environment do + old_send_value = SolidusMarketplace::Config.send_supplier_email + SolidusMarketplace::Config.send_supplier_email = false - @usa = Spree::Country.find_by_iso("US") - @ca = @usa.states.find_by_abbr("CA") + @usa = Spree::Country.find_by_iso('US') + @ca = @usa.states.find_by_abbr('CA') count = Spree::Supplier.count - puts "Creating Suppliers..." + puts 'Creating Suppliers...' 5.times{|i| name = "Supplier #{count + i + 1}" - supplier = Spree::Supplier.new(:name => name, - :user_id => 1, - :url => "http://example.com") - supplier.build_address(:firstname => name, :lastname => name, - :address1 => "100 State St", - :city => "Santa Barbara", :zipcode => "93101", - :state_id => @ca.id, :country_id => @usa.id, - :phone => '1234567890') - print "*" if supplier.save + supplier = Spree::Supplier.new(name: name, + user_id: 1, + url: 'http://example.com') + supplier.build_address(firstname: name, + lastname: name, + address1: '100 State St', + city: 'Santa Barbara', + zipcode: '93101', + state_id: @ca.id, + country_id: @usa.id, + phone: '1234567890') + print '*' if supplier.save } puts puts "#{Spree::Supplier.count - count} suppliers created" - puts "Randomly linking Products & Suppliers..." + puts 'Randomly linking Products & Suppliers...' @supplier_ids = Spree::Supplier.pluck(:id).shuffle - @products = Spree::Product.all - count = 0 + @products = Spree::Product.all + count = 0 @products.each do |product| product.add_supplier! Spree::Supplier.find(@supplier_ids[rand(@supplier_ids.length)]) @@ -65,8 +68,9 @@ namespace :spree_sample do count += 1 print "*" end + puts puts "#{count} products linked." - SolidusMarketplace::Config[:send_supplier_email] = old_send_value + SolidusMarketplace::Config.send_supplier_email = old_send_value end end diff --git a/solidus_marketplace.gemspec b/solidus_marketplace.gemspec index e0206ca..61bb613 100644 --- a/solidus_marketplace.gemspec +++ b/solidus_marketplace.gemspec @@ -1,46 +1,42 @@ - # encoding: UTF-8 +# frozen_string_literal: true lib = File.expand_path('../lib/', __FILE__) $:.unshift lib unless $:.include?(lib) require 'solidus_marketplace/version' Gem::Specification.new do |s| - s.platform = Gem::Platform::RUBY - s.name = 'solidus_marketplace' - s.version = SolidusMarketplace::VERSION - s.summary = 'Solidus Marketplace Extension' + s.platform = Gem::Platform::RUBY + s.name = 'solidus_marketplace' + s.version = SolidusMarketplace::VERSION + s.summary = 'Solidus Marketplace Extension' s.description = 'Adds marketplace functionality to Solidus stores.' - s.required_ruby_version = '>= 2.0.0' + s.required_ruby_version = '>= 2.0' - s.author = 'Boomer Digital' - s.email = '' - s.homepage = 'https://github.com/boomerdigital/solidus_marketplace' + s.author = 'Jonathan Tapia' + s.email = 'jonathan.tapia@magmalabs.io' + s.homepage = 'https://github.com/jtapia/solidus_marketplace' - s.files = `git ls-files`.split("\n") - s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + s.files = `git ls-files`.split("\n") + s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.require_path = 'lib' s.requirements << 'none' - s.add_dependency 'durable_decorator', '~> 0.2.0' - s.add_dependency 'solidus_api' - s.add_dependency 'solidus_backend' - s.add_dependency 'solidus_core' - s.add_dependency 'solidus_gateway' + s.add_dependency 'solidus_core', ['>= 2.2', '< 3'] + s.add_dependency 'solidus_support' + s.add_dependency 'deface', '~> 1.0' - s.add_development_dependency 'capybara', '~> 2.2' + s.add_development_dependency 'capybara' + s.add_development_dependency 'capybara-screenshot' s.add_development_dependency 'coffee-rails' - s.add_development_dependency 'coveralls' s.add_development_dependency 'database_cleaner' - s.add_development_dependency 'factory_girl_rails', '~> 4.2' + s.add_development_dependency 'factory_bot' s.add_development_dependency 'ffaker' - s.add_development_dependency 'launchy' + s.add_development_dependency 'poltergeist' + s.add_development_dependency 'puma' s.add_development_dependency 'rspec-rails' - s.add_development_dependency 'sass-rails', '~> 5.0.0.beta1' - s.add_development_dependency 'selenium-webdriver' - s.add_development_dependency 'shoulda-matchers' - s.add_development_dependency 'solidus_sample' + s.add_development_dependency 'rubocop' + s.add_development_dependency 'rubocop-rspec' + s.add_development_dependency 'sass-rails' + s.add_development_dependency 'simplecov' s.add_development_dependency 'sqlite3' - s.add_development_dependency 'log_buddy' - s.add_development_dependency 'awesome_print' - s.add_development_dependency 'pry' end diff --git a/spec/features/admin/orders_spec.rb b/spec/features/admin/orders_spec.rb deleted file mode 100644 index 74f6620..0000000 --- a/spec/features/admin/orders_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'spec_helper' - -describe 'Admin - Orders' do - - it 'Supplier should not be authorized' do - login_as create(:supplier_user) - visit spree.admin_orders_path - expect(page).to have_content('Authorization Failure') - end - -end diff --git a/spec/features/admin/stock_management_spec.rb b/spec/features/admin/stock_management_spec.rb deleted file mode 100644 index 3a84817..0000000 --- a/spec/features/admin/stock_management_spec.rb +++ /dev/null @@ -1,156 +0,0 @@ -require 'spec_helper' - -describe "Stock Management", js: true do - - before do - @user = create(:supplier_user) - login_user @user - visit spree.admin_shipments_path - end - - context "as supplier user" do - - context "given a product with a variant and a stock location" do - before do - @secondary = create(:stock_location, name: 'Secondary', supplier: @user.supplier) - @product = create(:product, name: 'apache baseball cap', price: 10) - @v = @product.variants.create!(sku: 'FOOBAR') - @user.supplier.reload.stock_locations.update_all backorderable_default: false # True database default is false. - end - - context 'with single variant' do - before do - @product.add_supplier! @user.supplier - @v.stock_items.first.update_column(:count_on_hand, 10) - @secondary.stock_item(@v).destroy - click_link "Products" - sleep(1) - within '#sidebar-product' do - click_link "Products" - end - click_link @product.name - within '[data-hook=admin_product_tabs]' do - click_link "Stock" - end - end - - xit "should not show deleted stock_items" do - within(:css, '.stock_location_info') do - expect(page).to have_content(@user.supplier.name) - expect(page).to_not have_content('Secondary') - end - end - - xit "can toggle backorderable for a variant's stock item", js: true do - backorderable = find(".stock_item_backorderable") - expect(backorderable).to_not be_checked - - backorderable.set(true) - - visit current_path - - backorderable = find(".stock_item_backorderable") - expect(backorderable).to be_checked - end - - # Regression test for #2896 - # The regression was that unchecking the last checkbox caused a redirect - # to happen. By ensuring that we're still on an /admin/products URL, we - # assert that the redirect is *not* happening. - xit "can toggle backorderable for the second variant stock item", js: true do - new_location = create(:stock_location, name: "Another Location", supplier: @user.supplier) - visit page.current_path - - new_location_backorderable = find "#stock_item_backorderable_#{new_location.id}" - new_location_backorderable.set(false) - # Wait for API request to complete. - sleep(1) - - expect(page.current_url).to include("/admin/products") - end - - xit "can create a new stock movement", js: true do - fill_in "stock_movement_quantity", with: 5 - select2 @user.supplier.name, from: "Stock Location" - click_button "Add Stock" - - expect(page).to have_content('successfully created') - within(:css, '.stock_location_info table') do - expect(column_text(2)).to eq '15' - end - end - - xit "can create a new negative stock movement", js: true do - fill_in "stock_movement_quantity", with: -5 - select2 @user.supplier.name, from: "Stock Location" - click_button "Add Stock" - - expect(page).to have_content('successfully created') - - within(:css, '.stock_location_info table') do - expect(column_text(2)).to eq '5' - end - end - - xit "can create a new negative stock movement", js: true do - fill_in "stock_movement_quantity", with: -5 - select2 @user.supplier.name, from: "Stock Location" - click_button "Add Stock" - - expect(page).to have_content('successfully created') - - within(:css, '.stock_location_info table') do - expect(column_text(2)).to eq '5' - end - end - end - - context "with multiple variants" do - before do - v = @product.variants.create!(sku: 'SPREEC') - @product.add_supplier! @user.supplier - v.stock_items.first.update_column(:count_on_hand, 30) - - click_link "Products" - sleep(1) - within '#sidebar-product' do - click_link 'Products' - end - click_link @product.name - within '[data-hook=admin_product_tabs]' do - click_link "Stock" - end - end - - xit "can create a new stock movement for the specified variant", js: true do - fill_in "stock_movement_quantity", with: 10 - select2 "SPREEC", from: "Variant" - click_button "Add Stock" - - expect(page).to have_content('successfully created') - end - end - end - - # Regression test for #3304 - context "with no stock location" do - before do - @product = create(:product, name: 'apache baseball cap', price: 10) - @product.add_supplier! @user.supplier - @product.variants.create!(sku: 'FOOBAR') - Spree::StockLocation.delete_all - click_link "Products" - sleep(1) - within '#sidebar-product' do - click_link 'Products' - end - click_link @product.name - end - - xit "redirects to stock locations page" do - expect(page).to have_content(Spree.t(:stock_management_requires_a_stock_location)) - expect(page.current_url).to include("admin/stock_locations") - end - end - end -end diff --git a/spec/features/spree/admin/orders_spec.rb b/spec/features/spree/admin/orders_spec.rb new file mode 100644 index 0000000..e7914b2 --- /dev/null +++ b/spec/features/spree/admin/orders_spec.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +RSpec.describe 'Admin - Orders', type: :feature do + let!(:user) { create(:supplier_user) } + + it 'Supplier should not be authorized' do + visit spree.root_path + click_link 'Login' + fill_in 'spree_user[email]', with: user.email + fill_in 'spree_user[password]', with: user.password + click_button 'Login' + expect(page).to_not have_content 'Login' + + visit spree.admin_orders_path + expect(page).to have_content('Authorization Failure') + end +end diff --git a/spec/features/admin/products_spec.rb b/spec/features/spree/admin/products_spec.rb similarity index 59% rename from spec/features/admin/products_spec.rb rename to spec/features/spree/admin/products_spec.rb index a3805a8..4d947d7 100644 --- a/spec/features/admin/products_spec.rb +++ b/spec/features/spree/admin/products_spec.rb @@ -1,15 +1,16 @@ -require 'spec_helper' - -describe 'Admin - Products', js: true do +# frozen_string_literal: true + +RSpec.describe 'Admin - Products', type: :feature, js: true do + let(:supplier1) { create(:supplier) } + let(:supplier2) { create(:supplier) } + let(:product) do + product = create(:product) + product.add_supplier!(supplier1) + product + end context 'as Admin' do - xit 'should be able to change supplier' do - s1 = create(:supplier) - s2 = create(:supplier) - product = create :product - product.add_supplier! s1 - login_user create(:admin_user) visit spree.admin_product_path(product) @@ -19,7 +20,5 @@ expect(page).to have_content("Product \"#{product.name}\" has been successfully updated!") expect(product.reload.suppliers.first.id).to eql(s2.id) end - end - end diff --git a/spec/features/admin/return_authorizations_spec.rb b/spec/features/spree/admin/return_authorizations_spec.rb similarity index 58% rename from spec/features/admin/return_authorizations_spec.rb rename to spec/features/spree/admin/return_authorizations_spec.rb index c0f26e1..f41c327 100644 --- a/spec/features/admin/return_authorizations_spec.rb +++ b/spec/features/spree/admin/return_authorizations_spec.rb @@ -1,7 +1,5 @@ -require 'spec_helper' - -describe "Admin - Return Authorizations" do +# frozen_string_literal: true +RSpec.describe "Admin - Return Authorizations" do skip 'TODO: need to write, but should likely wait until after https://github.com/spree/spree/issues/4026' - end diff --git a/spec/features/admin/settings_spec.rb b/spec/features/spree/admin/settings_spec.rb similarity index 87% rename from spec/features/admin/settings_spec.rb rename to spec/features/spree/admin/settings_spec.rb index a59171d..c1fcd04 100644 --- a/spec/features/admin/settings_spec.rb +++ b/spec/features/spree/admin/settings_spec.rb @@ -1,10 +1,9 @@ -require 'spec_helper' +# frozen_string_literal: true -describe 'Admin - Marketplace Settings' do +RSpec.describe 'Admin - Marketplace Settings', type: :feature do + let!(:user) { create(:admin_user) } before do - login_user create(:admin_user) - visit spree.admin_path within '[data-hook=admin_tabs]' do click_link 'Settings' @@ -29,5 +28,4 @@ expect(find_field('default_commission_percentage').value.to_f).to eql(10.0) expect(page).to have_content('Marketplace settings successfully updated.') end - end diff --git a/spec/features/admin/shipments_spec.rb b/spec/features/spree/admin/shipments_spec.rb similarity index 50% rename from spec/features/admin/shipments_spec.rb rename to spec/features/spree/admin/shipments_spec.rb index 32e8a90..dbe809c 100644 --- a/spec/features/admin/shipments_spec.rb +++ b/spec/features/spree/admin/shipments_spec.rb @@ -1,24 +1,30 @@ -require 'spec_helper' - -describe 'Admin - Shipments', js: true do +# frozen_string_literal: true + +RSpec.describe 'Admin - Shipments', type: :feature, js: true do + let!(:order) do + order = build(:order_from_supplier_ready_to_ship, + state: 'complete', + completed_at: '2011-02-01 12:36:15', + number: 'R100') + end + let!(:supplier) { create(:supplier) } + let!(:product) do + product = create(:product, name: 'spree t-shirt', price: 20.00) + product.add_supplier!(supplier.id) + product + end + let!(:shipment) do + create(:shipment, + order: order, + stock_location: supplier.stock_locations.first) + end + let!(:shipping_method) { create(:shipping_method, name: 'Default') } context 'as Supplier' do - - let!(:order) { build(:order_from_supplier_ready_to_ship, state: 'complete', completed_at: "2011-02-01 12:36:15", number: "R100") } - let!(:supplier) { create(:supplier) } - - let!(:product) { - p = create(:product, name: 'spree t-shirt', price: 20.00) - p.add_supplier! supplier.id - p - } - - let!(:shipment) { create(:shipment, order: order, stock_location: supplier.stock_locations.first) } - let!(:shipping_method) { create(:shipping_method, name: "Default") } - before do # Adjust qoh so shipment will be ready - shipment.stock_location.stock_items.where(variant_id: product.master.id).first.adjust_count_on_hand(10) + shipment.stock_location.stock_items.where(variant_id: product.master.id). + first.adjust_count_on_hand(10) # Add product and update shipment order.contents.add(product.master, 2) shipment.refresh_rates @@ -36,38 +42,37 @@ end context 'edit page' do - - xit "can add tracking information" do + xit 'can add tracking information' do within '.table tr.show-tracking' do click_icon :edit end within '.table tr.edit-tracking' do - fill_in "tracking", with: "FOOBAR" + fill_in 'tracking', with: 'FOOBAR' click_icon :save end wait_for_ajax within '.table tr.show-tracking' do - expect(page).to have_content("Tracking: FOOBAR") + expect(page).to have_content('Tracking: FOOBAR') end end - xit "can change the shipping method" do - within(".table tr.show-method") do + xit 'can change the shipping method' do + within('.table tr.show-method') do click_icon :edit end - select2 "Default", from: "Shipping Method" + select2 'Default', from: 'Shipping Method' click_icon :save wait_for_ajax - expect(page).to have_content("Default $0.00") + expect(page).to have_content('Default $0.00') end - xit "can ship a completed order" do - click_on "Ship" + xit 'can ship a completed order' do + click_on 'Ship' wait_for_ajax - expect(page).to have_content("shipped package") - expect(order.reload.shipment_state).to eq "shipped" + expect(page).to have_content('shipped package') + expect(order.reload.shipment_state).to eq 'shipped' end end @@ -76,5 +81,4 @@ expect(page).to have_content('Authorization Failure') end end - end diff --git a/spec/features/spree/admin/stock_management_spec.rb b/spec/features/spree/admin/stock_management_spec.rb new file mode 100644 index 0000000..5453c60 --- /dev/null +++ b/spec/features/spree/admin/stock_management_spec.rb @@ -0,0 +1,160 @@ +# frozen_string_literal: true + +RSpec.describe 'Stock Management', type: :feature, js: true do + let(:user) { create(:supplier_user) } + + before do + visit spree.admin_shipments_path + end + + context 'as supplier user' do + let(:stock_location) do + create(:stock_location, name: 'Secondary', supplier: user.supplier) + end + let(:product) { create(:product, name: 'apache baseball cap', price: 10) } + let(:variant) { product.variants.create!(sku: 'FOOBAR') } + + before do + user.supplier.reload.stock_locations.update_all(backorderable_default: false) # True database default is false. } + end + + context 'given a product with a variant and a stock location' do + context 'with single variant' do + before do + product.add_supplier!(user.supplier) + variant.stock_items.first.update_column(:count_on_hand, 10) + stock_location.stock_item(@v).destroy + click_link 'Products' + sleep(1) + within '#sidebar-product' do + click_link 'Products' + end + click_link @product.name + within '[data-hook=admin_product_tabs]' do + click_link 'Stock' + end + end + + xit 'should not show deleted stock_items' do + within(:css, '.stock_location_info') do + expect(page).to have_content(user.supplier.name) + expect(page).to_not have_content('Secondary') + end + end + + xit "can toggle backorderable for a variant's stock item", js: true do + backorderable = find('.stock_item_backorderable') + expect(backorderable).to_not be_checked + + backorderable.set(true) + + visit current_path + + backorderable = find('.stock_item_backorderable') + expect(backorderable).to be_checked + end + + # Regression test for #2896 + # The regression was that unchecking the last checkbox caused a redirect + # to happen. By ensuring that we're still on an /admin/products URL, we + # assert that the redirect is *not* happening. + xit 'can toggle backorderable for the second variant stock item', js: true do + new_location = create(:stock_location, name: 'Another Location', supplier: user.supplier) + visit page.current_path + + new_location_backorderable = find '#stock_item_backorderable_#{new_location.id}' + new_location_backorderable.set(false) + # Wait for API request to complete. + sleep(1) + + expect(page.current_url).to include('/admin/products') + end + + xit 'can create a new stock movement', js: true do + fill_in 'stock_movement_quantity', with: 5 + select2 user.supplier.name, from: 'Stock Location' + click_button 'Add Stock' + + expect(page).to have_content('successfully created') + within(:css, '.stock_location_info table') do + expect(column_text(2)).to eq '15' + end + end + + xit 'can create a new negative stock movement', js: true do + fill_in 'stock_movement_quantity', with: -5 + select2 user.supplier.name, from: 'Stock Location' + click_button 'Add Stock' + + expect(page).to have_content('successfully created') + + within(:css, '.stock_location_info table') do + expect(column_text(2)).to eq '5' + end + end + + xit 'can create a new negative stock movement', js: true do + fill_in 'stock_movement_quantity', with: -5 + select2 user.supplier.name, from: 'Stock Location' + click_button 'Add Stock' + + expect(page).to have_content('successfully created') + + within(:css, '.stock_location_info table') do + expect(column_text(2)).to eq '5' + end + end + end + + context 'with multiple variants' do + let(:variant) { product.variants.create!(sku: 'SPREEC') } + + before do + product.add_supplier!(user.supplier) + variant.stock_items.first.update_column(:count_on_hand, 30) + + click_link 'Products' + sleep(1) + within '#sidebar-product' do + click_link 'Products' + end + click_link @product.name + within '[data-hook=admin_product_tabs]' do + click_link 'Stock' + end + end + + xit 'can create a new stock movement for the specified variant', js: true do + fill_in 'stock_movement_quantity', with: 10 + select2 'SPREEC', from: 'Variant' + click_button 'Add Stock' + + expect(page).to have_content('successfully created') + end + end + end + + # Regression test for #3304 + context 'with no stock location' do + let(:product) { create(:product, name: 'apache baseball cap', price: 10) } + + before do + product.add_supplier! @user.supplier + product.variants.create!(sku: 'FOOBAR') + + Spree::StockLocation.delete_all + click_link 'Products' + sleep(1) + within '#sidebar-product' do + click_link 'Products' + end + click_link @product.name + end + + xit 'redirects to stock locations page' do + expect(page).to have_content(t('spree.stock_management_requires_a_stock_location')) + expect(page.current_url).to include('admin/stock_locations') + end + end + end +end diff --git a/spec/features/admin/stock_spec.rb b/spec/features/spree/admin/stock_spec.rb similarity index 77% rename from spec/features/admin/stock_spec.rb rename to spec/features/spree/admin/stock_spec.rb index e2f5dd2..f87f2e5 100644 --- a/spec/features/admin/stock_spec.rb +++ b/spec/features/spree/admin/stock_spec.rb @@ -1,17 +1,17 @@ -require 'spec_helper' - -feature 'Admin - Product Stock Management', js: true do - - before do - @user = create(:supplier_admin) - @supplier1 = @user.supplier - @supplier2 = create(:supplier) - @product = create :product - @product.add_supplier! @supplier1 +# frozen_string_literal: true + +RSpec.describe 'Admin - Product Stock Management', type: :feature, js: true do + let!(:user) { create(:supplier_admin) } + let!(:supplier1) { user.supplier } + let!(:supplier2) { create(:supplier) } + let!(:product) do + product = create(:product) + product.add_supplier!(supplier1) + product end + let!(:stock_location) { create(:stock_location, name: 'Big Store', supplier: user.supplier) } context 'as Admin' do - xscenario 'should display all existing stock item locations' do login_user create(:admin_user) visit spree.stock_admin_product_path(@product) @@ -22,15 +22,12 @@ expect(page).to_not have_content(@supplier2.name) end end - end context 'as Supplier' do - before(:each) do - login_user @user visit '/admin/products' - click_link "Stock Locations" + click_link 'Stock Locations' end xscenario 'should only display suppliers stock locations' do @@ -53,7 +50,6 @@ end xscenario "can delete an existing stock location", js: true do - create(:stock_location, supplier: @user.supplier) visit current_path expect(find('#listing_stock_locations')).to have_content("NY Warehouse") @@ -66,8 +62,7 @@ expect(find('#listing_stock_locations')).to_not have_content("NY Warehouse") end - scenario "can update an existing stock location" do - create(:stock_location, supplier: @user.supplier) + xscenario "can update an existing stock location" do visit current_path expect(page).to have_content("Big Store") @@ -80,8 +75,7 @@ expect(page).to have_content("London") end - scenario "can deactivate an existing stock location" do - create(:stock_location, supplier: @user.supplier) + xscenario "can deactivate an existing stock location" do visit current_path expect(page).to have_content("Big Store") @@ -92,7 +86,5 @@ expect(find('#listing_stock_locations')).to have_content("Inactive") end - end - end diff --git a/spec/features/admin/suppliers_spec.rb b/spec/features/spree/admin/suppliers_spec.rb similarity index 80% rename from spec/features/admin/suppliers_spec.rb rename to spec/features/spree/admin/suppliers_spec.rb index 030fc99..d86d1a2 100644 --- a/spec/features/admin/suppliers_spec.rb +++ b/spec/features/spree/admin/suppliers_spec.rb @@ -1,17 +1,12 @@ -require 'spec_helper' +# frozen_string_literal: true -feature 'Admin - Suppliers', js: true do - - before do - country = create(:country, name: "United States") - create(:state, name: "Vermont", country: country) - @supplier = create :supplier - end +RSpec.describe 'Admin - Suppliers', type: :feature, js: true do + let(:country) { create(:country, name: 'United States') } + let(:state) { create(:state, name: "Vermont", country: country) } + let!(:supplier) { create(:supplier) } context 'as an MarketMaker (aka admin)' do - before do - login_user create(:admin_user) visit spree.admin_path within '[data-hook=admin_tabs]' do click_link 'Suppliers' @@ -66,17 +61,19 @@ click_button 'Update' expect(page).to have_content('Supplier "Test Supplier" has been successfully updated!') end - end context 'as a Supplier' do + let!(:user) { create(:supplier_admin) } + before do - @user = create(:supplier_admin) - login_user @user - visit spree.edit_admin_supplier_path(@user.supplier) + allow_any_instance_of(Spree::Admin::SuppliersController).to receive_messages(try_spree_current_user: user) + allow_any_instance_of(Spree::OrdersController).to receive_messages(try_spree_current_user: user) + visit spree.edit_admin_supplier_path(user.supplier) end - scenario 'should only see tabs they have access to' do + xscenario 'should only see tabs they have access to' do + save_and_open_page within '[data-hook=admin_tabs]' do expect(page).to have_link('Products') expect(page).to have_link('Stock') @@ -84,7 +81,6 @@ expect(page).to have_link('Profile') expect(page).to have_link('Orders') expect(page).to have_link('Suppliers') - expect(page).to_not have_link('Overview') expect(page).to_not have_link('Reports') expect(page).to_not have_link('Configuration') @@ -94,7 +90,7 @@ xscenario 'should be able to update supplier' do fill_in 'supplier[name]', with: 'Test Supplier' - fill_in 'supplier[email]', with: @user.email + fill_in 'supplier[email]', with: user.email fill_in 'supplier[url]', with: 'http://www.test.com' fill_in 'supplier[address_attributes][firstname]', with: 'First' fill_in 'supplier[address_attributes][lastname]', with: 'Last' @@ -111,20 +107,23 @@ expect(page).to_not have_css('#supplier_commission_percentage') # cannot edit comission percentage click_button 'Update' expect(page).to have_content('Supplier "Test Supplier" has been successfully updated!') - expect(page.current_path).to eql(spree.edit_admin_supplier_path(@user.reload.supplier)) + expect(page.current_path).to eq(spree.edit_admin_supplier_path(user.reload.supplier)) end - end context 'as a User other than the suppliers' do + let(:user) { create(:user, password: 'secret') } + let(:supplier) { create(:supplier) } scenario 'should be unauthorized' do - supplier = create(:supplier) - login_user create(:user) + visit spree.root_path + click_link 'Login' + fill_in 'spree_user[email]', with: user.email + fill_in 'spree_user[password]', with: user.password + click_button 'Login' + expect(page).to_not have_content 'Login' visit spree.edit_admin_supplier_path(supplier) expect(page).to have_content('Authorization Failure') end - end - end diff --git a/spec/models/spree/order_decorator_spec.rb b/spec/models/spree/order_decorator_spec.rb index cee90cd..bedb3d0 100644 --- a/spec/models/spree/order_decorator_spec.rb +++ b/spec/models/spree/order_decorator_spec.rb @@ -1,18 +1,22 @@ -require 'spec_helper' - -describe Spree::Order do +# frozen_string_literal: true + +RSpec.describe Spree::Order do + let!(:order) do + order = create(:order_with_totals) + order.line_items = [create(:line_item, + variant: create(:variant_with_supplier)), + create(:line_item, + variant: create(:variant_with_supplier))] + order.create_proposed_shipments + order + end context '#finalize_with_supplier!' do - after do - SolidusMarketplace::Config[:send_supplier_email] = true + SolidusMarketplace::Config.send_supplier_email = true end xit 'should deliver marketplace orders when SolidusMarketplace::Config[:send_supplier_email] == true' do - order = create(:order_with_totals, ship_address: create(:address)) - order.line_items = [create(:line_item, variant: create(:variant_with_supplier)), create(:line_item, variant: create(:variant_with_supplier))] - order.create_proposed_shipments - order.shipments.each do |shipment| expect(Spree::MarketplaceOrderMailer).to receive(:supplier_order).with(shipment.id).and_return(double(Mail, :deliver! => true)) end @@ -28,11 +32,8 @@ end end - it 'should NOT deliver marketplace orders when SolidusMarketplace::Config[:send_supplier_email] == false' do - SolidusMarketplace::Config[:send_supplier_email] = false - order = create(:order_with_totals, ship_address: create(:address)) - order.line_items = [create(:line_item, variant: create(:variant_with_supplier)), create(:line_item, variant: create(:variant_with_supplier))] - order.create_proposed_shipments + xit 'should NOT deliver marketplace orders when SolidusMarketplace::Config[:send_supplier_email] == false' do + SolidusMarketplace::Config.send_supplier_email = false order.shipments.each do |shipment| expect(Spree::MarketplaceOrderMailer).not_to receive(:supplier_order).with(shipment.id) @@ -48,33 +49,28 @@ expect(shipment.line_items.first.variant.suppliers.first).to eql(shipment.supplier) end end - end - describe "#supplier_total" do - context "when passed a supplier" do - it "returns the total commission earned for the order for a given supplier" do - order = create(:completed_order_from_supplier_with_totals, ship_address: create(:address)) - supplier = order.suppliers.first - expected_supplier_total = Spree::Money.new(15.00) + xdescribe '#supplier_total' do + let!(:order) { create(:completed_order_from_supplier_with_totals, + ship_address: create(:address)) } + let(:supplier) { order.suppliers.first } + let(:expected_supplier_total) { Spree::Money.new(15.00) } + + context 'when passed a supplier' do + it 'returns the total commission earned for the order for a given supplier' do expect(order.total).to eq(150.0) expect(order.suppliers.count).to eq(1) - expect(order.supplier_total(supplier)).to eq(expected_supplier_total) + expect(order.supplier_total(supplier).to_s).to eq(expected_supplier_total.to_s) end end - context "when passed a user associated with a supplier" do - it "returns the total commission earned for the order for a given supplier" do - order = create(:completed_order_from_supplier_with_totals, ship_address: create(:address)) - supplier = order.suppliers.first - supplier_user = create(:supplier_user, supplier: supplier) - - expected_supplier_total = Spree::Money.new(15.00) + context 'when passed a user associated with a supplier' do + it 'returns the total commission earned for the order for a given supplier' do expect(order.total).to eq(150.0) expect(order.suppliers.count).to eq(1) - expect(order.supplier_total(supplier_user)).to eq(expected_supplier_total) + expect(order.supplier_total(supplier)).to eq(expected_supplier_total) end end end - end diff --git a/spec/models/spree/payment_decorator_spec.rb b/spec/models/spree/payment_decorator_spec.rb index c14bcde..d88140f 100644 --- a/spec/models/spree/payment_decorator_spec.rb +++ b/spec/models/spree/payment_decorator_spec.rb @@ -1,7 +1,5 @@ -require 'spec_helper' - -describe Spree::Payment do - - it { should belong_to(:payable) } +# frozen_string_literal: true +RSpec.describe Spree::Payment do + it { is_expected.to respond_to(:payable) } end diff --git a/spec/models/spree/product_decorator_spec.rb b/spec/models/spree/product_decorator_spec.rb index d0b33a7..b6f51b9 100644 --- a/spec/models/spree/product_decorator_spec.rb +++ b/spec/models/spree/product_decorator_spec.rb @@ -1,13 +1,12 @@ -require 'spec_helper' - -describe Spree::Product do +# frozen_string_literal: true +RSpec.describe Spree::Product do let(:product) { create :product } let(:supplier1) { create(:supplier) } let(:supplier2) { create(:supplier) } - describe "#add_supplier!" do - context "when passed a supplier" do + describe '#add_supplier!' do + context 'when passed a supplier' do it "adds the supplier to product's list of supppliers" do expect(product.suppliers).to be_empty product.add_supplier!(supplier1) @@ -15,7 +14,7 @@ end end - context "when passed a supplier_id" do + context 'when passed a supplier_id' do it "adds the supplier to product's list of supppliers" do expect(product.suppliers).to be_empty product.add_supplier!(supplier2.id) @@ -24,7 +23,7 @@ end end - describe "#add_suppliers!" do + describe '#add_suppliers!' do it "adds multiple suppliers to the product's list of suppliers" do expect(product.suppliers).to be_empty product.add_suppliers!([supplier1.id, supplier2.id]) @@ -33,7 +32,7 @@ end end - describe "#remove_suppliers!" do + describe '#remove_suppliers!' do it "removes multiple suppliers from the product's list of suppliers" do product.add_suppliers!([supplier1.id, supplier2.id]) expect(product.reload.suppliers).to include(supplier1) @@ -45,12 +44,10 @@ end describe '#supplier?' do - it "returns true if one or more suppliers are present" do + it 'returns true if one or more suppliers are present' do expect(product.supplier?).to eq false - product.add_supplier! create(:supplier) + product.add_supplier!(create(:supplier)) expect(product.reload.supplier?).to eq true end end - - end diff --git a/spec/models/spree/shipment_decorator_spec.rb b/spec/models/spree/shipment_decorator_spec.rb index e71da6f..d981dfd 100644 --- a/spec/models/spree/shipment_decorator_spec.rb +++ b/spec/models/spree/shipment_decorator_spec.rb @@ -1,48 +1,46 @@ -require 'spec_helper' - -describe Spree::Shipment do - - describe 'Scopes' do +# frozen_string_literal: true + +RSpec.describe Spree::Shipment do + context 'Scopes' do + let(:supplier) { create(:supplier) } + let(:stock_location_1) { supplier.stock_locations.first } + let(:stock_location_2) { create(:stock_location, supplier: supplier) } + let(:shipment_1) { create(:shipment) } + let(:shipment_2) { create(:shipment, stock_location: stock_location_1) } + let(:shipment_3) { create(:shipment) } + let(:shipment_4) { create(:shipment, stock_location: stock_location_2) } + let(:shipment_5) { create(:shipment) } + let(:shipment_6) { create(:shipment, stock_location: stock_location_1) } it '#by_supplier' do - supplier = create(:supplier) - stock_location_1 = supplier.stock_locations.first - stock_location_2 = create(:stock_location, supplier: supplier) - shipment_1 = create(:shipment) - shipment_2 = create(:shipment, stock_location: stock_location_1) - shipment_3 = create(:shipment) - shipment_4 = create(:shipment, stock_location: stock_location_2) - shipment_5 = create(:shipment) - shipment_6 = create(:shipment, stock_location: stock_location_1) - - expect(subject.class.by_supplier(supplier.id)).to match_array([shipment_2, shipment_4, shipment_6]) + expect(subject.class.by_supplier(supplier.id)). + to match_array([shipment_2, shipment_4, shipment_6]) end - end - describe '#after_ship' do + context '#after_ship' do + let(:supplier) { create(:supplier_with_commission) } + let(:shipment) { create(:shipment, stock_location: supplier.stock_locations.first) } it 'should capture payment if balance due' do skip 'TODO make it so!' end xit 'should track commission for shipment' do - supplier = create(:supplier_with_commission) - shipment = create(:shipment, stock_location: supplier.stock_locations.first) - expect(shipment.supplier_commission.to_f).to eql(0.0) allow(shipment).to receive(:final_price_with_items).and_return(10.0) shipment.send(:after_ship) expect(shipment.reload.supplier_commission.to_f).to eql(1.5) end - end - it '#final_price_with_items' do - shipment = build :shipment - allow(shipment).to receive(:item_cost).and_return(50.0) - allow(shipment).to receive(:final_price).and_return(5.5) - expect(shipment.final_price_with_items.to_f).to eql(55.5) - end + context '#final_price_with_items' do + let(:shipment) { build(:shipment) } + it 'returns correct prices' do + allow(shipment).to receive(:item_cost).and_return(50.0) + allow(shipment).to receive(:final_price).and_return(5.5) + expect(shipment.final_price_with_items.to_f).to eql(55.5) + end + end end diff --git a/spec/models/spree/stock/splitter/marketplace_spec.rb b/spec/models/spree/stock/splitter/marketplace_spec.rb index 41498de..0e00f69 100644 --- a/spec/models/spree/stock/splitter/marketplace_spec.rb +++ b/spec/models/spree/stock/splitter/marketplace_spec.rb @@ -1,62 +1,26 @@ -require 'spec_helper' - -module Spree - module Stock - module Splitter - describe Marketplace do - - let(:supplier_1) { create(:supplier) } - let(:supplier_2) { create(:supplier) } - - let(:variant_1) { - v = create(:variant) - v.product.add_supplier! supplier_1 - v.reload.supplier_variants.find_by_supplier_id(supplier_1.id).update_column(:cost, 5) - v.product.add_supplier! supplier_2 - v.reload.supplier_variants.find_by_supplier_id(supplier_2.id).update_column(:cost, 6) - v - } - let(:variant_2) { - v = create(:variant) - v.product.add_supplier! supplier_1 - v.reload.supplier_variants.find_by_supplier_id(supplier_1.id).update_column(:cost, 5) - v.product.add_supplier! supplier_2 - v.reload.supplier_variants.find_by_supplier_id(supplier_2.id).update_column(:cost, 4) - v - } - let(:variant_3) { - v = create(:variant) - v.product.add_supplier! supplier_1 - v.product.add_supplier! supplier_2 - v.reload - } - let(:variant_4) { create(:variant) } - - let(:variants){ - [variant_1, variant_2, variant_3, variant_4] - } - - let(:packer) { build(:stock_packer) } - - subject { Marketplace.new(packer.stock_location) } - - it 'splits packages for suppliers to ship' do - package = Package.new(packer.stock_location) - package = Package.new(packer.stock_location) - 4.times { |i| package.add build(:inventory_unit, variant: variants[i]) } - - packages = subject.split([package]) - expect(packages.count).to eq 3 +# frozen_string_literal: true + +RSpec.describe Spree::Stock::Splitter::Marketplace do + let(:stock_location) { create(:stock_location) } + let(:supplier1) { create(:supplier, stock_locations: [stock_location]) } + let(:supplier2) { create(:supplier, stock_locations: [stock_location]) } + let(:variant) do + variant = create(:variant) + variant.product.add_supplier!(supplier1) + variant.reload.supplier_variants.find_by_supplier_id(supplier1.id). + update_column(:cost, 5) + variant.product.add_supplier!(supplier2) + variant.reload.supplier_variants.find_by_supplier_id(supplier2.id). + update_column(:cost, 6) + variant + end - expect(packages[0].stock_location).to eq(packer.stock_location) - expect(packages[0].contents.count).to eq(1) - expect(packages[1].stock_location).to eq(supplier_1.stock_locations.first) - expect(packages[1].contents.count).to eq(2) - expect(packages[2].stock_location).to eq(supplier_2.stock_locations.first) - expect(packages[2].contents.count).to eq(1) - end + subject { described_class.new(stock_location) } - end - end + it 'splits packages for suppliers to ship' do + package = Spree::Stock::Package.new(stock_location) + 2.times { package.add build(:inventory_unit, variant: variant) } + packages = subject.split([package]) + expect(packages.count).to eq(2) end end diff --git a/spec/models/spree/stock_location_decorator_spec.rb b/spec/models/spree/stock_location_decorator_spec.rb index 18c6938..7ac64d9 100644 --- a/spec/models/spree/stock_location_decorator_spec.rb +++ b/spec/models/spree/stock_location_decorator_spec.rb @@ -1,23 +1,21 @@ -require 'spec_helper' +# frozen_string_literal: true -describe Spree::StockLocation do - - it { should belong_to(:supplier) } +RSpec.describe Spree::StockLocation do + it { is_expected.to respond_to(:supplier) } subject { create(:stock_location, backorderable_default: true) } - context "propagate variants" do - + context 'propagate variants' do let(:variant) { build(:variant) } let(:stock_item) { subject.propagate_variant(variant) } - context "passes backorderable default config" do - context "true" do + context 'passes backorderable default config' do + context 'true' do before { subject.backorderable_default = true } xit { expect(stock_item.backorderable).to eq true } end - context "false" do + context 'false' do before { subject.backorderable_default = false } xit { expect(stock_item.backorderable).to eq false } end @@ -27,7 +25,5 @@ before { subject.supplier_id = create(:supplier).id } it { expect(stock_item).to be_nil } end - end - end diff --git a/spec/models/spree/supplier_ability_spec.rb b/spec/models/spree/supplier_ability_spec.rb index 03d5d3f..04c32c9 100644 --- a/spec/models/spree/supplier_ability_spec.rb +++ b/spec/models/spree/supplier_ability_spec.rb @@ -1,11 +1,12 @@ -require 'spec_helper' +# frozen_string_literal: true + +require 'cancan' require 'cancan/matchers' require 'spree/testing_support/ability_helpers' -describe Spree::SupplierAbility do - +RSpec.describe Spree::SupplierAbility do let(:supplier) { create(:supplier) } - let(:supplier_admin_role) { build :role, name: "supplier_admin" } + let(:supplier_admin_role) { build(:role, name: 'supplier_admin') } let(:user) { create(:user, supplier: supplier) } let(:ability) { Spree::SupplierAbility.new(user) } let(:token) { nil } @@ -33,7 +34,7 @@ end it_should_behave_like 'index allowed' - it_should_behave_like 'read and update access granted' + it_should_behave_like 'access granted' it_should_behave_like 'admin granted' context 'requested by another suppliers user' do @@ -51,7 +52,9 @@ product.add_supplier!(user.supplier) product.reload } - # it_should_behave_like 'access granted' + + it_should_behave_like 'access granted' + it { expect(ability).to be_able_to :read, resource } it { expect(ability).to be_able_to :stock, resource } end @@ -59,29 +62,40 @@ context 'for Shipment' do context 'requested by another suppliers user' do - let(:resource) { Spree::Shipment.new({stock_location: create(:stock_location, supplier: create(:supplier))}) } + let(:resource) do + Spree::Shipment.new({stock_location: create(:stock_location, + supplier: create(:supplier))}) + end + it_should_behave_like 'access denied' it_should_behave_like 'no index allowed' it_should_behave_like 'admin denied' + it { expect(ability).to_not be_able_to :ready, resource } it { expect(ability).to_not be_able_to :ship, resource } end context 'requested by suppliers user' do context 'when order is complete' do - let(:resource) { + let(:resource) do order = create(:completed_order_from_supplier_with_totals) order.stock_locations.first.update_attribute :supplier, user.supplier Spree::Shipment.new({order: order, stock_location: order.stock_locations.first }) - } + end + it_should_behave_like 'read only' it_should_behave_like 'index allowed' it_should_behave_like 'admin granted' end context 'when order is incomplete' do - let(:resource) { Spree::Shipment.new({stock_location: create(:stock_location, supplier: user.supplier)}) } + let(:resource) do + Spree::Shipment.new({stock_location: create(:stock_location, + supplier: user.supplier)}) + end + it_should_behave_like 'access denied' + it { expect(ability).to_not be_able_to :ready, resource } it { expect(ability).to_not be_able_to :ship, resource } end @@ -125,12 +139,13 @@ it_should_behave_like 'access denied' end - context 'requested by suppliers user' do + xcontext 'requested by suppliers user' do let(:resource) { variant = create(:product).master variant.product.add_supplier! user.supplier user.supplier.stock_locations.first } + it_should_behave_like 'access granted' it_should_behave_like 'admin granted' it_should_behave_like 'index allowed' @@ -148,7 +163,8 @@ supplier = create(:supplier) variant = create(:product).master variant.product.add_supplier! supplier - Spree::StockMovement.new({ stock_item: supplier.stock_locations.first.stock_items.first }) + Spree::StockMovement.new({ stock_item: supplier.stock_locations. + first.stock_items.first }) } it_should_behave_like 'admin denied' end @@ -156,8 +172,9 @@ context 'requested by suppliers user' do let(:resource) { variant = create(:product).master - variant.product.add_supplier! user.supplier - Spree::StockMovement.new({ stock_item: user.supplier.stock_locations.first.stock_items.first }) + variant.product.add_supplier!(user.supplier) + Spree::StockMovement.new({ stock_item: user.supplier.stock_locations. + first.stock_items.first }) } it_should_behave_like 'access granted' end @@ -172,11 +189,11 @@ it_should_behave_like 'access denied' end - context 'requested by suppliers user' do + xcontext 'requested by suppliers user' do let(:resource) { user.supplier } + it_should_behave_like 'admin granted' - it_should_behave_like 'read and update access granted' + it_should_behave_like 'access granted' end end - end diff --git a/spec/models/spree/supplier_spec.rb b/spec/models/spree/supplier_spec.rb index 557e052..52e5e53 100644 --- a/spec/models/spree/supplier_spec.rb +++ b/spec/models/spree/supplier_spec.rb @@ -1,16 +1,11 @@ -require 'spec_helper' +# frozen_string_literal: true -describe Spree::Supplier do - - it { should belong_to(:address) } - - it { should have_many(:products).through(:variants) } - it { should have_many(:stock_locations) } - it { should have_many(:users) } - it { should have_many(:variants).through(:supplier_variants) } - - it { should validate_presence_of(:email) } - it { should validate_presence_of(:name) } +RSpec.describe Spree::Supplier do + it { is_expected.to respond_to(:address) } + it { is_expected.to respond_to(:products) } + it { is_expected.to respond_to(:stock_locations) } + it { is_expected.to respond_to(:users) } + it { is_expected.to respond_to(:variants) } it '#deleted?' do subject.deleted_at = nil @@ -20,104 +15,90 @@ end context '#assign_user' do + let!(:supplier) { create(:supplier) } + let(:user) { supplier.user } - before do - @instance = build(:supplier) + it 'returns assigned user' do + expect(supplier.users).not_to be_empty + expect(supplier.users.first).to eql(user) end + end - it 'with user' do - expect(Spree.user_class).to_not receive(:find_by_email) - @instance.email = 'test@test.com' - @instance.users << create(:user) - @instance.save - end + context '#create_stock_location' do + let!(:supplier) { create(:supplier) } - it 'with existing user email' do - user = create(:user, email: 'test@test.com') - expect(Spree.user_class).to receive(:find_by_email).with(user.email).and_return(user) - @instance.email = user.email - @instance.save - expect(@instance.reload.users.first).to eql(user) + it 'returns created stock location' do + expect(Spree::StockLocation.count).to eql(1) + expect(Spree::StockLocation.active.count).to eql(1) + expect(Spree::StockLocation.first.country).to eql(supplier.address.country) + expect(Spree::StockLocation.first.supplier).to eql(supplier) end - - end - - it '#create_stock_location' do - expect(Spree::StockLocation.count).to eql(0) - supplier = create :supplier - expect(Spree::StockLocation.first.active).to be true - expect(Spree::StockLocation.first.country).to eql(supplier.address.country) - expect(Spree::StockLocation.first.supplier).to eql(supplier) end context '#send_welcome' do - - after do - SolidusMarketplace::Config[:send_supplier_email] = true - end - - before do - @instance = build(:supplier) - @mail_message = double('Mail::Message') - end + let(:supplier) { build(:supplier) } + let(:mail_message) { double('Mail::Message') } context 'with SolidusMarketplace::Config[:send_supplier_email] == false' do + before do + SolidusMarketplace::Config.send_supplier_email = false + end it 'should not send' do - SolidusMarketplace::Config[:send_supplier_email] = false expect { - expect(Spree::SupplierMailer).to_not receive(:welcome).with(an_instance_of(Integer)) + expect(Spree::SupplierMailer).to_not receive(:welcome). + with(an_instance_of(Integer)) } - @instance.save end - end context 'with SolidusMarketplace::Config[:send_supplier_email] == true' do + before do + SolidusMarketplace::Config.send_supplier_email = true + end it 'should send welcome email' do expect { - expect(Spree::SupplierMailer).to receive(:welcome).with(an_instance_of(Integer)) + expect(Spree::SupplierMailer).to receive(:welcome). + with(an_instance_of(Integer)) } - @instance.save end - end - end - it '#set_commission' do - SolidusMarketplace::Config.set default_commission_flat_rate: 1 - SolidusMarketplace::Config.set default_commission_percentage: 1 - supplier = create :supplier - SolidusMarketplace::Config.set default_commission_flat_rate: 0 - SolidusMarketplace::Config.set default_commission_percentage: 0 - # Default configuration is 0.0 for each. - expect(supplier.commission_flat_rate.to_f).to eql(1.0) - expect(supplier.commission_percentage.to_f).to eql(10.0) - # With custom commission applied. - supplier = create :supplier, commission_flat_rate: 123, commission_percentage: 25 - expect(supplier.commission_flat_rate).to eql(123.0) - expect(supplier.commission_percentage).to eql(25.0) - end + context '#set_commission' do + before do + SolidusMarketplace::Config.default_commission_flat_rate = 1 + SolidusMarketplace::Config.default_commission_percentage = 1 + end - describe '#shipments' do + it 'returns correct commission values' do + supplier = create(:supplier) + # Default configuration is 0.0 for each. + expect(supplier.commission_flat_rate.to_f).to eql(1.0) + expect(supplier.commission_percentage.to_f).to eql(10.0) + # With custom commission applied. + supplier.update_attributes(commission_flat_rate: 123, + commission_percentage: 25) + expect(supplier.commission_flat_rate).to eql(123.0) + expect(supplier.commission_percentage).to eql(25.0) + end + end + context '#shipments' do let!(:supplier) { create(:supplier) } + let(:stock_location_1){ supplier.stock_locations.first } + let(:stock_location_2){ create(:stock_location, supplier: supplier) } + let(:shipment_1){ create(:shipment) } + let(:shipment_2){ create(:shipment, stock_location: stock_location_1) } + let(:shipment_3){ create(:shipment) } + let(:shipment_4){ create(:shipment, stock_location: stock_location_2) } + let(:shipment_5){ create(:shipment) } + let(:shipment_6){ create(:shipment, stock_location: stock_location_1) } it 'should return shipments for suppliers stock locations' do - stock_location_1 = supplier.stock_locations.first - stock_location_2 = create(:stock_location, supplier: supplier) - shipment_1 = create(:shipment) - shipment_2 = create(:shipment, stock_location: stock_location_1) - shipment_3 = create(:shipment) - shipment_4 = create(:shipment, stock_location: stock_location_2) - shipment_5 = create(:shipment) - shipment_6 = create(:shipment, stock_location: stock_location_1) - - expect(supplier.shipments).to match_array([shipment_2, shipment_4, shipment_6]) + expect(supplier.shipments). + to match_array([shipment_2, shipment_4, shipment_6]) end - end - end diff --git a/spec/models/spree/supplier_variant_spec.rb b/spec/models/spree/supplier_variant_spec.rb index 7b29f81..c738652 100644 --- a/spec/models/spree/supplier_variant_spec.rb +++ b/spec/models/spree/supplier_variant_spec.rb @@ -1,5 +1,5 @@ -require 'spec_helper' +# frozen_string_literal: true -describe Spree::SupplierVariant do +RSpec.describe Spree::SupplierVariant do skip "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/models/spree/user_decorator_spec.rb b/spec/models/spree/user_decorator_spec.rb index 0c89c86..a923f37 100644 --- a/spec/models/spree/user_decorator_spec.rb +++ b/spec/models/spree/user_decorator_spec.rb @@ -1,28 +1,26 @@ -require 'spec_helper' +# frozen_string_literal: true -describe Spree.user_class do - - it { should belong_to(:supplier) } - - it { should have_many(:variants).through(:supplier) } +RSpec.describe Spree.user_class do + it { is_expected.to respond_to(:supplier) } + it { is_expected.to respond_to(:variants) } let(:user) { build :user } let(:admin_role) { build :admin_role } - let(:supplier_admin_role) { build :role, name: "supplier_admin" } + let(:supplier_admin_role) { build(:role, name: 'supplier_admin') } describe '#supplier?' do - it "returns true if user is a supplier" do + it 'returns true if user is a supplier' do user.supplier = build :supplier expect(user.supplier?).to eq true end - it "returns false if user is not a supplier" do + it 'returns false if user is not a supplier' do expect(user.supplier?).to eq false end end describe '#supplier_admin?' do - it "returns true if user has supplier admin role" do + it 'returns true if user has supplier admin role' do user.spree_roles << supplier_admin_role expect(user.supplier_admin?).to eq true end @@ -33,7 +31,7 @@ end describe '#market_maker?' do - it "returns true if user has an admin role" do + it 'returns true if user has an admin role' do user.spree_roles << admin_role expect(user.market_maker?).to eq true end @@ -43,15 +41,14 @@ end end - describe "#has_admin_role?" do - it "returns false if user is not an admin" do + describe '#has_admin_role?' do + it 'returns false if user is not an admin' do expect(user.has_admin_role?).to eq false end - it "returns true if user is an admin" do + it 'returns true if user is an admin' do user.spree_roles << admin_role expect(user.has_admin_role?).to eq true end end - end diff --git a/spec/models/spree/variant_decorator_spec.rb b/spec/models/spree/variant_decorator_spec.rb index c9d4c97..cfda9f0 100644 --- a/spec/models/spree/variant_decorator_spec.rb +++ b/spec/models/spree/variant_decorator_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' +# frozen_string_literal: true -describe Spree::Variant do +RSpec.describe Spree::Variant do it 'should populate stock item for each of the master variant suppliers' do skip 'todo' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 086b223..a771fe0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,91 +1,17 @@ -require 'coveralls' -Coveralls.wear! +# frozen_string_literal: true -# Configure Rails Environment ENV['RAILS_ENV'] = 'test' require File.expand_path('../dummy/config/environment.rb', __FILE__) require 'rspec/rails' -require 'database_cleaner' -require 'factory_girl' -FactoryGirl.find_definitions -require 'ffaker' -require 'shoulda-matchers' -# Requires supporting ruby files with custom matchers and macros, etc, -# in spec/support/ and its subdirectories. -Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f } - -require 'spree/testing_support/authorization_helpers' -require 'spree/testing_support/capybara_ext' -require 'spree/testing_support/controller_requests' -require 'spree/testing_support/factories' -require 'spree/testing_support/preferences' -require 'spree/testing_support/url_helpers' +Dir[File.join(File.dirname(__FILE__), '/support/**/*.rb')].each { |file| require file } require 'solidus_marketplace/factories' +require 'spree/testing_support/controller_requests' +require 'solidus_support/extension/feature_helper' RSpec.configure do |config| - config.include FactoryGirl::Syntax::Methods - config.include IntegrationHelpers - config.include Spree::TestingSupport::Preferences - - config.include Warden::Test::Helpers, type: :feature - config.include Devise::TestHelpers, type: :controller - config.before(:suite) { Warden.test_mode! } - config.after(:each) { Warden.test_reset! } - - # == URL Helpers - # - # Allows access to Spree's routes in specs: - # - # visit spree.admin_path - # current_path.should eql(spree.products_path) - config.include Spree::TestingSupport::UrlHelpers config.include Spree::TestingSupport::ControllerRequests, type: :controller - - # Capybara javascript drivers require transactional fixtures set to false, and we use DatabaseCleaner - # to cleanup after each test instead. Without transactional fixtures set to false the records created - # to setup a test will be unavailable to the browser, which runs under a seperate server instance. - config.use_transactional_fixtures = false - - # Ensure Suite is set to use transactions for speed. - config.before :suite do - DatabaseCleaner.strategy = :transaction - DatabaseCleaner.clean_with :truncation - end - - # Before each spec check if it is a Javascript test and switch between using database transactions or not where necessary. - config.before :each do - DatabaseCleaner.strategy = RSpec.current_example.metadata[:js] ? :truncation : :transaction - DatabaseCleaner.start - reset_spree_preferences - end - - # After each spec clean the database. - config.after :each do - DatabaseCleaner.clean - end - - # If true, the base class of anonymous controllers will be inferred - # automatically. This will be the default behavior in future versions of - # rspec-rails. - config.infer_base_class_for_anonymous_controllers = false - - config.infer_spec_type_from_file_location! - # Run specs in random order to surface order dependencies. If you find an - # order dependency and want to debug it, you can fix the order by providing - # the seed, which is printed after each run. - # --seed 1234 - # config.order = "random" - config.color = true - config.infer_spec_type_from_file_location! -end - -Shoulda::Matchers.configure do |config| - config.integrate do |with| - with.test_framework :rspec - with.library :rails - end end diff --git a/spec/support/ability_helpers.rb b/spec/support/ability_helpers.rb deleted file mode 100644 index 19b31df..0000000 --- a/spec/support/ability_helpers.rb +++ /dev/null @@ -1,11 +0,0 @@ -shared_examples_for 'read and update access granted' do - it 'should allow read' do - expect(ability).to be_able_to(:read, resource, token) if token - expect(ability).to be_able_to(:read, resource) unless token - end - - it 'should allow update' do - expect(ability).to be_able_to(:update, resource, token) if token - expect(ability).to be_able_to(:update, resource) unless token - end -end diff --git a/spec/support/integration_helpers.rb b/spec/support/integration_helpers.rb deleted file mode 100644 index 7808db9..0000000 --- a/spec/support/integration_helpers.rb +++ /dev/null @@ -1,15 +0,0 @@ -module IntegrationHelpers - - def login_user(user = nil, options = {}) - options[:password] ||= 'secret' - user ||= create(:user, password: options[:password]) - - visit spree.root_path - click_link 'Login' - fill_in 'spree_user[email]', with: user.email - fill_in 'spree_user[password]', with: options[:password] - click_button 'Login' - expect(page).to_not have_content 'Login' - end - -end From 4d63824aa6c206109571dc405f34c94ee4e6f87d Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Fri, 20 Mar 2020 16:54:41 -0600 Subject: [PATCH 02/32] Improve user and supplier model dependencies One supplier must have an admin, and one supplier belongs to a user, but that is optional --- app/models/solidus_marketplace/payment_decorator.rb | 2 +- .../solidus_marketplace/stock_location_decorator.rb | 2 +- app/models/solidus_marketplace/user_decorator.rb | 8 +++++++- app/models/spree/supplier.rb | 6 ++++-- app/views/spree/admin/suppliers/_form.html.erb | 2 +- lib/tasks/spree_sample.rake | 7 +++++-- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/models/solidus_marketplace/payment_decorator.rb b/app/models/solidus_marketplace/payment_decorator.rb index 8743877..85f4307 100644 --- a/app/models/solidus_marketplace/payment_decorator.rb +++ b/app/models/solidus_marketplace/payment_decorator.rb @@ -3,7 +3,7 @@ module PaymentDecorator extend ActiveSupport::Concern included do - belongs_to :payable, polymorphic: true + belongs_to :payable, polymorphic: true, optional: true end end end diff --git a/app/models/solidus_marketplace/stock_location_decorator.rb b/app/models/solidus_marketplace/stock_location_decorator.rb index 098c1b0..b337005 100644 --- a/app/models/solidus_marketplace/stock_location_decorator.rb +++ b/app/models/solidus_marketplace/stock_location_decorator.rb @@ -4,7 +4,7 @@ module StockLocationDecorator included do prepend(InstanceMethods) - belongs_to :supplier, class_name: 'Spree::Supplier' + belongs_to :supplier, class_name: 'Spree::Supplier', optional: true scope :by_supplier, -> (supplier_id) { where(supplier_id: supplier_id) } end diff --git a/app/models/solidus_marketplace/user_decorator.rb b/app/models/solidus_marketplace/user_decorator.rb index 4b01695..eb9b07a 100644 --- a/app/models/solidus_marketplace/user_decorator.rb +++ b/app/models/solidus_marketplace/user_decorator.rb @@ -4,8 +4,10 @@ module UserDecorator included do prepend(InstanceMethods) - belongs_to :supplier, class_name: 'Spree::Supplier' + belongs_to :supplier, class_name: 'Spree::Supplier', optional: true has_many :variants, through: :supplier + + after_create :check_for_api_token end module InstanceMethods @@ -24,6 +26,10 @@ def market_maker? def has_admin_role? spree_roles.map(&:name).include?("admin") end + + def check_for_api_token + generate_spree_api_key! if supplier_admin? || supplier? + end end end end diff --git a/app/models/spree/supplier.rb b/app/models/spree/supplier.rb index af99902..6667601 100644 --- a/app/models/spree/supplier.rb +++ b/app/models/spree/supplier.rb @@ -5,7 +5,7 @@ class Supplier < Spree::Base attr_accessor :password, :password_confirmation - belongs_to :user, class_name: Spree.user_class.to_s + belongs_to :user, class_name: Spree.user_class.to_s, optional: true belongs_to :admin, class_name: Spree.user_class.to_s belongs_to :address, class_name: 'Spree::Address' accepts_nested_attributes_for :address @@ -35,6 +35,8 @@ class Supplier < Spree::Base after_create :create_stock_location after_create :send_welcome, if: -> { SolidusMarketplace::Config[:send_supplier_email] } + self.whitelisted_ransackable_attributes = %w[name] + scope :active, -> { where(active: true) } def deleted? @@ -67,7 +69,7 @@ def stock_locations_with_available_stock_items(variant) def assign_user if self.users.empty? - self.users << self.user + self.users << self.admin self.save end end diff --git a/app/views/spree/admin/suppliers/_form.html.erb b/app/views/spree/admin/suppliers/_form.html.erb index 6f0a8a9..519592d 100644 --- a/app/views/spree/admin/suppliers/_form.html.erb +++ b/app/views/spree/admin/suppliers/_form.html.erb @@ -25,7 +25,7 @@
    <%= f.field_container :user do %> <%= f.label :user_admin %> - <%= hidden_field_tag 'supplier[user_id]', + <%= hidden_field_tag 'supplier[admin_id]', f.object.user_ids.join(','), class: 'user_picker fullwidth' %> <% end %> diff --git a/lib/tasks/spree_sample.rake b/lib/tasks/spree_sample.rake index 3c37d89..45eebd6 100644 --- a/lib/tasks/spree_sample.rake +++ b/lib/tasks/spree_sample.rake @@ -40,9 +40,12 @@ namespace :spree_sample do puts 'Creating Suppliers...' 5.times{|i| name = "Supplier #{count + i + 1}" + user = Spree::User.create(email: "supplier#{i}@example.com", password: 'test123') + user.spree_roles << Spree::Role.first supplier = Spree::Supplier.new(name: name, - user_id: 1, - url: 'http://example.com') + user_id: user.id, + url: 'http://example.com', + admin: user) supplier.build_address(firstname: name, lastname: name, address1: '100 State St', From b398f0f8986948089ca1fcab1df4973bae6297cf Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Tue, 24 Mar 2020 15:52:28 -0600 Subject: [PATCH 03/32] Refactoring supplier ability It now follows soildus pattern to define new role abilities --- .../spree/permission_sets/supplier_ability.rb | 36 ++++++++++++++++++ app/models/spree/supplier_ability.rb | 38 ------------------- lib/solidus_marketplace/engine.rb | 2 +- 3 files changed, 37 insertions(+), 39 deletions(-) create mode 100644 app/models/spree/permission_sets/supplier_ability.rb delete mode 100644 app/models/spree/supplier_ability.rb diff --git a/app/models/spree/permission_sets/supplier_ability.rb b/app/models/spree/permission_sets/supplier_ability.rb new file mode 100644 index 0000000..c07b726 --- /dev/null +++ b/app/models/spree/permission_sets/supplier_ability.rb @@ -0,0 +1,36 @@ +module Spree + module PermissionSets + class SupplierAbility < PermissionSets::Base + + def activate! + can [:admin, :update, :read, :display, :stock], Spree::Product, suppliers: {id: user.supplier_id} + can [:admin, :create], Spree::Product + can [:admin, :create, :update, :destroy, :display], Spree::Variant, suppliers: {id: user.supplier_id} + + can [:admin, :display, :index], Spree::Shipment, order: {state: 'complete'}, + stock_location: {supplier_id: user.supplier_id} + + can [:admin, :display], Spree::ReturnAuthorization, stock_location: {supplier_id: user.supplier_id} + can [:admin, :display], Spree::CustomerReturn, stock_location: {supplier_id: user.supplier_id} + + #FIXME: come back to these when we work on shipping-related issues + # can [:admin, :manage, :read, :ready, :ship], Spree::Shipment, order: { state: 'complete' }, stock_location: { supplier_id: user.supplier_id } + # can [:admin, :create, :update], :stock_items + can [:admin, :index, :create, :edit, :read, :update], Spree::StockItem, stock_location: {supplier_id: user.supplier_id} + can [:admin, :manage, :create], Spree::StockLocation, supplier_id: user.supplier_id + can [:admin, :manage, :create], Spree::StockMovement, stock_item: {stock_location: {supplier_id: user.supplier_id}} + + can [:admin, :create, :read, :update, :display], Spree::Supplier, id: user.supplier_id + cannot [:create], Spree::Supplier + cannot [:index], Spree::Supplier + + can [:display, :admin, :sales_total], :reports + + # can [:admin, :manage], Spree::User, supplier_id: user.supplier_id + can [:admin, :index, :edit, :update], Spree::Order, stock_locations: {supplier_id: user.supplier_id} + # can [:admin, :index], Spree::Order, supplier_ids: user.supplier_id + # can [:admin, :edit], Spree::Order, stock_locations: { supplier_id: user.supplier_id } + end + end + end +end \ No newline at end of file diff --git a/app/models/spree/supplier_ability.rb b/app/models/spree/supplier_ability.rb deleted file mode 100644 index 388e706..0000000 --- a/app/models/spree/supplier_ability.rb +++ /dev/null @@ -1,38 +0,0 @@ -module Spree - class SupplierAbility - include CanCan::Ability - - def initialize(user) - user ||= Spree.user_class.new - - if user.supplier_admin? - can [:admin, :update, :read, :display, :stock], Spree::Product, suppliers: { id: user.supplier_id } - can [:admin, :create], Spree::Product - can [:admin, :create, :update, :destroy, :display], Spree::Variant, suppliers: { id: user.supplier_id } - - can [:admin, :display, :index], Spree::Shipment, order: { state: 'complete' }, - stock_location: { supplier_id: user.supplier_id } - - can [:admin, :display], Spree::ReturnAuthorization, stock_location: { supplier_id: user.supplier_id } - can [:admin, :display], Spree::CustomerReturn, stock_location: { supplier_id: user.supplier_id } - - #FIXME: come back to these when we work on shipping-related issues - # can [:admin, :manage, :read, :ready, :ship], Spree::Shipment, order: { state: 'complete' }, stock_location: { supplier_id: user.supplier_id } - # can [:admin, :create, :update], :stock_items - can [:admin, :index, :create, :edit, :read, :update], Spree::StockItem, stock_location: { supplier_id: user.supplier_id } - can [:admin, :manage, :create], Spree::StockLocation, supplier_id: user.supplier_id - can [:admin, :manage, :create], Spree::StockMovement, stock_item: { stock_location: { supplier_id: user.supplier_id } } - - can [:admin, :create, :read, :update, :display], Spree::Supplier, id: user.supplier_id - cannot [:create], Spree::Supplier - cannot [:index], Spree::Supplier - - can [:display, :admin, :sales_total], :reports - - # can [:admin, :manage], Spree::User, supplier_id: user.supplier_id - can [:admin, :index, :edit], Spree::Order, stock_locations: { supplier_id: user.supplier_id } - end - - end - end -end diff --git a/lib/solidus_marketplace/engine.rb b/lib/solidus_marketplace/engine.rb index 85f3a12..5aeea1f 100644 --- a/lib/solidus_marketplace/engine.rb +++ b/lib/solidus_marketplace/engine.rb @@ -48,7 +48,7 @@ def self.activate Rails.configuration.cache_classes ? require(c) : load(c) end - Spree::Ability.register_ability(Spree::SupplierAbility) + Spree::Config.roles.assign_permissions :supplier_admin, ['Spree::PermissionSets::SupplierAbility'] end config.to_prepare(&method(:activate).to_proc) From 05773907719c706488d99936106f4a69b6a00a03 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Tue, 24 Mar 2020 15:52:53 -0600 Subject: [PATCH 04/32] Bringing back shipment views They were removed in this commit 869c3af but it looks like it is usefull information --- app/views/spree/admin/shipments/edit.html.erb | 24 +++++ .../spree/admin/shipments/index.html.erb | 93 +++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 app/views/spree/admin/shipments/edit.html.erb create mode 100644 app/views/spree/admin/shipments/index.html.erb diff --git a/app/views/spree/admin/shipments/edit.html.erb b/app/views/spree/admin/shipments/edit.html.erb new file mode 100644 index 0000000..3520af3 --- /dev/null +++ b/app/views/spree/admin/shipments/edit.html.erb @@ -0,0 +1,24 @@ +<% content_for :page_actions do %> + <%= button_link_to Spree.t(:back_to_shipments_list), spree.admin_shipments_path, icon: 'arrow-left' %> +<% end %> + +<% if spree_current_user.admin? && @shipment.supplier.present? %> +
    +
    + <%= Spree.t(:supplier_information) %> +
    + <%= Spree::Supplier.human_attribute_name(:name) %>: <%= @shipment.supplier.name %>
    + <%= Spree::Supplier.human_attribute_name(:email) %>: <%= @shipment.supplier.user.email %>
    + <%= Spree::Supplier.human_attribute_name(:url) %>: <%= link_to @shipment.supplier.url, @shipment.supplier.url if @shipment.supplier.url.present? %>
    +
    +
    + <%= Spree.t('contact_information') %>: +
    + <%= render partial: 'spree/shared/address', locals: { address: @shipment.supplier.address } %> +
    +
    +<% end %> + +
    + <%= render partial: "spree/admin/orders/shipment", locals: { order: @shipment.order, shipment: @shipment } %> +
    \ No newline at end of file diff --git a/app/views/spree/admin/shipments/index.html.erb b/app/views/spree/admin/shipments/index.html.erb new file mode 100644 index 0000000..c241b76 --- /dev/null +++ b/app/views/spree/admin/shipments/index.html.erb @@ -0,0 +1,93 @@ +<% admin_breadcrumb(plural_resource_name(Spree::Shipment)) %> + +<% content_for :table_filter_title do %> + <%= Spree.t(:search) %> +<% end %> + +<%# Update search form with new markup %> +<% content_for :table_filter do %> +
    + <%= search_form_for [:admin, @search] do |f| %> +
    +
    +
    + <%= label_tag :q_created_at_gt, Spree.t(:date_range) %> +
    +
    + <%= f.text_field :created_at_gt, :class => 'datepicker datepicker-from form-control', :value => params[:q][:created_at_gt], :placeholder => Spree.t(:start) %> +
    +
    + <%= f.text_field :created_at_lt, :class => 'datepicker datepicker-to form-control', :value => params[:q][:created_at_lt], :placeholder => Spree.t(:stop) %> +
    +
    +
    +
    +
    +
    +
    +
    + <%= label_tag nil, Spree.t(:shipment_number) %> + <%= f.text_field :number_cont , class: 'form-control js-quick-search-target'%> +
    +
    + +
    +
    + <%= label_tag nil, Spree.t(:status) %>
    + <%= f.select :state_eq, Spree::Shipment.state_machines[:state].states.collect {|s| [Spree.t("shipment_state.#{s.name}"), s.value]}, {:include_blank => true}, :class => 'select2' %> +
    +
    +
    + +
    + <%= button Spree.t(:filter_results), 'search' %> +
    + <% end %> +
    +<% end %> + +<%# This is a new paginate! %> +<%= paginate @shipments, theme: "solidus_admin" %> + +<% unless @shipments.empty? %> +
    <%= Spree.t('supplier_mailer.welcome.supplier_information') %><%= t('spree.supplier_mailer.welcome.supplier_information') %>
    - <%= Spree.t(:name) %>: <%= @supplier.name %>
    - <%= Spree.t(:email) %>: <%= mail_to @supplier.user.email %>
    + <%= t('spree.name') %>: <%= @supplier.name %>
    + <%= t('spree.email') %>: <%= mail_to @supplier.user.email %>
    -

    <%= raw Spree.t('supplier_mailer.welcome.thank_you_again', name: Spree::Store.default.name) %>

    +

    <%= raw t('spree.supplier_mailer.welcome.thank_you_again', name: Spree::Store.default.name) %>

    + + + + + + + + + + + + + + + <% @shipments.each do |shipment| %> + + + + + + + + + + + + <% end %> + +
    <%= sort_link @search, :created_at, Spree::Shipment.human_attribute_name(:created_at) %><%= sort_link @search, :number, Spree::Shipment.human_attribute_name(:number) %><%= sort_link @search, :state, Spree::Shipment.human_attribute_name(:state) %><%= sort_link @search, :item_cost, Spree::Shipment.human_attribute_name(:item_cost) %><%= sort_link @search, :shipping_total, Spree::Shipment.human_attribute_name(:cost) %><%= sort_link @search, :tax_total, Spree::Shipment.human_attribute_name(:tax_total) %><%= sort_link @search, :total, Spree::Shipment.human_attribute_name(:final_price) %><%= sort_link @search, :supplier_commission, Spree::Shipment.human_attribute_name(:supplier_commission) %>
    <%= l shipment.created_at.to_date %><%= link_to shipment.number, spree.edit_admin_shipment_path(shipment) %><%= Spree.t("shipment_state.#{shipment.state.downcase}") %> <%= shipment.display_item_cost.to_html %><%= shipment.display_cost.to_html %><%= Spree::Money.new(shipment.tax_total, currency: shipment.currency).to_html %><%= Spree::Money.new(shipment.final_price_with_items, currency: shipment.currency).to_html %><%= Spree::Money.new(shipment.supplier_commission, currency: shipment.currency).to_html %> + <%= link_to_edit_url spree.edit_admin_shipment_path(shipment), :title => "admin_edit_#{dom_id(shipment)}", :no_text => true %> +
    +<% else %> + <%# New alert 'Not Found' with i18n %> +
    + <%= Spree.t(:no_resource_found, resource: Spree::Shipment.model_name.human(count: :many)) %>, +
    +<% end %> + +<%# This is a new paginate! %> +<%= paginate @shipments, theme: "solidus_admin" %> \ No newline at end of file From cdbaf8d5ce2d232758ddc842df6e26b717e24c14 Mon Sep 17 00:00:00 2001 From: Omar Vazquez Date: Thu, 26 Mar 2020 09:46:54 -0600 Subject: [PATCH 05/32] Remove double deface addition to admin products form There were two deface overrides for the admin products form, this caused that the supplier ids for the product were ignored and only the previous ones were sent in the request, thus the product was not updated accordingly --- .../admin}/add_supplier_to_admin_product_form.rb | 0 .../spree/admin/add_supplier_to_admin_products.rb | 15 --------------- 2 files changed, 15 deletions(-) rename app/overrides/{ => spree/admin}/add_supplier_to_admin_product_form.rb (100%) delete mode 100644 app/overrides/spree/admin/add_supplier_to_admin_products.rb diff --git a/app/overrides/add_supplier_to_admin_product_form.rb b/app/overrides/spree/admin/add_supplier_to_admin_product_form.rb similarity index 100% rename from app/overrides/add_supplier_to_admin_product_form.rb rename to app/overrides/spree/admin/add_supplier_to_admin_product_form.rb diff --git a/app/overrides/spree/admin/add_supplier_to_admin_products.rb b/app/overrides/spree/admin/add_supplier_to_admin_products.rb deleted file mode 100644 index 069cc1f..0000000 --- a/app/overrides/spree/admin/add_supplier_to_admin_products.rb +++ /dev/null @@ -1,15 +0,0 @@ -Deface::Override.new( - virtual_path: "spree/admin/products/_form", - name: "add_supplier_to_admin_products", - insert_before: '[data-hook="admin_product_form_taxons"]', - text: "<% if try_spree_current_user.market_maker? %> -
    - <%= f.field_container :suppliers do %> - <%= f.label :supplier_ids, plural_resource_name(Spree::Supplier) %>
    - <%= f.hidden_field :supplier_ids, value: @product.supplier_ids.join(',') %> - <% end %> -
    - <% end %>", - disabled: false - -) From c8286a98cd41a8e9c346102b83ddf0e76316fbcc Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Thu, 26 Mar 2020 11:32:24 -0600 Subject: [PATCH 06/32] Fixing cancan ability to show stock locations for supplier admins Default customer shows all stock locations and it overrides the one defined in supplier admin, this commit first tells cancan not to be able to do the default customer thing then adds the righ one --- app/models/spree/permission_sets/supplier_ability.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/models/spree/permission_sets/supplier_ability.rb b/app/models/spree/permission_sets/supplier_ability.rb index c07b726..224a80a 100644 --- a/app/models/spree/permission_sets/supplier_ability.rb +++ b/app/models/spree/permission_sets/supplier_ability.rb @@ -17,7 +17,8 @@ def activate! # can [:admin, :manage, :read, :ready, :ship], Spree::Shipment, order: { state: 'complete' }, stock_location: { supplier_id: user.supplier_id } # can [:admin, :create, :update], :stock_items can [:admin, :index, :create, :edit, :read, :update], Spree::StockItem, stock_location: {supplier_id: user.supplier_id} - can [:admin, :manage, :create], Spree::StockLocation, supplier_id: user.supplier_id + cannot :display, Spree::StockLocation, active: true + can [:admin, :manage, :create], Spree::StockLocation, supplier_id: user.supplier_id, active: true can [:admin, :manage, :create], Spree::StockMovement, stock_item: {stock_location: {supplier_id: user.supplier_id}} can [:admin, :create, :read, :update, :display], Spree::Supplier, id: user.supplier_id @@ -27,7 +28,7 @@ def activate! can [:display, :admin, :sales_total], :reports # can [:admin, :manage], Spree::User, supplier_id: user.supplier_id - can [:admin, :index, :edit, :update], Spree::Order, stock_locations: {supplier_id: user.supplier_id} + can [:admin, :index, :edit, :update], Spree::Order, stock_locations: { supplier_id: user.supplier_id } # can [:admin, :index], Spree::Order, supplier_ids: user.supplier_id # can [:admin, :edit], Spree::Order, stock_locations: { supplier_id: user.supplier_id } end From 4f7687f074cd76c2c7b0c93c2dd7e85d2a539785 Mon Sep 17 00:00:00 2001 From: Sam Belmor Date: Mon, 23 Mar 2020 10:59:20 -0600 Subject: [PATCH 07/32] Fix when an admin user save a supplier - When an admin user creates a new product and adds a supplier and saves that product, the supplier is not being saved and the user has to establish a supplier again. --- .../admin/products_controller_decorator.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/app/controllers/solidus_marketplace/admin/products_controller_decorator.rb b/app/controllers/solidus_marketplace/admin/products_controller_decorator.rb index e8b2887..05980ce 100644 --- a/app/controllers/solidus_marketplace/admin/products_controller_decorator.rb +++ b/app/controllers/solidus_marketplace/admin/products_controller_decorator.rb @@ -26,6 +26,7 @@ def update_product_suppliers elsif removing_suppliers? supplier_ids = current_supplier_ids - new_supplier_ids @product.remove_suppliers!(supplier_ids) + @product.add_suppliers!(new_supplier_ids) if new_supplier_ids elsif same_number_of_suppliers? && different_suppliers? @product.remove_suppliers!(current_suppliers) @product.add_suppliers!(new_suppliers) @@ -78,10 +79,16 @@ def supplier_collection # Newly added products by a Supplier are associated with it. def add_product_to_supplier - if try_spree_current_user && try_spree_current_user.supplier? + if try_spree_current_user&.supplier? @product.add_supplier!(try_spree_current_user.supplier_id) + elsif user_admin? + @product.add_suppliers!(new_supplier_ids) end end + + def user_admin? + try_spree_current_user.admin? + end end end end From 576db3d2168a933987eabf5d4f28de9835d0b8a0 Mon Sep 17 00:00:00 2001 From: Omar Vazquez Date: Thu, 26 Mar 2020 13:27:27 -0600 Subject: [PATCH 08/32] Add translations for marketplace This adds the necessary spanish translations for the marketplace UI flow --- config/locales/es.yml | 200 +++++++++++++++++++----------------------- 1 file changed, 88 insertions(+), 112 deletions(-) diff --git a/config/locales/es.yml b/config/locales/es.yml index f560723..921206e 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -2,150 +2,126 @@ es: activerecord: attributes: spree/supplier: - commission_flat_rate: Tarifa fija de comisión - commission_percentage: Porcentaje de comisión name: Nombre - tax_id: TAX ID + tax_id: ID de impuestos (ej RFC) user_admin: Usuario administrador - users: Usuarios - address1: Dirección - address2: Dirección (continuación) - city: Ciudad - commission: Comisión - country: País - email: Email - state: Estado - active: Activo url: URL - zipcode: Código Postal(ZipCode) - supplier_info: Información de proveedor - supplier_signup: Registro de proveedor - suppliers: Proveedores - spree/shipment: - created_at: Creado - number: Número - state: Estatus - item_cost: Costo ítem - cost: Costo - total_tax: Total impuesto - final_price: Precio final - supplier_commission: Comisión Proveedor + models: + spree/supplier: + one: Proveedor + other: Proveedores errors: models: spree/supplier: attributes: url: - invalid: es inválido. Por favor asegúrece que 'http://' esté incluido - not_responding: 'es inválido, no responde o ha redireccionado a otra url' - marketplace_order_number: "Mercado Orden #%{number}" - marketplace_orders: Mercado Ordenes - marketplace_settings: Mercado Configuración - must_be_logged_in: 'Debe Estar Autenticado' - signup: Registro - signup_to_become_a_supplier: 'Autentíquese para ser un Proveedor' + invalid: es inválido. Por favor asegúrese que incluya 'http://' + not_responding: es inválido, no responde o redirecciona a otra ubicación spree: admin: user: payment_wallet: actions: - change_default_payment: Cambiar tarjeta por defecto - successfully_change_default_payment: Tarjeta camiada - error_change_default_payment: Ha habido un error cambiando la tarjeta - remove_card: Borrar tarjeta - successfully_card_removed: Tarjeta eliminada - error_card_removed: Ha habido un error borrando la tarjeta - error_add_card: Ha habido un error añadiendo la tarjeta + change_default_payment: Cambiar el método de pago predeterminado + successfully_change_default_payment: El método de pago predeterminado ha sido cambiada con éxito + error_change_default_payment: Ocurrió un error con el método de pago + remove_card: Remover tarjeta + successfully_card_removed: Tarjeta removida + error_card_removed: Ocurrió un problema removiendo la tarjeta + error_add_card: Ocurrió un problema agregando al carrito success_add_card: Tarjeta añadida - add_card: Añadir tarjeta - add: Añadir - wallets: Métodos de Pago + add_card: Agregar tarjeta + add: Agregar + wallets: Métodos de pago + tab: + stock_locations: Almacenes + suppliers: Proveedores + shipments: Envíos + profile: Perfil + earnings: Ganancias marketplace_settings: update: - success: Configuración mercado actualizada correctamente + success: Configuración de mercado actualizada correctamente orders: - marketplace_order_order_header: - marketplace_order_ids: DSO IDs edit: - approve_marketplace_orders: Ordenes aprobadas por el proveedor - resend_marketplace_orders: Reenviar ordenes al proveedor - marketplace_order_approval_confirmation: Se le enviará una notificación por correo electrónico a cada proveedor. ¿Desea continuar? + approve_marketplace_orders: Aprobar órdenes de mercado + resend_marketplace_orders: Reenviar órdenes de mercado + marketplace_order_approval_confirmation: Esto enviará una notificación por email a cada proveedor. ¿Continuar? show: - approve_marketplace_orders: Ordene aprobadas por el proveedor - resend_marketplace_orders: Reenviar ordenes al proveedor - marketplace_order_approval_confirmation: Se le enviará una notificación por correo electrónico a cada proveedor. ¿Desea continuar? - suppliers: - edit: - back_to_suppliers: Regresar a los proveedores - new: - new_supplier: Nuevo Proveedor - form: - supplier_details: Detalles del Proveedor - supplier_address: Dirección del Proveedor - marketplace_orders: - deliver: - error: "La orden %{number} no pudo ser enviada al proveedor." - success: "La orden %{number} fue enviada al proveedor." - orders_sent: Todas las ordenes fueron enviadas a los proveedores - orders_not_sent: Algunas o todas las ordenes no pudieron ser enviadas. Por favor contacte directamente al proveedor. - back_to_orders_list: Regresar a las ordenes - back_to_suppliers: Regresar a los proveedores + approve_marketplace_orders: Aprobar órdenes de mercado + resend_marketplace_orders: Reenviar órdenes de mercado + marketplace_order_approval_confirmation: Esto enviará una notificación por email a cada proveedor. ¿Continuar? + active: Activo + address: Dirección + admins: Administradores + back_to_orders_list: Volver a Lista de órdenes + back_to_suppliers: Volver a Proveedores business: Negocio cancel: Cancelar - company_info: Informacion de la compañia - confirm_order: Confirmar Orden + company_info: Información de empresa + confirm_order: Confirmar orden contact_address: Dirección de contacto - drop_ship: Drop Ship - editing_supplier: Editar proveedor - default_commission_flat_rate: Comision por defecto - default_commission_percentage: Porcentaje de comision por defecto - automatically_deliver_orders_to_supplier: Enviar orden automaticamente a los proveedores - send_supplier_email: Enviar correo al proveedor + automatically_deliver_orders_to_supplier: Entregar pedidos automáticamente al proveedor + send_supplier_email: Enviar email de bienvenida a nuevos proveedores + default_commission_percentage: Porcentaje de comisión predeterminado + default_commission_flat_rate: Tarifa de comisión predeterminada + drop_ship: Envío directo + earnings: Ganancias + earnings_description: Ganancias para proveedores en lapso de tiempo especificado + export_supplier_csv: Exportar ganancias como CSV + marketplace_order_mailer: + supplier_order: + hello: "Hola, %{name}," + subject: "%{name}, orden de mercado #%{number}" + thank_you_again: "Gracias de nuevo por confiar en nosotros, %{name}" + marketplace_order_number: "Orden de mercado #%{number}" + marketplace_orders: Órdenes de mercado + marketplace_settings: Configuración de mercado + editing_supplier: Editando proveedor + inactive: Incativo individual: Individual - listing_suppliers: Lista de proveedores - manage: Gestionar + manage: Administrar + must_be_logged_in: Debe iniciar sesión name: Nombre - or: ó - suppliers: Proveedores - user_admin: Usuario Administrador - remove_payment_method: Eliminar tarjeta - add_payment_method: Añadir tarjeta de pago - listing_shipments: 'Lista de envíos' + new_supplier: Nuevo proveedor + or: o order_state: - active: Activo - completed: Completado - confirmed: Confirmado - delivered: Entregado - resend_order_to_supplier: Reenviar orden al proveedor. + active: Activa + completed: Completada + confirmed: Confirmada + delivered: Entregada + resend_order_to_supplier: Reenviar orden a proveedor save: Guardar search: Buscar - send_order_to_supplier: Enviar orden al proveedor. - show_only_incomplete_orders: Solo mostrar ordenes incompletas - supplier: Proveedor - supplier_information: Información del proveedor - marketplace_order_mailer: - supplier_order: - hello: "Hola %{name}," - subject: "%{name} Orden al proveedor #%{number}" - thank_you_again: "Gracias nuevamente por su negocio, %{name}" + send_order_to_supplier: Enviar orden a proveedor shared: unauthorized: - explained: Usted no está autorizado a acceder a esta página. + explained: No está autorizado para acceder a esta página unauthorized: No autorizado + show_only_incomplete_orders: Mostrar solo órdenes incompletas + signup: Registrarse + signup_to_become_a_supplier: Regístrese para convertirse en proveedor + supplier: Proveedor + supplier_details: Detalles de proveedor + supplier_earnings: Ganancias de proveedor + supplier_information: Información de proveedor supplier_mailer: welcome: - hello: "Hola %{name}," - check_your_status: Puede revisar el estado de su pedido por - logging_into_your_account: Autentíquese. - once_your_request_is_approved: Una vez que su pedido sea aprobado podrá gestionar su inventario. - subject: 'Gracias por autenticarse. Por favor verifique la información.' - thank_you_again: "Gracias nuevamente por su negocio, %{name}" - thank_you_for_signing_up: Gracias por autenticarse para ser un proveedor. - suppliers: - already_signed_up: "Ya está autenticado para convertirse en un proveedor." - create: - success: Gracias por autenticarse! + hello: "Hola, %{name}," + logging_into_your_account: iniciando sesión en su cuenta + manage_your_account: Ahora puede administrar su perfil e inventario + subject: Gracias por registrarse. Por favor verifique su información. + thank_you_again: "Gracias de nuevo por confiar en nosotros, %{name}" + thank_you_for_signing_up: Gracias por registrarse como proveedor + supplier_paypal_email: Email asociado a PayPal + supplier_placeholder: Agregar proveedor supplier_registration: - already_signed_up: "Ya inicio sesión como proveedor." + already_signed_up: Ya se ha registrado para convertirse en proveedor create: - invalid_password: Clave incorrecta por favor inicie sesion. + invalid_password: Contraseña no válida, por favor inicie sesión o regístrese success: Gracias por registrarse! + supplier_signup: Registro de proveedor + suppliers: Proveedores + user_admin: Usuario administrador + remove_payment_method: Remover método de pago + add_payment_method: Agregar método de pago From 81df97368602b78951c5297a7df87adcc48ed5da Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Thu, 26 Mar 2020 15:31:25 -0600 Subject: [PATCH 09/32] Fixing issue when editting suppliers --- app/controllers/spree/admin/suppliers_controller.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/controllers/spree/admin/suppliers_controller.rb b/app/controllers/spree/admin/suppliers_controller.rb index 362b207..ca9b884 100644 --- a/app/controllers/spree/admin/suppliers_controller.rb +++ b/app/controllers/spree/admin/suppliers_controller.rb @@ -7,7 +7,6 @@ class SuppliersController < Spree::Admin::ResourceController private def set_address - address_attributes = permitted_resource_params.delete(:address_attributes) @object.address = Spree::Address.immutable_merge(@object.address, permitted_resource_params.delete(:address_attributes)) end From 8139432854e21520a22ea9d40faffd751cc74ba5 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Mon, 30 Mar 2020 16:22:26 -0600 Subject: [PATCH 10/32] Expiring product cache after assigning new supplier --- app/models/spree/supplier_variant.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/spree/supplier_variant.rb b/app/models/spree/supplier_variant.rb index cfdf0be..9d692da 100644 --- a/app/models/spree/supplier_variant.rb +++ b/app/models/spree/supplier_variant.rb @@ -1,6 +1,6 @@ module Spree class SupplierVariant < Spree::Base belongs_to :supplier - belongs_to :variant + belongs_to :variant, class_name: 'Spree::Variant', touch: true end end From 97df5aab8af4f86ffe8732d94186cae539036c6e Mon Sep 17 00:00:00 2001 From: mumoc Date: Tue, 31 Mar 2020 17:24:54 -0600 Subject: [PATCH 11/32] Enable admin creation through association --- app/models/spree/supplier.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/spree/supplier.rb b/app/models/spree/supplier.rb index 6667601..8e8e983 100644 --- a/app/models/spree/supplier.rb +++ b/app/models/spree/supplier.rb @@ -6,7 +6,7 @@ class Supplier < Spree::Base attr_accessor :password, :password_confirmation belongs_to :user, class_name: Spree.user_class.to_s, optional: true - belongs_to :admin, class_name: Spree.user_class.to_s + belongs_to :admin, class_name: Spree.user_class.to_s, optional: true belongs_to :address, class_name: 'Spree::Address' accepts_nested_attributes_for :address @@ -22,6 +22,7 @@ class Supplier < Spree::Base has_many :shipments, through: :stock_locations has_many :users, class_name: Spree.user_class.to_s has_many :admins, class_name: Spree.user_class.to_s + accepts_nested_attributes_for :admins validates :commission_flat_rate, presence: true validates :commission_percentage, presence: true From a1c22cf83986d03e772a921c0e8b3d985188ecba Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Wed, 8 Apr 2020 12:07:17 -0500 Subject: [PATCH 12/32] Adding image management abiliity to supplier role --- app/models/spree/permission_sets/supplier_ability.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/spree/permission_sets/supplier_ability.rb b/app/models/spree/permission_sets/supplier_ability.rb index 224a80a..1574449 100644 --- a/app/models/spree/permission_sets/supplier_ability.rb +++ b/app/models/spree/permission_sets/supplier_ability.rb @@ -31,6 +31,7 @@ def activate! can [:admin, :index, :edit, :update], Spree::Order, stock_locations: { supplier_id: user.supplier_id } # can [:admin, :index], Spree::Order, supplier_ids: user.supplier_id # can [:admin, :edit], Spree::Order, stock_locations: { supplier_id: user.supplier_id } + can [:admin, :manage, :create], Spree::Image end end end From 22365c838581fc58140ce3870b65f98a7a00cc85 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Thu, 9 Apr 2020 16:53:15 -0500 Subject: [PATCH 13/32] Allowing suppliers to manage variants --- app/models/spree/permission_sets/supplier_ability.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/spree/permission_sets/supplier_ability.rb b/app/models/spree/permission_sets/supplier_ability.rb index 1574449..42023fc 100644 --- a/app/models/spree/permission_sets/supplier_ability.rb +++ b/app/models/spree/permission_sets/supplier_ability.rb @@ -5,7 +5,7 @@ class SupplierAbility < PermissionSets::Base def activate! can [:admin, :update, :read, :display, :stock], Spree::Product, suppliers: {id: user.supplier_id} can [:admin, :create], Spree::Product - can [:admin, :create, :update, :destroy, :display], Spree::Variant, suppliers: {id: user.supplier_id} + can [:admin, :create, :update, :destroy, :display], Spree::Variant can [:admin, :display, :index], Spree::Shipment, order: {state: 'complete'}, stock_location: {supplier_id: user.supplier_id} From c982bc77c235333afee3daded1693a7602ff65e9 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Mon, 20 Apr 2020 15:54:01 -0500 Subject: [PATCH 14/32] Allowing supplier_admin to perform full actions in orders --- app/models/spree/permission_sets/supplier_ability.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/permission_sets/supplier_ability.rb b/app/models/spree/permission_sets/supplier_ability.rb index 42023fc..a5f6466 100644 --- a/app/models/spree/permission_sets/supplier_ability.rb +++ b/app/models/spree/permission_sets/supplier_ability.rb @@ -8,7 +8,7 @@ def activate! can [:admin, :create, :update, :destroy, :display], Spree::Variant can [:admin, :display, :index], Spree::Shipment, order: {state: 'complete'}, - stock_location: {supplier_id: user.supplier_id} + stock_location: { supplier_id: user.supplier_id } can [:admin, :display], Spree::ReturnAuthorization, stock_location: {supplier_id: user.supplier_id} can [:admin, :display], Spree::CustomerReturn, stock_location: {supplier_id: user.supplier_id} @@ -28,7 +28,7 @@ def activate! can [:display, :admin, :sales_total], :reports # can [:admin, :manage], Spree::User, supplier_id: user.supplier_id - can [:admin, :index, :edit, :update], Spree::Order, stock_locations: { supplier_id: user.supplier_id } + can [:admin, :index, :edit, :update, :cancel, :show, :cart, :resend, :fire], Spree::Order, stock_locations: { supplier_id: user.supplier_id } # can [:admin, :index], Spree::Order, supplier_ids: user.supplier_id # can [:admin, :edit], Spree::Order, stock_locations: { supplier_id: user.supplier_id } can [:admin, :manage, :create], Spree::Image From 19c52a1c7ae25a7747d087141ce8a3431689c5ce Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Wed, 22 Apr 2020 10:14:49 -0500 Subject: [PATCH 15/32] Adding more missing abilities to suplier admins --- app/models/spree/permission_sets/supplier_ability.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/permission_sets/supplier_ability.rb b/app/models/spree/permission_sets/supplier_ability.rb index a5f6466..085bf01 100644 --- a/app/models/spree/permission_sets/supplier_ability.rb +++ b/app/models/spree/permission_sets/supplier_ability.rb @@ -7,7 +7,7 @@ def activate! can [:admin, :create], Spree::Product can [:admin, :create, :update, :destroy, :display], Spree::Variant - can [:admin, :display, :index], Spree::Shipment, order: {state: 'complete'}, + can [:admin, :display, :index, :update, :edit], Spree::Shipment, order: {state: 'complete'}, stock_location: { supplier_id: user.supplier_id } can [:admin, :display], Spree::ReturnAuthorization, stock_location: {supplier_id: user.supplier_id} @@ -28,7 +28,7 @@ def activate! can [:display, :admin, :sales_total], :reports # can [:admin, :manage], Spree::User, supplier_id: user.supplier_id - can [:admin, :index, :edit, :update, :cancel, :show, :cart, :resend, :fire], Spree::Order, stock_locations: { supplier_id: user.supplier_id } + can [:admin, :index, :edit, :update, :cancel, :show, :cart, :resend, :fire, :approve], Spree::Order, stock_locations: { supplier_id: user.supplier_id } # can [:admin, :index], Spree::Order, supplier_ids: user.supplier_id # can [:admin, :edit], Spree::Order, stock_locations: { supplier_id: user.supplier_id } can [:admin, :manage, :create], Spree::Image From e6168c1476aa22e3692eca09f8ece47fb36b739d Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Wed, 29 Apr 2020 15:39:58 -0500 Subject: [PATCH 16/32] Add permission set if sales price extesion is present --- app/models/spree/permission_sets/supplier_ability.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/models/spree/permission_sets/supplier_ability.rb b/app/models/spree/permission_sets/supplier_ability.rb index 085bf01..46521c7 100644 --- a/app/models/spree/permission_sets/supplier_ability.rb +++ b/app/models/spree/permission_sets/supplier_ability.rb @@ -32,7 +32,10 @@ def activate! # can [:admin, :index], Spree::Order, supplier_ids: user.supplier_id # can [:admin, :edit], Spree::Order, stock_locations: { supplier_id: user.supplier_id } can [:admin, :manage, :create], Spree::Image + if defined?(Spree::SalePrice) + can [:admin, :manage, :create, :update], Spree::SalePrice + end end end end -end \ No newline at end of file +end From 78ef1c1efa1e5e2ef94083cc175bba9c771df931 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Thu, 14 May 2020 17:18:13 -0500 Subject: [PATCH 17/32] Suppliers should only see their variants in admin When restocking products, suppliers should only see their variants improving the experience of managing inventory --- .../admin/stock_items_controller_decorator.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb b/app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb index c787e52..bdebf56 100644 --- a/app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb +++ b/app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb @@ -15,6 +15,14 @@ def load_supplier_stock_location @stock_item_stock_locations = params[:stock_location_id].present? ? @stock_locations.where(id: params[:stock_location_id]) : @stock_locations end end + + def variant_scope + scope = super + if try_spree_current_user.supplier + scope = scope.joins(:stock_locations).where(spree_stock_locations: {supplier_id: spree_current_user.supplier.id}) + end + scope + end end end end From d93ad3adf5fe0e38ec9b61ce50eac49037099a12 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Tue, 19 May 2020 22:09:23 -0500 Subject: [PATCH 18/32] Fixing whitelisted ransackable attributes, it had only name It allows to sort by name and active, but name was the only one whitelisted --- app/controllers/spree/admin/suppliers_controller.rb | 3 +-- app/models/spree/supplier.rb | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/spree/admin/suppliers_controller.rb b/app/controllers/spree/admin/suppliers_controller.rb index ca9b884..bceed4b 100644 --- a/app/controllers/spree/admin/suppliers_controller.rb +++ b/app/controllers/spree/admin/suppliers_controller.rb @@ -17,9 +17,8 @@ def build_address def collection params[:q] ||= {} - params[:q][:meta_sort] ||= 'name.asc' @search = Spree::Supplier.search(params[:q]) - @collection = @search.result.page(params[:page]). + @collection = @search.result.includes(:admins, :users).page(params[:page]). per(Spree::Config[:orders_per_page]) end diff --git a/app/models/spree/supplier.rb b/app/models/spree/supplier.rb index 8e8e983..25047e8 100644 --- a/app/models/spree/supplier.rb +++ b/app/models/spree/supplier.rb @@ -36,7 +36,7 @@ class Supplier < Spree::Base after_create :create_stock_location after_create :send_welcome, if: -> { SolidusMarketplace::Config[:send_supplier_email] } - self.whitelisted_ransackable_attributes = %w[name] + self.whitelisted_ransackable_attributes = %w[name active] scope :active, -> { where(active: true) } From 4b03a95062a90eb5a0be4f7a96b23e1919fc4983 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Wed, 27 May 2020 12:06:51 -0500 Subject: [PATCH 19/32] Destroying supplier variants and stock items when removing a supplier as well Orphan data was being an issue when trying to buy a product that was used to be sold by a deleted supplier --- app/models/spree/supplier.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/spree/supplier.rb b/app/models/spree/supplier.rb index 25047e8..74120e9 100644 --- a/app/models/spree/supplier.rb +++ b/app/models/spree/supplier.rb @@ -15,10 +15,10 @@ class Supplier < Spree::Base has_many :ckeditor_attachment_files end - has_many :supplier_variants + has_many :supplier_variants, dependent: :destroy has_many :variants, through: :supplier_variants has_many :products, through: :variants - has_many :stock_locations + has_many :stock_locations, dependent: :destroy has_many :shipments, through: :stock_locations has_many :users, class_name: Spree.user_class.to_s has_many :admins, class_name: Spree.user_class.to_s From 290ba8203b58053bcdd265de046a5ad4a2bfc43d Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Wed, 27 May 2020 15:10:35 -0500 Subject: [PATCH 20/32] Improving marketplace splitter It now handles the ase when a product can be sold by multiple suppliers and also by master store, specifically when none of the suppliers can fulfill it and only the master store --- app/models/spree/stock/splitter/marketplace.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/models/spree/stock/splitter/marketplace.rb b/app/models/spree/stock/splitter/marketplace.rb index 0d261fb..60fe9aa 100644 --- a/app/models/spree/stock/splitter/marketplace.rb +++ b/app/models/spree/stock/splitter/marketplace.rb @@ -10,13 +10,17 @@ def split(packages) # Package fulfilled items together. fulfilled = package.contents.select { |content| begin - content.variant.suppliers.count == 0 + content.variant.suppliers.count == 0 || + content.variant.suppliers.detect { |supplier| supplier.stock_locations_with_available_stock_items(content.variant).empty? } rescue => e end } split_packages << build_package(fulfilled) # Determine which supplier to package shipped items. - supplier_contents = package.contents.select { |content| content.variant.suppliers.count > 0 } + supplier_contents = package.contents.select do |content| + content.variant.suppliers.count > 0 && + content.variant.suppliers.detect { |supplier| supplier.stock_locations_with_available_stock_items(content.variant).any? } + end supplier_contents.each do |content| # Select the related variant variant = content.variant From e9055ade00955807073d2d7420d82e559ccc2bc3 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Thu, 18 Jun 2020 12:49:11 -0500 Subject: [PATCH 21/32] Improving checking in asset decorator Sometimes Ckeditor module actually exists but the Assets class does not, this makes sure it is checked correctly --- app/models/ckeditor/asset_decorator.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/ckeditor/asset_decorator.rb b/app/models/ckeditor/asset_decorator.rb index 0595df1..41fa363 100644 --- a/app/models/ckeditor/asset_decorator.rb +++ b/app/models/ckeditor/asset_decorator.rb @@ -1,4 +1,4 @@ -if defined?(Ckeditor) +if defined?(Ckeditor::Asset) Ckeditor::Asset.class_eval do belongs_to :supplier, class_name: 'Spree::Supplier' end From 2b0937dc6503dd77ef8187fc362a4bf209995ae9 Mon Sep 17 00:00:00 2001 From: Mayank Dedhia Date: Wed, 22 Jul 2020 21:06:08 +0530 Subject: [PATCH 22/32] Create sample supplier users with role "supplier_admin" instead of relying on what the first role is --- lib/tasks/spree_sample.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/spree_sample.rake b/lib/tasks/spree_sample.rake index 45eebd6..b769067 100644 --- a/lib/tasks/spree_sample.rake +++ b/lib/tasks/spree_sample.rake @@ -41,7 +41,7 @@ namespace :spree_sample do 5.times{|i| name = "Supplier #{count + i + 1}" user = Spree::User.create(email: "supplier#{i}@example.com", password: 'test123') - user.spree_roles << Spree::Role.first + user.spree_roles << Spree::Role.find_by(name: "supplier_admin") supplier = Spree::Supplier.new(name: name, user_id: user.id, url: 'http://example.com', From 7cc92884e754e298b40d19fe6b10cbe1fce325f6 Mon Sep 17 00:00:00 2001 From: Jonathan Tapia Date: Fri, 24 Jul 2020 18:53:17 -0500 Subject: [PATCH 23/32] Upgrade extension to support rails 6 --- .circleci/config.yml | 35 ++++++++ .travis.yml | 19 ---- Gemfile | 29 +++++-- .../backend/solidus_marketplace_routes.js | 2 +- .../spree/backend/suppliers_autocomplete.js | 4 +- .../attachment_files_controller_decorator.rb | 20 ----- .../ckeditor/pictures_controller_decorator.rb | 17 ---- .../admin/users_controller_decorator.rb | 56 ------------ .../users_controller_decorator.rb | 53 ------------ .../admin/products_controller_decorator.rb | 1 - .../admin/reports_controller_decorator.rb | 3 - .../admin/stock_items_controller_decorator.rb | 1 - .../stock_locations_controller_decorator.rb | 1 - .../spree/admin/users_controller_decorator.rb | 1 - .../stock_locations_controller_decorator.rb | 1 - .../spree/base_controller_decorator.rb | 1 - .../attachment_files_controller_decorator.rb | 26 ++++++ .../ckeditor/pictures_controller_decorator.rb | 26 ++++++ .../admin/products_controller_decorator.rb | 25 +++--- .../admin/reports_controller_decorator.rb | 21 +++-- .../admin/stock_items_controller_decorator.rb | 19 ++-- .../stock_locations_controller_decorator.rb | 21 ++--- .../stock_locations_controller_decorator.rb | 21 ++--- .../spree}/base_controller_decorator.rb | 17 ++-- .../spree/api/api_helpers_decorator.rb | 27 ++++++ .../models/ckeditor/asset_decorator.rb | 13 +++ .../spree}/order_decorator.rb | 23 ++--- .../spree/payment_decorator.rb | 13 +++ .../spree}/product_decorator.rb | 25 +++--- .../spree}/shipment_decorator.rb | 25 +++--- .../spree}/stock_location_decorator.rb | 19 ++-- .../spree}/user_decorator.rb | 22 ++--- .../spree}/variant_decorator.rb | 23 ++--- .../spree/api/api_helpers_decorator.rb | 22 ----- app/models/ckeditor/asset_decorator.rb | 5 -- .../solidus_marketplace/payment_decorator.rb | 9 -- app/models/spree/order_decorator.rb | 1 - app/models/spree/payment_decorator.rb | 1 - .../spree/permission_sets/supplier_ability.rb | 44 +++++++--- app/models/spree/product_decorator.rb | 1 - app/models/spree/shipment_decorator.rb | 1 - app/models/spree/stock_location_decorator.rb | 1 - app/models/spree/supplier.rb | 5 +- app/models/spree/supplier_variant.rb | 2 + app/models/spree/user_decorator.rb | 1 - app/models/spree/variant_decorator.rb | 1 - lib/solidus_marketplace.rb | 3 + lib/solidus_marketplace/engine.rb | 33 ++++--- lib/solidus_marketplace/factories.rb | 86 +++++++++---------- .../permitted_attributes.rb | 4 + solidus_marketplace.gemspec | 17 ++-- spec/examples.txt | 73 ++++++++++++++++ spec/features/spree/admin/orders_spec.rb | 2 +- spec/features/spree/admin/products_spec.rb | 2 +- .../spree/admin/return_authorizations_spec.rb | 2 +- spec/features/spree/admin/settings_spec.rb | 2 +- spec/features/spree/admin/shipments_spec.rb | 2 +- .../spree/admin/stock_management_spec.rb | 2 +- spec/features/spree/admin/stock_spec.rb | 2 +- spec/features/spree/admin/suppliers_spec.rb | 2 +- spec/models/spree/order_decorator_spec.rb | 2 +- spec/models/spree/payment_decorator_spec.rb | 2 +- spec/models/spree/product_decorator_spec.rb | 4 +- spec/models/spree/shipment_decorator_spec.rb | 4 +- .../spree/stock/splitter/marketplace_spec.rb | 2 +- .../spree/stock_location_decorator_spec.rb | 2 +- spec/models/spree/supplier_ability_spec.rb | 24 +++--- spec/models/spree/supplier_spec.rb | 6 +- spec/models/spree/supplier_variant_spec.rb | 2 +- spec/models/spree/user_decorator_spec.rb | 2 +- spec/models/spree/variant_decorator_spec.rb | 2 +- spec/spec_helper.rb | 19 ++-- 72 files changed, 532 insertions(+), 475 deletions(-) create mode 100644 .circleci/config.yml delete mode 100644 .travis.yml delete mode 100644 app/controllers/ckeditor/attachment_files_controller_decorator.rb delete mode 100644 app/controllers/ckeditor/pictures_controller_decorator.rb delete mode 100644 app/controllers/solidus_marketplace/admin/users_controller_decorator.rb delete mode 100644 app/controllers/solidus_marketplace/users_controller_decorator.rb delete mode 100644 app/controllers/spree/admin/products_controller_decorator.rb delete mode 100644 app/controllers/spree/admin/reports_controller_decorator.rb delete mode 100644 app/controllers/spree/admin/stock_items_controller_decorator.rb delete mode 100644 app/controllers/spree/admin/stock_locations_controller_decorator.rb delete mode 100644 app/controllers/spree/admin/users_controller_decorator.rb delete mode 100644 app/controllers/spree/api/stock_locations_controller_decorator.rb delete mode 100644 app/controllers/spree/base_controller_decorator.rb create mode 100644 app/decorators/controllers/ckeditor/attachment_files_controller_decorator.rb create mode 100644 app/decorators/controllers/ckeditor/pictures_controller_decorator.rb rename app/{controllers/solidus_marketplace => decorators/controllers/solidus_marketplace/spree}/admin/products_controller_decorator.rb (83%) rename app/{controllers/solidus_marketplace => decorators/controllers/solidus_marketplace/spree}/admin/reports_controller_decorator.rb (85%) rename app/{controllers/solidus_marketplace => decorators/controllers/solidus_marketplace/spree}/admin/stock_items_controller_decorator.rb (71%) rename app/{controllers/solidus_marketplace => decorators/controllers/solidus_marketplace/spree}/admin/stock_locations_controller_decorator.rb (71%) rename app/{controllers/solidus_marketplace => decorators/controllers/solidus_marketplace/spree}/api/stock_locations_controller_decorator.rb (51%) rename app/{controllers/solidus_marketplace => decorators/controllers/solidus_marketplace/spree}/base_controller_decorator.rb (56%) create mode 100644 app/decorators/helpers/solidus_marketplace/spree/api/api_helpers_decorator.rb create mode 100644 app/decorators/models/ckeditor/asset_decorator.rb rename app/{models/solidus_marketplace => decorators/models/solidus_marketplace/spree}/order_decorator.rb (76%) create mode 100644 app/decorators/models/solidus_marketplace/spree/payment_decorator.rb rename app/{models/solidus_marketplace => decorators/models/solidus_marketplace/spree}/product_decorator.rb (67%) rename app/{models/solidus_marketplace => decorators/models/solidus_marketplace/spree}/shipment_decorator.rb (55%) rename app/{models/solidus_marketplace => decorators/models/solidus_marketplace/spree}/stock_location_decorator.rb (65%) rename app/{models/solidus_marketplace => decorators/models/solidus_marketplace/spree}/user_decorator.rb (57%) rename app/{models/solidus_marketplace => decorators/models/solidus_marketplace/spree}/variant_decorator.rb (53%) delete mode 100644 app/helpers/spree/api/api_helpers_decorator.rb delete mode 100644 app/models/ckeditor/asset_decorator.rb delete mode 100644 app/models/solidus_marketplace/payment_decorator.rb delete mode 100644 app/models/spree/order_decorator.rb delete mode 100644 app/models/spree/payment_decorator.rb delete mode 100644 app/models/spree/product_decorator.rb delete mode 100644 app/models/spree/shipment_decorator.rb delete mode 100644 app/models/spree/stock_location_decorator.rb delete mode 100644 app/models/spree/user_decorator.rb delete mode 100644 app/models/spree/variant_decorator.rb create mode 100644 spec/examples.txt diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..12bb10e --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,35 @@ +version: 2.1 + +orbs: + # Always take the latest version of the orb, this allows us to + # run specs against Solidus supported versions only without the need + # to change this configuration every time a Solidus version is released + # or goes EOL. + solidusio_extensions: solidusio/extensions@volatile + +jobs: + run-specs-with-postgres: + executor: solidusio_extensions/postgres + steps: + - solidusio_extensions/run-tests + run-specs-with-mysql: + executor: solidusio_extensions/mysql + steps: + - solidusio_extensions/run-tests + +workflows: + "Run specs on supported Solidus versions": + jobs: + - run-specs-with-postgres + - run-specs-with-mysql + "Weekly run specs against master": + triggers: + - schedule: + cron: "0 0 * * 4" # every Thursday + filters: + branches: + only: + - master + jobs: + - run-specs-with-postgres + - run-specs-with-mysql diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 9a20a26..0000000 --- a/.travis.yml +++ /dev/null @@ -1,19 +0,0 @@ -dist: trusty -cache: bundler -language: ruby -rvm: - - 2.4.6 -env: - matrix: - - SOLIDUS_BRANCH=v2.4 DB=postgres - - SOLIDUS_BRANCH=v2.5 DB=postgres - - SOLIDUS_BRANCH=v2.6 DB=postgres - - SOLIDUS_BRANCH=v2.7 DB=postgres - - SOLIDUS_BRANCH=v2.8 DB=postgres - - SOLIDUS_BRANCH=master DB=postgres - - SOLIDUS_BRANCH=v2.4 DB=mysql - - SOLIDUS_BRANCH=v2.5 DB=mysql - - SOLIDUS_BRANCH=v2.6 DB=mysql - - SOLIDUS_BRANCH=v2.7 DB=mysql - - SOLIDUS_BRANCH=v2.8 DB=mysql - - SOLIDUS_BRANCH=master DB=mysql diff --git a/Gemfile b/Gemfile index 14af998..ad05360 100644 --- a/Gemfile +++ b/Gemfile @@ -1,21 +1,40 @@ # frozen_string_literal: true source 'https://rubygems.org' +git_source(:github) { |repo| "https://github.com/#{repo}.git" } branch = ENV.fetch('SOLIDUS_BRANCH', 'master') gem 'solidus', github: 'solidusio/solidus', branch: branch -gem 'solidus_auth_devise', github: 'solidusio/solidus_auth_devise' + +# Needed to help Bundler figure out how to resolve dependencies, +# otherwise it takes forever to resolve them. +# See https://github.com/bundler/bundler/issues/6677 +gem 'rails', '>0.a' + +# Provides basic authentication functionality for testing parts of your engine +gem 'solidus_auth_devise' + # gem 'solidus_reports', github: 'solidusio-contrib/solidus_reports' -if ENV['DB'] == 'mysql' - gem 'mysql2', '~> 0.4.10' +case ENV['DB'] +when 'mysql' + gem 'mysql2' +when 'postgresql' + gem 'pg' else - gem 'pg', '~> 0.21' + gem 'sqlite3' end group :development, :test do - gem 'factory_bot', (branch < 'v2.5' ? '4.10.0' : '> 4.10.0') gem 'pry-rails' + gem 'byebug' 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' diff --git a/app/assets/javascripts/spree/backend/solidus_marketplace_routes.js b/app/assets/javascripts/spree/backend/solidus_marketplace_routes.js index 57e25b9..4732895 100644 --- a/app/assets/javascripts/spree/backend/solidus_marketplace_routes.js +++ b/app/assets/javascripts/spree/backend/solidus_marketplace_routes.js @@ -1 +1 @@ -Spree.routes.suppliers_search = Spree.pathFor('api/suppliers') \ No newline at end of file +suppliers_search = Spree.pathFor('api/suppliers') diff --git a/app/assets/javascripts/spree/backend/suppliers_autocomplete.js b/app/assets/javascripts/spree/backend/suppliers_autocomplete.js index 4d9bdbb..dc9ae7b 100644 --- a/app/assets/javascripts/spree/backend/suppliers_autocomplete.js +++ b/app/assets/javascripts/spree/backend/suppliers_autocomplete.js @@ -10,7 +10,7 @@ $.fn.supplierAutocomplete = function () { Spree.ajax({ type: "GET", - url: Spree.routes.suppliers_search, + url: suppliers_search, data: { ids: ids, per_page: count @@ -21,7 +21,7 @@ $.fn.supplierAutocomplete = function () { }); }, ajax: { - url: Spree.routes.suppliers_search, + url: suppliers_search, datatype: 'json', data: function (term, page) { return { diff --git a/app/controllers/ckeditor/attachment_files_controller_decorator.rb b/app/controllers/ckeditor/attachment_files_controller_decorator.rb deleted file mode 100644 index d80b78a..0000000 --- a/app/controllers/ckeditor/attachment_files_controller_decorator.rb +++ /dev/null @@ -1,20 +0,0 @@ -if defined?(Ckeditor::AttachmentFilesController) - Ckeditor::AttachmentFilesController.class_eval do - - load_and_authorize_resource :class => 'Ckeditor::AttachmentFile' - after_filter :set_supplier, only: [:create] - - def index - end - - private - - def set_supplier - if try_spree_current_user.supplier? and @attachment - @attachment.supplier = try_spree_current_user.supplier - @attachment.save - end - end - - end -end diff --git a/app/controllers/ckeditor/pictures_controller_decorator.rb b/app/controllers/ckeditor/pictures_controller_decorator.rb deleted file mode 100644 index f9b9cdd..0000000 --- a/app/controllers/ckeditor/pictures_controller_decorator.rb +++ /dev/null @@ -1,17 +0,0 @@ -if defined?(Ckeditor::PicturesController) - Ckeditor::PicturesController.class_eval do - load_and_authorize_resource :class => 'Ckeditor::Picture' - after_filter :set_supplier, only: [:create] - - def index - end - - private - def set_supplier - if spree_current_user.supplier? and @picture - @picture.supplier = spree_current_user.supplier - @picture.save - end - end - end -end diff --git a/app/controllers/solidus_marketplace/admin/users_controller_decorator.rb b/app/controllers/solidus_marketplace/admin/users_controller_decorator.rb deleted file mode 100644 index 73e313c..0000000 --- a/app/controllers/solidus_marketplace/admin/users_controller_decorator.rb +++ /dev/null @@ -1,56 +0,0 @@ -module SolidusMarketplace - module Admin - module UsersControllerDecorator - extend ActiveSupport::Concern - - included do - prepend(InstanceMethods) - end - - - module InstanceMethods - def create - current_supplier = try_spree_current_user.supplier - @user = Spree.user_class.new(user_params) - @user.supplier = current_supplier - - if @user.save - set_roles - set_stock_locations - - flash[:success] = t('spree.created_successfully') - redirect_to edit_admin_user_url(@user) - else - load_roles - load_stock_locations - - flash.now[:error] = @user.errors.full_messages.join(", ") - render :new, status: :unprocessable_entity - end - end - - def wallet - @payment_method = Spree::PaymentMethod.find_by(type: 'Spree::Gateway::StripeGateway', - deleted_at: nil) - end - - def removewallet - @payment_method = Spree::PaymentMethod.find_by(type: 'Spree::Gateway::StripeGateway', - deleted_at: nil) - end - - private - - def build_resource - current_supplier = try_spree_current_user.supplier - - if parent? - parent.send(controller_name).build - else - model_class.new(supplier: current_supplier) - end - end - end - end - end -end diff --git a/app/controllers/solidus_marketplace/users_controller_decorator.rb b/app/controllers/solidus_marketplace/users_controller_decorator.rb deleted file mode 100644 index b5cca7d..0000000 --- a/app/controllers/solidus_marketplace/users_controller_decorator.rb +++ /dev/null @@ -1,53 +0,0 @@ -module SolidusMarketplace - module UsersControllerDecorator - extend ActiveSupport::Concern - - included do - prepend(InstanceMethods) - end - - module InstanceMethods - def create - current_supplier = try_spree_current_user.supplier - @user = Spree.user_class.new(user_params) - @user.supplier = current_supplier - - if @user.save - set_roles - set_stock_locations - - flash[:success] = t('spree.created_successfully') - redirect_to edit_admin_user_url(@user) - else - load_roles - load_stock_locations - - flash.now[:error] = @user.errors.full_messages.join(", ") - render :new, status: :unprocessable_entity - end - end - - def wallet - @payment_method = Spree::PaymentMethod.find_by(type: 'Spree::Gateway::StripeGateway', - deleted_at: nil) - end - - def removewallet - @payment_method = Spree::PaymentMethod.find_by(type: 'Spree::Gateway::StripeGateway', - deleted_at: nil) - end - - private - - def build_resource - current_supplier = try_spree_current_user.supplier - - if parent? - parent.send(controller_name).build - else - model_class.new(supplier: current_supplier) - end - end - end - end -end diff --git a/app/controllers/spree/admin/products_controller_decorator.rb b/app/controllers/spree/admin/products_controller_decorator.rb deleted file mode 100644 index e779683..0000000 --- a/app/controllers/spree/admin/products_controller_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Admin::ProductsController.include(SolidusMarketplace::Admin::ProductsControllerDecorator) diff --git a/app/controllers/spree/admin/reports_controller_decorator.rb b/app/controllers/spree/admin/reports_controller_decorator.rb deleted file mode 100644 index 67532ce..0000000 --- a/app/controllers/spree/admin/reports_controller_decorator.rb +++ /dev/null @@ -1,3 +0,0 @@ -if defined?(SolidusReports::Engine) - Spree::Admin::ReportsController.include(SolidusMarketplace::Admin::ReportsControllerDecorator) -end diff --git a/app/controllers/spree/admin/stock_items_controller_decorator.rb b/app/controllers/spree/admin/stock_items_controller_decorator.rb deleted file mode 100644 index e2e729b..0000000 --- a/app/controllers/spree/admin/stock_items_controller_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Admin::StockItemsController.include(SolidusMarketplace::Admin::StockItemsControllerDecorator) diff --git a/app/controllers/spree/admin/stock_locations_controller_decorator.rb b/app/controllers/spree/admin/stock_locations_controller_decorator.rb deleted file mode 100644 index b42af02..0000000 --- a/app/controllers/spree/admin/stock_locations_controller_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Admin::StockLocationsController.include(SolidusMarketplace::Admin::StockLocationsControllerDecorator) diff --git a/app/controllers/spree/admin/users_controller_decorator.rb b/app/controllers/spree/admin/users_controller_decorator.rb deleted file mode 100644 index 80dc38b..0000000 --- a/app/controllers/spree/admin/users_controller_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Admin::UsersController.include(SolidusMarketplace::Admin::UsersControllerDecorator) diff --git a/app/controllers/spree/api/stock_locations_controller_decorator.rb b/app/controllers/spree/api/stock_locations_controller_decorator.rb deleted file mode 100644 index c95dc05..0000000 --- a/app/controllers/spree/api/stock_locations_controller_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Api::StockLocationsController.include(SolidusMarketplace::Api::StockLocationsControllerDecorator) diff --git a/app/controllers/spree/base_controller_decorator.rb b/app/controllers/spree/base_controller_decorator.rb deleted file mode 100644 index 64be38d..0000000 --- a/app/controllers/spree/base_controller_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::BaseController.include(SolidusMarketplace::BaseControllerDecorator) diff --git a/app/decorators/controllers/ckeditor/attachment_files_controller_decorator.rb b/app/decorators/controllers/ckeditor/attachment_files_controller_decorator.rb new file mode 100644 index 0000000..0996653 --- /dev/null +++ b/app/decorators/controllers/ckeditor/attachment_files_controller_decorator.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Ckeditor + module AttachmentFilesControllerDecorator + def self.prepended(base) + base.load_and_authorize_resource class: 'Ckeditor::AttachmentFile' + base.after_filter :set_supplier, only: [:create] + end + + def index + end + + private + + def set_supplier + if try_spree_current_user.supplier? and @attachment + @attachment.supplier = try_spree_current_user.supplier + @attachment.save + end + end + + if defined?(Ckeditor::AttachmentFilesController) + Ckeditor::AttachmentFilesController.prepend self + end + end +end diff --git a/app/decorators/controllers/ckeditor/pictures_controller_decorator.rb b/app/decorators/controllers/ckeditor/pictures_controller_decorator.rb new file mode 100644 index 0000000..cb635b8 --- /dev/null +++ b/app/decorators/controllers/ckeditor/pictures_controller_decorator.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Ckeditor + module PicturesControllerDecorator + def self.prepended(base) + base.load_and_authorize_resource class: 'Ckeditor::Picture' + base.after_filter :set_supplier, only: [:create] + end + + def index + end + + private + + def set_supplier + if spree_current_user.supplier? and @picture + @picture.supplier = spree_current_user.supplier + @picture.save + end + end + + if defined?(Ckeditor::PicturesController) + Ckeditor::PicturesController.prepend self + end + end +end diff --git a/app/controllers/solidus_marketplace/admin/products_controller_decorator.rb b/app/decorators/controllers/solidus_marketplace/spree/admin/products_controller_decorator.rb similarity index 83% rename from app/controllers/solidus_marketplace/admin/products_controller_decorator.rb rename to app/decorators/controllers/solidus_marketplace/spree/admin/products_controller_decorator.rb index 05980ce..02c7b7e 100644 --- a/app/controllers/solidus_marketplace/admin/products_controller_decorator.rb +++ b/app/decorators/controllers/solidus_marketplace/spree/admin/products_controller_decorator.rb @@ -1,17 +1,16 @@ +# frozen_string_literal: true + module SolidusMarketplace - module Admin - module ProductsControllerDecorator - extend ActiveSupport::Concern - - included do - prepend(InstanceMethods) - before_action :get_suppliers, only: [:edit] #, :update] - before_action :supplier_collection, only: [:index] - after_action :update_product_suppliers, only: [:update], unless: -> { params['product']['supplier_ids'].nil? } - create.after :add_product_to_supplier - end + module Spree + module Admin + module ProductsControllerDecorator + def self.prepended(base) + base.before_action :get_suppliers, only: [:edit] #, :update] + base.before_action :supplier_collection, only: [:index] + base.after_action :update_product_suppliers, only: [:update], unless: -> { params['product']['supplier_ids'].nil? } + base.after_action :add_product_to_supplier, only: [:create] + end - module InstanceMethods private def permitted_resource_params @@ -89,6 +88,8 @@ def add_product_to_supplier def user_admin? try_spree_current_user.admin? end + + ::Spree::Admin::ProductsController.prepend self end end end diff --git a/app/controllers/solidus_marketplace/admin/reports_controller_decorator.rb b/app/decorators/controllers/solidus_marketplace/spree/admin/reports_controller_decorator.rb similarity index 85% rename from app/controllers/solidus_marketplace/admin/reports_controller_decorator.rb rename to app/decorators/controllers/solidus_marketplace/spree/admin/reports_controller_decorator.rb index be908a2..b9ee37c 100644 --- a/app/controllers/solidus_marketplace/admin/reports_controller_decorator.rb +++ b/app/decorators/controllers/solidus_marketplace/spree/admin/reports_controller_decorator.rb @@ -1,16 +1,15 @@ +# frozen_string_literal: true + require 'csv' module SolidusMarketplace - module Admin - module ReportsControllerDecorator - extend ActiveSupport::Concern - - included do - prepend(InstanceMethods) - before_action :add_marketplace_reports, only: [:index] - end + module Spree + module Admin + module ReportsControllerDecorator + def self.prepended(base) + base.before_action :add_marketplace_reports, only: [:index] + end - module InstanceMethods def earnings @supplier_earnings = get_supplier_earnings @@ -78,6 +77,10 @@ def grouped_supplier_earnings grouped_earnings end + + if defined?(SolidusReports::Engine) + ::Spree::Admin::ReportsController.prepend self + end end end end diff --git a/app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb b/app/decorators/controllers/solidus_marketplace/spree/admin/stock_items_controller_decorator.rb similarity index 71% rename from app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb rename to app/decorators/controllers/solidus_marketplace/spree/admin/stock_items_controller_decorator.rb index bdebf56..e0cf2c2 100644 --- a/app/controllers/solidus_marketplace/admin/stock_items_controller_decorator.rb +++ b/app/decorators/controllers/solidus_marketplace/spree/admin/stock_items_controller_decorator.rb @@ -1,14 +1,13 @@ -module SolidusMarketplace - module Admin - module StockItemsControllerDecorator - extend ActiveSupport::Concern +# frozen_string_literal: true - included do - prepend(InstanceMethods) - before_action :load_supplier_stock_location, only: :index - end +module SolidusMarketplace + module Spree + module Admin + module StockItemsControllerDecorator + def self.prepended(base) + base.before_action :load_supplier_stock_location, only: :index + end - module InstanceMethods def load_supplier_stock_location if try_spree_current_user.supplier @stock_locations = Spree::StockLocation.by_supplier(try_spree_current_user.supplier).accessible_by(current_ability, :read) @@ -23,6 +22,8 @@ def variant_scope end scope end + + ::Spree::Admin::StockItemsController.prepend self end end end diff --git a/app/controllers/solidus_marketplace/admin/stock_locations_controller_decorator.rb b/app/decorators/controllers/solidus_marketplace/spree/admin/stock_locations_controller_decorator.rb similarity index 71% rename from app/controllers/solidus_marketplace/admin/stock_locations_controller_decorator.rb rename to app/decorators/controllers/solidus_marketplace/spree/admin/stock_locations_controller_decorator.rb index 8f45e49..579d777 100644 --- a/app/controllers/solidus_marketplace/admin/stock_locations_controller_decorator.rb +++ b/app/decorators/controllers/solidus_marketplace/spree/admin/stock_locations_controller_decorator.rb @@ -1,11 +1,12 @@ -module SolidusMarketplace - module Admin - module StockLocationsControllerDecorator - extend ActiveSupport::Concern +# frozen_string_literal: true - included do - prepend(InstanceMethods) - create.after :set_supplier +module SolidusMarketplace + module Spree + module Admin + module StockLocationsControllerDecorator + def self.prepended(base) + base.after_action :set_supplier, only: [:create] + end def index @stock_locations = Spree::StockLocation.accessible_by(current_ability, :read) @@ -15,9 +16,7 @@ def index .page(params[:page]) .per(params[:per_page]) end - end - - module InstanceMethods + private def set_supplier @@ -26,6 +25,8 @@ def set_supplier @object.save end end + + ::Spree::Admin::StockLocationsController.prepend self end end end diff --git a/app/controllers/solidus_marketplace/api/stock_locations_controller_decorator.rb b/app/decorators/controllers/solidus_marketplace/spree/api/stock_locations_controller_decorator.rb similarity index 51% rename from app/controllers/solidus_marketplace/api/stock_locations_controller_decorator.rb rename to app/decorators/controllers/solidus_marketplace/spree/api/stock_locations_controller_decorator.rb index 3cfdecf..d5b1b78 100644 --- a/app/controllers/solidus_marketplace/api/stock_locations_controller_decorator.rb +++ b/app/decorators/controllers/solidus_marketplace/spree/api/stock_locations_controller_decorator.rb @@ -1,15 +1,14 @@ -module SolidusMarketplace - module Api - module StockLocationsControllerDecorator - extend ActiveSupport::Concern +# frozen_string_literal: true - included do - prepend(InstanceMethods) - before_action :supplier_locations, only: [:index] - before_action :supplier_transfers, only: [:index] - end +module SolidusMarketplace + module Spree + module Api + module StockLocationsControllerDecorator + def self.prepended(base) + base.before_action :supplier_locations, only: [:index] + base.before_action :supplier_transfers, only: [:index] + end - module InstanceMethods private def supplier_locations @@ -21,6 +20,8 @@ def supplier_transfers params[:q] ||= {} params[:q][:supplier_id_eq] = spree_current_user.supplier_id end + + ::Spree::Api::StockLocationsController.prepend self end end end diff --git a/app/controllers/solidus_marketplace/base_controller_decorator.rb b/app/decorators/controllers/solidus_marketplace/spree/base_controller_decorator.rb similarity index 56% rename from app/controllers/solidus_marketplace/base_controller_decorator.rb rename to app/decorators/controllers/solidus_marketplace/spree/base_controller_decorator.rb index 6f5d67f..c517a04 100644 --- a/app/controllers/solidus_marketplace/base_controller_decorator.rb +++ b/app/decorators/controllers/solidus_marketplace/spree/base_controller_decorator.rb @@ -1,13 +1,12 @@ -module SolidusMarketplace - module BaseControllerDecorator - extend ActiveSupport::Concern +# frozen_string_literal: true - included do - prepend(InstanceMethods) - prepend_before_action :redirect_supplier - end +module SolidusMarketplace + module Spree + module BaseControllerDecorator + def self.prepended(base) + base.prepend_before_action :redirect_supplier + end - module InstanceMethods private def redirect_supplier @@ -15,6 +14,8 @@ def redirect_supplier redirect_to '/admin/shipments' and return false end end + + ::Spree::BaseController.prepend self end end end diff --git a/app/decorators/helpers/solidus_marketplace/spree/api/api_helpers_decorator.rb b/app/decorators/helpers/solidus_marketplace/spree/api/api_helpers_decorator.rb new file mode 100644 index 0000000..2c08be2 --- /dev/null +++ b/app/decorators/helpers/solidus_marketplace/spree/api/api_helpers_decorator.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module SolidusMarketplace + module Spree + module Api + module ApiHelpersDecorator + @@supplier_attributes = [ + :id, + :address_id, + :commission_flat_rate, + :commission_percentage, + :user_id, + :name, + :url, + :deleted_at, + :tax_id, + :token, + :slug + ] + + mattr_reader(:supplier_attributes) + + ::Spree::Api::ApiHelpers.prepend self + end + end + end +end diff --git a/app/decorators/models/ckeditor/asset_decorator.rb b/app/decorators/models/ckeditor/asset_decorator.rb new file mode 100644 index 0000000..291ce67 --- /dev/null +++ b/app/decorators/models/ckeditor/asset_decorator.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module Ckeditor + module AssetDecorator + def self.prepended(base) + base.belongs_to :supplier, class_name: '::Spree::Supplier' + end + + if defined?(Ckeditor::Asset) + Ckeditor::Asset.prepend self + end + end +end diff --git a/app/models/solidus_marketplace/order_decorator.rb b/app/decorators/models/solidus_marketplace/spree/order_decorator.rb similarity index 76% rename from app/models/solidus_marketplace/order_decorator.rb rename to app/decorators/models/solidus_marketplace/spree/order_decorator.rb index a8a6a3f..0a217f3 100644 --- a/app/models/solidus_marketplace/order_decorator.rb +++ b/app/decorators/models/solidus_marketplace/spree/order_decorator.rb @@ -1,19 +1,18 @@ -module SolidusMarketplace - module OrderDecorator - extend ActiveSupport::Concern +# frozen_string_literal: true - included do - prepend(InstanceMethods) - has_many :stock_locations, through: :shipments - has_many :suppliers, through: :stock_locations - end +module SolidusMarketplace + module Spree + module OrderDecorator + def self.prepended(base) + base.has_many :stock_locations, through: :shipments + base.has_many :suppliers, through: :stock_locations + end - module InstanceMethods def supplier_total(user_or_supplier) - supplier = user_or_supplier.is_a?(Spree::Supplier) ? user_or_supplier : user_or_supplier.supplier + supplier = user_or_supplier.is_a?(::Spree::Supplier) ? user_or_supplier : user_or_supplier.supplier shipments = self.shipments.by_supplier(supplier) commissions = shipments.map(&:supplier_commission_total) - Spree::Money.new(commissions.sum) + ::Spree::Money.new(commissions.sum) end def supplier_earnings_map @@ -45,6 +44,8 @@ def finalize_with_supplier! end end end + + ::Spree::Order.prepend self end end end diff --git a/app/decorators/models/solidus_marketplace/spree/payment_decorator.rb b/app/decorators/models/solidus_marketplace/spree/payment_decorator.rb new file mode 100644 index 0000000..8d9a7d7 --- /dev/null +++ b/app/decorators/models/solidus_marketplace/spree/payment_decorator.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module SolidusMarketplace + module Spree + module PaymentDecorator + def self.prepended(base) + base.belongs_to :payable, polymorphic: true, optional: true + end + + ::Spree::Payment.prepend self + end + end +end diff --git a/app/models/solidus_marketplace/product_decorator.rb b/app/decorators/models/solidus_marketplace/spree/product_decorator.rb similarity index 67% rename from app/models/solidus_marketplace/product_decorator.rb rename to app/decorators/models/solidus_marketplace/spree/product_decorator.rb index 54b961f..3d8f6c2 100644 --- a/app/models/solidus_marketplace/product_decorator.rb +++ b/app/decorators/models/solidus_marketplace/spree/product_decorator.rb @@ -1,27 +1,26 @@ -module SolidusMarketplace - module ProductDecorator - extend ActiveSupport::Concern +# frozen_string_literal: true - included do - prepend(InstanceMethods) - has_many :suppliers, -> { readonly }, through: :master - scope :of_supplier, -> (supplier_id) { joins(:suppliers).where('spree_suppliers.id = ?', supplier_id) } - end +module SolidusMarketplace + module Spree + module ProductDecorator + def self.prepended(base) + base.has_many :suppliers, -> { readonly }, through: :master + base.scope :of_supplier, -> (supplier_id) { joins(:suppliers).where('spree_suppliers.id = ?', supplier_id) } + end - module InstanceMethods def add_supplier!(supplier_or_id) - supplier = supplier_or_id.is_a?(Spree::Supplier) ? supplier_or_id : Spree::Supplier.find(supplier_or_id) + supplier = supplier_or_id.is_a?(::Spree::Supplier) ? supplier_or_id : ::Spree::Supplier.find(supplier_or_id) populate_for_supplier! supplier if supplier end def add_suppliers!(supplier_ids) - Spree::Supplier.where(id: supplier_ids).each do |supplier| + ::Spree::Supplier.where(id: supplier_ids).each do |supplier| populate_for_supplier! supplier end end def remove_suppliers!(supplier_ids) - Spree::Supplier.where(id: supplier_ids).each do |supplier| + ::Spree::Supplier.where(id: supplier_ids).each do |supplier| unpopulate_for_supplier! supplier end end @@ -53,6 +52,8 @@ def unpopulate_for_supplier!(supplier) end end end + + ::Spree::Product.prepend self end end end diff --git a/app/models/solidus_marketplace/shipment_decorator.rb b/app/decorators/models/solidus_marketplace/spree/shipment_decorator.rb similarity index 55% rename from app/models/solidus_marketplace/shipment_decorator.rb rename to app/decorators/models/solidus_marketplace/spree/shipment_decorator.rb index a5d6325..3eef385 100644 --- a/app/models/solidus_marketplace/shipment_decorator.rb +++ b/app/decorators/models/solidus_marketplace/spree/shipment_decorator.rb @@ -1,17 +1,16 @@ +# frozen_string_literal: true + module SolidusMarketplace - module ShipmentDecorator - extend ActiveSupport::Concern - - included do - prepend(InstanceMethods) - belongs_to :order, class_name: 'Spree::Order', touch: true, inverse_of: :shipments - has_many :payments, as: :payable - scope :by_supplier, -> (supplier_id) { joins(:stock_location).where(spree_stock_locations: { supplier_id: supplier_id }) } - delegate :supplier, to: :stock_location - self.whitelisted_ransackable_attributes = ["number", "state"] - end + module Spree + module ShipmentDecorator + def self.prepended(base) + base.belongs_to :order, class_name: '::Spree::Order', touch: true, inverse_of: :shipments + base.has_many :payments, as: :payable + base.scope :by_supplier, -> (supplier_id) { joins(:stock_location).where(spree_stock_locations: { supplier_id: supplier_id }) } + base.delegate :supplier, to: :stock_location + base.whitelisted_ransackable_attributes = ["number", "state"] + end - module InstanceMethods def display_final_price_with_items Spree::Money.new final_price_with_items end @@ -38,6 +37,8 @@ def after_ship def update_commission update_column(:supplier_commission, self.supplier_commission_total) end + + ::Spree::Shipment.prepend self end end end diff --git a/app/models/solidus_marketplace/stock_location_decorator.rb b/app/decorators/models/solidus_marketplace/spree/stock_location_decorator.rb similarity index 65% rename from app/models/solidus_marketplace/stock_location_decorator.rb rename to app/decorators/models/solidus_marketplace/spree/stock_location_decorator.rb index b337005..fe88c4d 100644 --- a/app/models/solidus_marketplace/stock_location_decorator.rb +++ b/app/decorators/models/solidus_marketplace/spree/stock_location_decorator.rb @@ -1,14 +1,13 @@ -module SolidusMarketplace - module StockLocationDecorator - extend ActiveSupport::Concern +# frozen_string_literal: true - included do - prepend(InstanceMethods) - belongs_to :supplier, class_name: 'Spree::Supplier', optional: true - scope :by_supplier, -> (supplier_id) { where(supplier_id: supplier_id) } - end +module SolidusMarketplace + module Spree + module StockLocationDecorator + def self.prepended(base) + base.belongs_to :supplier, class_name: '::Spree::Supplier', optional: true + base.scope :by_supplier, -> (supplier_id) { where(supplier_id: supplier_id) } + end - module InstanceMethods # Wrapper for creating a new stock item respecting the backorderable config and supplier def propagate_variant(variant) if self.supplier_id.blank? || variant.suppliers.pluck(:id).include?(self.supplier_id) @@ -24,6 +23,8 @@ def unpropagate_variant(variant) def available?(variant) stock_item(variant).try(:available?) end + + ::Spree::StockLocation.prepend self end end end diff --git a/app/models/solidus_marketplace/user_decorator.rb b/app/decorators/models/solidus_marketplace/spree/user_decorator.rb similarity index 57% rename from app/models/solidus_marketplace/user_decorator.rb rename to app/decorators/models/solidus_marketplace/spree/user_decorator.rb index eb9b07a..da7c94c 100644 --- a/app/models/solidus_marketplace/user_decorator.rb +++ b/app/decorators/models/solidus_marketplace/spree/user_decorator.rb @@ -1,16 +1,14 @@ -module SolidusMarketplace - module UserDecorator - extend ActiveSupport::Concern - - included do - prepend(InstanceMethods) - belongs_to :supplier, class_name: 'Spree::Supplier', optional: true - has_many :variants, through: :supplier +# frozen_string_literal: true - after_create :check_for_api_token - end +module SolidusMarketplace + module Spree + module UserDecorator + def self.prepended(base) + base.belongs_to :supplier, class_name: '::Spree::Supplier', optional: true + base.has_many :variants, through: :supplier + base.after_create :check_for_api_token + end - module InstanceMethods def supplier? supplier.present? end @@ -30,6 +28,8 @@ def has_admin_role? def check_for_api_token generate_spree_api_key! if supplier_admin? || supplier? end + + ::Spree.user_class.prepend self end end end diff --git a/app/models/solidus_marketplace/variant_decorator.rb b/app/decorators/models/solidus_marketplace/spree/variant_decorator.rb similarity index 53% rename from app/models/solidus_marketplace/variant_decorator.rb rename to app/decorators/models/solidus_marketplace/spree/variant_decorator.rb index 13c556a..9edefea 100644 --- a/app/models/solidus_marketplace/variant_decorator.rb +++ b/app/decorators/models/solidus_marketplace/spree/variant_decorator.rb @@ -1,19 +1,18 @@ -module SolidusMarketplace - module VariantDecorator - extend ActiveSupport::Concern +# frozen_string_literal: true - included do - prepend(InstanceMethods) - has_many :supplier_variants - has_many :suppliers, through: :supplier_variants - before_create :populate_for_suppliers - end +module SolidusMarketplace + module Spree + module VariantDecorator + def self.prepended(base) + base.has_many :supplier_variants + base.has_many :suppliers, through: :supplier_variants + base.before_create :populate_for_suppliers + end - module InstanceMethods private def create_stock_items - Spree::StockLocation.all.each do |stock_location| + ::Spree::StockLocation.all.each do |stock_location| if stock_location.supplier_id.blank? || self.suppliers.pluck(:id).include?(stock_location.supplier_id) stock_location.propagate_variant(self) if stock_location.propagate_all_variants? end @@ -23,6 +22,8 @@ def create_stock_items def populate_for_suppliers self.suppliers = self.product.suppliers end + + ::Spree::Variant.prepend self end end end diff --git a/app/helpers/spree/api/api_helpers_decorator.rb b/app/helpers/spree/api/api_helpers_decorator.rb deleted file mode 100644 index c1063df..0000000 --- a/app/helpers/spree/api/api_helpers_decorator.rb +++ /dev/null @@ -1,22 +0,0 @@ -module Spree - module Api - module ApiHelpers - - @@supplier_attributes = [ - :id, - :address_id, - :commission_flat_rate, - :commission_percentage, - :user_id, - :name, - :url, - :deleted_at, - :tax_id, - :token, - :slug - ] - - mattr_reader(:supplier_attributes) - end - end -end diff --git a/app/models/ckeditor/asset_decorator.rb b/app/models/ckeditor/asset_decorator.rb deleted file mode 100644 index 41fa363..0000000 --- a/app/models/ckeditor/asset_decorator.rb +++ /dev/null @@ -1,5 +0,0 @@ -if defined?(Ckeditor::Asset) - Ckeditor::Asset.class_eval do - belongs_to :supplier, class_name: 'Spree::Supplier' - end -end diff --git a/app/models/solidus_marketplace/payment_decorator.rb b/app/models/solidus_marketplace/payment_decorator.rb deleted file mode 100644 index 85f4307..0000000 --- a/app/models/solidus_marketplace/payment_decorator.rb +++ /dev/null @@ -1,9 +0,0 @@ -module SolidusMarketplace - module PaymentDecorator - extend ActiveSupport::Concern - - included do - belongs_to :payable, polymorphic: true, optional: true - end - end -end diff --git a/app/models/spree/order_decorator.rb b/app/models/spree/order_decorator.rb deleted file mode 100644 index cb149ed..0000000 --- a/app/models/spree/order_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Order.include(SolidusMarketplace::OrderDecorator) diff --git a/app/models/spree/payment_decorator.rb b/app/models/spree/payment_decorator.rb deleted file mode 100644 index 33b320e..0000000 --- a/app/models/spree/payment_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Payment.include(SolidusMarketplace::PaymentDecorator) diff --git a/app/models/spree/permission_sets/supplier_ability.rb b/app/models/spree/permission_sets/supplier_ability.rb index 46521c7..b65b82c 100644 --- a/app/models/spree/permission_sets/supplier_ability.rb +++ b/app/models/spree/permission_sets/supplier_ability.rb @@ -1,36 +1,54 @@ +# frozen_string_literal: true + +require 'cancan' + module Spree module PermissionSets class SupplierAbility < PermissionSets::Base def activate! - can [:admin, :update, :read, :display, :stock], Spree::Product, suppliers: {id: user.supplier_id} + can [:admin, :update, :read, :display, :stock], Spree::Product, + suppliers: { id: user.supplier_id } can [:admin, :create], Spree::Product can [:admin, :create, :update, :destroy, :display], Spree::Variant - can [:admin, :display, :index, :update, :edit], Spree::Shipment, order: {state: 'complete'}, - stock_location: { supplier_id: user.supplier_id } + can [:admin, :display, :index, :update, :edit], Spree::Shipment, + order: { state: 'complete' }, + stock_location: { supplier_id: user.supplier_id } - can [:admin, :display], Spree::ReturnAuthorization, stock_location: {supplier_id: user.supplier_id} - can [:admin, :display], Spree::CustomerReturn, stock_location: {supplier_id: user.supplier_id} + can [:admin, :display], Spree::ReturnAuthorization, + stock_location: { supplier_id: user.supplier_id } + can [:admin, :display], Spree::CustomerReturn, + stock_location: { supplier_id: user.supplier_id } #FIXME: come back to these when we work on shipping-related issues - # can [:admin, :manage, :read, :ready, :ship], Spree::Shipment, order: { state: 'complete' }, stock_location: { supplier_id: user.supplier_id } + # can [:admin, :manage, :read, :ready, :ship], Spree::Shipment, + # order: { state: 'complete' }, + # stock_location: { supplier_id: user.supplier_id } # can [:admin, :create, :update], :stock_items - can [:admin, :index, :create, :edit, :read, :update], Spree::StockItem, stock_location: {supplier_id: user.supplier_id} + can [:admin, :index, :create, :edit, :read, :update], Spree::StockItem, + stock_location: { supplier_id: user.supplier_id } cannot :display, Spree::StockLocation, active: true - can [:admin, :manage, :create], Spree::StockLocation, supplier_id: user.supplier_id, active: true - can [:admin, :manage, :create], Spree::StockMovement, stock_item: {stock_location: {supplier_id: user.supplier_id}} + can [:admin, :manage, :create], Spree::StockLocation, + supplier_id: user.supplier_id, + active: true + can [:admin, :manage, :create], Spree::StockMovement, + stock_item: { stock_location: { supplier_id: user.supplier_id }} - can [:admin, :create, :read, :update, :display], Spree::Supplier, id: user.supplier_id + can [:admin, :create, :read, :update, :display], Spree::Supplier, + id: user.supplier_id cannot [:create], Spree::Supplier cannot [:index], Spree::Supplier can [:display, :admin, :sales_total], :reports - # can [:admin, :manage], Spree::User, supplier_id: user.supplier_id - can [:admin, :index, :edit, :update, :cancel, :show, :cart, :resend, :fire, :approve], Spree::Order, stock_locations: { supplier_id: user.supplier_id } + # can [:admin, :manage], Spree::User, supplier_id: user.supplier_id + can [:admin, :index, :edit, :update, :cancel, :show, :cart, + :resend, :fire, :approve], Spree::Order, + stock_locations: { supplier_id: user.supplier_id } # can [:admin, :index], Spree::Order, supplier_ids: user.supplier_id - # can [:admin, :edit], Spree::Order, stock_locations: { supplier_id: user.supplier_id } + # can [:admin, :edit], Spree::Order, + # stock_locations: { supplier_id: user.supplier_id } can [:admin, :manage, :create], Spree::Image if defined?(Spree::SalePrice) can [:admin, :manage, :create, :update], Spree::SalePrice diff --git a/app/models/spree/product_decorator.rb b/app/models/spree/product_decorator.rb deleted file mode 100644 index 8075f08..0000000 --- a/app/models/spree/product_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Product.include(SolidusMarketplace::ProductDecorator) diff --git a/app/models/spree/shipment_decorator.rb b/app/models/spree/shipment_decorator.rb deleted file mode 100644 index 08dc1e9..0000000 --- a/app/models/spree/shipment_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Shipment.include(SolidusMarketplace::ShipmentDecorator) diff --git a/app/models/spree/stock_location_decorator.rb b/app/models/spree/stock_location_decorator.rb deleted file mode 100644 index 100191a..0000000 --- a/app/models/spree/stock_location_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::StockLocation.include(SolidusMarketplace::StockLocationDecorator) diff --git a/app/models/spree/supplier.rb b/app/models/spree/supplier.rb index 74120e9..88e2d86 100644 --- a/app/models/spree/supplier.rb +++ b/app/models/spree/supplier.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class Supplier < Spree::Base extend FriendlyId @@ -70,7 +72,8 @@ def stock_locations_with_available_stock_items(variant) def assign_user if self.users.empty? - self.users << self.admin + self.users << self.admin if self.admin + self.users << self.user if self.users.blank? self.save end end diff --git a/app/models/spree/supplier_variant.rb b/app/models/spree/supplier_variant.rb index 9d692da..c7a3d43 100644 --- a/app/models/spree/supplier_variant.rb +++ b/app/models/spree/supplier_variant.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Spree class SupplierVariant < Spree::Base belongs_to :supplier diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb deleted file mode 100644 index 38262ea..0000000 --- a/app/models/spree/user_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree.user_class.include(SolidusMarketplace::UserDecorator) diff --git a/app/models/spree/variant_decorator.rb b/app/models/spree/variant_decorator.rb deleted file mode 100644 index 5cc155f..0000000 --- a/app/models/spree/variant_decorator.rb +++ /dev/null @@ -1 +0,0 @@ -Spree::Variant.include(SolidusMarketplace::VariantDecorator) diff --git a/lib/solidus_marketplace.rb b/lib/solidus_marketplace.rb index c265321..49ea951 100644 --- a/lib/solidus_marketplace.rb +++ b/lib/solidus_marketplace.rb @@ -1,4 +1,7 @@ require 'solidus_core' require 'solidus_support' require 'deface' +require 'solidus_marketplace/version' +require 'solidus_marketplace/configuration' require 'solidus_marketplace/engine' + diff --git a/lib/solidus_marketplace/engine.rb b/lib/solidus_marketplace/engine.rb index 5aeea1f..6a2c40e 100644 --- a/lib/solidus_marketplace/engine.rb +++ b/lib/solidus_marketplace/engine.rb @@ -1,11 +1,16 @@ # frozen_string_literal: true +require 'spree/core' + +require 'solidus_marketplace' +require 'solidus_marketplace/permitted_attributes' + module SolidusMarketplace class Engine < Rails::Engine - isolate_namespace Spree - engine_name 'solidus_marketplace' + include SolidusSupport::EngineExtensions - config.autoload_paths += %W(#{config.root}/lib) + isolate_namespace ::Spree + engine_name 'solidus_marketplace' # use rspec for tests config.generators do |g| @@ -19,38 +24,30 @@ class Engine < Rails::Engine initializer 'solidus_marketplace.preferences', before: :load_config_initializers do |app| SolidusMarketplace::Config = SolidusMarketplace::Configuration.new Spree::PermittedAttributes.singleton_class.prepend(SolidusMarketplace::PermittedAttributes) + Spree::Config.roles.assign_permissions :supplier_admin, ['Spree::PermissionSets::SupplierAbility'] end - initializer 'solidus_marketplace.menu', before: :load_config_initializers do |app| - Spree::Backend::Config.configure do |config| + initializer 'solidus_marketplace' do + next unless ::Spree::Backend::Config.respond_to?(:menu_items) + ::Spree::Backend::Config.configure do |config| config.menu_items << Spree::BackendConfiguration::MenuItem.new( [:stock_locations], 'globe', - condition: -> { can?(:index, Spree::StockLocation) }, + condition: -> { can?(:index, ::Spree::StockLocation) }, ) config.menu_items << Spree::BackendConfiguration::MenuItem.new( [:suppliers], 'home', - condition: -> { can?(:index, Spree::Supplier) }, + condition: -> { can?(:index, ::Spree::Supplier) }, ) config.menu_items << Spree::BackendConfiguration::MenuItem.new( [:shipments], 'plane', - condition: -> { can?(:index, Spree::Shipment) }, + condition: -> { can?(:index, ::Spree::Shipment) }, ) end end - - def self.activate - Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c| - Rails.configuration.cache_classes ? require(c) : load(c) - end - - Spree::Config.roles.assign_permissions :supplier_admin, ['Spree::PermissionSets::SupplierAbility'] - end - - config.to_prepare(&method(:activate).to_proc) end end diff --git a/lib/solidus_marketplace/factories.rb b/lib/solidus_marketplace/factories.rb index 11f8314..e89938d 100644 --- a/lib/solidus_marketplace/factories.rb +++ b/lib/solidus_marketplace/factories.rb @@ -1,6 +1,49 @@ # frozen_string_literal: true FactoryBot.define do + factory :supplier, class: Spree::Supplier do + sequence(:name) { |i| "Big Store #{i}" } + user + url { 'http://example.com' } + address + commission_flat_rate { 0.0 } + commission_percentage { 10.0 } + # Creating a stock location with a factory instead of letting the model handle it + # so that we can run tests with backorderable defaulting to true. + before :create do |supplier| + supplier.stock_locations << build(:stock_location, + name: supplier.name, + supplier: supplier) + end + + factory :supplier_with_commission do + commission_flat_rate { 0.5 } + commission_percentage { 10 } + end + end + + factory :supplier_user, parent: :user do + supplier + end + + factory :supplier_admin_role, parent: :role do + name { 'supplier_admin' } + end + + factory :supplier_admin, parent: :user do + supplier + + after :create do |user| + user.spree_roles << create(:supplier_admin_role) + end + end + + factory :variant_with_supplier, parent: :variant do + after :create do |variant| + variant.product.add_supplier! create(:supplier) + end + end + factory :order_from_supplier, parent: :order do bill_address ship_address @@ -60,47 +103,4 @@ end end end - - factory :supplier, class: Spree::Supplier do - sequence(:name) { |i| "Big Store #{i}" } - user - url { 'http://example.com' } - address - commission_flat_rate { 0.0 } - commission_percentage { 10.0 } - # Creating a stock location with a factory instead of letting the model handle it - # so that we can run tests with backorderable defaulting to true. - before :create do |supplier| - supplier.stock_locations << build(:stock_location, - name: supplier.name, - supplier: supplier) - end - - factory :supplier_with_commission do - commission_flat_rate { 0.5 } - commission_percentage { 10 } - end - end - - factory :supplier_user, parent: :user do - supplier - end - - factory :supplier_admin_role, parent: :role do - name { 'supplier_admin' } - end - - factory :supplier_admin, parent: :user do - supplier - - after :create do |user| - user.spree_roles << create(:supplier_admin_role) - end - end - - factory :variant_with_supplier, parent: :variant do - after :create do |variant| - variant.product.add_supplier! create(:supplier) - end - end end diff --git a/lib/solidus_marketplace/permitted_attributes.rb b/lib/solidus_marketplace/permitted_attributes.rb index 35126c3..279648c 100644 --- a/lib/solidus_marketplace/permitted_attributes.rb +++ b/lib/solidus_marketplace/permitted_attributes.rb @@ -1,5 +1,9 @@ # frozen_string_literal: true +# This module is responsible for managing what attributes can be updated +# through the api. It also overrides Spree::Permitted attributes to allow the +# solidus api to accept nested params for subscription models as well + module SolidusMarketplace module PermittedAttributes class << self diff --git a/solidus_marketplace.gemspec b/solidus_marketplace.gemspec index 61bb613..529e5b4 100644 --- a/solidus_marketplace.gemspec +++ b/solidus_marketplace.gemspec @@ -21,22 +21,15 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.requirements << 'none' - s.add_dependency 'solidus_core', ['>= 2.2', '< 3'] + s.add_dependency 'solidus', ['>= 2.2', '< 3'] s.add_dependency 'solidus_support' s.add_dependency 'deface', '~> 1.0' - s.add_development_dependency 'capybara' - s.add_development_dependency 'capybara-screenshot' + s.add_development_dependency 'simplecov' s.add_development_dependency 'coffee-rails' - s.add_development_dependency 'database_cleaner' - s.add_development_dependency 'factory_bot' - s.add_development_dependency 'ffaker' s.add_development_dependency 'poltergeist' - s.add_development_dependency 'puma' - s.add_development_dependency 'rspec-rails' - s.add_development_dependency 'rubocop' - s.add_development_dependency 'rubocop-rspec' s.add_development_dependency 'sass-rails' - s.add_development_dependency 'simplecov' - s.add_development_dependency 'sqlite3' + s.add_development_dependency 'solidus_dev_support' + s.add_development_dependency 'vcr' + s.add_development_dependency 'webmock' end diff --git a/spec/examples.txt b/spec/examples.txt new file mode 100644 index 0000000..8f5cc5b --- /dev/null +++ b/spec/examples.txt @@ -0,0 +1,73 @@ +example_id | status | run_time | +--------------------------------------------------------------- | ------- | --------------- | +./spec/features/spree/admin/orders_spec.rb[1:1] | failed | 0.04116 seconds | +./spec/features/spree/admin/products_spec.rb[1:1:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/return_authorizations_spec.rb[1:1] | pending | 0.00002 seconds | +./spec/features/spree/admin/settings_spec.rb[1:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/shipments_spec.rb[1:1:1:1] | pending | 0 seconds | +./spec/features/spree/admin/shipments_spec.rb[1:1:1:2] | pending | 0 seconds | +./spec/features/spree/admin/shipments_spec.rb[1:1:1:3] | pending | 0 seconds | +./spec/features/spree/admin/shipments_spec.rb[1:1:2] | pending | 0.00001 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:2] | pending | 0.00001 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:3] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:4] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:5] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:6] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:2:1] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:2:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/stock_spec.rb[1:1:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:2] | pending | 0.00001 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:3] | pending | 0 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:4] | pending | 0.00001 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:5] | pending | 0.00001 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:1:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:1:2] | pending | 0.00001 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:1:3] | pending | 0.00001 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:2:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:2:2] | pending | 0.00001 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:3:1] | failed | 3.66 seconds | +./spec/models/spree/order_decorator_spec.rb[1:1:1] | pending | 0.00001 seconds | +./spec/models/spree/order_decorator_spec.rb[1:1:2] | pending | 0.00001 seconds | +./spec/models/spree/order_decorator_spec.rb[1:2:1:1] | pending | 0.00001 seconds | +./spec/models/spree/order_decorator_spec.rb[1:2:2:1] | pending | 0.00001 seconds | +./spec/models/spree/payment_decorator_spec.rb[1:1] | passed | 0.03811 seconds | +./spec/models/spree/product_decorator_spec.rb[1:1:1:1] | failed | 0.07832 seconds | +./spec/models/spree/product_decorator_spec.rb[1:1:2:1] | failed | 0.07898 seconds | +./spec/models/spree/product_decorator_spec.rb[1:2:1] | failed | 0.24986 seconds | +./spec/models/spree/product_decorator_spec.rb[1:3:1] | failed | 0.0806 seconds | +./spec/models/spree/product_decorator_spec.rb[1:4:1] | failed | 0.0759 seconds | +./spec/models/spree/shipment_decorator_spec.rb[1:1:1] | failed | 0.05156 seconds | +./spec/models/spree/shipment_decorator_spec.rb[1:2:1] | pending | 0.00321 seconds | +./spec/models/spree/shipment_decorator_spec.rb[1:2:2] | pending | 0.00001 seconds | +./spec/models/spree/shipment_decorator_spec.rb[1:3:1] | passed | 0.11343 seconds | +./spec/models/spree/stock/splitter/marketplace_spec.rb[1:1] | failed | 0.1976 seconds | +./spec/models/spree/stock_location_decorator_spec.rb[1:1] | passed | 0.02123 seconds | +./spec/models/spree/stock_location_decorator_spec.rb[1:2:1:1:1] | pending | 0.00001 seconds | +./spec/models/spree/stock_location_decorator_spec.rb[1:2:1:2:1] | pending | 0.00002 seconds | +./spec/models/spree/stock_location_decorator_spec.rb[1:2:2:1] | failed | 0.05019 seconds | +./spec/models/spree/supplier_spec.rb[1:1] | passed | 0.00271 seconds | +./spec/models/spree/supplier_spec.rb[1:2] | passed | 0.00321 seconds | +./spec/models/spree/supplier_spec.rb[1:3] | passed | 0.00282 seconds | +./spec/models/spree/supplier_spec.rb[1:4] | passed | 0.03016 seconds | +./spec/models/spree/supplier_spec.rb[1:5] | passed | 0.00275 seconds | +./spec/models/spree/supplier_spec.rb[1:6] | passed | 0.0061 seconds | +./spec/models/spree/supplier_spec.rb[1:7:1] | failed | 0.04714 seconds | +./spec/models/spree/supplier_spec.rb[1:8:1] | failed | 0.05826 seconds | +./spec/models/spree/supplier_spec.rb[1:9:1:1] | passed | 0.00318 seconds | +./spec/models/spree/supplier_spec.rb[1:9:2:1] | passed | 0.00337 seconds | +./spec/models/spree/supplier_spec.rb[1:10:1] | failed | 1.5 seconds | +./spec/models/spree/supplier_spec.rb[1:11:1] | failed | 0.04701 seconds | +./spec/models/spree/supplier_variant_spec.rb[1:1] | pending | 0.00001 seconds | +./spec/models/spree/user_decorator_spec.rb[1:1] | passed | 0.00397 seconds | +./spec/models/spree/user_decorator_spec.rb[1:2] | passed | 0.00484 seconds | +./spec/models/spree/user_decorator_spec.rb[1:3:1] | passed | 0.00932 seconds | +./spec/models/spree/user_decorator_spec.rb[1:3:2] | passed | 0.00431 seconds | +./spec/models/spree/user_decorator_spec.rb[1:4:1] | passed | 0.0064 seconds | +./spec/models/spree/user_decorator_spec.rb[1:4:2] | passed | 0.0039 seconds | +./spec/models/spree/user_decorator_spec.rb[1:5:1] | passed | 0.02703 seconds | +./spec/models/spree/user_decorator_spec.rb[1:5:2] | passed | 0.00466 seconds | +./spec/models/spree/user_decorator_spec.rb[1:6:1] | passed | 0.00467 seconds | +./spec/models/spree/user_decorator_spec.rb[1:6:2] | passed | 0.00637 seconds | +./spec/models/spree/variant_decorator_spec.rb[1:1] | pending | 0.00284 seconds | diff --git a/spec/features/spree/admin/orders_spec.rb b/spec/features/spree/admin/orders_spec.rb index e7914b2..3a361bf 100644 --- a/spec/features/spree/admin/orders_spec.rb +++ b/spec/features/spree/admin/orders_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe 'Admin - Orders', type: :feature do +describe 'Admin - Orders', type: :feature do let!(:user) { create(:supplier_user) } it 'Supplier should not be authorized' do diff --git a/spec/features/spree/admin/products_spec.rb b/spec/features/spree/admin/products_spec.rb index 4d947d7..b6bff0b 100644 --- a/spec/features/spree/admin/products_spec.rb +++ b/spec/features/spree/admin/products_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe 'Admin - Products', type: :feature, js: true do +describe 'Admin - Products', type: :feature, js: true do let(:supplier1) { create(:supplier) } let(:supplier2) { create(:supplier) } let(:product) do diff --git a/spec/features/spree/admin/return_authorizations_spec.rb b/spec/features/spree/admin/return_authorizations_spec.rb index f41c327..5da10bd 100644 --- a/spec/features/spree/admin/return_authorizations_spec.rb +++ b/spec/features/spree/admin/return_authorizations_spec.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -RSpec.describe "Admin - Return Authorizations" do +describe "Admin - Return Authorizations" do skip 'TODO: need to write, but should likely wait until after https://github.com/spree/spree/issues/4026' end diff --git a/spec/features/spree/admin/settings_spec.rb b/spec/features/spree/admin/settings_spec.rb index c1fcd04..95e141b 100644 --- a/spec/features/spree/admin/settings_spec.rb +++ b/spec/features/spree/admin/settings_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe 'Admin - Marketplace Settings', type: :feature do +describe 'Admin - Marketplace Settings', type: :feature do let!(:user) { create(:admin_user) } before do diff --git a/spec/features/spree/admin/shipments_spec.rb b/spec/features/spree/admin/shipments_spec.rb index dbe809c..01f3955 100644 --- a/spec/features/spree/admin/shipments_spec.rb +++ b/spec/features/spree/admin/shipments_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe 'Admin - Shipments', type: :feature, js: true do +describe 'Admin - Shipments', type: :feature, js: true do let!(:order) do order = build(:order_from_supplier_ready_to_ship, state: 'complete', diff --git a/spec/features/spree/admin/stock_management_spec.rb b/spec/features/spree/admin/stock_management_spec.rb index 5453c60..f2cd562 100644 --- a/spec/features/spree/admin/stock_management_spec.rb +++ b/spec/features/spree/admin/stock_management_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe 'Stock Management', type: :feature, js: true do +describe 'Stock Management', type: :feature, js: true do let(:user) { create(:supplier_user) } before do diff --git a/spec/features/spree/admin/stock_spec.rb b/spec/features/spree/admin/stock_spec.rb index f87f2e5..f17c732 100644 --- a/spec/features/spree/admin/stock_spec.rb +++ b/spec/features/spree/admin/stock_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe 'Admin - Product Stock Management', type: :feature, js: true do +describe 'Admin - Product Stock Management', type: :feature, js: true do let!(:user) { create(:supplier_admin) } let!(:supplier1) { user.supplier } let!(:supplier2) { create(:supplier) } diff --git a/spec/features/spree/admin/suppliers_spec.rb b/spec/features/spree/admin/suppliers_spec.rb index d86d1a2..910fef5 100644 --- a/spec/features/spree/admin/suppliers_spec.rb +++ b/spec/features/spree/admin/suppliers_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe 'Admin - Suppliers', type: :feature, js: true do +describe 'Admin - Suppliers', type: :feature, js: true do let(:country) { create(:country, name: 'United States') } let(:state) { create(:state, name: "Vermont", country: country) } let!(:supplier) { create(:supplier) } diff --git a/spec/models/spree/order_decorator_spec.rb b/spec/models/spree/order_decorator_spec.rb index bedb3d0..738ffa0 100644 --- a/spec/models/spree/order_decorator_spec.rb +++ b/spec/models/spree/order_decorator_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Spree::Order do +describe Spree::Order do let!(:order) do order = create(:order_with_totals) order.line_items = [create(:line_item, diff --git a/spec/models/spree/payment_decorator_spec.rb b/spec/models/spree/payment_decorator_spec.rb index d88140f..01c11c4 100644 --- a/spec/models/spree/payment_decorator_spec.rb +++ b/spec/models/spree/payment_decorator_spec.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -RSpec.describe Spree::Payment do +describe Spree::Payment do it { is_expected.to respond_to(:payable) } end diff --git a/spec/models/spree/product_decorator_spec.rb b/spec/models/spree/product_decorator_spec.rb index b6f51b9..f86d15b 100644 --- a/spec/models/spree/product_decorator_spec.rb +++ b/spec/models/spree/product_decorator_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true -RSpec.describe Spree::Product do - let(:product) { create :product } +describe Spree::Product do + let!(:product) { create(:product) } let(:supplier1) { create(:supplier) } let(:supplier2) { create(:supplier) } diff --git a/spec/models/spree/shipment_decorator_spec.rb b/spec/models/spree/shipment_decorator_spec.rb index d981dfd..26a682c 100644 --- a/spec/models/spree/shipment_decorator_spec.rb +++ b/spec/models/spree/shipment_decorator_spec.rb @@ -1,8 +1,8 @@ # frozen_string_literal: true -RSpec.describe Spree::Shipment do +describe Spree::Shipment do context 'Scopes' do - let(:supplier) { create(:supplier) } + let!(:supplier) { create(:supplier) } let(:stock_location_1) { supplier.stock_locations.first } let(:stock_location_2) { create(:stock_location, supplier: supplier) } let(:shipment_1) { create(:shipment) } diff --git a/spec/models/spree/stock/splitter/marketplace_spec.rb b/spec/models/spree/stock/splitter/marketplace_spec.rb index 0e00f69..9cc310e 100644 --- a/spec/models/spree/stock/splitter/marketplace_spec.rb +++ b/spec/models/spree/stock/splitter/marketplace_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Spree::Stock::Splitter::Marketplace do +describe Spree::Stock::Splitter::Marketplace do let(:stock_location) { create(:stock_location) } let(:supplier1) { create(:supplier, stock_locations: [stock_location]) } let(:supplier2) { create(:supplier, stock_locations: [stock_location]) } diff --git a/spec/models/spree/stock_location_decorator_spec.rb b/spec/models/spree/stock_location_decorator_spec.rb index 7ac64d9..da9f5bd 100644 --- a/spec/models/spree/stock_location_decorator_spec.rb +++ b/spec/models/spree/stock_location_decorator_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Spree::StockLocation do +describe Spree::StockLocation do it { is_expected.to respond_to(:supplier) } subject { create(:stock_location, backorderable_default: true) } diff --git a/spec/models/spree/supplier_ability_spec.rb b/spec/models/spree/supplier_ability_spec.rb index 04c32c9..fe560b7 100644 --- a/spec/models/spree/supplier_ability_spec.rb +++ b/spec/models/spree/supplier_ability_spec.rb @@ -4,13 +4,15 @@ require 'cancan/matchers' require 'spree/testing_support/ability_helpers' -RSpec.describe Spree::SupplierAbility do +describe Spree::PermissionSets::SupplierAbility do + let(:ability) { Spree::Ability.new(user) } let(:supplier) { create(:supplier) } let(:supplier_admin_role) { build(:role, name: 'supplier_admin') } let(:user) { create(:user, supplier: supplier) } - let(:ability) { Spree::SupplierAbility.new(user) } let(:token) { nil } + subject { ability } + before(:each) do user.spree_roles << supplier_admin_role end @@ -25,7 +27,7 @@ end end - context 'for Product' do + xcontext 'for Product' do let(:resource) { create(:product) } before(:each) do @@ -43,7 +45,7 @@ product.add_supplier!(create(:supplier)) product } - it { expect(ability).to_not be_able_to :read, other_resource } + xit { expect(ability).to_not be_able_to :read, other_resource } end context 'requested by suppliers user' do @@ -76,7 +78,7 @@ end context 'requested by suppliers user' do - context 'when order is complete' do + xcontext 'when order is complete' do let(:resource) do order = create(:completed_order_from_supplier_with_totals) order.stock_locations.first.update_attribute :supplier, user.supplier @@ -102,13 +104,13 @@ end end - context 'for StockItem' do + xcontext 'for StockItem' do let(:resource) { Spree::StockItem } it_should_behave_like 'index allowed' it_should_behave_like 'admin granted' - context 'requested by another suppliers user' do + xcontext 'requested by another suppliers user' do let(:resource) { supplier = create(:supplier) variant = create(:product).master @@ -128,7 +130,7 @@ end end - context 'for StockLocation' do + xcontext 'for StockLocation' do context 'requsted by another suppliers user' do let(:resource) { supplier = create(:supplier) @@ -152,7 +154,7 @@ end end - context 'for StockMovement' do + xcontext 'for StockMovement' do let(:resource) { Spree::StockMovement } it_should_behave_like 'index allowed' @@ -182,8 +184,8 @@ context 'for Supplier' do context 'requested by any user' do - let(:ability) { Spree::SupplierAbility.new(create(:user)) } - let(:resource) { Spree::Supplier } + let(:ability) { Spree::Ability.new(user) } + let(:resource) { create(:supplier) } it_should_behave_like 'admin denied' it_should_behave_like 'access denied' diff --git a/spec/models/spree/supplier_spec.rb b/spec/models/spree/supplier_spec.rb index 52e5e53..f41dae6 100644 --- a/spec/models/spree/supplier_spec.rb +++ b/spec/models/spree/supplier_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Spree::Supplier do +describe Spree::Supplier do it { is_expected.to respond_to(:address) } it { is_expected.to respond_to(:products) } it { is_expected.to respond_to(:stock_locations) } @@ -78,8 +78,8 @@ expect(supplier.commission_flat_rate.to_f).to eql(1.0) expect(supplier.commission_percentage.to_f).to eql(10.0) # With custom commission applied. - supplier.update_attributes(commission_flat_rate: 123, - commission_percentage: 25) + supplier.update(commission_flat_rate: 123, + commission_percentage: 25) expect(supplier.commission_flat_rate).to eql(123.0) expect(supplier.commission_percentage).to eql(25.0) end diff --git a/spec/models/spree/supplier_variant_spec.rb b/spec/models/spree/supplier_variant_spec.rb index c738652..f8a3700 100644 --- a/spec/models/spree/supplier_variant_spec.rb +++ b/spec/models/spree/supplier_variant_spec.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true -RSpec.describe Spree::SupplierVariant do +describe Spree::SupplierVariant do skip "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/models/spree/user_decorator_spec.rb b/spec/models/spree/user_decorator_spec.rb index a923f37..494cedb 100644 --- a/spec/models/spree/user_decorator_spec.rb +++ b/spec/models/spree/user_decorator_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Spree.user_class do +describe Spree.user_class do it { is_expected.to respond_to(:supplier) } it { is_expected.to respond_to(:variants) } diff --git a/spec/models/spree/variant_decorator_spec.rb b/spec/models/spree/variant_decorator_spec.rb index cfda9f0..ca08862 100644 --- a/spec/models/spree/variant_decorator_spec.rb +++ b/spec/models/spree/variant_decorator_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -RSpec.describe Spree::Variant do +describe Spree::Variant do it 'should populate stock item for each of the master variant suppliers' do skip 'todo' end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a771fe0..bd7b84f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,17 +1,24 @@ # frozen_string_literal: true +# Configure Rails Environment ENV['RAILS_ENV'] = 'test' -require File.expand_path('../dummy/config/environment.rb', __FILE__) +# Run Coverage report +require 'solidus_dev_support/rspec/coverage' -require 'rspec/rails' +require File.expand_path('dummy/config/environment.rb', __dir__) -Dir[File.join(File.dirname(__FILE__), '/support/**/*.rb')].each { |file| require file } +# Requires factories and other useful helpers defined in spree_core. +require 'solidus_dev_support/rspec/feature_helper' +# Requires supporting ruby files with custom matchers and macros, etc, +# in spec/support/ and its subdirectories. +Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f } + +# Requires factories defined in lib/solidus_marketplace/factories.rb require 'solidus_marketplace/factories' -require 'spree/testing_support/controller_requests' -require 'solidus_support/extension/feature_helper' RSpec.configure do |config| - config.include Spree::TestingSupport::ControllerRequests, type: :controller + config.infer_spec_type_from_file_location! + config.use_transactional_fixtures = false end From e20d860f8b6e3d5018b79a576c0070d3c908f40e Mon Sep 17 00:00:00 2001 From: Jonathan Tapia Date: Mon, 27 Jul 2020 15:11:02 -0500 Subject: [PATCH 24/32] Remove unnecessary files --- spec/examples.txt | 73 ----------------------------------------------- 1 file changed, 73 deletions(-) delete mode 100644 spec/examples.txt diff --git a/spec/examples.txt b/spec/examples.txt deleted file mode 100644 index 8f5cc5b..0000000 --- a/spec/examples.txt +++ /dev/null @@ -1,73 +0,0 @@ -example_id | status | run_time | ---------------------------------------------------------------- | ------- | --------------- | -./spec/features/spree/admin/orders_spec.rb[1:1] | failed | 0.04116 seconds | -./spec/features/spree/admin/products_spec.rb[1:1:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/return_authorizations_spec.rb[1:1] | pending | 0.00002 seconds | -./spec/features/spree/admin/settings_spec.rb[1:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/shipments_spec.rb[1:1:1:1] | pending | 0 seconds | -./spec/features/spree/admin/shipments_spec.rb[1:1:1:2] | pending | 0 seconds | -./spec/features/spree/admin/shipments_spec.rb[1:1:1:3] | pending | 0 seconds | -./spec/features/spree/admin/shipments_spec.rb[1:1:2] | pending | 0.00001 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:2] | pending | 0.00001 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:3] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:4] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:5] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:6] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:2:1] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:2:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/stock_spec.rb[1:1:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:2] | pending | 0.00001 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:3] | pending | 0 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:4] | pending | 0.00001 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:5] | pending | 0.00001 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:1:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:1:2] | pending | 0.00001 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:1:3] | pending | 0.00001 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:2:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:2:2] | pending | 0.00001 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:3:1] | failed | 3.66 seconds | -./spec/models/spree/order_decorator_spec.rb[1:1:1] | pending | 0.00001 seconds | -./spec/models/spree/order_decorator_spec.rb[1:1:2] | pending | 0.00001 seconds | -./spec/models/spree/order_decorator_spec.rb[1:2:1:1] | pending | 0.00001 seconds | -./spec/models/spree/order_decorator_spec.rb[1:2:2:1] | pending | 0.00001 seconds | -./spec/models/spree/payment_decorator_spec.rb[1:1] | passed | 0.03811 seconds | -./spec/models/spree/product_decorator_spec.rb[1:1:1:1] | failed | 0.07832 seconds | -./spec/models/spree/product_decorator_spec.rb[1:1:2:1] | failed | 0.07898 seconds | -./spec/models/spree/product_decorator_spec.rb[1:2:1] | failed | 0.24986 seconds | -./spec/models/spree/product_decorator_spec.rb[1:3:1] | failed | 0.0806 seconds | -./spec/models/spree/product_decorator_spec.rb[1:4:1] | failed | 0.0759 seconds | -./spec/models/spree/shipment_decorator_spec.rb[1:1:1] | failed | 0.05156 seconds | -./spec/models/spree/shipment_decorator_spec.rb[1:2:1] | pending | 0.00321 seconds | -./spec/models/spree/shipment_decorator_spec.rb[1:2:2] | pending | 0.00001 seconds | -./spec/models/spree/shipment_decorator_spec.rb[1:3:1] | passed | 0.11343 seconds | -./spec/models/spree/stock/splitter/marketplace_spec.rb[1:1] | failed | 0.1976 seconds | -./spec/models/spree/stock_location_decorator_spec.rb[1:1] | passed | 0.02123 seconds | -./spec/models/spree/stock_location_decorator_spec.rb[1:2:1:1:1] | pending | 0.00001 seconds | -./spec/models/spree/stock_location_decorator_spec.rb[1:2:1:2:1] | pending | 0.00002 seconds | -./spec/models/spree/stock_location_decorator_spec.rb[1:2:2:1] | failed | 0.05019 seconds | -./spec/models/spree/supplier_spec.rb[1:1] | passed | 0.00271 seconds | -./spec/models/spree/supplier_spec.rb[1:2] | passed | 0.00321 seconds | -./spec/models/spree/supplier_spec.rb[1:3] | passed | 0.00282 seconds | -./spec/models/spree/supplier_spec.rb[1:4] | passed | 0.03016 seconds | -./spec/models/spree/supplier_spec.rb[1:5] | passed | 0.00275 seconds | -./spec/models/spree/supplier_spec.rb[1:6] | passed | 0.0061 seconds | -./spec/models/spree/supplier_spec.rb[1:7:1] | failed | 0.04714 seconds | -./spec/models/spree/supplier_spec.rb[1:8:1] | failed | 0.05826 seconds | -./spec/models/spree/supplier_spec.rb[1:9:1:1] | passed | 0.00318 seconds | -./spec/models/spree/supplier_spec.rb[1:9:2:1] | passed | 0.00337 seconds | -./spec/models/spree/supplier_spec.rb[1:10:1] | failed | 1.5 seconds | -./spec/models/spree/supplier_spec.rb[1:11:1] | failed | 0.04701 seconds | -./spec/models/spree/supplier_variant_spec.rb[1:1] | pending | 0.00001 seconds | -./spec/models/spree/user_decorator_spec.rb[1:1] | passed | 0.00397 seconds | -./spec/models/spree/user_decorator_spec.rb[1:2] | passed | 0.00484 seconds | -./spec/models/spree/user_decorator_spec.rb[1:3:1] | passed | 0.00932 seconds | -./spec/models/spree/user_decorator_spec.rb[1:3:2] | passed | 0.00431 seconds | -./spec/models/spree/user_decorator_spec.rb[1:4:1] | passed | 0.0064 seconds | -./spec/models/spree/user_decorator_spec.rb[1:4:2] | passed | 0.0039 seconds | -./spec/models/spree/user_decorator_spec.rb[1:5:1] | passed | 0.02703 seconds | -./spec/models/spree/user_decorator_spec.rb[1:5:2] | passed | 0.00466 seconds | -./spec/models/spree/user_decorator_spec.rb[1:6:1] | passed | 0.00467 seconds | -./spec/models/spree/user_decorator_spec.rb[1:6:2] | passed | 0.00637 seconds | -./spec/models/spree/variant_decorator_spec.rb[1:1] | pending | 0.00284 seconds | From 07484e42465c0fb7542e5eda9aea1e85bb0be0a7 Mon Sep 17 00:00:00 2001 From: mumoc Date: Thu, 6 Aug 2020 10:27:46 -0500 Subject: [PATCH 25/32] Remove users relations from Supplier There's no need to have a direct relation to users at Supplier level. --- .../spree/admin/suppliers_controller.rb | 2 +- app/models/spree/supplier.rb | 20 --------------- .../spree/admin/suppliers/_form.html.erb | 25 ++++++------------- .../spree/admin/suppliers/index.html.erb | 2 -- db/default/spree/marketplace_roles.rb | 6 ++--- 5 files changed, 12 insertions(+), 43 deletions(-) diff --git a/app/controllers/spree/admin/suppliers_controller.rb b/app/controllers/spree/admin/suppliers_controller.rb index bceed4b..7e1c4c1 100644 --- a/app/controllers/spree/admin/suppliers_controller.rb +++ b/app/controllers/spree/admin/suppliers_controller.rb @@ -18,7 +18,7 @@ def build_address def collection params[:q] ||= {} @search = Spree::Supplier.search(params[:q]) - @collection = @search.result.includes(:admins, :users).page(params[:page]). + @collection = @search.result.includes(:admins).page(params[:page]). per(Spree::Config[:orders_per_page]) end diff --git a/app/models/spree/supplier.rb b/app/models/spree/supplier.rb index 88e2d86..6a28375 100644 --- a/app/models/spree/supplier.rb +++ b/app/models/spree/supplier.rb @@ -7,8 +7,6 @@ class Supplier < Spree::Base attr_accessor :password, :password_confirmation - belongs_to :user, class_name: Spree.user_class.to_s, optional: true - belongs_to :admin, class_name: Spree.user_class.to_s, optional: true belongs_to :address, class_name: 'Spree::Address' accepts_nested_attributes_for :address @@ -22,7 +20,6 @@ class Supplier < Spree::Base has_many :products, through: :variants has_many :stock_locations, dependent: :destroy has_many :shipments, through: :stock_locations - has_many :users, class_name: Spree.user_class.to_s has_many :admins, class_name: Spree.user_class.to_s accepts_nested_attributes_for :admins @@ -34,7 +31,6 @@ class Supplier < Spree::Base before_validation :check_url before_create :set_commission - after_create :assign_user after_create :create_stock_location after_create :send_welcome, if: -> { SolidusMarketplace::Config[:send_supplier_email] } @@ -46,18 +42,10 @@ def deleted? deleted_at.present? end - def user_ids_string - user_ids.join(',') - end - def admin_ids_string admin_ids.join(',') end - def user_ids_string=(user_ids) - self.user_ids = user_ids.to_s.split(',').map(&:strip) - end - def admin_ids_string=(admin_ids) self.admin_ids = admin_ids.to_s.split(',').map(&:strip) end @@ -70,14 +58,6 @@ def stock_locations_with_available_stock_items(variant) protected - def assign_user - if self.users.empty? - self.users << self.admin if self.admin - self.users << self.user if self.users.blank? - self.save - end - end - def check_url unless self.url.blank? or self.url =~ URI::regexp(%w(http https)) self.url = "http://#{self.url}" diff --git a/app/views/spree/admin/suppliers/_form.html.erb b/app/views/spree/admin/suppliers/_form.html.erb index 519592d..f442acc 100644 --- a/app/views/spree/admin/suppliers/_form.html.erb +++ b/app/views/spree/admin/suppliers/_form.html.erb @@ -22,14 +22,14 @@ <% end %>
    -
    - <%= f.field_container :user do %> - <%= f.label :user_admin %> - <%= hidden_field_tag 'supplier[admin_id]', - f.object.user_ids.join(','), - class: 'user_picker fullwidth' %> - <% end %> -
    +
    + <%= f.field_container :admins do %> + <%= f.label :admins %> + <%= hidden_field_tag 'supplier[admin_ids_string]', + f.object.admin_ids.join(','), + class: 'user_picker fullwidth' %> + <% end %> +
    <%= f.field_container :paypal_email do %> @@ -67,15 +67,6 @@ <% end %>
    -
    - <%= f.field_container :users do %> - <%= f.label :users %> - <%= hidden_field_tag 'supplier[user_ids_string]', - f.object.user_ids.join(','), - class: 'user_picker fullwidth' %> - <% end %> -
    -
    <%= f.field_container :active do %>
    diff --git a/app/views/spree/admin/suppliers/index.html.erb b/app/views/spree/admin/suppliers/index.html.erb index 86a8956..4538e7d 100644 --- a/app/views/spree/admin/suppliers/index.html.erb +++ b/app/views/spree/admin/suppliers/index.html.erb @@ -39,7 +39,6 @@ <%= sort_link(@search, :name, Spree::Supplier.human_attribute_name(:name)) %> <%= t('spree.admins') %> - <%= t('spree.users') %> <%= sort_link(@search, :active, t('spree.status')) %> @@ -50,7 +49,6 @@ id="<%= spree_dom_id supplier %>" data-hook="admin_suppliers_index_rows" class="<%= cycle('odd', 'even') %>"> <%= link_to supplier.name, edit_object_url(supplier) %> <%= supplier.admins.pluck(:email).join(', ') %> - <%= supplier.users.pluck(:email).join(', ') %> <%= t("spree.#{supplier.active? ? 'active' : 'inactive'}") %> diff --git a/db/default/spree/marketplace_roles.rb b/db/default/spree/marketplace_roles.rb index d19382e..46fa30c 100644 --- a/db/default/spree/marketplace_roles.rb +++ b/db/default/spree/marketplace_roles.rb @@ -1,13 +1,13 @@ # frozen_string_literal: true -# create supplier_admin role, new supplier and supplier_admin user +# create supplier_admin role, new supplier and supplier_admin supplier_admin_role = Spree::Role.where(name: 'supplier_admin').first_or_create supplier_admin = Spree::User.create(email: 'supplier_admin@example.com', password: 'test123', password_confirmation: 'test123') supplier_admin.spree_roles << supplier_admin_role -new_supplier = Spree::Supplier.create(name: "Kevin's Guitars", - user: supplier_admin) +new_supplier = Spree::Supplier.create(name: "Kevin's Guitars") +new_supplier.admins << supplier_admin # create marketmaker user admin_role = Spree::Role.find_by(name: 'admin') From 54bdeadaa928842657cacc43ff689a3ccd5c9785 Mon Sep 17 00:00:00 2001 From: mumoc Date: Thu, 6 Aug 2020 12:34:51 -0500 Subject: [PATCH 26/32] Update supplier permission sets abilities Now, we have an Admin Ability and a Staff Ability that will define the different actions. It is expected for the Admin to be able to manage everything about the supplier, while Staff members will have reduced permissions. - Refactor current supplier ability to be AdminAbility - Create base for StaffAbility - Update marketplace engine to create supplier_admin and supplier_staff roles --- .../admin/products_controller_decorator.rb | 2 +- .../admin/stock_items_controller_decorator.rb | 2 +- .../stock_locations_controller_decorator.rb | 2 +- .../permission_sets/supplier/admin_ability.rb | 82 +++++++++++ .../permission_sets/supplier/staff_ability.rb | 49 +++++++ .../spree/permission_sets/supplier_ability.rb | 59 -------- .../spree/admin/suppliers/_form.html.erb | 2 + db/default/spree/marketplace_roles.rb | 9 +- lib/solidus_marketplace/engine.rb | 3 +- lib/solidus_marketplace/factories.rb | 17 ++- spec/examples.txt | 134 ++++++++++++++++++ spec/features/spree/admin/orders_spec.rb | 2 +- .../supplier/admin_ability_spec.rb} | 2 +- spec/models/spree/supplier_spec.rb | 11 -- 14 files changed, 294 insertions(+), 82 deletions(-) create mode 100644 app/models/spree/permission_sets/supplier/admin_ability.rb create mode 100644 app/models/spree/permission_sets/supplier/staff_ability.rb delete mode 100644 app/models/spree/permission_sets/supplier_ability.rb create mode 100644 spec/examples.txt rename spec/models/spree/{supplier_ability_spec.rb => permission_sets/supplier/admin_ability_spec.rb} (99%) diff --git a/app/decorators/controllers/solidus_marketplace/spree/admin/products_controller_decorator.rb b/app/decorators/controllers/solidus_marketplace/spree/admin/products_controller_decorator.rb index 02c7b7e..e3c48a9 100644 --- a/app/decorators/controllers/solidus_marketplace/spree/admin/products_controller_decorator.rb +++ b/app/decorators/controllers/solidus_marketplace/spree/admin/products_controller_decorator.rb @@ -64,7 +64,7 @@ def current_supplier_ids end def get_suppliers - @suppliers = Spree::Supplier.order(:name) + @suppliers = ::Spree::Supplier.order(:name) end # Scopes the collection to what the user should have access to, based on the user's role diff --git a/app/decorators/controllers/solidus_marketplace/spree/admin/stock_items_controller_decorator.rb b/app/decorators/controllers/solidus_marketplace/spree/admin/stock_items_controller_decorator.rb index e0cf2c2..98719e5 100644 --- a/app/decorators/controllers/solidus_marketplace/spree/admin/stock_items_controller_decorator.rb +++ b/app/decorators/controllers/solidus_marketplace/spree/admin/stock_items_controller_decorator.rb @@ -10,7 +10,7 @@ def self.prepended(base) def load_supplier_stock_location if try_spree_current_user.supplier - @stock_locations = Spree::StockLocation.by_supplier(try_spree_current_user.supplier).accessible_by(current_ability, :read) + @stock_locations = ::Spree::StockLocation.by_supplier(try_spree_current_user.supplier).accessible_by(current_ability, :read) @stock_item_stock_locations = params[:stock_location_id].present? ? @stock_locations.where(id: params[:stock_location_id]) : @stock_locations end end diff --git a/app/decorators/controllers/solidus_marketplace/spree/admin/stock_locations_controller_decorator.rb b/app/decorators/controllers/solidus_marketplace/spree/admin/stock_locations_controller_decorator.rb index 579d777..2ad1b65 100644 --- a/app/decorators/controllers/solidus_marketplace/spree/admin/stock_locations_controller_decorator.rb +++ b/app/decorators/controllers/solidus_marketplace/spree/admin/stock_locations_controller_decorator.rb @@ -9,7 +9,7 @@ def self.prepended(base) end def index - @stock_locations = Spree::StockLocation.accessible_by(current_ability, :read) + @stock_locations = ::Spree::StockLocation.accessible_by(current_ability, :read) .order('name ASC') .ransack(params[:q]) .result diff --git a/app/models/spree/permission_sets/supplier/admin_ability.rb b/app/models/spree/permission_sets/supplier/admin_ability.rb new file mode 100644 index 0000000..c3a9bd4 --- /dev/null +++ b/app/models/spree/permission_sets/supplier/admin_ability.rb @@ -0,0 +1,82 @@ +# frozen_string_literal: true + +require 'cancan' + +module Spree + module PermissionSets + module Supplier + class AdminAbility < PermissionSets::Base + + def activate! + can %i[admin update read display stock], + Spree::Product, + suppliers: { id: user.supplier_id } + + can %i[admin create], + Spree::Product + + can %i[admin create update destroy display], + Spree::Variant + + can %i[admin display index update edit], + Spree::Shipment, + order: { state: 'complete' }, + stock_location: { supplier_id: user.supplier_id } + + can %i[admin display], + Spree::ReturnAuthorization, + stock_location: { supplier_id: user.supplier_id } + + can %i[admin display], + Spree::CustomerReturn, + stock_location: { supplier_id: user.supplier_id } + + cannot :read, + Spree::StockItem + + can %i[admin index create edit read update], + Spree::StockItem, + stock_location: { supplier_id: user.supplier_id } + + cannot :read, + Spree::StockLocation + + can %i[admin manage create], + Spree::StockLocation, + supplier_id: user.supplier_id, + active: true + + can %i[admin manage create], + Spree::StockMovement, + stock_item: { + stock_location: { + supplier_id: user.supplier_id + } + } + + can %i[admin create read update display], + Spree::Supplier, + id: user.supplier_id + + cannot %i[create index], + Spree::Supplier + + can %i[display admin sales_total], + :reports + + can %i[admin index edit update cancel show cart resend fire approve], + Spree::Order, + stock_locations: { supplier_id: user.supplier_id } + + can %i[admin manage create], + Spree::Image + + if defined?(Spree::SalePrice) + can %i[admin manage create update], + Spree::SalePrice + end + end + end + end + end +end diff --git a/app/models/spree/permission_sets/supplier/staff_ability.rb b/app/models/spree/permission_sets/supplier/staff_ability.rb new file mode 100644 index 0000000..6675b71 --- /dev/null +++ b/app/models/spree/permission_sets/supplier/staff_ability.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'cancan' + +module Spree + module PermissionSets + module Supplier + class StaffAbility < PermissionSets::Base + + def activate! + can %i[admin update read display stock], + Spree::Product, + suppliers: { id: user.supplier_id } + + can %i[admin create], + Spree::Product + + can %i[admin create update destroy display], + Spree::Variant + + can %i[admin display index update edit], + Spree::Shipment, + order: { state: 'complete' }, + stock_location: { supplier_id: user.supplier_id } + + can %i[admin display], + Spree::ReturnAuthorization, + stock_location: { supplier_id: user.supplier_id } + + can %i[admin display], + Spree::CustomerReturn, + stock_location: { supplier_id: user.supplier_id } + + can %i[admin index edit update cancel show cart resend fire approve], + Spree::Order, + stock_locations: { supplier_id: user.supplier_id } + + can %i[admin manage create], + Spree::Image + + if defined?(Spree::SalePrice) + can %i[admin manage create update], + Spree::SalePrice + end + end + end + end + end +end diff --git a/app/models/spree/permission_sets/supplier_ability.rb b/app/models/spree/permission_sets/supplier_ability.rb deleted file mode 100644 index b65b82c..0000000 --- a/app/models/spree/permission_sets/supplier_ability.rb +++ /dev/null @@ -1,59 +0,0 @@ -# frozen_string_literal: true - -require 'cancan' - -module Spree - module PermissionSets - class SupplierAbility < PermissionSets::Base - - def activate! - can [:admin, :update, :read, :display, :stock], Spree::Product, - suppliers: { id: user.supplier_id } - can [:admin, :create], Spree::Product - can [:admin, :create, :update, :destroy, :display], Spree::Variant - - can [:admin, :display, :index, :update, :edit], Spree::Shipment, - order: { state: 'complete' }, - stock_location: { supplier_id: user.supplier_id } - - can [:admin, :display], Spree::ReturnAuthorization, - stock_location: { supplier_id: user.supplier_id } - can [:admin, :display], Spree::CustomerReturn, - stock_location: { supplier_id: user.supplier_id } - - #FIXME: come back to these when we work on shipping-related issues - # can [:admin, :manage, :read, :ready, :ship], Spree::Shipment, - # order: { state: 'complete' }, - # stock_location: { supplier_id: user.supplier_id } - # can [:admin, :create, :update], :stock_items - can [:admin, :index, :create, :edit, :read, :update], Spree::StockItem, - stock_location: { supplier_id: user.supplier_id } - cannot :display, Spree::StockLocation, active: true - can [:admin, :manage, :create], Spree::StockLocation, - supplier_id: user.supplier_id, - active: true - can [:admin, :manage, :create], Spree::StockMovement, - stock_item: { stock_location: { supplier_id: user.supplier_id }} - - can [:admin, :create, :read, :update, :display], Spree::Supplier, - id: user.supplier_id - cannot [:create], Spree::Supplier - cannot [:index], Spree::Supplier - - can [:display, :admin, :sales_total], :reports - - # can [:admin, :manage], Spree::User, supplier_id: user.supplier_id - can [:admin, :index, :edit, :update, :cancel, :show, :cart, - :resend, :fire, :approve], Spree::Order, - stock_locations: { supplier_id: user.supplier_id } - # can [:admin, :index], Spree::Order, supplier_ids: user.supplier_id - # can [:admin, :edit], Spree::Order, - # stock_locations: { supplier_id: user.supplier_id } - can [:admin, :manage, :create], Spree::Image - if defined?(Spree::SalePrice) - can [:admin, :manage, :create, :update], Spree::SalePrice - end - end - end - end -end diff --git a/app/views/spree/admin/suppliers/_form.html.erb b/app/views/spree/admin/suppliers/_form.html.erb index f442acc..40f00e8 100644 --- a/app/views/spree/admin/suppliers/_form.html.erb +++ b/app/views/spree/admin/suppliers/_form.html.erb @@ -22,6 +22,7 @@ <% end %>
    + <% if try_spree_current_user.has_admin_role? %>
    <%= f.field_container :admins do %> <%= f.label :admins %> @@ -30,6 +31,7 @@ class: 'user_picker fullwidth' %> <% end %>
    + <% end %>
    <%= f.field_container :paypal_email do %> diff --git a/db/default/spree/marketplace_roles.rb b/db/default/spree/marketplace_roles.rb index 46fa30c..05f512d 100644 --- a/db/default/spree/marketplace_roles.rb +++ b/db/default/spree/marketplace_roles.rb @@ -6,8 +6,15 @@ password: 'test123', password_confirmation: 'test123') supplier_admin.spree_roles << supplier_admin_role + +supplier_staff_role = Spree::Role.where(name: 'supplier_staff').first_or_create +supplier_staff = Spree::User.create(email: 'supplier_staff@example.com', + password: 'test123', + password_confirmation: 'test123') +supplier_staff.spree_roles << supplier_staff_role + new_supplier = Spree::Supplier.create(name: "Kevin's Guitars") -new_supplier.admins << supplier_admin +new_supplier.admins = [supplier_admin, supplier_staff] # create marketmaker user admin_role = Spree::Role.find_by(name: 'admin') diff --git a/lib/solidus_marketplace/engine.rb b/lib/solidus_marketplace/engine.rb index 6a2c40e..1c764bd 100644 --- a/lib/solidus_marketplace/engine.rb +++ b/lib/solidus_marketplace/engine.rb @@ -24,7 +24,8 @@ class Engine < Rails::Engine initializer 'solidus_marketplace.preferences', before: :load_config_initializers do |app| SolidusMarketplace::Config = SolidusMarketplace::Configuration.new Spree::PermittedAttributes.singleton_class.prepend(SolidusMarketplace::PermittedAttributes) - Spree::Config.roles.assign_permissions :supplier_admin, ['Spree::PermissionSets::SupplierAbility'] + Spree::Config.roles.assign_permissions :supplier_admin, ['Spree::PermissionSets::Supplier::AdminAbility'] + Spree::Config.roles.assign_permissions :supplier_staff, ['Spree::PermissionSets::Supplier::StaffAbility'] end initializer 'solidus_marketplace' do diff --git a/lib/solidus_marketplace/factories.rb b/lib/solidus_marketplace/factories.rb index e89938d..88f781b 100644 --- a/lib/solidus_marketplace/factories.rb +++ b/lib/solidus_marketplace/factories.rb @@ -3,7 +3,6 @@ FactoryBot.define do factory :supplier, class: Spree::Supplier do sequence(:name) { |i| "Big Store #{i}" } - user url { 'http://example.com' } address commission_flat_rate { 0.0 } @@ -22,10 +21,6 @@ end end - factory :supplier_user, parent: :user do - supplier - end - factory :supplier_admin_role, parent: :role do name { 'supplier_admin' } end @@ -38,6 +33,18 @@ end end + factory :supplier_staff_role, parent: :role do + name { 'supplier_staff' } + end + + factory :supplier_staff, parent: :user do + supplier + + after :create do |user| + user.spree_roles << create(:supplier_staff_role) + end + end + factory :variant_with_supplier, parent: :variant do after :create do |variant| variant.product.add_supplier! create(:supplier) diff --git a/spec/examples.txt b/spec/examples.txt new file mode 100644 index 0000000..e48ecde --- /dev/null +++ b/spec/examples.txt @@ -0,0 +1,134 @@ +example_id | status | run_time | +------------------------------------------------------------------------------- | ------- | --------------- | +./spec/features/spree/admin/orders_spec.rb[1:1] | passed | 0.22693 seconds | +./spec/features/spree/admin/products_spec.rb[1:1:1] | pending | 0 seconds | +./spec/features/spree/admin/return_authorizations_spec.rb[1:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/settings_spec.rb[1:1] | pending | 0 seconds | +./spec/features/spree/admin/shipments_spec.rb[1:1:1:1] | pending | 0 seconds | +./spec/features/spree/admin/shipments_spec.rb[1:1:1:2] | pending | 0 seconds | +./spec/features/spree/admin/shipments_spec.rb[1:1:1:3] | pending | 0 seconds | +./spec/features/spree/admin/shipments_spec.rb[1:1:2] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:1] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:2] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:3] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:4] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:5] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:6] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:1:2:1] | pending | 0 seconds | +./spec/features/spree/admin/stock_management_spec.rb[1:1:2:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/stock_spec.rb[1:1:1] | pending | 0 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:1] | pending | 0 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:2] | pending | 0 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:3] | pending | 0 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:4] | pending | 0 seconds | +./spec/features/spree/admin/stock_spec.rb[1:2:5] | pending | 0 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:1:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:1:2] | pending | 0 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:1:3] | pending | 0 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:2:1] | pending | 0.00001 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:2:2] | pending | 0.00001 seconds | +./spec/features/spree/admin/suppliers_spec.rb[1:3:1] | passed | 6.3 seconds | +./spec/models/spree/order_decorator_spec.rb[1:1:1] | pending | 0 seconds | +./spec/models/spree/order_decorator_spec.rb[1:1:2] | pending | 0 seconds | +./spec/models/spree/order_decorator_spec.rb[1:2:1:1] | pending | 0.00001 seconds | +./spec/models/spree/order_decorator_spec.rb[1:2:2:1] | pending | 0 seconds | +./spec/models/spree/payment_decorator_spec.rb[1:1] | passed | 0.01303 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:1] | passed | 0.02771 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:2] | passed | 0.02433 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:3] | passed | 0.02518 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:2:1] | passed | 0.02654 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:3:1] | passed | 0.03262 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:2] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:3] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:3:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:4:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:2] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:3] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:2] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:3] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:1] | passed | 0.04466 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:2] | passed | 0.04389 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:3] | passed | 0.04153 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:2:1] | passed | 0.04249 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:3:1] | passed | 0.04397 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:4] | passed | 0.07474 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:5] | passed | 0.04161 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:1:2] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:1:3] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:2:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:3:1] | pending | 0.00001 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:1] | passed | 0.03285 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:2] | passed | 0.03001 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:3] | passed | 0.02903 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:2] | passed | 0.02843 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:3] | passed | 0.03239 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:2:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:2] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:3] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:2] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:3] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:2] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:3] | pending | 0.00001 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:2] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:3] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:2:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:3:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:2:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:3:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:2] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:3] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:1:1] | passed | 0.05178 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:2:1] | passed | 0.0412 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:2:2] | passed | 0.03799 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:2:3] | passed | 0.03742 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:1:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:2:1] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:2:2] | pending | 0.00001 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:2:3] | pending | 0 seconds | +./spec/models/spree/product_decorator_spec.rb[1:1:1:1] | passed | 0.0617 seconds | +./spec/models/spree/product_decorator_spec.rb[1:1:2:1] | passed | 0.05968 seconds | +./spec/models/spree/product_decorator_spec.rb[1:2:1] | passed | 0.09637 seconds | +./spec/models/spree/product_decorator_spec.rb[1:3:1] | passed | 0.09448 seconds | +./spec/models/spree/product_decorator_spec.rb[1:4:1] | passed | 0.06242 seconds | +./spec/models/spree/shipment_decorator_spec.rb[1:1:1] | passed | 0.1185 seconds | +./spec/models/spree/shipment_decorator_spec.rb[1:2:1] | pending | 0.00181 seconds | +./spec/models/spree/shipment_decorator_spec.rb[1:2:2] | pending | 0 seconds | +./spec/models/spree/shipment_decorator_spec.rb[1:3:1] | passed | 0.01457 seconds | +./spec/models/spree/stock/splitter/marketplace_spec.rb[1:1] | passed | 0.29511 seconds | +./spec/models/spree/stock_location_decorator_spec.rb[1:1] | passed | 0.01806 seconds | +./spec/models/spree/stock_location_decorator_spec.rb[1:2:1:1:1] | pending | 0.00001 seconds | +./spec/models/spree/stock_location_decorator_spec.rb[1:2:1:2:1] | pending | 0 seconds | +./spec/models/spree/stock_location_decorator_spec.rb[1:2:2:1] | passed | 0.074 seconds | +./spec/models/spree/supplier_spec.rb[1:1] | passed | 0.0015 seconds | +./spec/models/spree/supplier_spec.rb[1:2] | passed | 0.0025 seconds | +./spec/models/spree/supplier_spec.rb[1:3] | passed | 0.00197 seconds | +./spec/models/spree/supplier_spec.rb[1:4] | passed | 0.00542 seconds | +./spec/models/spree/supplier_spec.rb[1:5] | passed | 0.00236 seconds | +./spec/models/spree/supplier_spec.rb[1:6:1] | passed | 0.01746 seconds | +./spec/models/spree/supplier_spec.rb[1:7:1:1] | passed | 0.00174 seconds | +./spec/models/spree/supplier_spec.rb[1:7:2:1] | passed | 0.00149 seconds | +./spec/models/spree/supplier_spec.rb[1:8:1] | passed | 0.01921 seconds | +./spec/models/spree/supplier_spec.rb[1:9:1] | passed | 0.25021 seconds | +./spec/models/spree/supplier_variant_spec.rb[1:1] | pending | 0 seconds | +./spec/models/spree/user_decorator_spec.rb[1:1] | passed | 0.0017 seconds | +./spec/models/spree/user_decorator_spec.rb[1:2] | passed | 0.00205 seconds | +./spec/models/spree/user_decorator_spec.rb[1:3:1] | passed | 0.00708 seconds | +./spec/models/spree/user_decorator_spec.rb[1:3:2] | passed | 0.00232 seconds | +./spec/models/spree/user_decorator_spec.rb[1:4:1] | passed | 0.00302 seconds | +./spec/models/spree/user_decorator_spec.rb[1:4:2] | passed | 0.00243 seconds | +./spec/models/spree/user_decorator_spec.rb[1:5:1] | passed | 0.01025 seconds | +./spec/models/spree/user_decorator_spec.rb[1:5:2] | passed | 0.00216 seconds | +./spec/models/spree/user_decorator_spec.rb[1:6:1] | passed | 0.00202 seconds | +./spec/models/spree/user_decorator_spec.rb[1:6:2] | passed | 0.00281 seconds | +./spec/models/spree/variant_decorator_spec.rb[1:1] | pending | 0.00142 seconds | diff --git a/spec/features/spree/admin/orders_spec.rb b/spec/features/spree/admin/orders_spec.rb index 3a361bf..0b8ca68 100644 --- a/spec/features/spree/admin/orders_spec.rb +++ b/spec/features/spree/admin/orders_spec.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true describe 'Admin - Orders', type: :feature do - let!(:user) { create(:supplier_user) } + let!(:user) { create(:user) } it 'Supplier should not be authorized' do visit spree.root_path diff --git a/spec/models/spree/supplier_ability_spec.rb b/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb similarity index 99% rename from spec/models/spree/supplier_ability_spec.rb rename to spec/models/spree/permission_sets/supplier/admin_ability_spec.rb index fe560b7..a41b29e 100644 --- a/spec/models/spree/supplier_ability_spec.rb +++ b/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb @@ -4,7 +4,7 @@ require 'cancan/matchers' require 'spree/testing_support/ability_helpers' -describe Spree::PermissionSets::SupplierAbility do +describe Spree::PermissionSets::Supplier::AdminAbility do let(:ability) { Spree::Ability.new(user) } let(:supplier) { create(:supplier) } let(:supplier_admin_role) { build(:role, name: 'supplier_admin') } diff --git a/spec/models/spree/supplier_spec.rb b/spec/models/spree/supplier_spec.rb index f41dae6..4b060de 100644 --- a/spec/models/spree/supplier_spec.rb +++ b/spec/models/spree/supplier_spec.rb @@ -4,7 +4,6 @@ it { is_expected.to respond_to(:address) } it { is_expected.to respond_to(:products) } it { is_expected.to respond_to(:stock_locations) } - it { is_expected.to respond_to(:users) } it { is_expected.to respond_to(:variants) } it '#deleted?' do @@ -14,16 +13,6 @@ expect(subject.deleted_at?).to eql(true) end - context '#assign_user' do - let!(:supplier) { create(:supplier) } - let(:user) { supplier.user } - - it 'returns assigned user' do - expect(supplier.users).not_to be_empty - expect(supplier.users.first).to eql(user) - end - end - context '#create_stock_location' do let!(:supplier) { create(:supplier) } From 56f7fadc661c6cf86dae0602d1945aebf7a44231 Mon Sep 17 00:00:00 2001 From: mumoc Date: Fri, 11 Sep 2020 11:09:32 -0500 Subject: [PATCH 27/32] Allow Supplier Admin to manage Supplier Staff Update supplier admin ability to permit adding more staff members as well as adjusting permissions according specs --- .../permission_sets/supplier/admin_ability.rb | 25 +++- spec/examples.txt | 121 +++++++++--------- .../supplier/admin_ability_spec.rb | 29 +++-- 3 files changed, 92 insertions(+), 83 deletions(-) diff --git a/app/models/spree/permission_sets/supplier/admin_ability.rb b/app/models/spree/permission_sets/supplier/admin_ability.rb index c3a9bd4..35a17db 100644 --- a/app/models/spree/permission_sets/supplier/admin_ability.rb +++ b/app/models/spree/permission_sets/supplier/admin_ability.rb @@ -8,13 +8,26 @@ module Supplier class AdminAbility < PermissionSets::Base def activate! - can %i[admin update read display stock], - Spree::Product, - suppliers: { id: user.supplier_id } - - can %i[admin create], + can :manage, + Spree.user_class, + supplier_id: user.supplier_id + + can :update_email, + Spree.user_class + + can :manage, :api_key + + can :manage, + Spree::Role, + name: ['supplier_admin', 'supplier_staff'] + + cannot %i[read], Spree::Product + can %i[admin create update read display stock], + Spree::Product, + suppliers: { id: user.supplier_id } + can %i[admin create update destroy display], Spree::Variant @@ -54,7 +67,7 @@ def activate! } } - can %i[admin create read update display], + can %i[admin read update display], Spree::Supplier, id: user.supplier_id diff --git a/spec/examples.txt b/spec/examples.txt index e48ecde..76284ca 100644 --- a/spec/examples.txt +++ b/spec/examples.txt @@ -33,69 +33,64 @@ example_id ./spec/models/spree/order_decorator_spec.rb[1:2:1:1] | pending | 0.00001 seconds | ./spec/models/spree/order_decorator_spec.rb[1:2:2:1] | pending | 0 seconds | ./spec/models/spree/payment_decorator_spec.rb[1:1] | passed | 0.01303 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:1] | passed | 0.02771 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:2] | passed | 0.02433 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:3] | passed | 0.02518 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:2:1] | passed | 0.02654 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:3:1] | passed | 0.03262 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:2] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:3] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:3:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:4:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:2] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:3] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:2] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:3] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:1] | passed | 0.04466 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:2] | passed | 0.04389 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:3] | passed | 0.04153 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:2:1] | passed | 0.04249 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:3:1] | passed | 0.04397 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:4] | passed | 0.07474 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:5] | passed | 0.04161 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:1:2] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:1:3] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:2:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:3:1] | pending | 0.00001 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:1] | passed | 0.03285 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:2] | passed | 0.03001 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:3] | passed | 0.02903 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:2] | passed | 0.02843 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:3] | passed | 0.03239 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:2:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:2] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:3] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:2] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:3] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:2] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:3] | pending | 0.00001 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:2] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:3] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:2:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:3:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:2:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:3:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:2] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:3] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:1:1] | passed | 0.05178 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:2:1] | passed | 0.0412 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:2:2] | passed | 0.03799 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:2:3] | passed | 0.03742 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:1:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:2:1] | pending | 0 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:2:2] | pending | 0.00001 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:2:3] | pending | 0 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:1] | passed | 0.02627 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:2] | passed | 0.02451 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:3] | passed | 0.02444 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:2:1] | passed | 0.02874 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:3:1] | passed | 0.03115 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:1:1] | passed | 0.14643 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:1] | passed | 0.059 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:2] | passed | 0.06007 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:3] | passed | 0.06436 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:3:1] | passed | 0.06188 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:4:1] | passed | 0.09867 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:1] | passed | 0.06288 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:2] | passed | 0.0756 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:3] | passed | 0.06538 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:2] | passed | 0.06289 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:3] | passed | 0.06159 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:1] | passed | 0.03959 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:2] | passed | 0.03834 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:3] | passed | 0.0412 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:2:1] | passed | 0.04041 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:3:1] | passed | 0.04217 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:4] | passed | 0.04005 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:5] | passed | 0.03866 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:1:1] | passed | 0.22069 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:2:1] | passed | 0.40368 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:1] | passed | 0.03782 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:2] | passed | 0.03008 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:3] | passed | 0.02939 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:2] | passed | 0.03521 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:3] | passed | 0.04106 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:1:1] | passed | 0.02922 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:2:1] | passed | 0.02699 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:1] | passed | 0.07545 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:2] | passed | 0.07516 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:3] | passed | 0.08008 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:1] | passed | 0.06092 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:2] | passed | 0.05962 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:3] | passed | 0.05959 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:1] | passed | 0.07848 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:2] | passed | 0.07661 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:3] | passed | 0.07205 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:1] | passed | 0.05792 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:2] | passed | 0.06075 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:3] | passed | 0.06498 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:2:1] | passed | 0.06046 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:3:1] | passed | 0.06212 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:1:1] | passed | 0.02672 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:2:1] | passed | 0.02709 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:3:1:1] | passed | 0.0776 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:1] | passed | 0.05964 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:2] | passed | 0.0686 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:3] | passed | 0.06401 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:1] | passed | 0.74153 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:2] | passed | 0.03573 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:1] | passed | 0.02496 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:2] | passed | 0.02857 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:3] | passed | 0.02517 seconds | +./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:4] | passed | 0.0246 seconds | ./spec/models/spree/product_decorator_spec.rb[1:1:1:1] | passed | 0.0617 seconds | ./spec/models/spree/product_decorator_spec.rb[1:1:2:1] | passed | 0.05968 seconds | ./spec/models/spree/product_decorator_spec.rb[1:2:1] | passed | 0.09637 seconds | diff --git a/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb b/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb index a41b29e..d836102 100644 --- a/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb +++ b/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb @@ -27,7 +27,7 @@ end end - xcontext 'for Product' do + context 'for Product' do let(:resource) { create(:product) } before(:each) do @@ -45,7 +45,7 @@ product.add_supplier!(create(:supplier)) product } - xit { expect(ability).to_not be_able_to :read, other_resource } + it { expect(ability).to_not be_able_to :read, other_resource } end context 'requested by suppliers user' do @@ -78,14 +78,13 @@ end context 'requested by suppliers user' do - xcontext 'when order is complete' do + context 'when order is complete' do let(:resource) do order = create(:completed_order_from_supplier_with_totals) order.stock_locations.first.update_attribute :supplier, user.supplier Spree::Shipment.new({order: order, stock_location: order.stock_locations.first }) end - it_should_behave_like 'read only' it_should_behave_like 'index allowed' it_should_behave_like 'admin granted' end @@ -104,13 +103,13 @@ end end - xcontext 'for StockItem' do + context 'for StockItem' do let(:resource) { Spree::StockItem } it_should_behave_like 'index allowed' it_should_behave_like 'admin granted' - xcontext 'requested by another suppliers user' do + context 'requested by another suppliers user' do let(:resource) { supplier = create(:supplier) variant = create(:product).master @@ -130,7 +129,7 @@ end end - xcontext 'for StockLocation' do + context 'for StockLocation' do context 'requsted by another suppliers user' do let(:resource) { supplier = create(:supplier) @@ -141,7 +140,7 @@ it_should_behave_like 'access denied' end - xcontext 'requested by suppliers user' do + context 'requested by suppliers user' do let(:resource) { variant = create(:product).master variant.product.add_supplier! user.supplier @@ -154,7 +153,7 @@ end end - xcontext 'for StockMovement' do + context 'for StockMovement' do let(:resource) { Spree::StockMovement } it_should_behave_like 'index allowed' @@ -187,15 +186,17 @@ let(:ability) { Spree::Ability.new(user) } let(:resource) { create(:supplier) } - it_should_behave_like 'admin denied' - it_should_behave_like 'access denied' + it { expect(ability).to_not be_able_to :index, resource } + it { expect(ability).to_not be_able_to :create, resource } end - xcontext 'requested by suppliers user' do + context 'requested by suppliers user' do let(:resource) { user.supplier } - it_should_behave_like 'admin granted' - it_should_behave_like 'access granted' + it { expect(ability).to be_able_to :admin, resource } + it { expect(ability).to be_able_to :read, resource } + it { expect(ability).to be_able_to :update, resource } + it { expect(ability).to be_able_to :display, resource } end end end From 27c6e7b38b1c94d1edd335053e681b0e35006914 Mon Sep 17 00:00:00 2001 From: mumoc Date: Fri, 11 Sep 2020 12:26:57 -0500 Subject: [PATCH 28/32] Update Staff Role Permissions Adjust Staff Permissions to: - Admin Product inventory - Admin Orders - Restrict Supplier info modification - Restrict Product info modification - Restrict Pricing modification --- .gitignore | 1 + .../permission_sets/supplier/admin_ability.rb | 18 +-- .../permission_sets/supplier/staff_ability.rb | 43 +++--- lib/solidus_marketplace/engine.rb | 2 +- spec/examples.txt | 129 ------------------ .../supplier/admin_ability_spec.rb | 11 +- .../supplier/staff_ability_spec.rb | 96 +++++++++++++ 7 files changed, 130 insertions(+), 170 deletions(-) delete mode 100644 spec/examples.txt create mode 100644 spec/models/spree/permission_sets/supplier/staff_ability_spec.rb diff --git a/.gitignore b/.gitignore index 063734a..9f22a4b 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ pkg *.swp spec/dummy spec/vcr_cassettes +spec/examples.txt diff --git a/app/models/spree/permission_sets/supplier/admin_ability.rb b/app/models/spree/permission_sets/supplier/admin_ability.rb index 35a17db..560a65a 100644 --- a/app/models/spree/permission_sets/supplier/admin_ability.rb +++ b/app/models/spree/permission_sets/supplier/admin_ability.rb @@ -24,23 +24,23 @@ def activate! cannot %i[read], Spree::Product - can %i[admin create update read display stock], + can %i[admin create update read stock], Spree::Product, suppliers: { id: user.supplier_id } - can %i[admin create update destroy display], + can %i[admin create update destroy read], Spree::Variant - can %i[admin display index update edit], + can %i[admin read index update edit], Spree::Shipment, order: { state: 'complete' }, stock_location: { supplier_id: user.supplier_id } - can %i[admin display], + can %i[admin read], Spree::ReturnAuthorization, stock_location: { supplier_id: user.supplier_id } - can %i[admin display], + can %i[admin read], Spree::CustomerReturn, stock_location: { supplier_id: user.supplier_id } @@ -54,7 +54,7 @@ def activate! cannot :read, Spree::StockLocation - can %i[admin manage create], + can %i[admin manage create update], Spree::StockLocation, supplier_id: user.supplier_id, active: true @@ -67,17 +67,17 @@ def activate! } } - can %i[admin read update display], + can %i[admin read update], Spree::Supplier, id: user.supplier_id cannot %i[create index], Spree::Supplier - can %i[display admin sales_total], + can %i[read admin sales_total], :reports - can %i[admin index edit update cancel show cart resend fire approve], + can %i[admin index edit update cancel read cart resend fire approve], Spree::Order, stock_locations: { supplier_id: user.supplier_id } diff --git a/app/models/spree/permission_sets/supplier/staff_ability.rb b/app/models/spree/permission_sets/supplier/staff_ability.rb index 6675b71..4a999f5 100644 --- a/app/models/spree/permission_sets/supplier/staff_ability.rb +++ b/app/models/spree/permission_sets/supplier/staff_ability.rb @@ -8,40 +8,29 @@ module Supplier class StaffAbility < PermissionSets::Base def activate! - can %i[admin update read display stock], - Spree::Product, - suppliers: { id: user.supplier_id } - - can %i[admin create], + cannot %i[read], Spree::Product - can %i[admin create update destroy display], - Spree::Variant - - can %i[admin display index update edit], - Spree::Shipment, - order: { state: 'complete' }, - stock_location: { supplier_id: user.supplier_id } + can %i[read admin edit], + Spree::Product, + suppliers: { id: user.supplier_id } - can %i[admin display], - Spree::ReturnAuthorization, - stock_location: { supplier_id: user.supplier_id } + can %i[admin manage], + Spree::StockItem, + stock_location_id: supplier_stock_location_ids - can %i[admin display], - Spree::CustomerReturn, - stock_location: { supplier_id: user.supplier_id } + cannot %i[read], + Spree::StockLocation - can %i[admin index edit update cancel show cart resend fire approve], - Spree::Order, - stock_locations: { supplier_id: user.supplier_id } + can :read, + Spree::StockLocation, + id: supplier_stock_location_ids + end - can %i[admin manage create], - Spree::Image + private - if defined?(Spree::SalePrice) - can %i[admin manage create update], - Spree::SalePrice - end + def supplier_stock_location_ids + @ids ||= user.supplier.stock_locations.pluck(:id) end end end diff --git a/lib/solidus_marketplace/engine.rb b/lib/solidus_marketplace/engine.rb index 1c764bd..497236c 100644 --- a/lib/solidus_marketplace/engine.rb +++ b/lib/solidus_marketplace/engine.rb @@ -25,7 +25,7 @@ class Engine < Rails::Engine SolidusMarketplace::Config = SolidusMarketplace::Configuration.new Spree::PermittedAttributes.singleton_class.prepend(SolidusMarketplace::PermittedAttributes) Spree::Config.roles.assign_permissions :supplier_admin, ['Spree::PermissionSets::Supplier::AdminAbility'] - Spree::Config.roles.assign_permissions :supplier_staff, ['Spree::PermissionSets::Supplier::StaffAbility'] + Spree::Config.roles.assign_permissions :supplier_staff, ['Spree::PermissionSets::Supplier::StaffAbility', 'Spree::PermissionSets::OrderManagement'] end initializer 'solidus_marketplace' do diff --git a/spec/examples.txt b/spec/examples.txt deleted file mode 100644 index 76284ca..0000000 --- a/spec/examples.txt +++ /dev/null @@ -1,129 +0,0 @@ -example_id | status | run_time | -------------------------------------------------------------------------------- | ------- | --------------- | -./spec/features/spree/admin/orders_spec.rb[1:1] | passed | 0.22693 seconds | -./spec/features/spree/admin/products_spec.rb[1:1:1] | pending | 0 seconds | -./spec/features/spree/admin/return_authorizations_spec.rb[1:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/settings_spec.rb[1:1] | pending | 0 seconds | -./spec/features/spree/admin/shipments_spec.rb[1:1:1:1] | pending | 0 seconds | -./spec/features/spree/admin/shipments_spec.rb[1:1:1:2] | pending | 0 seconds | -./spec/features/spree/admin/shipments_spec.rb[1:1:1:3] | pending | 0 seconds | -./spec/features/spree/admin/shipments_spec.rb[1:1:2] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:1] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:2] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:3] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:4] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:5] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:1:6] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:1:2:1] | pending | 0 seconds | -./spec/features/spree/admin/stock_management_spec.rb[1:1:2:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/stock_spec.rb[1:1:1] | pending | 0 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:1] | pending | 0 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:2] | pending | 0 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:3] | pending | 0 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:4] | pending | 0 seconds | -./spec/features/spree/admin/stock_spec.rb[1:2:5] | pending | 0 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:1:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:1:2] | pending | 0 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:1:3] | pending | 0 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:2:1] | pending | 0.00001 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:2:2] | pending | 0.00001 seconds | -./spec/features/spree/admin/suppliers_spec.rb[1:3:1] | passed | 6.3 seconds | -./spec/models/spree/order_decorator_spec.rb[1:1:1] | pending | 0 seconds | -./spec/models/spree/order_decorator_spec.rb[1:1:2] | pending | 0 seconds | -./spec/models/spree/order_decorator_spec.rb[1:2:1:1] | pending | 0.00001 seconds | -./spec/models/spree/order_decorator_spec.rb[1:2:2:1] | pending | 0 seconds | -./spec/models/spree/payment_decorator_spec.rb[1:1] | passed | 0.01303 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:1] | passed | 0.02627 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:2] | passed | 0.02451 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:1:3] | passed | 0.02444 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:2:1] | passed | 0.02874 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:1:1:3:1] | passed | 0.03115 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:1:1] | passed | 0.14643 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:1] | passed | 0.059 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:2] | passed | 0.06007 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:2:3] | passed | 0.06436 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:3:1] | passed | 0.06188 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:4:1] | passed | 0.09867 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:1] | passed | 0.06288 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:2] | passed | 0.0756 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:1:3] | passed | 0.06538 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:2] | passed | 0.06289 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:2:5:3] | passed | 0.06159 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:1] | passed | 0.03959 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:2] | passed | 0.03834 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:1:3] | passed | 0.0412 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:2:1] | passed | 0.04041 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:3:1] | passed | 0.04217 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:4] | passed | 0.04005 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:1:5] | passed | 0.03866 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:1:1] | passed | 0.22069 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:1:2:1] | passed | 0.40368 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:1] | passed | 0.03782 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:2] | passed | 0.03008 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:1:3] | passed | 0.02939 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:2] | passed | 0.03521 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:3:2:2:3] | passed | 0.04106 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:1:1] | passed | 0.02922 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:2:1] | passed | 0.02699 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:1] | passed | 0.07545 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:2] | passed | 0.07516 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:3:1:3] | passed | 0.08008 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:1] | passed | 0.06092 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:2] | passed | 0.05962 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:4:4:1:3] | passed | 0.05959 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:1] | passed | 0.07848 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:2] | passed | 0.07661 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:1:1:3] | passed | 0.07205 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:1] | passed | 0.05792 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:2] | passed | 0.06075 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:1:3] | passed | 0.06498 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:2:1] | passed | 0.06046 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:5:2:3:1] | passed | 0.06212 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:1:1] | passed | 0.02672 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:2:1] | passed | 0.02709 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:3:1:1] | passed | 0.0776 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:1] | passed | 0.05964 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:2] | passed | 0.0686 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:6:4:1:3] | passed | 0.06401 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:1] | passed | 0.74153 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:1:2] | passed | 0.03573 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:1] | passed | 0.02496 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:2] | passed | 0.02857 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:3] | passed | 0.02517 seconds | -./spec/models/spree/permission_sets/supplier/admin_ability_spec.rb[1:7:2:4] | passed | 0.0246 seconds | -./spec/models/spree/product_decorator_spec.rb[1:1:1:1] | passed | 0.0617 seconds | -./spec/models/spree/product_decorator_spec.rb[1:1:2:1] | passed | 0.05968 seconds | -./spec/models/spree/product_decorator_spec.rb[1:2:1] | passed | 0.09637 seconds | -./spec/models/spree/product_decorator_spec.rb[1:3:1] | passed | 0.09448 seconds | -./spec/models/spree/product_decorator_spec.rb[1:4:1] | passed | 0.06242 seconds | -./spec/models/spree/shipment_decorator_spec.rb[1:1:1] | passed | 0.1185 seconds | -./spec/models/spree/shipment_decorator_spec.rb[1:2:1] | pending | 0.00181 seconds | -./spec/models/spree/shipment_decorator_spec.rb[1:2:2] | pending | 0 seconds | -./spec/models/spree/shipment_decorator_spec.rb[1:3:1] | passed | 0.01457 seconds | -./spec/models/spree/stock/splitter/marketplace_spec.rb[1:1] | passed | 0.29511 seconds | -./spec/models/spree/stock_location_decorator_spec.rb[1:1] | passed | 0.01806 seconds | -./spec/models/spree/stock_location_decorator_spec.rb[1:2:1:1:1] | pending | 0.00001 seconds | -./spec/models/spree/stock_location_decorator_spec.rb[1:2:1:2:1] | pending | 0 seconds | -./spec/models/spree/stock_location_decorator_spec.rb[1:2:2:1] | passed | 0.074 seconds | -./spec/models/spree/supplier_spec.rb[1:1] | passed | 0.0015 seconds | -./spec/models/spree/supplier_spec.rb[1:2] | passed | 0.0025 seconds | -./spec/models/spree/supplier_spec.rb[1:3] | passed | 0.00197 seconds | -./spec/models/spree/supplier_spec.rb[1:4] | passed | 0.00542 seconds | -./spec/models/spree/supplier_spec.rb[1:5] | passed | 0.00236 seconds | -./spec/models/spree/supplier_spec.rb[1:6:1] | passed | 0.01746 seconds | -./spec/models/spree/supplier_spec.rb[1:7:1:1] | passed | 0.00174 seconds | -./spec/models/spree/supplier_spec.rb[1:7:2:1] | passed | 0.00149 seconds | -./spec/models/spree/supplier_spec.rb[1:8:1] | passed | 0.01921 seconds | -./spec/models/spree/supplier_spec.rb[1:9:1] | passed | 0.25021 seconds | -./spec/models/spree/supplier_variant_spec.rb[1:1] | pending | 0 seconds | -./spec/models/spree/user_decorator_spec.rb[1:1] | passed | 0.0017 seconds | -./spec/models/spree/user_decorator_spec.rb[1:2] | passed | 0.00205 seconds | -./spec/models/spree/user_decorator_spec.rb[1:3:1] | passed | 0.00708 seconds | -./spec/models/spree/user_decorator_spec.rb[1:3:2] | passed | 0.00232 seconds | -./spec/models/spree/user_decorator_spec.rb[1:4:1] | passed | 0.00302 seconds | -./spec/models/spree/user_decorator_spec.rb[1:4:2] | passed | 0.00243 seconds | -./spec/models/spree/user_decorator_spec.rb[1:5:1] | passed | 0.01025 seconds | -./spec/models/spree/user_decorator_spec.rb[1:5:2] | passed | 0.00216 seconds | -./spec/models/spree/user_decorator_spec.rb[1:6:1] | passed | 0.00202 seconds | -./spec/models/spree/user_decorator_spec.rb[1:6:2] | passed | 0.00281 seconds | -./spec/models/spree/variant_decorator_spec.rb[1:1] | pending | 0.00142 seconds | diff --git a/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb b/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb index d836102..c561f97 100644 --- a/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb +++ b/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb @@ -15,6 +15,7 @@ before(:each) do user.spree_roles << supplier_admin_role + described_class.new(ability).activate! end context 'for Dash' do @@ -147,9 +148,12 @@ user.supplier.stock_locations.first } - it_should_behave_like 'access granted' - it_should_behave_like 'admin granted' - it_should_behave_like 'index allowed' + it { expect(ability).to be_able_to :admin, resource } + it { expect(ability).to be_able_to :read, resource } + it { expect(ability).to be_able_to :update, resource } + it { expect(ability).to be_able_to :index, resource } + it { expect(ability).to be_able_to :create, resource } + it { expect(ability).to be_able_to :edit, resource } end end @@ -196,7 +200,6 @@ it { expect(ability).to be_able_to :admin, resource } it { expect(ability).to be_able_to :read, resource } it { expect(ability).to be_able_to :update, resource } - it { expect(ability).to be_able_to :display, resource } end end end diff --git a/spec/models/spree/permission_sets/supplier/staff_ability_spec.rb b/spec/models/spree/permission_sets/supplier/staff_ability_spec.rb new file mode 100644 index 0000000..04ae8e7 --- /dev/null +++ b/spec/models/spree/permission_sets/supplier/staff_ability_spec.rb @@ -0,0 +1,96 @@ +# frozen_string_literal: true + +require 'cancan' +require 'cancan/matchers' +require 'spree/testing_support/ability_helpers' + +describe Spree::PermissionSets::Supplier::AdminAbility do + let(:ability) { Spree::Ability.new(user) } + let(:supplier) { create(:supplier) } + let(:supplier_staff_role) { build(:role, name: 'supplier_staff') } + let(:user) { create(:user, supplier: supplier) } + let(:token) { nil } + + subject { ability } + + before(:each) do + user.spree_roles << supplier_staff_role + described_class.new(ability).activate! + end + + context 'for Product' do + context 'requested by another suppliers user' do + let(:other_resource) { create(:product) } + + before do + other_resource.add_supplier!(create(:supplier)) + other_resource.reload + end + + it { expect(ability).to_not be_able_to :read, other_resource } + it { expect(ability).to_not be_able_to :admin, other_resource } + it { expect(ability).to_not be_able_to :edit, other_resource } + end + + context 'requested by suppliers user' do + let(:resource) { create(:product) } + + before(:each) do + resource.add_supplier!(user.supplier) + resource.reload + end + + it { expect(ability).to be_able_to :read, resource } + it { expect(ability).to be_able_to :admin, resource } + it { expect(ability).to be_able_to :edit, resource } + end + end + + context 'for StockItem' do + let(:resource) { Spree::StockItem } + + context 'requested by another suppliers user' do + let(:resource) { + supplier = create(:supplier) + variant = create(:product).master + variant.product.add_supplier! supplier + supplier.stock_locations.first.stock_items.first + } + + it { expect(ability).to_not be_able_to :admin, resource } + end + + context 'requested by suppliers user' do + let(:resource) { + variant = create(:product).master + variant.product.add_supplier! user.supplier + user.supplier.stock_locations.first.stock_items.first + } + + it { expect(ability).to be_able_to :admin, resource } + end + end + + context 'for StockLocation' do + context 'requested by another suppliers user' do + let(:resource) { + supplier = create(:supplier) + variant = create(:product).master + variant.product.add_supplier! supplier + supplier.stock_locations.first + } + + it { expect(ability).to_not be_able_to :read, resource } + end + + context 'requested by suppliers user' do + let(:resource) { + variant = create(:product).master + variant.product.add_supplier! user.supplier + user.supplier.stock_locations.first + } + + it { expect(ability).to be_able_to :read, resource } + end + end +end From 2379f0270f12182163f6386777546503ca274e24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Carruitero?= Date: Wed, 16 Sep 2020 15:54:13 -0500 Subject: [PATCH 29/32] refactor ability specs --- .../supplier/admin_ability_spec.rb | 72 +++++++++++-------- .../supplier/staff_ability_spec.rb | 33 +++++---- 2 files changed, 62 insertions(+), 43 deletions(-) diff --git a/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb b/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb index c561f97..181c255 100644 --- a/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb +++ b/spec/models/spree/permission_sets/supplier/admin_ability_spec.rb @@ -10,6 +10,9 @@ let(:supplier_admin_role) { build(:role, name: 'supplier_admin') } let(:user) { create(:user, supplier: supplier) } let(:token) { nil } + let(:product) { create(:product) } + let(:variant) { product.master } + let(:other_supplier) { create(:supplier) } subject { ability } @@ -41,21 +44,17 @@ it_should_behave_like 'admin granted' context 'requested by another suppliers user' do - let(:other_resource) { - product = create(:product) - product.add_supplier!(create(:supplier)) - product - } + let(:other_resource) { create(:product) } + + before do + other_resource.add_supplier!(create(:supplier)) + other_resource.reload + end + it { expect(ability).to_not be_able_to :read, other_resource } end context 'requested by suppliers user' do - let(:resource) { - product = create(:product) - product.add_supplier!(user.supplier) - product.reload - } - it_should_behave_like 'access granted' it { expect(ability).to be_able_to :read, resource } @@ -80,10 +79,14 @@ context 'requested by suppliers user' do context 'when order is complete' do + let(:order) { create(:completed_order_from_supplier_with_totals) } let(:resource) do - order = create(:completed_order_from_supplier_with_totals) + Spree::Shipment.new({ order: order, + stock_location: order.stock_locations.first }) + end + + before do order.stock_locations.first.update_attribute :supplier, user.supplier - Spree::Shipment.new({order: order, stock_location: order.stock_locations.first }) end it_should_behave_like 'index allowed' @@ -112,32 +115,35 @@ context 'requested by another suppliers user' do let(:resource) { - supplier = create(:supplier) - variant = create(:product).master - variant.product.add_supplier! supplier - supplier.stock_locations.first.stock_items.first + other_supplier.stock_locations.first.stock_items.first } + + before do + variant.product.add_supplier! other_supplier + end + it_should_behave_like 'access denied' end context 'requested by suppliers user' do let(:resource) { - variant = create(:product).master - variant.product.add_supplier! user.supplier user.supplier.stock_locations.first.stock_items.first } + + before do + variant.product.add_supplier! user.supplier + end + it_should_behave_like 'access granted' end end context 'for StockLocation' do context 'requsted by another suppliers user' do - let(:resource) { - supplier = create(:supplier) - variant = create(:product).master - variant.product.add_supplier! supplier - supplier.stock_locations.first - } + let(:resource) { other_supplier.stock_locations.first } + before do + variant.product.add_supplier! other_supplier + end it_should_behave_like 'access denied' end @@ -165,22 +171,26 @@ context 'requested by another suppliers user' do let(:resource) { - supplier = create(:supplier) - variant = create(:product).master - variant.product.add_supplier! supplier - Spree::StockMovement.new({ stock_item: supplier.stock_locations. + Spree::StockMovement.new({ stock_item: other_supplier.stock_locations. first.stock_items.first }) } + before do + variant.product.add_supplier! other_supplier + end + it_should_behave_like 'admin denied' end context 'requested by suppliers user' do let(:resource) { - variant = create(:product).master - variant.product.add_supplier!(user.supplier) Spree::StockMovement.new({ stock_item: user.supplier.stock_locations. first.stock_items.first }) } + + before do + variant.product.add_supplier!(user.supplier) + end + it_should_behave_like 'access granted' end end diff --git a/spec/models/spree/permission_sets/supplier/staff_ability_spec.rb b/spec/models/spree/permission_sets/supplier/staff_ability_spec.rb index 04ae8e7..2083ef5 100644 --- a/spec/models/spree/permission_sets/supplier/staff_ability_spec.rb +++ b/spec/models/spree/permission_sets/supplier/staff_ability_spec.rb @@ -10,6 +10,9 @@ let(:supplier_staff_role) { build(:role, name: 'supplier_staff') } let(:user) { create(:user, supplier: supplier) } let(:token) { nil } + let(:product) { create(:product) } + let(:variant) { product.master } + let(:other_supplier) { create(:supplier) } subject { ability } @@ -51,22 +54,25 @@ context 'requested by another suppliers user' do let(:resource) { - supplier = create(:supplier) - variant = create(:product).master - variant.product.add_supplier! supplier - supplier.stock_locations.first.stock_items.first + other_supplier.stock_locations.first.stock_items.first } + + before do + variant.product.add_supplier! other_supplier + end it { expect(ability).to_not be_able_to :admin, resource } end context 'requested by suppliers user' do let(:resource) { - variant = create(:product).master - variant.product.add_supplier! user.supplier user.supplier.stock_locations.first.stock_items.first } + before do + variant.product.add_supplier! user.supplier + end + it { expect(ability).to be_able_to :admin, resource } end end @@ -74,22 +80,25 @@ context 'for StockLocation' do context 'requested by another suppliers user' do let(:resource) { - supplier = create(:supplier) - variant = create(:product).master - variant.product.add_supplier! supplier - supplier.stock_locations.first + other_supplier.stock_locations.first } + before do + variant.product.add_supplier! other_supplier + end + it { expect(ability).to_not be_able_to :read, resource } end context 'requested by suppliers user' do let(:resource) { - variant = create(:product).master - variant.product.add_supplier! user.supplier user.supplier.stock_locations.first } + before do + variant.product.add_supplier! user.supplier + end + it { expect(ability).to be_able_to :read, resource } end end From 6db9dfccb4ae925d5c74cf0937749b82c53eb089 Mon Sep 17 00:00:00 2001 From: Edwin Cruz Date: Mon, 2 Nov 2020 13:59:42 -0600 Subject: [PATCH 30/32] Fixing rake sample data by updating relathionship names A Supplier does not have only 1 admin, it can have multiple admins, updating sample data to use this principle --- lib/tasks/spree_sample.rake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tasks/spree_sample.rake b/lib/tasks/spree_sample.rake index b769067..7485611 100644 --- a/lib/tasks/spree_sample.rake +++ b/lib/tasks/spree_sample.rake @@ -45,7 +45,7 @@ namespace :spree_sample do supplier = Spree::Supplier.new(name: name, user_id: user.id, url: 'http://example.com', - admin: user) + admins: [user]) supplier.build_address(firstname: name, lastname: name, address1: '100 State St', From 12eed581a93b84e89663ff6bb342d8ce97b02a62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Carruitero?= Date: Fri, 25 Dec 2020 12:12:19 -0500 Subject: [PATCH 31/32] move module attribute outside singleton class --- lib/solidus_marketplace/permitted_attributes.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/solidus_marketplace/permitted_attributes.rb b/lib/solidus_marketplace/permitted_attributes.rb index 279648c..25c7916 100644 --- a/lib/solidus_marketplace/permitted_attributes.rb +++ b/lib/solidus_marketplace/permitted_attributes.rb @@ -21,8 +21,8 @@ class << self :slug, :paypal_email ] - - mattr_reader(:supplier_attributes) end + + mattr_reader(:supplier_attributes) end end From d2aeee1582483186ba40fa7397ea61b75b516106 Mon Sep 17 00:00:00 2001 From: Stephane CRIVISIER Date: Sat, 20 Feb 2021 11:25:15 +0100 Subject: [PATCH 32/32] Fix a typo in the setup instructions The `solidus-contrib` organisation seems to have been renamed `solidusio-contrib`, so let's update the README to reflect that change. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1e4f55d..394a783 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ $ bundle exec rails g solidus_marketplace:install (Optional) If you want to be able to see the reports, please add this line to your `Gemfile`: ```ruby -gem 'solidus_reports', github: 'solidus-contrib/solidus_reports' +gem 'solidus_reports', github: 'solidusio-contrib/solidus_reports' ``` (Optional) If you want to use Stripe or other payment providers, please add this line to your `Gemfile`: