tiny_builder 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e052508ea63349c2f46abe6399a5c8bc1300601f832f03dc1494b515935e5449
4
- data.tar.gz: 1e2393239ba81d30bd1051db3da032579f4aec616b6fa4df50020f368ddafbed
3
+ metadata.gz: 8b46b87356444087ac075c399868e32548a53a135a451e1697f1f670b5f4085e
4
+ data.tar.gz: cb7757ded6ebbbb6d2d10da0a1e143b3e7a815927801372c4aad212121580686
5
5
  SHA512:
6
- metadata.gz: 5894b6bc636d5e63db1233e5035bd10154ca97ec5b9b933a480c7e50a8d77b7a1fd9a5c2a6c53d809aca3f0ffdfb6608c97b3617bf5ec131cd85fee6cd1c4448
7
- data.tar.gz: 83e9a103b7e32e27e0987444df03407fc766cb7aa200abc9c3c1232bc846db48c7fd55e84b265b327aed5bcb6f6fafa3f8e629d5c0b967f920bc39635fcad134
6
+ metadata.gz: ae0bda9b41bf3be01d1ba27314efa948c618f712b82b1d10b82d01339a3fa6c46a671e2ff9a3b08759d3a85692e3c0b291ff46a12a891c02ef22904a0f3eea6c
7
+ data.tar.gz: 2798fc7272b2414ce3739bb40a0da74ed9c1cbeb91a7ec6f31afa624a1f099b85b962a87e269ddfb01de991de5727c89736644ec89cc5211947d603bc5a56fcd
@@ -4,7 +4,7 @@ class MongoVariantListing
4
4
  include Mongoid::Document
5
5
  include Mongoid::Attributes::Dynamic
6
6
 
7
- store_in collection: 'listings'
7
+ store_in collection: 'variant_listings'
8
8
 
9
9
  def local_item_id
10
10
  item_listing.local_id
@@ -7,13 +7,13 @@ class TinyBuilder
7
7
  def to_h(listing)
8
8
  {
9
9
  active: listing.active,
10
- active_variant: active_variant
10
+ active_variant: active_variant(listing)
11
11
  }
12
12
  end
13
13
 
14
14
  private
15
15
 
16
- def active_variant
16
+ def active_variant(listing)
17
17
  listing.item_listing.variants.map do |v_id|
18
18
  MongoVariantListing.find(v_id)
19
19
  end.select { |mvl| mvl.active }.size
@@ -4,7 +4,7 @@ require 'tiny_builder/data_loader'
4
4
 
5
5
  class TinyBuilder
6
6
  class BaseBuilder
7
- DataLoader
7
+ include DataLoader
8
8
 
9
9
  attr_reader :listings
10
10
  attr_reader :mwh
@@ -36,7 +36,7 @@ class TinyBuilder
36
36
  end
37
37
 
38
38
  def mapped_wh_mappings
39
- @mapped_wh_mappings ||= warehouse_mappings.select(:warehouse_id, :channel_warehouse_id).to_h
39
+ @mapped_wh_mappings ||= warehouse_mappings.pluck(:warehouse_id, :channel_warehouse_id).to_h
40
40
  end
41
41
 
42
42
  def associated_listings
@@ -53,7 +53,7 @@ class TinyBuilder
53
53
 
54
54
  def stock_allocations
55
55
  @stock_allocations ||= VariantListingStockAllocation.where(
56
- variant_association_id: listing_ids
56
+ variant_association_id: listings.map(&:icava_id)
57
57
  ).where('end_at >= NOW()')
58
58
  end
59
59
 
@@ -90,7 +90,7 @@ class TinyBuilder
90
90
  end
91
91
 
92
92
  def associated_stock_allocs(listing)
93
- ids = associated_listings.select{ |l| l.master_variant_id == listing.maaster_variant_id }.map(&:icava_id)
93
+ ids = associated_listings.select{ |l| l.master_variant_id == listing.master_variant_id }.map(&:icava_id)
94
94
  associated_stock_allocations.map do |asa|
95
95
  if ids.include?(asa)
96
96
  asa
@@ -2,22 +2,19 @@
2
2
 
3
3
  class TinyBuilder
4
4
  module ModeExecutor
5
- def build_quantity
6
- TinyBuilder::QuantityBuilder.new(
5
+ def perform_builder
6
+ builder_classes[mode].new(
7
7
  listings: listings,
8
8
  mwh: mwh
9
9
  ).perform
10
10
  end
11
11
 
12
- def build_price
13
- []
14
- end
15
-
16
- def build_active
17
- []
18
- end
19
-
20
- def quantity_params(listing)
12
+ def builder_classes
13
+ {
14
+ quantity: TinyBuilder::QuantityBuilder,
15
+ price: TinyBuilder::PriceBuilder,
16
+ active: TinyBuilder::ActiveBuilder
17
+ }
21
18
  end
22
19
 
23
20
  def listings
@@ -7,10 +7,28 @@ class TinyBuilder
7
7
  def to_h(listing)
8
8
  {
9
9
  price: listing.price,
10
- sale_price: listing.sale_price,
11
- sale_start_at: listing.sale_start_at,
12
- sale_end_at: listing.sale_end_at
10
+ sale_price: sale_price(listing),
11
+ sale_start_at: sale_start_at(listing),
12
+ sale_end_at: sale_end_at(listing)
13
13
  }
14
14
  end
15
+
16
+ def sale_price(listing)
17
+ listing.sale_price
18
+ rescue
19
+ nil
20
+ end
21
+
22
+ def sale_start_at(listing)
23
+ listing.sale_start_at
24
+ rescue
25
+ nil
26
+ end
27
+
28
+ def sale_end_at(listing)
29
+ listing.sale_end_at
30
+ rescue
31
+ nil
32
+ end
15
33
  end
16
34
  end
@@ -14,7 +14,7 @@ class TinyBuilder
14
14
  end
15
15
 
16
16
  def warehouse_quantities(listing)
17
- results = multi_warehouse_spaces(listing).map do |wh_space|
17
+ multi_warehouse_spaces(listing).map do |wh_space|
18
18
  {
19
19
  id: wh_space&.warehouse_id,
20
20
  quantity: quantity(listing, wh_space),
@@ -31,22 +31,42 @@ class TinyBuilder
31
31
 
32
32
  def counter_params(listing, wh_space = nil)
33
33
  params = mapped_data[listing.icava_id].slice(:variant, :stock_alloc, :associated_stock_allocs)
34
- wh_space ||= single_warehouse_space(mapped_data[listing.icava_id][:warehouse_spaces])
34
+ wh_space ||= single_warehouse_space(listing)
35
35
  params.merge(warehouse_space: wh_space)
36
36
  end
37
37
 
38
- def single_warehouse_space
39
- if warehouse_id
40
- warehuse_spaces.find{ |wh_space| wh_space.warehouse_id == wh_id }
38
+ def single_warehouse_space(listing)
39
+ variant_wh_spaces = wh_spaces(listing)
40
+ if warehouse_mapping
41
+ variant_wh_spaces.find{ |wh_space| wh_space.warehouse_id == warehouse_mapping.warehouse_id }
41
42
  else
42
- warehuse_spaces.last
43
+ variant_wh_spaces.last
43
44
  end
44
45
  end
45
46
 
46
47
  def multi_warehouse_spaces(listing)
47
- mapped_data[listing.icava_id][:warehouse_spaces].select do |wh_space|
48
+ wh_spaces(listing).select do |wh_space|
48
49
  warehouse_mappings.map(&:warehouse_id).include?(wh_space.warehouse_id)
49
50
  end
50
51
  end
52
+
53
+ def bundle_warehouse_spaces(variant)
54
+ bundle = MasterProduct.find_by("bundle_variants.master_variant_id": variant.id)
55
+ variant_id = bundle.bundle_variants.find { |bv| !bv[:main] }[:master_variant_id]
56
+ WarehouseSpace.where(
57
+ item_variant_id: variant_id
58
+ ).where.not(
59
+ warehouse_id: nil
60
+ )
61
+ end
62
+
63
+ def wh_spaces(listing)
64
+ variant = mapped_data[listing.icava_id][:variant]
65
+ wh_spaces = if variant.config == 'default'
66
+ mapped_data[listing.icava_id][:warehouse_spaces]
67
+ else
68
+ bundle_warehouse_spaces(variant)
69
+ end
70
+ end
51
71
  end
52
72
  end
@@ -50,12 +50,12 @@ class TinyBuilder
50
50
  end
51
51
 
52
52
  def qty_bundle
53
- if bundle_product
53
+ if bundle_variants.present?
54
54
  qty_list = []
55
55
  bundle_variants.each do |bv|
56
56
  wh_space = WarehouseSpace.find_by(
57
57
  item_variant_id: bv[:master_variant_id],
58
- warehouse_id: wh_space.warehouse_id
58
+ warehouse_id: warehouse_space&.warehouse_id
59
59
  )
60
60
  qty = wh_space.quantity if wh_space.present?
61
61
  qty ||= 0
@@ -69,6 +69,10 @@ class TinyBuilder
69
69
  end
70
70
 
71
71
  def bundle_variants
72
+ @bundle_variants ||= search_bundle_variants
73
+ end
74
+
75
+ def search_bundle_variants
72
76
  bundle = MasterProduct.where("bundle_variants.master_variant_id": variant.id).last
73
77
  bundle.bundle_variants.select { |bv| !bv[:main] }
74
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TinyBuilder
4
- VERSION = '0.0.1'
4
+ VERSION = '0.0.2'
5
5
  end
data/lib/tiny_builder.rb CHANGED
@@ -1,26 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'tiny_builder/version'
4
- require 'tiny_builder/mode_executor'
3
+ require 'mongoid'
4
+ require 'models/master_product'
5
5
  require 'models/mongo_item_listing'
6
6
  require 'models/mongo_variant_listing'
7
+ require 'tiny_builder/version'
8
+ require 'tiny_builder/mode_executor'
9
+ require 'tiny_builder/active_builder'
10
+ require 'tiny_builder/price_builder'
11
+ require 'tiny_builder/quantity_builder'
7
12
 
8
13
  class TinyBuilder
9
14
  include ModeExecutor
10
-
11
- attr_reader :listing_ids, :mode, :multiwarehouse
12
15
 
13
- def self.build(listing_ids, mode, multiwarehouse)
16
+ attr_reader :listing_ids, :mode, :mwh
17
+
18
+ def self.build(listing_ids, mode, mwh)
14
19
  new(listing_ids, mode, mwh).perform
15
20
  end
16
21
 
17
22
  def initialize(listing_ids, mode, mwh)
18
23
  @listing_ids = listing_ids
19
24
  @mode = mode
20
- @multiwarehouse = multiwarehouse
25
+ @mwh = mwh
21
26
  end
22
27
 
23
28
  def perform
24
- send("build_#{mode}")
29
+ perform_builder
25
30
  end
26
31
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - hayakaza