solidus_stock_supplier 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -0
  3. data/.rspec +1 -0
  4. data/.rubocop.yml +235 -0
  5. data/.rvmrc +1 -0
  6. data/.travis.yml +30 -0
  7. data/Gemfile +8 -0
  8. data/LICENSE +26 -0
  9. data/README.md +36 -0
  10. data/Rakefile +30 -0
  11. data/app/assets/javascripts/spree/backend/solidus_stock_supplier.js +47 -0
  12. data/app/assets/javascripts/spree/backend/templates/stock_transfers/supplier_checkbox.hbs +4 -0
  13. data/app/assets/javascripts/spree/frontend/solidus_stock_supplier.js +2 -0
  14. data/app/assets/stylesheets/spree/backend/solidus_stock_supplier.css +4 -0
  15. data/app/assets/stylesheets/spree/frontend/solidus_stock_supplier.css +4 -0
  16. data/app/controllers/spree/admin/stock_items_controller_decorator.rb +1 -0
  17. data/app/controllers/spree/admin/stock_locations_controller_decorator.rb +6 -0
  18. data/app/controllers/spree/admin/stock_transfers_controller_decorator.rb +13 -0
  19. data/app/controllers/spree/admin/suppliers_controller.rb +12 -0
  20. data/app/models/concerns/solidus_stock_supplier/location_scopes_concern.rb +10 -0
  21. data/app/models/concerns/solidus_stock_supplier/stock_items_controller_extension.rb +7 -0
  22. data/app/models/spree/supplier.rb +17 -0
  23. data/app/models/spree/supplier_stock_location.rb +5 -0
  24. data/app/models/stock_location_decorator.rb +1 -0
  25. data/app/views/spree/admin/stock_locations/index.json.jbuilder +11 -0
  26. data/app/views/spree/admin/suppliers/_form.html.erb +20 -0
  27. data/app/views/spree/admin/suppliers/edit.html.erb +13 -0
  28. data/app/views/spree/admin/suppliers/index.html.erb +43 -0
  29. data/app/views/spree/admin/suppliers/new.html.erb +15 -0
  30. data/bin/rails +7 -0
  31. data/config/locales/en.yml +13 -0
  32. data/config/routes.rb +5 -0
  33. data/db/migrate/20171028052439_create_suppliers.rb +11 -0
  34. data/db/migrate/20171029052411_add_type_to_stock_locations.rb +6 -0
  35. data/lib/generators/solidus_stock_supplier/install/install_generator.rb +30 -0
  36. data/lib/solidus_stock_supplier.rb +3 -0
  37. data/lib/solidus_stock_supplier/engine.rb +30 -0
  38. data/lib/solidus_stock_supplier/version.rb +3 -0
  39. data/solidus_stock_supplier.gemspec +36 -0
  40. data/spec/controllers/spree/admin/suppliers_controller_spec.rb +100 -0
  41. data/spec/factories.rb +6 -0
  42. data/spec/models/spree/supplier_spec.rb +8 -0
  43. data/spec/spec_helper.rb +96 -0
  44. metadata +295 -0
@@ -0,0 +1,4 @@
1
+ <div id="stock_transfer_supplier_field" class="field">
2
+ <label>Supplier</label>
3
+ <input id='supplier' type=checkbox />
4
+ </div>
@@ -0,0 +1,2 @@
1
+ // Placeholder manifest file.
2
+ // the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/frontend/all.js'
@@ -0,0 +1,4 @@
1
+ /*
2
+ Placeholder manifest file.
3
+ the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/backend/all.css'
4
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Placeholder manifest file.
3
+ the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/frontend/all.css'
4
+ */
@@ -0,0 +1 @@
1
+ Spree::Admin::StockItemsController.prepend SolidusStockSupplier::StockItemsControllerExtension
@@ -0,0 +1,6 @@
1
+ Spree::Admin::StockLocationsController.class_eval do
2
+ def index
3
+ @stock_locations = Spree::StockLocation.for_store
4
+ @locations = Spree::StockLocation.accessible_by(current_ability, :read)
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ Spree::Admin::StockTransfersController.class_eval do
2
+ def load_viewable_stock_locations
3
+ @stock_locations = Spree::StockLocation.for_store.accessible_by(current_ability, :read)
4
+ end
5
+
6
+ def load_source_stock_locations
7
+ @source_stock_locations ||= Spree::StockLocation.for_store.accessible_by(current_ability, :transfer_from)
8
+ end
9
+
10
+ def load_destination_stock_locations
11
+ @destination_stock_locations ||= Spree::StockLocation.for_store.accessible_by(current_ability, :transfer_to).where.not(id: @stock_transfer.source_location_id)
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ class Spree::Admin::SuppliersController < Spree::Admin::ResourceController
2
+ def index
3
+ @suppliers = Spree::Supplier.all.page(params[:page]).
4
+ per(params[:per_page] || Spree::Config[:orders_per_page])
5
+ end
6
+
7
+ private
8
+
9
+ def supplier_params
10
+ params.require(:supplier).permit(:name, :phone, :email, :uid)
11
+ end
12
+ end
@@ -0,0 +1,10 @@
1
+ module SolidusStockSupplier
2
+ module LocationScopesConcern
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ scope :for_store, -> { where(type: 'Spree::StockLocation') }
7
+ scope :for_supplier, -> { where(type: 'Spree::SupplierStockLocation') }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module SolidusStockSupplier
2
+ module StockItemsControllerExtension
3
+ def load_stock_management_data
4
+ super | @stock_locations = Spree::StockLocation.for_store.accessible_by(current_ability, :read)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ module Spree
2
+ class Supplier < Spree::Base
3
+ has_many :stock_locations,
4
+ class_name: 'Spree::SupplierStockLocation',
5
+ dependent: :destroy
6
+
7
+ validates :name, :uid, presence: true, uniqueness: true
8
+
9
+ scope :active, -> { where(active: true) }
10
+
11
+ after_create :build_default_stock_location
12
+
13
+ def build_default_stock_location
14
+ stock_locations.create(name: name)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ module Spree
2
+ class SupplierStockLocation < StockLocation
3
+ belongs_to :supplier, class_name: 'Spree::Supplier'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ Spree::StockLocation.include SolidusStockSupplier::LocationScopesConcern
@@ -0,0 +1,11 @@
1
+ json.set! :store do
2
+ json.array! @locations.for_store do |location|
3
+ json.extract! location, :id, :name
4
+ end
5
+ end
6
+
7
+ json.set! :supplier do
8
+ json.array! @locations.for_supplier do |location|
9
+ json.extract! location, :id, :name
10
+ end
11
+ end
@@ -0,0 +1,20 @@
1
+ <div class="col-md-6">
2
+ <fieldset class="no-border-bottom">
3
+ <%= f.field_container :name do %>
4
+ <%= f.label :name, class: 'required' %>
5
+ <%= f.text_field :name, class: 'fullwidth', required: true %>
6
+ <% end %>
7
+ <%= f.field_container :uid do %>
8
+ <%= f.label :uid, class: 'required' %>
9
+ <%= f.number_field :uid, class: 'fullwidth', required: true %>
10
+ <% end %>
11
+ <%= f.field_container :email do %>
12
+ <%= f.label :email, t('spree.email') %>
13
+ <%= f.email_field :email, class: 'fullwidth' %>
14
+ <% end %>
15
+ <%= f.field_container :phone do %>
16
+ <%= f.label :phone, t('spree.phone') %>:<br />
17
+ <%= f.text_field :phone, class: 'fullwidth' %>
18
+ <% end %>
19
+ </fieldset>
20
+ </div>
@@ -0,0 +1,13 @@
1
+ <% admin_breadcrumb(link_to plural_resource_name(Spree::Supplier), spree.admin_suppliers_path) %>
2
+ <% admin_breadcrumb(@supplier.name) %>
3
+
4
+ <%= render 'spree/shared/error_messages', target: @supplier %>
5
+
6
+ <%= form_for [:admin, @supplier] do |f| %>
7
+ <%= render 'form', f: f %>
8
+ <div class="col-md-8">
9
+ <%= render :partial => 'spree/admin/shared/edit_resource_links' %>
10
+ </div>
11
+ <% end %>
12
+
13
+
@@ -0,0 +1,43 @@
1
+ <% admin_breadcrumb(plural_resource_name(Spree::Supplier)) %>
2
+
3
+ <% content_for :page_actions do %>
4
+ <%= button_link_to Spree.t('new_supplier'), new_object_url %>
5
+ <% end %>
6
+
7
+ <%= paginate @suppliers, theme: "solidus_admin" %>
8
+ <% if @suppliers.any? %>
9
+ <table class="index" id="listing_suppliers">
10
+ <thead>
11
+ <tr>
12
+ <th class="align-center"><%= t('spree.uid') %></th>
13
+ <th class="align-center"><%= t('spree.name') %></th>
14
+ <th class="align-center"><%= t('spree.email') %></th>
15
+ <th class="align-center"><%= t('spree.phone') %></th>
16
+ <th class="actions"></th>
17
+ </tr>
18
+ </thead>
19
+
20
+ <tbody>
21
+ <% @suppliers.each do |supplier| %>
22
+ <tr>
23
+ <td><%= supplier.uid %></td>
24
+ <td><%= link_to supplier.name, edit_object_url(supplier) %></td>
25
+ <td><%= mail_to supplier.email %></td>
26
+ <td><%= supplier.phone %></td>
27
+ <td class="actions">
28
+ <%= link_to_edit supplier, no_text: true, class: 'edit' %>
29
+ <%= link_to_delete supplier, no_text: true %>
30
+ </td>
31
+ </tr>
32
+ <% end %>
33
+ </tbody>
34
+ </table>
35
+ <% else %>
36
+ <div class="col-9 no-objects-found">
37
+ <%= render 'spree/admin/shared/no_objects_found',
38
+ resource: Spree::Supplier,
39
+ new_resource_url: new_object_url %>
40
+ </div>
41
+ <% end %>
42
+
43
+ <%= paginate @suppliers, theme: "solidus_admin" %>
@@ -0,0 +1,15 @@
1
+ <% admin_breadcrumb(link_to plural_resource_name(Spree::Supplier), spree.admin_suppliers_path) %>
2
+ <% admin_breadcrumb(t('spree.new_supplier')) %>
3
+
4
+ <% content_for :page_actions do %>
5
+ <%= button_link_to Spree.t('back_to_suppliers'), spree.admin_suppliers_path, icon: 'arrow-left' %>
6
+ <% end %>
7
+
8
+ <%= render 'spree/shared/error_messages', target: @supplier %>
9
+
10
+ <%= form_for [:admin, @supplier] do |f| %>
11
+ <%= render 'form', f: f %>
12
+ <div class="col-md-8">
13
+ <%= render :partial => 'spree/admin/shared/new_resource_links' %>
14
+ </div>
15
+ <% end %>
data/bin/rails ADDED
@@ -0,0 +1,7 @@
1
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
2
+
3
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
4
+ ENGINE_PATH = File.expand_path('../../lib/solidus_stock_supplier/engine', __FILE__)
5
+
6
+ require 'rails/all'
7
+ require 'rails/engine/commands'
@@ -0,0 +1,13 @@
1
+ en:
2
+ spree:
3
+ new_supplier: New Supplier
4
+ back_to_suppliers: Back to Suppliers
5
+ uid: Tax ID
6
+ admin:
7
+ tab:
8
+ suppliers: Suppliers
9
+ activerecord:
10
+ models:
11
+ spree/supplier:
12
+ one: Supplier
13
+ other: Suppliers
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ Spree::Core::Engine.routes.draw do
2
+ namespace :admin do
3
+ resources :suppliers
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ class CreateSuppliers < SolidusSupport::Migration[4.2]
2
+ def change
3
+ create_table :spree_suppliers do |t|
4
+ t.string :email
5
+ t.string :phone
6
+ t.string :name
7
+ t.string :uid
8
+ t.boolean :active, default: true
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,6 @@
1
+ class AddTypeToStockLocations < SolidusSupport::Migration[5.1]
2
+ def change
3
+ add_column :spree_stock_locations, :type, :string, default: 'Spree::StockLocation'
4
+ add_column :spree_stock_locations, :supplier_id, :integer
5
+ end
6
+ end
@@ -0,0 +1,30 @@
1
+ module SolidusStockSupplier
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ class_option :auto_run_migrations, type: :boolean, default: false
5
+
6
+ def add_javascripts
7
+ append_file 'vendor/assets/javascripts/spree/frontend/all.js', "//= require spree/frontend/solidus_stock_supplier\n"
8
+ append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/solidus_stock_supplier\n"
9
+ end
10
+
11
+ def add_stylesheets
12
+ inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css', " *= require spree/frontend/solidus_stock_supplier\n", before: /\*\//, verbose: true
13
+ inject_into_file 'vendor/assets/stylesheets/spree/backend/all.css', " *= require spree/backend/solidus_stock_supplier\n", before: /\*\//, verbose: true
14
+ end
15
+
16
+ def add_migrations
17
+ run 'bundle exec rake railties:install:migrations FROM=solidus_stock_supplier'
18
+ end
19
+
20
+ def run_migrations
21
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask('Would you like to run the migrations now? [Y/n]'))
22
+ if run_migrations
23
+ run 'bundle exec rake db:migrate'
24
+ else
25
+ puts 'Skipping rake db:migrate, don\'t forget to run it!'
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ require 'solidus_core'
2
+ require 'solidus_support'
3
+ require 'solidus_stock_supplier/engine'
@@ -0,0 +1,30 @@
1
+ module SolidusStockSupplier
2
+ class Engine < Rails::Engine
3
+ require 'spree/core'
4
+ isolate_namespace Spree
5
+ engine_name 'solidus_stock_supplier'
6
+
7
+ # use rspec for tests
8
+ config.generators do |g|
9
+ g.test_framework :rspec
10
+ end
11
+
12
+ initializer 'solidus_stock_supplier.menu', before: :load_config_initializers do
13
+ Spree::Backend::Config.configure do |config|
14
+ config.menu_items << config.class::MenuItem.new(
15
+ [:suppliers],
16
+ 'industry',
17
+ condition: -> { can?(:admin, :suppliers) }
18
+ )
19
+ end
20
+ end
21
+
22
+ def self.activate
23
+ Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
24
+ Rails.configuration.cache_classes ? require(c) : load(c)
25
+ end
26
+ end
27
+
28
+ config.to_prepare(&method(:activate).to_proc)
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module SolidusStockSupplier
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ $:.push File.expand_path('../lib', __FILE__)
4
+ require 'solidus_stock_supplier/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'solidus_stock_supplier'
8
+ s.version = SolidusStockSupplier::VERSION
9
+ s.summary = 'Adds support to receive stock from supplier in Solidus'
10
+ s.description = s.summary
11
+ s.license = 'BSD-3-Clause'
12
+
13
+ s.author = 'César Carruitero'
14
+ s.email = 'ccarruitero@protonmail.com'
15
+ s.homepage = 'https://github.com/ccarruitero/solidus_stock_supplier'
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+
20
+ s.add_dependency 'solidus_core'
21
+ s.add_dependency 'solidus_support'
22
+
23
+ s.add_development_dependency 'capybara'
24
+ s.add_development_dependency 'database_cleaner'
25
+ s.add_development_dependency 'factory_bot'
26
+ s.add_development_dependency 'ffaker'
27
+ s.add_development_dependency 'mysql2'
28
+ s.add_development_dependency 'pg'
29
+ s.add_development_dependency 'poltergeist'
30
+ s.add_development_dependency 'rspec-rails'
31
+ s.add_development_dependency 'rubocop'
32
+ s.add_development_dependency 'sass-rails'
33
+ s.add_development_dependency 'shoulda-matchers'
34
+ s.add_development_dependency 'simplecov'
35
+ s.add_development_dependency 'sqlite3'
36
+ end
@@ -0,0 +1,100 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Spree::Admin::SuppliersController, type: :controller do
4
+ stub_authorization!
5
+
6
+ let(:valid_attributes) {
7
+ { name: FFaker::Company.name, uid: FFaker::CompanyIT.partita_iva }
8
+ }
9
+
10
+ let(:invalid_attributes) { { name: '' } }
11
+
12
+ describe 'GET #index' do
13
+ it 'returns a success response' do
14
+ get :index
15
+ expect(response).to be_success
16
+ end
17
+ end
18
+
19
+ describe 'GET #new' do
20
+ it 'returns a success response' do
21
+ get :new, params: {}
22
+ expect(response).to be_success
23
+ end
24
+ end
25
+
26
+ describe 'POST #create' do
27
+ context 'with valid params' do
28
+ it 'creates a new Spree::Supplier' do
29
+ expect {
30
+ post :create, params: { supplier: valid_attributes }
31
+ }.to change(Spree::Supplier, :count).by(1)
32
+ end
33
+
34
+ it 'redirects to the created spree_admin' do
35
+ post :create, params: { supplier: valid_attributes }
36
+ expect(response).to redirect_to(admin_suppliers_path)
37
+ end
38
+ end
39
+
40
+ context 'with invalid params' do
41
+ it 'returns a success response' do
42
+ post :create, params: { supplier: invalid_attributes }
43
+ expect(response).to be_success
44
+ end
45
+ end
46
+ end
47
+
48
+ context 'with supplier' do
49
+ let!(:supplier) { create(:supplier) }
50
+
51
+ describe 'GET #edit' do
52
+ it 'returns a success response' do
53
+ get :edit, params: { id: supplier.to_param }
54
+ expect(response).to be_success
55
+ end
56
+ end
57
+
58
+ describe 'PUT #update' do
59
+ context 'with valid params' do
60
+ it 'updates the requested supplier' do
61
+ new_name = FFaker::Company.name
62
+ put :update, params: {
63
+ id: supplier.to_param, supplier: { name: new_name }
64
+ }
65
+ supplier.reload
66
+ expect(supplier.name).to eq(new_name)
67
+ end
68
+
69
+ it 'redirects to the spree_admin' do
70
+ put :update, params: {
71
+ id: supplier.to_param, supplier: valid_attributes
72
+ }
73
+ expect(response).to redirect_to(spree.admin_suppliers_path)
74
+ end
75
+ end
76
+
77
+ context 'with invalid params' do
78
+ it 'returns a success response' do
79
+ put :update, params: {
80
+ id: supplier.to_param, supplier: invalid_attributes
81
+ }
82
+ expect(response).to be_success
83
+ end
84
+ end
85
+ end
86
+
87
+ describe 'DELETE #destroy' do
88
+ it 'destroys the requested spree_admin' do
89
+ expect {
90
+ delete :destroy, params: { id: supplier.to_param }
91
+ }.to change(Spree::Supplier, :count).by(-1)
92
+ end
93
+
94
+ it 'redirects to the admin_suppliers' do
95
+ delete :destroy, params: { id: supplier.to_param }
96
+ expect(response).to redirect_to(admin_suppliers_url)
97
+ end
98
+ end
99
+ end
100
+ end