stall 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +45 -0
  3. data/app/assets/javascripts/stall.coffee +2 -0
  4. data/app/assets/javascripts/stall/cart-form.coffee +23 -0
  5. data/app/controllers/stall/application_controller.rb +10 -2
  6. data/app/controllers/stall/carts_controller.rb +31 -10
  7. data/app/controllers/stall/checkout/steps_controller.rb +36 -4
  8. data/app/controllers/stall/checkouts_controller.rb +12 -3
  9. data/app/controllers/stall/line_items_controller.rb +3 -3
  10. data/app/controllers/stall/payments_controller.rb +5 -3
  11. data/app/helpers/stall/add_to_cart_helper.rb +1 -1
  12. data/app/helpers/stall/checkout_helper.rb +22 -2
  13. data/app/helpers/stall/payments_helper.rb +8 -0
  14. data/app/mailers/stall/admin_mailer.rb +11 -0
  15. data/app/mailers/stall/base_mailer.rb +17 -0
  16. data/app/mailers/stall/customer_mailer.rb +13 -0
  17. data/app/models/address.rb +3 -0
  18. data/app/models/address_ownership.rb +3 -0
  19. data/app/models/adjustment.rb +3 -0
  20. data/app/models/cart.rb +3 -0
  21. data/app/models/customer.rb +3 -0
  22. data/app/models/line_item.rb +3 -0
  23. data/app/models/payment.rb +3 -0
  24. data/app/models/payment_method.rb +3 -0
  25. data/app/models/product_list.rb +3 -0
  26. data/app/models/shipment.rb +3 -0
  27. data/app/models/shipping_method.rb +3 -0
  28. data/app/models/stall/models.rb +4 -0
  29. data/app/models/stall/models/address.rb +45 -0
  30. data/app/models/stall/models/address_ownership.rb +26 -0
  31. data/app/models/stall/models/adjustment.rb +49 -0
  32. data/app/models/stall/models/cart.rb +37 -0
  33. data/app/models/stall/models/customer.rb +18 -0
  34. data/app/models/stall/models/line_item.rb +57 -0
  35. data/app/models/stall/models/payment.rb +33 -0
  36. data/app/models/stall/models/payment_method.rb +18 -0
  37. data/app/models/stall/models/product_list.rb +147 -0
  38. data/app/models/stall/models/shipment.rb +29 -0
  39. data/app/models/stall/models/shipping_method.rb +16 -0
  40. data/app/services/stall/add_to_cart_service.rb +11 -1
  41. data/app/services/stall/cart_update_service.rb +26 -0
  42. data/app/services/stall/payment_notification_service.rb +23 -9
  43. data/app/services/stall/shipping_fee_calculator_service.rb +13 -2
  44. data/app/views/checkout/steps/_informations.html.haml +3 -3
  45. data/app/views/checkout/steps/_payment.html.haml +25 -4
  46. data/app/views/checkout/steps/_payment_method.html.haml +1 -3
  47. data/app/views/checkout/steps/_payment_return.html.haml +6 -0
  48. data/app/views/checkout/steps/_shipping_method.html.haml +1 -1
  49. data/app/views/stall/admin_mailer/order_paid_email.html.haml +4 -0
  50. data/app/views/stall/carts/show.html.haml +36 -8
  51. data/app/views/stall/customer_mailer/order_paid_email.html.haml +8 -0
  52. data/app/views/stall/line_items/_added.html.haml +1 -1
  53. data/app/views/stall/shared/mailers/_address.html.haml +18 -0
  54. data/app/views/stall/shared/mailers/_cart.html.haml +114 -0
  55. data/config/locales/stall.fr.yml +90 -8
  56. data/db/migrate/20160304134849_change_all_json_columns_to_jsonb.rb +18 -0
  57. data/db/migrate/20160307142924_add_state_to_stall_addresses.rb +5 -0
  58. data/db/migrate/20160308142713_create_adjustments.rb +16 -0
  59. data/db/migrate/20160309165136_add_identifier_to_stall_product_lists.rb +5 -0
  60. data/db/migrate/20160316114649_add_data_to_stall_shipments.rb +5 -0
  61. data/db/migrate/20160317141632_add_state_to_stall_shipments.rb +5 -0
  62. data/db/migrate/20160629102943_add_active_to_stall_shipping_methods.rb +11 -0
  63. data/db/migrate/20160629104617_add_active_to_stall_payment_methods.rb +11 -0
  64. data/db/migrate/20160705110151_add_locale_to_stall_customers.rb +5 -0
  65. data/lib/generators/stall/checkout/step/templates/step.html.haml.erb +1 -1
  66. data/lib/generators/stall/checkout/step/templates/step.rb.erb +10 -2
  67. data/lib/generators/stall/checkout/wizard/templates/wizard.rb.erb +1 -1
  68. data/lib/generators/stall/install/templates/initializer.rb +85 -1
  69. data/lib/generators/stall/model/model_generator.rb +27 -0
  70. data/lib/generators/stall/service/service_generator.rb +39 -0
  71. data/lib/generators/stall/service/templates/service.rb.erb +16 -0
  72. data/lib/generators/stall/shipping/calculator/calculator_generator.rb +17 -0
  73. data/lib/generators/stall/shipping/calculator/templates/calculator.rb.erb +29 -0
  74. data/lib/stall.rb +4 -0
  75. data/lib/stall/addressable.rb +28 -2
  76. data/lib/stall/cart_helper.rb +84 -0
  77. data/lib/stall/carts_cleaner.rb +46 -0
  78. data/lib/stall/checkout.rb +2 -0
  79. data/lib/stall/checkout/informations_checkout_step.rb +3 -5
  80. data/lib/stall/checkout/payment_checkout_step.rb +3 -0
  81. data/lib/stall/checkout/payment_return_checkout_step.rb +11 -0
  82. data/lib/stall/checkout/shipping_method_checkout_step.rb +2 -1
  83. data/lib/stall/checkout/step.rb +47 -11
  84. data/lib/stall/checkout/step_form.rb +71 -0
  85. data/lib/stall/checkout/wizard.rb +15 -5
  86. data/lib/stall/config.rb +51 -0
  87. data/lib/stall/engine.rb +24 -2
  88. data/lib/stall/payable.rb +26 -0
  89. data/lib/stall/payments.rb +7 -3
  90. data/lib/stall/payments/config.rb +37 -0
  91. data/lib/stall/payments/fake_gateway_payment_notification.rb +34 -0
  92. data/lib/stall/payments/gateway.rb +19 -12
  93. data/lib/stall/payments/urls_config.rb +40 -0
  94. data/lib/stall/priceable.rb +7 -0
  95. data/lib/stall/rails/currency_helper.rb +1 -1
  96. data/lib/stall/routes.rb +17 -4
  97. data/lib/stall/sellable.rb +1 -2
  98. data/lib/stall/shipping.rb +3 -1
  99. data/lib/stall/shipping/calculator.rb +36 -3
  100. data/lib/stall/shipping/config.rb +17 -3
  101. data/lib/stall/shipping/country_weight_table_calculator.rb +1 -1
  102. data/lib/stall/shipping/free_shipping_calculator.rb +1 -1
  103. data/lib/stall/utils.rb +2 -2
  104. data/lib/stall/utils/config_dsl.rb +5 -1
  105. data/lib/stall/version.rb +1 -1
  106. data/lib/tasks/stall_tasks.rake +11 -0
  107. metadata +73 -15
  108. data/app/assets/javascripts/stall/application.js +0 -13
  109. data/app/assets/javascripts/stall/carts.js +0 -2
  110. data/app/helpers/stall/cart_helper.rb +0 -28
  111. data/app/models/stall/address.rb +0 -5
  112. data/app/models/stall/address_ownership.rb +0 -8
  113. data/app/models/stall/cart.rb +0 -31
  114. data/app/models/stall/customer.rb +0 -8
  115. data/app/models/stall/line_item.rb +0 -49
  116. data/app/models/stall/payment.rb +0 -14
  117. data/app/models/stall/payment_method.rb +0 -7
  118. data/app/models/stall/product_list.rb +0 -68
  119. data/app/models/stall/shipment.rb +0 -15
  120. data/app/models/stall/shipping_method.rb +0 -5
@@ -8,9 +8,9 @@ module Stall
8
8
  @config = config
9
9
  end
10
10
 
11
- def available_for?(_address)
11
+ def available?
12
12
  raise NoMethodError,
13
- 'Shipping calculators must implement the #available_for? method ' \
13
+ 'Shipping calculators must implement the #available? method ' \
14
14
  'to allow filtering available shipping methods'
15
15
  end
16
16
 
@@ -20,8 +20,41 @@ module Stall
20
20
  'the actual shipping price for the given cart'
21
21
  end
22
22
 
23
+ def eot_price
24
+ price / (1 + (vat_rate / 100.0))
25
+ end
26
+
27
+ def vat_rate
28
+ Stall.config.vat_rate
29
+ end
30
+
31
+ # Register a calculator from inside the class.
32
+ #
33
+ # This is useful for registering the calculator direclty in the class
34
+ # body, but is not suited for "auto-loaded" classes because of Rails'
35
+ # class unloading behavior
36
+ #
23
37
  def self.register(name)
24
- Stall::Shipping.calculators[name] = self
38
+ Stall.config.shipping.register_calculator(name, self)
39
+ end
40
+
41
+ # Fetch a shipping calculator from a shipping method or a shipping
42
+ # method identifier
43
+ #
44
+ def self.for(shipping_method)
45
+ identifier = case shipping_method
46
+ when String, Symbol then shipping_method.to_s
47
+ else shipping_method.identifier
48
+ end
49
+
50
+ name = Stall::Shipping.calculators[identifier]
51
+ String === name ? name.constantize : name
52
+ end
53
+
54
+ private
55
+
56
+ def address
57
+ cart.shipping_address || cart.billing_address
25
58
  end
26
59
  end
27
60
  end
@@ -1,13 +1,27 @@
1
1
  module Stall
2
2
  module Shipping
3
3
  class Config
4
+ # This mehtod allows for registering an in-app calculator that can be
5
+ # auto loaded without having to explicitly require it
6
+ #
7
+ # Use a string representing the calculator name if the target class is
8
+ # auto-loaded by Rails
9
+ #
10
+ def register_calculator(name, calculator)
11
+ Stall::Shipping.calculators[name] = calculator
12
+ end
13
+
4
14
  def configure
5
- yield config
15
+ yield self
6
16
  end
7
17
 
8
18
  def method_missing(name, *args)
9
- if (calculator = Stall::Shipping.calculators[name])
10
- calculator
19
+ if (calculator = Stall::Shipping::Calculator.for(name))
20
+ if block_given?
21
+ yield calculator
22
+ else
23
+ calculator
24
+ end
11
25
  else
12
26
  super
13
27
  end
@@ -3,7 +3,7 @@ require 'csv'
3
3
  module Stall
4
4
  module Shipping
5
5
  class CountryWeightTableCalculator < Stall::Shipping::Calculator
6
- def available_for?(address)
6
+ def available?
7
7
  country?(address.country)
8
8
  end
9
9
 
@@ -9,7 +9,7 @@ module Stall
9
9
  0
10
10
  end
11
11
 
12
- def available_for?(address)
12
+ def available?
13
13
  if Array === available
14
14
  available.include?(address.country)
15
15
  elsif Proc === available
@@ -8,11 +8,11 @@ module Stall
8
8
  def try_load_constant(name)
9
9
  begin
10
10
  name.constantize
11
- rescue NameError
11
+ rescue NameError => e
12
12
  begin
13
13
  require name.underscore
14
14
  name.constantize
15
- rescue LoadError, NameError
15
+ rescue LoadError, NameError => e
16
16
  nil
17
17
  end
18
18
  end
@@ -10,8 +10,12 @@ module Stall
10
10
  instance_variable_name = :"@#{ name }"
11
11
 
12
12
  define_method(name) do
13
- instance_variable_get(instance_variable_name) ||
13
+ if (value = instance_variable_get(instance_variable_name))
14
+ value
15
+ else
16
+ default = default.call if default.is_a?(Proc)
14
17
  instance_variable_set(instance_variable_name, default)
18
+ end
15
19
  end
16
20
  end
17
21
  end
@@ -1,3 +1,3 @@
1
1
  module Stall
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -2,3 +2,14 @@
2
2
  # task :stall do
3
3
  # # Task goes here
4
4
  # end
5
+
6
+ namespace :stall do
7
+ namespace :carts do
8
+ task clean: :environment do |task, args|
9
+ require 'stall/carts_cleaner'
10
+
11
+ model = (args.extras.first || 'Cart').constantize
12
+ Stall::CartsCleaner.new(model).clean!
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stall
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - vala
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-27 00:00:00.000000000 Z
11
+ date: 2016-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
152
  version: '2.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: cocoon
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '1.0'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '1.0'
153
167
  - !ruby/object:Gem::Dependency
154
168
  name: rspec-rails
155
169
  requirement: !ruby/object:Gem::Requirement
@@ -274,8 +288,7 @@ files:
274
288
  - Rakefile
275
289
  - app/assets/javascripts/stall.coffee
276
290
  - app/assets/javascripts/stall/add-to-cart-form.coffee
277
- - app/assets/javascripts/stall/application.js
278
- - app/assets/javascripts/stall/carts.js
291
+ - app/assets/javascripts/stall/cart-form.coffee
279
292
  - app/assets/stylesheets/stall/application.css
280
293
  - app/assets/stylesheets/stall/carts.css
281
294
  - app/controllers/stall/application_controller.rb
@@ -286,32 +299,54 @@ files:
286
299
  - app/controllers/stall/payments_controller.rb
287
300
  - app/helpers/stall/add_to_cart_helper.rb
288
301
  - app/helpers/stall/application_helper.rb
289
- - app/helpers/stall/cart_helper.rb
290
302
  - app/helpers/stall/checkout_helper.rb
291
- - app/models/stall/address.rb
292
- - app/models/stall/address_ownership.rb
293
- - app/models/stall/cart.rb
294
- - app/models/stall/customer.rb
295
- - app/models/stall/line_item.rb
296
- - app/models/stall/payment.rb
297
- - app/models/stall/payment_method.rb
298
- - app/models/stall/product_list.rb
299
- - app/models/stall/shipment.rb
300
- - app/models/stall/shipping_method.rb
303
+ - app/helpers/stall/payments_helper.rb
304
+ - app/mailers/stall/admin_mailer.rb
305
+ - app/mailers/stall/base_mailer.rb
306
+ - app/mailers/stall/customer_mailer.rb
307
+ - app/models/address.rb
308
+ - app/models/address_ownership.rb
309
+ - app/models/adjustment.rb
310
+ - app/models/cart.rb
311
+ - app/models/customer.rb
312
+ - app/models/line_item.rb
313
+ - app/models/payment.rb
314
+ - app/models/payment_method.rb
315
+ - app/models/product_list.rb
316
+ - app/models/shipment.rb
317
+ - app/models/shipping_method.rb
318
+ - app/models/stall/models.rb
319
+ - app/models/stall/models/address.rb
320
+ - app/models/stall/models/address_ownership.rb
321
+ - app/models/stall/models/adjustment.rb
322
+ - app/models/stall/models/cart.rb
323
+ - app/models/stall/models/customer.rb
324
+ - app/models/stall/models/line_item.rb
325
+ - app/models/stall/models/payment.rb
326
+ - app/models/stall/models/payment_method.rb
327
+ - app/models/stall/models/product_list.rb
328
+ - app/models/stall/models/shipment.rb
329
+ - app/models/stall/models/shipping_method.rb
301
330
  - app/services/stall/add_to_cart_service.rb
302
331
  - app/services/stall/base_service.rb
332
+ - app/services/stall/cart_update_service.rb
303
333
  - app/services/stall/payment_notification_service.rb
304
334
  - app/services/stall/shipping_fee_calculator_service.rb
305
335
  - app/views/checkout/steps/_informations.html.haml
306
336
  - app/views/checkout/steps/_payment.html.haml
307
337
  - app/views/checkout/steps/_payment_method.html.haml
338
+ - app/views/checkout/steps/_payment_return.html.haml
308
339
  - app/views/checkout/steps/_shipping_method.html.haml
309
340
  - app/views/layouts/stall/application.html.erb
341
+ - app/views/stall/admin_mailer/order_paid_email.html.haml
310
342
  - app/views/stall/carts/show.html.haml
311
343
  - app/views/stall/checkout/steps/show.html.haml
344
+ - app/views/stall/customer_mailer/order_paid_email.html.haml
312
345
  - app/views/stall/line_items/_add_error.html.haml
313
346
  - app/views/stall/line_items/_added.html.haml
314
347
  - app/views/stall/line_items/_form.html.haml
348
+ - app/views/stall/shared/mailers/_address.html.haml
349
+ - app/views/stall/shared/mailers/_cart.html.haml
315
350
  - config/locales/stall.en.yml
316
351
  - config/locales/stall.fr.yml
317
352
  - db/migrate/20151103154313_create_stall_line_items.rb
@@ -324,6 +359,15 @@ files:
324
359
  - db/migrate/20160125100733_create_stall_payment_methods.rb
325
360
  - db/migrate/20160125100734_create_stall_payments.rb
326
361
  - db/migrate/20160127113619_add_user_to_customer.rb
362
+ - db/migrate/20160304134849_change_all_json_columns_to_jsonb.rb
363
+ - db/migrate/20160307142924_add_state_to_stall_addresses.rb
364
+ - db/migrate/20160308142713_create_adjustments.rb
365
+ - db/migrate/20160309165136_add_identifier_to_stall_product_lists.rb
366
+ - db/migrate/20160316114649_add_data_to_stall_shipments.rb
367
+ - db/migrate/20160317141632_add_state_to_stall_shipments.rb
368
+ - db/migrate/20160629102943_add_active_to_stall_shipping_methods.rb
369
+ - db/migrate/20160629104617_add_active_to_stall_payment_methods.rb
370
+ - db/migrate/20160705110151_add_locale_to_stall_customers.rb
327
371
  - lib/generators/stall/checkout/step/step_generator.rb
328
372
  - lib/generators/stall/checkout/step/templates/step.html.haml.erb
329
373
  - lib/generators/stall/checkout/step/templates/step.rb.erb
@@ -331,19 +375,33 @@ files:
331
375
  - lib/generators/stall/checkout/wizard/wizard_generator.rb
332
376
  - lib/generators/stall/install/install_generator.rb
333
377
  - lib/generators/stall/install/templates/initializer.rb
378
+ - lib/generators/stall/model/model_generator.rb
379
+ - lib/generators/stall/service/service_generator.rb
380
+ - lib/generators/stall/service/templates/service.rb.erb
381
+ - lib/generators/stall/shipping/calculator/calculator_generator.rb
382
+ - lib/generators/stall/shipping/calculator/templates/calculator.rb.erb
334
383
  - lib/stall.rb
335
384
  - lib/stall/addressable.rb
385
+ - lib/stall/cart_helper.rb
386
+ - lib/stall/carts_cleaner.rb
336
387
  - lib/stall/checkout.rb
337
388
  - lib/stall/checkout/informations_checkout_step.rb
338
389
  - lib/stall/checkout/payment_checkout_step.rb
339
390
  - lib/stall/checkout/payment_method_checkout_step.rb
391
+ - lib/stall/checkout/payment_return_checkout_step.rb
340
392
  - lib/stall/checkout/shipping_method_checkout_step.rb
341
393
  - lib/stall/checkout/step.rb
394
+ - lib/stall/checkout/step_form.rb
342
395
  - lib/stall/checkout/wizard.rb
343
396
  - lib/stall/config.rb
344
397
  - lib/stall/engine.rb
398
+ - lib/stall/payable.rb
345
399
  - lib/stall/payments.rb
400
+ - lib/stall/payments/config.rb
401
+ - lib/stall/payments/fake_gateway_payment_notification.rb
346
402
  - lib/stall/payments/gateway.rb
403
+ - lib/stall/payments/urls_config.rb
404
+ - lib/stall/priceable.rb
347
405
  - lib/stall/rails/currency_helper.rb
348
406
  - lib/stall/rails/routing_mapper.rb
349
407
  - lib/stall/routes.rb
@@ -1,13 +0,0 @@
1
- // This is a manifest file that'll be compiled into application.js, which will include all the files
2
- // listed below.
3
- //
4
- // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
- // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
- //
7
- // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
- // compiled file.
9
- //
10
- // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
- // about supported directives.
12
- //
13
- //= require_tree .
@@ -1,2 +0,0 @@
1
- // Place all the behaviors and hooks related to the matching controller here.
2
- // All this logic will automatically be available in application.js.
@@ -1,28 +0,0 @@
1
- module Stall
2
- module CartHelper
3
- def current_cart
4
- RequestStore.store[cart_store_key_for(:default)] ||= load_current_cart
5
- end
6
-
7
- private
8
-
9
- def load_current_cart
10
- if (cart_token = session[cart_store_key_for(:default)])
11
- if (current_cart = Stall::Cart.find_by_token(cart_token))
12
- return current_cart
13
- end
14
- end
15
-
16
- # If no token was stored or the token does not exist anymore, create a
17
- # new cart and store the new token
18
- #
19
- Stall::Cart.create!.tap do |cart|
20
- session[cart_store_key_for(:default)] = cart.token
21
- end
22
- end
23
-
24
- def cart_store_key_for(identifier)
25
- ['stall', 'cart', identifier.to_s].join('.')
26
- end
27
- end
28
- end
@@ -1,5 +0,0 @@
1
- module Stall
2
- class Address < ActiveRecord::Base
3
- has_one :addressable_ownership, dependent: :destroy
4
- end
5
- end
@@ -1,8 +0,0 @@
1
- module Stall
2
- class AddressOwnership < ActiveRecord::Base
3
- belongs_to :address
4
- accepts_nested_attributes_for :address
5
-
6
- belongs_to :addressable, polymorphic: true
7
- end
8
- end
@@ -1,31 +0,0 @@
1
- module Stall
2
- class Cart < Stall::ProductList
3
- include Stall::Addressable
4
-
5
- store_accessor :data, :reference
6
-
7
- has_one :shipment, dependent: :destroy, inverse_of: :cart
8
- accepts_nested_attributes_for :shipment
9
-
10
- has_one :payment, dependent: :destroy, inverse_of: :cart
11
- accepts_nested_attributes_for :payment
12
-
13
- after_save :ensure_reference, on: :create
14
-
15
- def total_weight
16
- line_items.reduce(0) do |total, line_item|
17
- total + (line_item.weight || Stall.config.default_product_weight)
18
- end
19
- end
20
-
21
- private
22
-
23
- def ensure_reference
24
- unless reference.present?
25
- reference = [Time.now.strftime('%Y%m%d'), ('%05d' % id)].join('-')
26
- self.reference = reference
27
- save(validate: false)
28
- end
29
- end
30
- end
31
- end
@@ -1,8 +0,0 @@
1
- module Stall
2
- class Customer < ActiveRecord::Base
3
- include Stall::Addressable
4
-
5
- belongs_to :user, polymorphic: true, inverse_of: :customer
6
- accepts_nested_attributes_for :user
7
- end
8
- end
@@ -1,49 +0,0 @@
1
- module Stall
2
- class LineItem < ActiveRecord::Base
3
- store_accessor :data, :weight
4
-
5
- monetize :unit_eot_price_cents, :unit_price_cents,
6
- :eot_price_cents, :price_cents,
7
- with_model_currency: :currency, allow_nil: true
8
-
9
- belongs_to :sellable, polymorphic: true
10
- belongs_to :product_list
11
-
12
- validates :name, :unit_price, :unit_eot_price, :vat_rate, :price, :quantity,
13
- :eot_price, :sellable, presence: true
14
-
15
- validates :unit_price, :unit_eot_price, :vat_rate, :price, :eot_price,
16
- numericality: true
17
-
18
- validates :quantity, numericality: { greater_than: 0 }
19
-
20
- validate :stock_availability
21
-
22
- before_validation :refresh_total_prices
23
-
24
- def like?(other)
25
- [:sellable_id, :sellable_type].all? do |property|
26
- public_send(property) == other.public_send(property)
27
- end
28
- end
29
-
30
- def currency
31
- product_list.try(:currency) || Money.default_currency
32
- end
33
-
34
- def vat
35
- price - eot_price
36
- end
37
-
38
- private
39
-
40
- # TODO : Stocks availibility handling
41
- def stock_availability
42
- end
43
-
44
- def refresh_total_prices
45
- self.eot_price = unit_eot_price * quantity if unit_eot_price && quantity
46
- self.price = unit_price * quantity if unit_price && quantity
47
- end
48
- end
49
- end