From faf56045b9b500e64bebf273f9b7286d3785de64 Mon Sep 17 00:00:00 2001 From: tiere Date: Wed, 18 Nov 2015 16:08:06 +0200 Subject: [PATCH 01/46] Add OnlineStoreTheme --- app/models/online_store_theme.rb | 2 ++ ...20151118134711_create_online_store_themes.rb | 9 +++++++++ test/fixtures/online_store_themes.yml | 5 +++++ test/models/online_store_theme_test.rb | 17 +++++++++++++++++ 4 files changed, 33 insertions(+) create mode 100644 app/models/online_store_theme.rb create mode 100644 db/migrate/20151118134711_create_online_store_themes.rb create mode 100644 test/fixtures/online_store_themes.yml create mode 100644 test/models/online_store_theme_test.rb diff --git a/app/models/online_store_theme.rb b/app/models/online_store_theme.rb new file mode 100644 index 000000000..c263f4f89 --- /dev/null +++ b/app/models/online_store_theme.rb @@ -0,0 +1,2 @@ +class OnlineStoreTheme < ActiveRecord::Base +end diff --git a/db/migrate/20151118134711_create_online_store_themes.rb b/db/migrate/20151118134711_create_online_store_themes.rb new file mode 100644 index 000000000..b50937050 --- /dev/null +++ b/db/migrate/20151118134711_create_online_store_themes.rb @@ -0,0 +1,9 @@ +class CreateOnlineStoreThemes < ActiveRecord::Migration + def change + create_table :online_store_themes do |t| + t.string :name + + t.timestamps null: false + end + end +end diff --git a/test/fixtures/online_store_themes.yml b/test/fixtures/online_store_themes.yml new file mode 100644 index 000000000..62e345966 --- /dev/null +++ b/test/fixtures/online_store_themes.yml @@ -0,0 +1,5 @@ +one: + name: Theme 1 + +two: + name: Theme 2 diff --git a/test/models/online_store_theme_test.rb b/test/models/online_store_theme_test.rb new file mode 100644 index 000000000..27f85d36e --- /dev/null +++ b/test/models/online_store_theme_test.rb @@ -0,0 +1,17 @@ +require 'test_helper' + +class OnlineStoreThemeTest < ActiveSupport::TestCase + fixtures %w( + online_store_themes + ) + + setup do + @one = online_store_themes(:one) + @two = online_store_themes(:two) + end + + test 'fixtures are valid' do + assert @one.valid?, @one.errors.full_messages + assert @two.valid?, @two.errors.full_messages + end +end From bf97a299d215627486ab0cad221a72f7e8ca8983 Mon Sep 17 00:00:00 2001 From: tiere Date: Wed, 18 Nov 2015 16:12:40 +0200 Subject: [PATCH 02/46] Add OnlineStoreType --- app/models/online_store_type.rb | 2 ++ ...0151118140831_create_online_store_types.rb | 9 +++++++++ test/fixtures/online_store_types.yml | 8 ++++++++ test/models/online_store_type_test.rb | 19 +++++++++++++++++++ 4 files changed, 38 insertions(+) create mode 100644 app/models/online_store_type.rb create mode 100644 db/migrate/20151118140831_create_online_store_types.rb create mode 100644 test/fixtures/online_store_types.yml create mode 100644 test/models/online_store_type_test.rb diff --git a/app/models/online_store_type.rb b/app/models/online_store_type.rb new file mode 100644 index 000000000..1afd44d1f --- /dev/null +++ b/app/models/online_store_type.rb @@ -0,0 +1,2 @@ +class OnlineStoreType < ActiveRecord::Base +end diff --git a/db/migrate/20151118140831_create_online_store_types.rb b/db/migrate/20151118140831_create_online_store_types.rb new file mode 100644 index 000000000..ca6e906a2 --- /dev/null +++ b/db/migrate/20151118140831_create_online_store_types.rb @@ -0,0 +1,9 @@ +class CreateOnlineStoreTypes < ActiveRecord::Migration + def change + create_table :online_store_types do |t| + t.string :name + + t.timestamps null: false + end + end +end diff --git a/test/fixtures/online_store_types.yml b/test/fixtures/online_store_types.yml new file mode 100644 index 000000000..9b72879bb --- /dev/null +++ b/test/fixtures/online_store_types.yml @@ -0,0 +1,8 @@ +one: + name: Pupesoft Shop + +two: + name: Magento + +three: + name: PrestaShop diff --git a/test/models/online_store_type_test.rb b/test/models/online_store_type_test.rb new file mode 100644 index 000000000..81f7ea2ae --- /dev/null +++ b/test/models/online_store_type_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class OnlineStoreTypeTest < ActiveSupport::TestCase + fixtures %w( + online_store_types + ) + + setup do + @one = online_store_types(:one) + @two = online_store_types(:two) + @three = online_store_types(:three) + end + + test 'fixtures are valid' do + assert @one.valid?, @one.errors.full_messages + assert @two.valid?, @two.errors.full_messages + assert @three.valid?, @three.errors.full_messages + end +end From c7fe4c177cef88893343b718599e97ecdeb359af Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 09:12:33 +0200 Subject: [PATCH 03/46] Delete OnlineStoreType --- app/models/online_store_type.rb | 2 -- ...0151118140831_create_online_store_types.rb | 9 --------- test/fixtures/online_store_types.yml | 8 -------- test/models/online_store_type_test.rb | 19 ------------------- 4 files changed, 38 deletions(-) delete mode 100644 app/models/online_store_type.rb delete mode 100644 db/migrate/20151118140831_create_online_store_types.rb delete mode 100644 test/fixtures/online_store_types.yml delete mode 100644 test/models/online_store_type_test.rb diff --git a/app/models/online_store_type.rb b/app/models/online_store_type.rb deleted file mode 100644 index 1afd44d1f..000000000 --- a/app/models/online_store_type.rb +++ /dev/null @@ -1,2 +0,0 @@ -class OnlineStoreType < ActiveRecord::Base -end diff --git a/db/migrate/20151118140831_create_online_store_types.rb b/db/migrate/20151118140831_create_online_store_types.rb deleted file mode 100644 index ca6e906a2..000000000 --- a/db/migrate/20151118140831_create_online_store_types.rb +++ /dev/null @@ -1,9 +0,0 @@ -class CreateOnlineStoreTypes < ActiveRecord::Migration - def change - create_table :online_store_types do |t| - t.string :name - - t.timestamps null: false - end - end -end diff --git a/test/fixtures/online_store_types.yml b/test/fixtures/online_store_types.yml deleted file mode 100644 index 9b72879bb..000000000 --- a/test/fixtures/online_store_types.yml +++ /dev/null @@ -1,8 +0,0 @@ -one: - name: Pupesoft Shop - -two: - name: Magento - -three: - name: PrestaShop diff --git a/test/models/online_store_type_test.rb b/test/models/online_store_type_test.rb deleted file mode 100644 index 81f7ea2ae..000000000 --- a/test/models/online_store_type_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'test_helper' - -class OnlineStoreTypeTest < ActiveSupport::TestCase - fixtures %w( - online_store_types - ) - - setup do - @one = online_store_types(:one) - @two = online_store_types(:two) - @three = online_store_types(:three) - end - - test 'fixtures are valid' do - assert @one.valid?, @one.errors.full_messages - assert @two.valid?, @two.errors.full_messages - assert @three.valid?, @three.errors.full_messages - end -end From b47e4c891f1a020df8030ed2f732992ab999f381 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 09:34:50 +0200 Subject: [PATCH 04/46] Add OnlineStore --- app/models/online_store.rb | 3 ++ app/models/online_store/magento.rb | 2 ++ app/models/online_store/pupeshop.rb | 2 ++ .../20151123071517_create_online_stores.rb | 11 +++++++ test/fixtures/online_stores.yml | 9 ++++++ test/models/online_store_test.rb | 31 +++++++++++++++++++ 6 files changed, 58 insertions(+) create mode 100644 app/models/online_store.rb create mode 100644 app/models/online_store/magento.rb create mode 100644 app/models/online_store/pupeshop.rb create mode 100644 db/migrate/20151123071517_create_online_stores.rb create mode 100644 test/fixtures/online_stores.yml create mode 100644 test/models/online_store_test.rb diff --git a/app/models/online_store.rb b/app/models/online_store.rb new file mode 100644 index 000000000..204bd4bb4 --- /dev/null +++ b/app/models/online_store.rb @@ -0,0 +1,3 @@ +class OnlineStore < ActiveRecord::Base + belongs_to :theme, foreign_key: :online_store_theme_id, class_name: 'OnlineStoreTheme' +end diff --git a/app/models/online_store/magento.rb b/app/models/online_store/magento.rb new file mode 100644 index 000000000..2ae479f67 --- /dev/null +++ b/app/models/online_store/magento.rb @@ -0,0 +1,2 @@ +class OnlineStore::Magento < OnlineStore +end diff --git a/app/models/online_store/pupeshop.rb b/app/models/online_store/pupeshop.rb new file mode 100644 index 000000000..579e821ea --- /dev/null +++ b/app/models/online_store/pupeshop.rb @@ -0,0 +1,2 @@ +class OnlineStore::Pupeshop < OnlineStore +end diff --git a/db/migrate/20151123071517_create_online_stores.rb b/db/migrate/20151123071517_create_online_stores.rb new file mode 100644 index 000000000..18379c284 --- /dev/null +++ b/db/migrate/20151123071517_create_online_stores.rb @@ -0,0 +1,11 @@ +class CreateOnlineStores < ActiveRecord::Migration + def change + create_table :online_stores do |t| + t.string :name + t.string :type + t.references :online_store_theme, index: true, foreign_key: true + + t.timestamps null: false + end + end +end diff --git a/test/fixtures/online_stores.yml b/test/fixtures/online_stores.yml new file mode 100644 index 000000000..62c034d81 --- /dev/null +++ b/test/fixtures/online_stores.yml @@ -0,0 +1,9 @@ +pupeshop: + name: Pupeshop + type: OnlineStore::Pupeshop + theme: one + +magento: + name: Magento + type: OnlineStore::Magento + theme: two diff --git a/test/models/online_store_test.rb b/test/models/online_store_test.rb new file mode 100644 index 000000000..cfc7d6f63 --- /dev/null +++ b/test/models/online_store_test.rb @@ -0,0 +1,31 @@ +require 'test_helper' + +class OnlineStoreTest < ActiveSupport::TestCase + fixtures %w( + online_store_themes + online_stores + ) + + setup do + @pupeshop = online_stores(:pupeshop) + @magento = online_stores(:magento) + + @theme_one = online_store_themes(:one) + @theme_two = online_store_themes(:two) + end + + test 'fixtures are valid' do + assert @pupeshop.valid?, @pupeshop.errors.full_messages + assert @magento.valid?, @magento.errors.full_messages + end + + test 'associations work' do + assert_equal @theme_one, @pupeshop.theme + assert_equal @theme_two, @magento.theme + end + + test 'STI works' do + assert_instance_of OnlineStore::Pupeshop, @pupeshop + assert_instance_of OnlineStore::Magento, @magento + end +end From 644a9c64508b3a37a9c11f0427a810230eb8d3c2 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 09:43:01 +0200 Subject: [PATCH 05/46] Add validations --- app/models/online_store.rb | 2 ++ app/models/online_store_theme.rb | 1 + 2 files changed, 3 insertions(+) diff --git a/app/models/online_store.rb b/app/models/online_store.rb index 204bd4bb4..1ec7dca26 100644 --- a/app/models/online_store.rb +++ b/app/models/online_store.rb @@ -1,3 +1,5 @@ class OnlineStore < ActiveRecord::Base belongs_to :theme, foreign_key: :online_store_theme_id, class_name: 'OnlineStoreTheme' + + validates :name, presence: true, length: { in: 1..255 } end diff --git a/app/models/online_store_theme.rb b/app/models/online_store_theme.rb index c263f4f89..ea3336b52 100644 --- a/app/models/online_store_theme.rb +++ b/app/models/online_store_theme.rb @@ -1,2 +1,3 @@ class OnlineStoreTheme < ActiveRecord::Base + validates :name, presence: true, length: { in: 1..255 } end From 278c0a65b975604a36004fe33c04f0a40613919c Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 10:28:56 +0200 Subject: [PATCH 06/46] Add online stores products join table --- app/models/online_store.rb | 2 + app/models/product.rb | 2 + ...123074934_create_online_stores_products.rb | 8 + test/fixtures/online_stores.yml | 2 + test/models/online_store_test.rb | 4 + test/models/product_test.rb | 195 ++++++++++++++++++ 6 files changed, 213 insertions(+) create mode 100644 db/migrate/20151123074934_create_online_stores_products.rb diff --git a/app/models/online_store.rb b/app/models/online_store.rb index 1ec7dca26..3c43a9dee 100644 --- a/app/models/online_store.rb +++ b/app/models/online_store.rb @@ -1,5 +1,7 @@ class OnlineStore < ActiveRecord::Base belongs_to :theme, foreign_key: :online_store_theme_id, class_name: 'OnlineStoreTheme' + has_and_belongs_to_many :products, join_table: :online_stores_products + validates :name, presence: true, length: { in: 1..255 } end diff --git a/app/models/product.rb b/app/models/product.rb index e8fd9fc7d..8568e09fc 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -22,6 +22,8 @@ class Product < BaseModel o.has_one :cover_thumbnail, -> { where(kayttotarkoitus: :th).order(:jarjestys, :tunnus) } end + has_and_belongs_to_many :online_stores, join_table: :online_stores_products + delegate :images, to: :attachments delegate :thumbnails, to: :attachments diff --git a/db/migrate/20151123074934_create_online_stores_products.rb b/db/migrate/20151123074934_create_online_stores_products.rb new file mode 100644 index 000000000..a52d574f0 --- /dev/null +++ b/db/migrate/20151123074934_create_online_stores_products.rb @@ -0,0 +1,8 @@ +class CreateOnlineStoresProducts < ActiveRecord::Migration + def change + create_table :online_stores_products do |t| + t.references :online_store, index: true, foreign_key: true + t.references :product, index: true, foreign_key: true + end + end +end diff --git a/test/fixtures/online_stores.yml b/test/fixtures/online_stores.yml index 62c034d81..afcf16f3d 100644 --- a/test/fixtures/online_stores.yml +++ b/test/fixtures/online_stores.yml @@ -2,8 +2,10 @@ pupeshop: name: Pupeshop type: OnlineStore::Pupeshop theme: one + products: hammer, helmet magento: name: Magento type: OnlineStore::Magento theme: two + products: helmet diff --git a/test/models/online_store_test.rb b/test/models/online_store_test.rb index cfc7d6f63..0823ec9d3 100644 --- a/test/models/online_store_test.rb +++ b/test/models/online_store_test.rb @@ -4,6 +4,7 @@ class OnlineStoreTest < ActiveSupport::TestCase fixtures %w( online_store_themes online_stores + products ) setup do @@ -22,6 +23,9 @@ class OnlineStoreTest < ActiveSupport::TestCase test 'associations work' do assert_equal @theme_one, @pupeshop.theme assert_equal @theme_two, @magento.theme + + assert_not_empty @pupeshop.products + assert_not_empty @magento.products end test 'STI works' do diff --git a/test/models/product_test.rb b/test/models/product_test.rb index 9631b666a..137ba2641 100644 --- a/test/models/product_test.rb +++ b/test/models/product_test.rb @@ -52,6 +52,201 @@ class ProductTest < ActiveSupport::TestCase assert_includes @product.product_links, category_links(:product_category_shirts_hammer) assert_includes @product.product_categories, category_products(:product_category_shirts) assert @product.warehouses.count > 0 + assert_not_empty @product.online_stores + end + + test 'product stock' do + stock = @product.stock + assert stock > 0 + + loc = @product.shelf_locations.first.dup + loc.saldo = 100 + loc.hyllytaso = 'foo' + loc.save! + + assert_equal stock + 100, @product.stock + + @product.update_attribute :ei_saldoa, :no_inventory_management + assert_equal 0, @product.stock + end + + test 'product reserved stock' do + # these rows should affect reserved stock, let's zero them out + @product.sales_order_rows.update_all(varattu: 0) + @product.manufacture_rows.update_all(varattu: 0) + @product.stock_transfer_rows.update_all(varattu: 0) + assert_equal 0, @product.stock_reserved + + brand = keywords :brand_tools + assert_equal brand.name, @product.brand.name + + @product.sales_order_rows.first.update!(varattu: 10) + assert_equal 10, @product.stock_reserved + + @product.manufacture_rows.first.update!(varattu: 5) + assert_equal 15, @product.stock_reserved + + @product.stock_transfer_rows.first.update!(varattu: 6) + assert_equal 21, @product.stock_reserved + + @product.update_attribute :ei_saldoa, :no_inventory_management + assert_equal 0, @product.stock_reserved + end + + test 'product stock available' do + # these should affect stock available, let's zero them out + @product.shelf_locations.update_all(saldo: 0) + @product.sales_order_rows.update_all(varattu: 0) + @product.manufacture_rows.update_all(varattu: 0) + @product.stock_transfer_rows.update_all(varattu: 0) + assert_equal 0, @product.stock_available + + @product.sales_order_rows.first.update!(varattu: 10) + assert_equal -10, @product.stock_available + + @product.shelf_locations.first.update!(saldo: 100) + assert_equal 90, @product.stock_available + + @product.update_attribute :ei_saldoa, :no_inventory_management + assert_equal 0, @product.stock_available + end + + test 'sales_order_rows product stock reserved by pick date' do + # set stock management by pick date + @product.company.parameter.update! saldo_kasittely: :stock_management_by_pick_date + assert_equal 0, @product.stock_reserved + + one = @product.sales_order_rows.first.dup + two = @product.sales_order_rows.first.dup + + # SO rows due to be picked today or in the past, should reserve stock (picked or not picked) + one.update! kerayspvm: Date.today, varattu: 10, keratty: '' + two.update! kerayspvm: Date.today, varattu: 10, keratty: 'joe' + assert_equal 20, @product.stock_reserved + + # SO rows due to be picked in the future, should not affect reserve stock, unless picked + one.update! kerayspvm: 1.day.from_now, varattu: 5, keratty: '' + two.update! kerayspvm: 1.day.from_now, varattu: 15, keratty: 'joe' + assert_equal 15, @product.stock_reserved + end + + test 'manufacture_rows product stock reserved by pick date' do + # set stock management by pick date + @product.company.parameter.update! saldo_kasittely: :stock_management_by_pick_date + assert_equal 0, @product.stock_reserved + + one = @product.manufacture_rows.first.dup + two = @product.manufacture_rows.first.dup + + # MF rows due to be picked today or in the past, should reserve stock (picked or not picked) + one.update! kerayspvm: Date.today, varattu: 10, keratty: '' + two.update! kerayspvm: Date.today, varattu: 10, keratty: 'joe' + assert_equal 20, @product.stock_reserved + + # MF rows due to be picked in the future, should not affect reserve stock, unless picked + one.update! kerayspvm: 1.day.from_now, varattu: 5, keratty: '' + two.update! kerayspvm: 1.day.from_now, varattu: 15, keratty: 'joe' + assert_equal 15, @product.stock_reserved + end + + test 'stock_transfer_rows product stock reserved by pick date' do + # set stock management by pick date + @product.company.parameter.update! saldo_kasittely: :stock_management_by_pick_date + assert_equal 0, @product.stock_reserved + + one = @product.stock_transfer_rows.first.dup + two = @product.stock_transfer_rows.first.dup + + # ST rows due to be picked today or in the past, should reserve stock (picked or not picked) + one.update! kerayspvm: Date.today, varattu: 10, keratty: '' + two.update! kerayspvm: Date.today, varattu: 10, keratty: 'joe' + assert_equal 20, @product.stock_reserved + + # ST rows due to be picked in the future, should not affect reserve stock, unless picked + one.update! kerayspvm: 1.day.from_now, varattu: 5, keratty: '' + two.update! kerayspvm: 1.day.from_now, varattu: 15, keratty: 'joe' + assert_equal 15, @product.stock_reserved + end + + test 'manufacture_composite_rows product stock reserved by pick date' do + # set stock management by pick date + @product.company.parameter.update! saldo_kasittely: :stock_management_by_pick_date + assert_equal 0, @product.stock_reserved + + # MF composite rows due to be picked today or earlier, should decrease stock reservation + @product.manufacture_composite_rows.first.update! kerayspvm: Date.today, varattu: 10, keratty: '' + assert_equal -10, @product.stock_reserved + + # MF composite rows due to be picked in the future, should not affect reserve stock + @product.manufacture_composite_rows.first.update! kerayspvm: 1.day.from_now, varattu: 10, keratty: '' + assert_equal 0, @product.stock_reserved + end + + test 'manufacture_recursive_composite_rows product stock reserved by pick date' do + # set stock management by pick date + @product.company.parameter.update! saldo_kasittely: :stock_management_by_pick_date + assert_equal 0, @product.stock_reserved + + # MF recursive composite rows due to be picked today or earlier, should decrease stock reservation + @product.manufacture_recursive_composite_rows.first.update! kerayspvm: Date.today, varattu: 10, keratty: '' + assert_equal -10, @product.stock_reserved + + # MF recursive composite rows due to be picked in the future, should not reserve stock + @product.manufacture_recursive_composite_rows.first.update! kerayspvm: 1.day.from_now, varattu: 10, keratty: '' + assert_equal 0, @product.stock_reserved + end + + test 'purchase_order_rows product stock reserved by pick date' do + # set stock management by pick date + @product.company.parameter.update! saldo_kasittely: :stock_management_by_pick_date + assert_equal 0, @product.stock_reserved + + # PO rows due in today or earlier, should decrease stock reservation + @product.purchase_order_rows.first.update! toimaika: Date.today, varattu: 10 + assert_equal -10, @product.stock_reserved + + # PO rows in the future, should not affect reserve stock + @product.purchase_order_rows.first.update! toimaika: 1.day.from_now, varattu: 10 + assert_equal 0, @product.reload.stock_reserved + end + + test 'product stock reserved by pick date with future reservations' do + # set stock management by pick date with future reservations + # this uses same logic as stock_management_by_pick_date, so we don't need to test everything again + # this returns the "worst case" stock reserve, se we'll never sell out our stock + @product.company.parameter.update! saldo_kasittely: :stock_management_by_pick_date_and_with_future_reservations + assert_equal 0, @product.stock_reserved + + one = @product.stock_transfer_rows.first + two = @product.stock_transfer_rows.first.dup + po_one = @product.purchase_order_rows.first + + # sell 100 day one, we have 100 reserved no matter what date + one.update! varattu: 100, kerayspvm: 1.day.from_now + assert_equal 100, @product.stock_reserved(stock_date: 1.day.ago) + assert_equal 100, @product.stock_reserved + assert_equal 100, @product.stock_reserved(stock_date: 1.day.from_now) + assert_equal 100, @product.stock_reserved(stock_date: 2.day.from_now) + assert_equal 100, @product.stock_reserved(stock_date: 3.day.from_now) + assert_equal 100, @product.stock_reserved(stock_date: 9.day.from_now) + + # purchase 60 day three, we have 100 reserved before purchase, and 40 after + po_one.update! varattu: 60, toimaika: 3.day.from_now + assert_equal 100, @product.stock_reserved(stock_date: 1.day.ago) + assert_equal 100, @product.stock_reserved + assert_equal 100, @product.stock_reserved(stock_date: 1.day.from_now) + assert_equal 100, @product.stock_reserved(stock_date: 2.day.from_now) + assert_equal 40, @product.stock_reserved(stock_date: 3.day.from_now) + assert_equal 40, @product.stock_reserved(stock_date: 9.day.from_now) + + # sell 30 day five, we have 100 reserved before puchase, and 70 after + two.update! varattu: 30, kerayspvm: 5.day.from_now + assert_equal 100, @product.stock_reserved(stock_date: 1.day.ago) + assert_equal 100, @product.stock_reserved + assert_equal 100, @product.stock_reserved(stock_date: 1.day.from_now) + assert_equal 100, @product.stock_reserved(stock_date: 2.day.from_now) + assert_equal 70, @product.stock_reserved(stock_date: 3.day.from_now) + assert_equal 70, @product.stock_reserved(stock_date: 9.day.from_now) end test 'valid status' do From 8d3d13b8389f9bc22ee1051c062f9f0815c8af13 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 10:48:17 +0200 Subject: [PATCH 07/46] Add online stores menu --- .../20151123084500_add_online_stores_menu.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 db/migrate/20151123084500_add_online_stores_menu.rb diff --git a/db/migrate/20151123084500_add_online_stores_menu.rb b/db/migrate/20151123084500_add_online_stores_menu.rb new file mode 100644 index 000000000..72880504a --- /dev/null +++ b/db/migrate/20151123084500_add_online_stores_menu.rb @@ -0,0 +1,18 @@ +require File.expand_path('test/permission_helper') +include PermissionHelper + +class AddOnlineStoresMenu < ActiveRecord::Migration + def up + PermissionHelper.add_item( + program: 'Tuotehallinta', + name: 'Verkkokaupat', + uri: 'pupenext/online_stores' + ) + end + + def down + PermissionHelper.remove_all( + uri: 'pupenext/online_stores' + ) + end +end From adf428603a2ba0ffffc19cfbbfee95d929478a3c Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 10:52:22 +0200 Subject: [PATCH 08/46] Add routes for online stores --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index 5954d4b7c..c59bd7322 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -57,6 +57,7 @@ resources :fiscal_years, except: :destroy resources :incoming_mails, only: :index resources :mail_servers + resources :online_stores resources :packages resources :packing_areas resources :printers From 91a5d401b3fe10db6fc83326490e2a22ffc75495 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 11:20:07 +0200 Subject: [PATCH 09/46] Override to_s --- app/models/online_store.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/online_store.rb b/app/models/online_store.rb index 3c43a9dee..bcc8fe5e1 100644 --- a/app/models/online_store.rb +++ b/app/models/online_store.rb @@ -4,4 +4,8 @@ class OnlineStore < ActiveRecord::Base has_and_belongs_to_many :products, join_table: :online_stores_products validates :name, presence: true, length: { in: 1..255 } + + def to_s + name + end end From d03d532cdc9f923b469ab0ce48a7151d878eb409 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 11:28:40 +0200 Subject: [PATCH 10/46] Add translations --- config/locales/fi.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/locales/fi.yml b/config/locales/fi.yml index e6620513b..89b9ba6f5 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -222,6 +222,8 @@ fi: smtp_password: SMPT-palvelimen salasana smtp_server: SMPT-palvelin smtp_username: SMPT-palvelimen käyttäjätunnus + online_store: + name: Nimi package: erikoispakkaus: Erikoispakkaus jarjestys: Järjestys From e05c5e85ee722b864974abef52cef891ae7203de Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 11:29:05 +0200 Subject: [PATCH 11/46] Add online stores form partial --- .../administration/online_stores/_form.html.erb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 app/views/administration/online_stores/_form.html.erb diff --git a/app/views/administration/online_stores/_form.html.erb b/app/views/administration/online_stores/_form.html.erb new file mode 100644 index 000000000..725e685a0 --- /dev/null +++ b/app/views/administration/online_stores/_form.html.erb @@ -0,0 +1,16 @@ +<%= render 'administration/form_errors', resource: @online_store %> + +<%= simple_form_for @online_store, + builder: AdminFormBuilder, + defaults: { input_html: { class: :large } }, + alias_set: params[:alias_set] do |f| %> + + + <%= f.input :name %> +
+ +
+ + <%= f.button :submit %> + +<% end %> From 00c4d46b33d42ea2e31e3ee4fb75daa0f8ccec26 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 11:39:51 +0200 Subject: [PATCH 12/46] Add translations --- config/locales/fi.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config/locales/fi.yml b/config/locales/fi.yml index 89b9ba6f5..7367468cb 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -572,6 +572,12 @@ fi: header: Sähköpostipalvelimet new: header: Uusi sähköpostipalvelin + online_stores: + edit: + button_remove: Poista verkkokauppa + confirm_remove: Haluatko varmasti poistaa tämän verkkokakupan? + index: + header: Verkkokaupat packages: create: create_success: Pakkaus lisättiin onnistuneesti From ae6b8efa925c0c1782605deff91350f96903f871 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 11:41:34 +0200 Subject: [PATCH 13/46] Add edit view --- app/views/administration/online_stores/edit.html.erb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 app/views/administration/online_stores/edit.html.erb diff --git a/app/views/administration/online_stores/edit.html.erb b/app/views/administration/online_stores/edit.html.erb new file mode 100644 index 000000000..fcc73a443 --- /dev/null +++ b/app/views/administration/online_stores/edit.html.erb @@ -0,0 +1,11 @@ +<%= return_link(t('administration.online_stores.index.header'), online_stores_path) %> + +<%= render 'administration/header', text: t('administration.online_stores.index.header') %> +<%= render 'form' %> + +
+ +<%= button_to(t('.button_remove'), + @online_store, + data: { confirm: t('administration.online_stores.edit.confirm_remove') }, + method: 'delete') %> From a7b44ad6b6d60c1e99bd82a377bf2dddaed900cf Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 11:46:16 +0200 Subject: [PATCH 14/46] Add index view --- .../online_stores/index.html.erb | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 app/views/administration/online_stores/index.html.erb diff --git a/app/views/administration/online_stores/index.html.erb b/app/views/administration/online_stores/index.html.erb new file mode 100644 index 000000000..56a0811ab --- /dev/null +++ b/app/views/administration/online_stores/index.html.erb @@ -0,0 +1,23 @@ +<%= render 'administration/header', text: t('.header') %> + +
+ + + + + + + + + + <% @online_stores.each do |online_store| %> + + + + <% end %> + +
<%= OnlineStore.human_attribute_name(:name) %>
<%= link_to online_store, online_store %>
+ +
+ +<%= button_to_new 'online_store', new_online_store_path %> From a17c442853df03389709656ecc7cd26fb156b85c Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 11:47:47 +0200 Subject: [PATCH 15/46] Add new view --- app/views/administration/online_stores/new.html.erb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 app/views/administration/online_stores/new.html.erb diff --git a/app/views/administration/online_stores/new.html.erb b/app/views/administration/online_stores/new.html.erb new file mode 100644 index 000000000..fb3e7ddc0 --- /dev/null +++ b/app/views/administration/online_stores/new.html.erb @@ -0,0 +1,4 @@ +<%= return_link(t('administration.online_stores.index.header'), online_stores_path) %> + +<%= render 'administration/header', text: t('administration.online_stores.index.header') %> +<%= render 'form' %> From 5085b5bfc6aab1e85012ff0273401e4c77ba620b Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 12:05:48 +0200 Subject: [PATCH 16/46] Add online_stores to permissions fixtures --- test/fixtures/permissions.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fixtures/permissions.yml b/test/fixtures/permissions.yml index e8efd459d..a4afea01c 100644 --- a/test/fixtures/permissions.yml +++ b/test/fixtures/permissions.yml @@ -23,6 +23,7 @@ full_installments incoming_mails mail_servers + online_stores packages packing_areas pending_product_updates From ea989cdf826e756649978bfb133300b80f673d8e Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 12:06:19 +0200 Subject: [PATCH 17/46] Use OnlineStore.model_name as the model name for OnlineStore's children --- app/models/online_store/magento.rb | 3 +++ app/models/online_store/pupeshop.rb | 3 +++ 2 files changed, 6 insertions(+) diff --git a/app/models/online_store/magento.rb b/app/models/online_store/magento.rb index 2ae479f67..661a962be 100644 --- a/app/models/online_store/magento.rb +++ b/app/models/online_store/magento.rb @@ -1,2 +1,5 @@ class OnlineStore::Magento < OnlineStore + def self.model_name + OnlineStore.model_name + end end diff --git a/app/models/online_store/pupeshop.rb b/app/models/online_store/pupeshop.rb index 579e821ea..444351eac 100644 --- a/app/models/online_store/pupeshop.rb +++ b/app/models/online_store/pupeshop.rb @@ -1,2 +1,5 @@ class OnlineStore::Pupeshop < OnlineStore + def self.model_name + OnlineStore.model_name + end end From b230cb7a9a0df500e636906db4b7da5af067da5d Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 12:06:47 +0200 Subject: [PATCH 18/46] Add OnlineStoresController and tests --- .../online_stores_controller.rb | 49 +++++++++++++++ .../online_stores_controller_test.rb | 62 +++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 app/controllers/administration/online_stores_controller.rb create mode 100644 test/controllers/administration/online_stores_controller_test.rb diff --git a/app/controllers/administration/online_stores_controller.rb b/app/controllers/administration/online_stores_controller.rb new file mode 100644 index 000000000..b8fd035ec --- /dev/null +++ b/app/controllers/administration/online_stores_controller.rb @@ -0,0 +1,49 @@ +class Administration::OnlineStoresController < AdministrationController + def index + @online_stores = OnlineStore.all + end + + def show + render :edit + end + + def new + @online_store = OnlineStore.new + end + + def edit; end + + def create + @online_store = OnlineStore.new(online_store_params) + + if @online_store.save + redirect_to online_stores_url, notice: t('.success') + else + render :new + end + end + + def update + if @online_store.update(online_store_params) + redirect_to online_stores_url, notice: t('.success') + else + render :edit + end + end + + def destroy + @online_store.destroy + + redirect_to online_stores_url, notice: t('.success') + end + + private + + def find_resource + @online_store = OnlineStore.find(params[:id]) + end + + def online_store_params + params.require(:online_store).permit(:name) + end +end diff --git a/test/controllers/administration/online_stores_controller_test.rb b/test/controllers/administration/online_stores_controller_test.rb new file mode 100644 index 000000000..e260f4e8f --- /dev/null +++ b/test/controllers/administration/online_stores_controller_test.rb @@ -0,0 +1,62 @@ +require 'test_helper' + +class Administration::OnlineStoresControllerTest < ActionController::TestCase + fixtures %w( + online_stores + ) + + setup do + login users(:bob) + + @pupeshop = online_stores(:pupeshop) + @magento = online_stores(:magento) + end + + test "index works" do + get :index + + assert_response :success + assert_not_nil assigns(:online_stores) + end + + test "new works" do + get :new + + assert_response :success + end + + test "create works" do + assert_difference('OnlineStore.count', 1) do + post :create, online_store: { name: 'Test name 1' } + end + + assert_redirected_to online_stores_url + end + + test "show renders edit" do + get :show, id: @pupeshop + + assert_response :success + assert_template :edit + end + + test "edit works" do + get :edit, id: @pupeshop + + assert_response :success + end + + test "update works" do + patch :update, id: @pupeshop, online_store: { name: 'Updated name' } + + assert_redirected_to online_stores_url + end + + test "destroy works" do + assert_difference('OnlineStore.count', -1) do + delete :destroy, id: @pupeshop + end + + assert_redirected_to online_stores_path + end +end From 72c40595bf3f0d7a9afd2e60557145fa7f6f1288 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 12:08:33 +0200 Subject: [PATCH 19/46] Add translations --- config/locales/fi.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/config/locales/fi.yml b/config/locales/fi.yml index 7367468cb..fc96166c7 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -573,11 +573,17 @@ fi: new: header: Uusi sähköpostipalvelin online_stores: + create: + success: Verkkokauppa luotiin onnistuneesti + destroy: + success: Verkkokauppa poistettiin onnistuneesti edit: button_remove: Poista verkkokauppa confirm_remove: Haluatko varmasti poistaa tämän verkkokakupan? index: header: Verkkokaupat + update: + success: Verkkokauppa päivitettiin onnistuneesti packages: create: create_success: Pakkaus lisättiin onnistuneesti From b805a879578b4b5cfe18494b5371ee50a9547494 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:14:12 +0200 Subject: [PATCH 20/46] Add translations --- config/locales/fi.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/locales/fi.yml b/config/locales/fi.yml index fc96166c7..5cc4d3776 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -224,6 +224,7 @@ fi: smtp_username: SMPT-palvelimen käyttäjätunnus online_store: name: Nimi + theme: Teema package: erikoispakkaus: Erikoispakkaus jarjestys: Järjestys From 850669873a90c01b0b7de30d4b3e30a63d715eb7 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:14:30 +0200 Subject: [PATCH 21/46] Override to_s method --- app/models/online_store_theme.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/online_store_theme.rb b/app/models/online_store_theme.rb index ea3336b52..3ec0188d0 100644 --- a/app/models/online_store_theme.rb +++ b/app/models/online_store_theme.rb @@ -1,3 +1,7 @@ class OnlineStoreTheme < ActiveRecord::Base validates :name, presence: true, length: { in: 1..255 } + + def to_s + name + end end From 91105abaabcf43ff60fd4f18925edc01fe49ba05 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:14:53 +0200 Subject: [PATCH 22/46] Show theme in index --- app/views/administration/online_stores/index.html.erb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/administration/online_stores/index.html.erb b/app/views/administration/online_stores/index.html.erb index 56a0811ab..e065600f4 100644 --- a/app/views/administration/online_stores/index.html.erb +++ b/app/views/administration/online_stores/index.html.erb @@ -6,6 +6,7 @@ <%= OnlineStore.human_attribute_name(:name) %> + <%= OnlineStore.human_attribute_name(:theme) %> @@ -13,6 +14,7 @@ <% @online_stores.each do |online_store| %> <%= link_to online_store, online_store %> + <%= online_store.theme %> <% end %> From 7f43ac383abc729c930f075f00127a0a4dc8aa59 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:15:04 +0200 Subject: [PATCH 23/46] Allow changing theme --- .../administration/online_stores_controller.rb | 3 ++- app/views/administration/online_stores/_form.html.erb | 1 + .../administration/online_stores_controller_test.rb | 10 ++++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/controllers/administration/online_stores_controller.rb b/app/controllers/administration/online_stores_controller.rb index b8fd035ec..8acf76ab6 100644 --- a/app/controllers/administration/online_stores_controller.rb +++ b/app/controllers/administration/online_stores_controller.rb @@ -44,6 +44,7 @@ def find_resource end def online_store_params - params.require(:online_store).permit(:name) + params.require(:online_store).permit(:name, + :online_store_theme_id) end end diff --git a/app/views/administration/online_stores/_form.html.erb b/app/views/administration/online_stores/_form.html.erb index 725e685a0..ff6e4e4a8 100644 --- a/app/views/administration/online_stores/_form.html.erb +++ b/app/views/administration/online_stores/_form.html.erb @@ -7,6 +7,7 @@ <%= f.input :name %> + <%= f.association :theme %>

diff --git a/test/controllers/administration/online_stores_controller_test.rb b/test/controllers/administration/online_stores_controller_test.rb index e260f4e8f..c02d44a44 100644 --- a/test/controllers/administration/online_stores_controller_test.rb +++ b/test/controllers/administration/online_stores_controller_test.rb @@ -2,12 +2,16 @@ class Administration::OnlineStoresControllerTest < ActionController::TestCase fixtures %w( + online_store_themes online_stores ) setup do login users(:bob) + @theme_one = online_store_themes(:one) + @theme_two = online_store_themes(:two) + @pupeshop = online_stores(:pupeshop) @magento = online_stores(:magento) end @@ -27,7 +31,8 @@ class Administration::OnlineStoresControllerTest < ActionController::TestCase test "create works" do assert_difference('OnlineStore.count', 1) do - post :create, online_store: { name: 'Test name 1' } + post :create, online_store: { name: 'Test name 1', + online_store_theme_id: @theme_one.id } end assert_redirected_to online_stores_url @@ -47,7 +52,8 @@ class Administration::OnlineStoresControllerTest < ActionController::TestCase end test "update works" do - patch :update, id: @pupeshop, online_store: { name: 'Updated name' } + patch :update, id: @pupeshop, online_store: { name: 'Updated name', + online_store_theme_id: @theme_two.id } assert_redirected_to online_stores_url end From 2e853f6a79f2a250baf3b0a40810c4fa5fb04a2c Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:41:55 +0200 Subject: [PATCH 24/46] Monkey patch class to respond_to demodulize --- config/initializers/patches.rb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 config/initializers/patches.rb diff --git a/config/initializers/patches.rb b/config/initializers/patches.rb new file mode 100644 index 000000000..22cba8a94 --- /dev/null +++ b/config/initializers/patches.rb @@ -0,0 +1,5 @@ +class Class + def demodulize + to_s.demodulize + end +end From e725702fa7281b69d5213c6df6efcfe5e97a173f Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:42:48 +0200 Subject: [PATCH 25/46] Allow selecting type in form --- app/views/administration/online_stores/_form.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/administration/online_stores/_form.html.erb b/app/views/administration/online_stores/_form.html.erb index ff6e4e4a8..94fc4b633 100644 --- a/app/views/administration/online_stores/_form.html.erb +++ b/app/views/administration/online_stores/_form.html.erb @@ -8,6 +8,7 @@ <%= f.input :name %> <%= f.association :theme %> + <%= f.input :type, collection: OnlineStore.subclasses, label_method: :demodulize %>

From b5fb05ba9405a28dfe4cbd0c046217d3afd2384d Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:43:56 +0200 Subject: [PATCH 26/46] Add translations --- config/locales/fi.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/locales/fi.yml b/config/locales/fi.yml index 5cc4d3776..5bf455720 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -225,6 +225,7 @@ fi: online_store: name: Nimi theme: Teema + type: Tyyppi package: erikoispakkaus: Erikoispakkaus jarjestys: Järjestys From ec911aab78cd2ae7abf0c2e1ae60cadbcc657786 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:44:18 +0200 Subject: [PATCH 27/46] Show type in index --- app/views/administration/online_stores/index.html.erb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/administration/online_stores/index.html.erb b/app/views/administration/online_stores/index.html.erb index e065600f4..ec3f8753b 100644 --- a/app/views/administration/online_stores/index.html.erb +++ b/app/views/administration/online_stores/index.html.erb @@ -7,6 +7,7 @@ <%= OnlineStore.human_attribute_name(:name) %> <%= OnlineStore.human_attribute_name(:theme) %> + <%= OnlineStore.human_attribute_name(:type) %> @@ -15,6 +16,7 @@ <%= link_to online_store, online_store %> <%= online_store.theme %> + <%= online_store.type.try(:demodulize) %> <% end %> From edaf0e964bafa69465aa8bb5514982228c859d13 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:44:34 +0200 Subject: [PATCH 28/46] Add type to allowed parameters --- app/controllers/administration/online_stores_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/administration/online_stores_controller.rb b/app/controllers/administration/online_stores_controller.rb index 8acf76ab6..eebe31ee4 100644 --- a/app/controllers/administration/online_stores_controller.rb +++ b/app/controllers/administration/online_stores_controller.rb @@ -45,6 +45,7 @@ def find_resource def online_store_params params.require(:online_store).permit(:name, - :online_store_theme_id) + :online_store_theme_id, + :type) end end From a6e19e9c55e7e5ed716f536021e2c37b00f84838 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 13:47:15 +0200 Subject: [PATCH 29/46] Use eager loading in development --- config/environments/development.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index 23018de3f..de6d3badf 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -7,7 +7,7 @@ config.cache_classes = false # Do not eager load code on boot. - config.eager_load = false + config.eager_load = true # Show full error reports and disable caching. config.consider_all_requests_local = true From 2af88ddfcbc3a9f9f946f625d710620a9d499ad9 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 14:13:40 +0200 Subject: [PATCH 30/46] Revert "Use eager loading in development" This reverts commit be3828b803a1a36470c52874f7c5d3d42aca8420. --- config/environments/development.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index de6d3badf..23018de3f 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -7,7 +7,7 @@ config.cache_classes = false # Do not eager load code on boot. - config.eager_load = true + config.eager_load = false # Show full error reports and disable caching. config.consider_all_requests_local = true From f72742f0636b8ddbfd2726d680896b9fa32d03c1 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 14:14:30 +0200 Subject: [PATCH 31/46] Include PupenextSingleTableInheritance --- app/models/online_store.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/models/online_store.rb b/app/models/online_store.rb index bcc8fe5e1..a35a449a5 100644 --- a/app/models/online_store.rb +++ b/app/models/online_store.rb @@ -1,4 +1,6 @@ class OnlineStore < ActiveRecord::Base + include PupenextSingleTableInheritance + belongs_to :theme, foreign_key: :online_store_theme_id, class_name: 'OnlineStoreTheme' has_and_belongs_to_many :products, join_table: :online_stores_products @@ -8,4 +10,11 @@ class OnlineStore < ActiveRecord::Base def to_s name end + + def self.child_class_names + [ + OnlineStore::Magento, + OnlineStore::Pupeshop + ].index_by(&:to_s) + end end From cdd891cc3d29b8650963e0c421253a380b9d07ca Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 14:16:59 +0200 Subject: [PATCH 32/46] Revert "Monkey patch class to respond_to demodulize" This reverts commit 15fd092d5f6b7bacfe85c99f72dbb1b65b3ac17c. --- config/initializers/patches.rb | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 config/initializers/patches.rb diff --git a/config/initializers/patches.rb b/config/initializers/patches.rb deleted file mode 100644 index 22cba8a94..000000000 --- a/config/initializers/patches.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Class - def demodulize - to_s.demodulize - end -end From fe45a07180f8bc9ea5b4b1aa3976e3f02933930f Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 14:18:09 +0200 Subject: [PATCH 33/46] Use child_class_names method --- app/views/administration/online_stores/_form.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/administration/online_stores/_form.html.erb b/app/views/administration/online_stores/_form.html.erb index 94fc4b633..bb3be9feb 100644 --- a/app/views/administration/online_stores/_form.html.erb +++ b/app/views/administration/online_stores/_form.html.erb @@ -8,7 +8,7 @@ <%= f.input :name %> <%= f.association :theme %> - <%= f.input :type, collection: OnlineStore.subclasses, label_method: :demodulize %> + <%= f.input :type, collection: OnlineStore.child_class_names.keys, label_method: :demodulize %>

From 6f26d51d3c1a5603ce7d7c789cc9a0d87a68a632 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 14:36:24 +0200 Subject: [PATCH 34/46] Load online_stores fixtures --- test/models/product_test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/models/product_test.rb b/test/models/product_test.rb index 137ba2641..cb624f863 100644 --- a/test/models/product_test.rb +++ b/test/models/product_test.rb @@ -11,6 +11,7 @@ class ProductTest < ActiveSupport::TestCase keyword/customer_subcategories keywords manufacture_order/rows + online_stores pending_updates product/keywords product/suppliers From 60e54cb4f4371557db95236f5781400656b06219 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 14:36:42 +0200 Subject: [PATCH 35/46] Post type too --- .../administration/online_stores_controller_test.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/controllers/administration/online_stores_controller_test.rb b/test/controllers/administration/online_stores_controller_test.rb index c02d44a44..984d220e5 100644 --- a/test/controllers/administration/online_stores_controller_test.rb +++ b/test/controllers/administration/online_stores_controller_test.rb @@ -32,7 +32,8 @@ class Administration::OnlineStoresControllerTest < ActionController::TestCase test "create works" do assert_difference('OnlineStore.count', 1) do post :create, online_store: { name: 'Test name 1', - online_store_theme_id: @theme_one.id } + online_store_theme_id: @theme_one.id, + type: 'OnlineStore::Pupeshop'} end assert_redirected_to online_stores_url @@ -53,7 +54,8 @@ class Administration::OnlineStoresControllerTest < ActionController::TestCase test "update works" do patch :update, id: @pupeshop, online_store: { name: 'Updated name', - online_store_theme_id: @theme_two.id } + online_store_theme_id: @theme_two.id, + type: 'OnlineStore::Magento'} assert_redirected_to online_stores_url end From da60bedc38a5285c46113b1f8b4d5d69a3fd6c91 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 14:48:11 +0200 Subject: [PATCH 36/46] Add company association to online store --- app/models/company.rb | 1 + app/models/online_store.rb | 3 ++- .../20151123123807_add_company_to_online_stores.rb | 5 +++++ test/fixtures/online_stores.yml | 14 ++++++++------ test/models/company_test.rb | 1 + test/models/online_store_test.rb | 3 +++ 6 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20151123123807_add_company_to_online_stores.rb diff --git a/app/models/company.rb b/app/models/company.rb index f4a25f0a8..66ac4e10b 100644 --- a/app/models/company.rb +++ b/app/models/company.rb @@ -73,6 +73,7 @@ class Company < ActiveRecord::Base has_many :customer_transports, through: :customers, source: :transports has_many :mail_servers has_many :incoming_mails, through: :mail_servers + has_many :online_stores accepts_nested_attributes_for :bank_accounts, :users diff --git a/app/models/online_store.rb b/app/models/online_store.rb index a35a449a5..1042cd879 100644 --- a/app/models/online_store.rb +++ b/app/models/online_store.rb @@ -1,6 +1,7 @@ -class OnlineStore < ActiveRecord::Base +class OnlineStore < BaseModel include PupenextSingleTableInheritance + belongs_to :company belongs_to :theme, foreign_key: :online_store_theme_id, class_name: 'OnlineStoreTheme' has_and_belongs_to_many :products, join_table: :online_stores_products diff --git a/db/migrate/20151123123807_add_company_to_online_stores.rb b/db/migrate/20151123123807_add_company_to_online_stores.rb new file mode 100644 index 000000000..5ef5e5591 --- /dev/null +++ b/db/migrate/20151123123807_add_company_to_online_stores.rb @@ -0,0 +1,5 @@ +class AddCompanyToOnlineStores < ActiveRecord::Migration + def change + add_reference :online_stores, :company, index: true, foreign_key: true + end +end diff --git a/test/fixtures/online_stores.yml b/test/fixtures/online_stores.yml index afcf16f3d..f24a8ea2b 100644 --- a/test/fixtures/online_stores.yml +++ b/test/fixtures/online_stores.yml @@ -1,11 +1,13 @@ pupeshop: - name: Pupeshop - type: OnlineStore::Pupeshop - theme: one + company: acme + name: Pupeshop + type: OnlineStore::Pupeshop + theme: one products: hammer, helmet magento: - name: Magento - type: OnlineStore::Magento - theme: two + company: acme + name: Magento + type: OnlineStore::Magento + theme: two products: helmet diff --git a/test/models/company_test.rb b/test/models/company_test.rb index c51f1a5ef..03a46fb69 100644 --- a/test/models/company_test.rb +++ b/test/models/company_test.rb @@ -54,6 +54,7 @@ class CompanyTest < ActiveSupport::TestCase assert @acme.warehouses.count > 0 assert @acme.incoming_mails.count > 0 assert @acme.permissions.count > 0 + assert @acme.online_stores.count > 0 assert_equal 1, @acme.menus.count assert_equal menus(:acme_menu_1), @acme.menus.first diff --git a/test/models/online_store_test.rb b/test/models/online_store_test.rb index 0823ec9d3..765aff0a2 100644 --- a/test/models/online_store_test.rb +++ b/test/models/online_store_test.rb @@ -26,6 +26,9 @@ class OnlineStoreTest < ActiveSupport::TestCase assert_not_empty @pupeshop.products assert_not_empty @magento.products + + assert_equal companies(:acme), @pupeshop.company + assert_equal companies(:acme), @magento.company end test 'STI works' do From f7e98c5b7a0ec4ce098e2b291b4d5d88c6297b63 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 14:51:38 +0200 Subject: [PATCH 37/46] Combine migrations --- db/migrate/20151123071517_create_online_stores.rb | 1 + db/migrate/20151123123807_add_company_to_online_stores.rb | 5 ----- 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 db/migrate/20151123123807_add_company_to_online_stores.rb diff --git a/db/migrate/20151123071517_create_online_stores.rb b/db/migrate/20151123071517_create_online_stores.rb index 18379c284..c2af2b9a4 100644 --- a/db/migrate/20151123071517_create_online_stores.rb +++ b/db/migrate/20151123071517_create_online_stores.rb @@ -4,6 +4,7 @@ def change t.string :name t.string :type t.references :online_store_theme, index: true, foreign_key: true + t.references :company, index: true, foreign_key: true t.timestamps null: false end diff --git a/db/migrate/20151123123807_add_company_to_online_stores.rb b/db/migrate/20151123123807_add_company_to_online_stores.rb deleted file mode 100644 index 5ef5e5591..000000000 --- a/db/migrate/20151123123807_add_company_to_online_stores.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddCompanyToOnlineStores < ActiveRecord::Migration - def change - add_reference :online_stores, :company, index: true, foreign_key: true - end -end From a4b69f9bdb176db1ef7df71e2482e6c8d072c31f Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 14:56:13 +0200 Subject: [PATCH 38/46] Add online store association to online store theme --- app/models/online_store_theme.rb | 2 ++ test/models/online_store_theme_test.rb | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/app/models/online_store_theme.rb b/app/models/online_store_theme.rb index 3ec0188d0..43ea278c7 100644 --- a/app/models/online_store_theme.rb +++ b/app/models/online_store_theme.rb @@ -1,4 +1,6 @@ class OnlineStoreTheme < ActiveRecord::Base + has_many :online_stores + validates :name, presence: true, length: { in: 1..255 } def to_s diff --git a/test/models/online_store_theme_test.rb b/test/models/online_store_theme_test.rb index 27f85d36e..4735fe72e 100644 --- a/test/models/online_store_theme_test.rb +++ b/test/models/online_store_theme_test.rb @@ -14,4 +14,9 @@ class OnlineStoreThemeTest < ActiveSupport::TestCase assert @one.valid?, @one.errors.full_messages assert @two.valid?, @two.errors.full_messages end + + test 'associations work' do + assert_not_empty @one.online_stores + assert_not_empty @two.online_stores + end end From 1fd93720c8b251decbbbd38a3114c2572152cd6c Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 15:04:54 +0200 Subject: [PATCH 39/46] Add translations --- config/locales/fi.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/locales/fi.yml b/config/locales/fi.yml index 5bf455720..28f7309ae 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -399,6 +399,7 @@ fi: keyword/customer_subcategory: Asiakasryhmä keyword/revenue_expenditure: Vaihtoehtoinen meno mail_server: Sähköpostipalvelin + online_store: Verkkokauppa package: Pakkaus package_code: Pakkauskoodi packing_area: Pakkaamo From 7910f1d1694d84901e3f2b1f8260ebd56c90d0e8 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 15:05:04 +0200 Subject: [PATCH 40/46] Don't include blank --- app/views/administration/online_stores/_form.html.erb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/views/administration/online_stores/_form.html.erb b/app/views/administration/online_stores/_form.html.erb index bb3be9feb..6a85492fc 100644 --- a/app/views/administration/online_stores/_form.html.erb +++ b/app/views/administration/online_stores/_form.html.erb @@ -8,7 +8,9 @@ <%= f.input :name %> <%= f.association :theme %> - <%= f.input :type, collection: OnlineStore.child_class_names.keys, label_method: :demodulize %> + <%= f.input :type, collection: OnlineStore.child_class_names.keys, + label_method: :demodulize, + include_blank: false %>

From c8ebf2849fd15767b79f51ea87f9f28294028352 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 15:07:48 +0200 Subject: [PATCH 41/46] Add PrestaShop --- app/models/online_store.rb | 1 + app/models/online_store/presta_shop.rb | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 app/models/online_store/presta_shop.rb diff --git a/app/models/online_store.rb b/app/models/online_store.rb index 1042cd879..5d499df10 100644 --- a/app/models/online_store.rb +++ b/app/models/online_store.rb @@ -15,6 +15,7 @@ def to_s def self.child_class_names [ OnlineStore::Magento, + OnlineStore::PrestaShop, OnlineStore::Pupeshop ].index_by(&:to_s) end diff --git a/app/models/online_store/presta_shop.rb b/app/models/online_store/presta_shop.rb new file mode 100644 index 000000000..8a597dbe5 --- /dev/null +++ b/app/models/online_store/presta_shop.rb @@ -0,0 +1,5 @@ +class OnlineStore::PrestaShop < OnlineStore + def self.model_name + OnlineStore.model_name + end +end From 1fd5c676f9774e8fde0185598b483e96aba943ac Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 15:10:25 +0200 Subject: [PATCH 42/46] Update schema --- db/schema.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/db/schema.rb b/db/schema.rb index 7ab16e386..37e7de974 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1705,6 +1705,32 @@ add_index "oikeu", ["yhtio", "kuka", "sovellus"], name: "sovellus_index", using: :btree add_index "oikeu", ["yhtio", "sovellus", "nimi", "alanimi"], name: "menut_index", using: :btree + create_table "online_store_themes", force: :cascade do |t| + t.string "name", limit: 255 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "online_stores", force: :cascade do |t| + t.string "name", limit: 255 + t.string "type", limit: 255 + t.integer "online_store_theme_id", limit: 4 + t.integer "company_id", limit: 4 + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "online_stores", ["company_id"], name: "index_online_stores_on_company_id", using: :btree + add_index "online_stores", ["online_store_theme_id"], name: "index_online_stores_on_online_store_theme_id", using: :btree + + create_table "online_stores_products", force: :cascade do |t| + t.integer "online_store_id", limit: 4 + t.integer "product_id", limit: 4 + end + + add_index "online_stores_products", ["online_store_id"], name: "index_online_stores_products_on_online_store_id", using: :btree + add_index "online_stores_products", ["product_id"], name: "index_online_stores_products_on_product_id", using: :btree + create_table "pakkaamo", primary_key: "tunnus", force: :cascade do |t| t.string "yhtio", limit: 5, default: "", null: false t.string "nimi", limit: 150, default: "", null: false From d8841531001ea0cab772f6183ab6fb8743471d90 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 15:15:36 +0200 Subject: [PATCH 43/46] Remove unnecessary try --- app/views/administration/online_stores/index.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/administration/online_stores/index.html.erb b/app/views/administration/online_stores/index.html.erb index ec3f8753b..9cc1bfd47 100644 --- a/app/views/administration/online_stores/index.html.erb +++ b/app/views/administration/online_stores/index.html.erb @@ -16,7 +16,7 @@ <%= link_to online_store, online_store %> <%= online_store.theme %> - <%= online_store.type.try(:demodulize) %> + <%= online_store.type.demodulize %> <% end %> From 4ecdd1e1c2dfa41ad3639e899c8a53c23d296838 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 16:26:18 +0200 Subject: [PATCH 44/46] Allow adding and updating products --- app/controllers/administration/online_stores_controller.rb | 1 + app/views/administration/online_stores/_form.html.erb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/app/controllers/administration/online_stores_controller.rb b/app/controllers/administration/online_stores_controller.rb index eebe31ee4..edeb5caea 100644 --- a/app/controllers/administration/online_stores_controller.rb +++ b/app/controllers/administration/online_stores_controller.rb @@ -46,6 +46,7 @@ def find_resource def online_store_params params.require(:online_store).permit(:name, :online_store_theme_id, + { product_ids: [] }, :type) end end diff --git a/app/views/administration/online_stores/_form.html.erb b/app/views/administration/online_stores/_form.html.erb index 6a85492fc..b2e724b67 100644 --- a/app/views/administration/online_stores/_form.html.erb +++ b/app/views/administration/online_stores/_form.html.erb @@ -11,6 +11,8 @@ <%= f.input :type, collection: OnlineStore.child_class_names.keys, label_method: :demodulize, include_blank: false %> + <%= f.association :products, collection: Product.active, + input_html: { class: %w(large chosen-select) } %>
From 6e2903951c9ca1aad7aa98e2040d61cfc26e45b7 Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 16:26:34 +0200 Subject: [PATCH 45/46] Override to_s method --- app/models/product.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/product.rb b/app/models/product.rb index 8568e09fc..556abf6f4 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -133,6 +133,10 @@ def contract_price?(target) end end + def to_s + "#{tuoteno}: #{nimitys}" + end + private def defaults From 7c281975b222c97683b3016a76273455b1dd255d Mon Sep 17 00:00:00 2001 From: tiere Date: Mon, 23 Nov 2015 16:30:02 +0200 Subject: [PATCH 46/46] Add translations --- config/locales/fi.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/locales/fi.yml b/config/locales/fi.yml index 28f7309ae..0351515bc 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -224,6 +224,7 @@ fi: smtp_username: SMPT-palvelimen käyttäjätunnus online_store: name: Nimi + products: Tuotteet theme: Teema type: Tyyppi package: