solidus_virtual_gift_card 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (73) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +35 -0
  3. data/Gemfile +20 -6
  4. data/README.md +4 -3
  5. data/Rakefile +2 -2
  6. data/app/controllers/spree/admin/gift_cards_controller.rb +14 -14
  7. data/app/controllers/spree/api/gift_cards_controller.rb +2 -2
  8. data/app/decorators/models/solidus_virtual_gift_card/spree/inventory_unit_decorator.rb +13 -0
  9. data/app/decorators/models/solidus_virtual_gift_card/spree/line_item_decorator.rb +24 -0
  10. data/app/{models/concerns/spree/gift_cards/order_contents_concerns.rb → decorators/models/solidus_virtual_gift_card/spree/order_contents_decorator.rb} +14 -20
  11. data/app/{models/concerns/spree/gift_cards/order_concerns.rb → decorators/models/solidus_virtual_gift_card/spree/order_decorator.rb} +14 -14
  12. data/app/{models → decorators/models/solidus_virtual_gift_card}/spree/store_credit_category_decorator.rb +1 -1
  13. data/app/mailers/spree/gift_card_mailer.rb +5 -2
  14. data/app/models/spree/gift_cards/gift_card_date_format_error.rb +6 -0
  15. data/app/models/{concerns/spree → spree}/redemption_code_generator.rb +0 -0
  16. data/app/models/spree/virtual_gift_card.rb +19 -16
  17. data/app/overrides/admin_gift_card_order_confirmation.rb +4 -4
  18. data/app/overrides/admin_item_view_gift_card_codes.rb +12 -12
  19. data/app/overrides/admin_user_sidebar_store_credits.rb +3 -3
  20. data/app/overrides/admin_user_sub_menu.rb +2 -2
  21. data/app/views/spree/admin/gift_cards/_lookup_form.html.erb +2 -2
  22. data/app/views/spree/admin/gift_cards/_redemption_form.html.erb +1 -1
  23. data/app/views/spree/admin/gift_cards/edit.html.erb +5 -5
  24. data/app/views/spree/admin/gift_cards/index.html.erb +1 -1
  25. data/app/views/spree/admin/gift_cards/lookup.html.erb +1 -1
  26. data/app/views/spree/admin/orders/_confirmation_gift_card_details.html.erb +9 -9
  27. data/app/views/spree/admin/orders/_gift_card_details.html.erb +2 -2
  28. data/app/views/spree/admin/users/_gift_card_sidebar.html.erb +2 -2
  29. data/app/views/spree/gift_card_mailer/gift_card_email.text.erb +3 -3
  30. data/config/initializers/line_item_controller_gift_card_details.rb +1 -1
  31. data/db/migrate/20140623152903_create_virtual_gift_card.rb +1 -1
  32. data/db/migrate/20140624175113_seed_gift_card_category.rb +1 -1
  33. data/db/migrate/20140627185148_add_timestamps_to_gift_cards.rb +1 -1
  34. data/db/migrate/20140702153907_add_gift_card_flag_to_products.rb +1 -1
  35. data/db/migrate/20140707200431_add_line_item_to_gift_card.rb +1 -1
  36. data/db/migrate/20151013172931_add_recipient_fields_to_virtual_gift_card.rb +1 -1
  37. data/db/migrate/20151013210615_add_active_flag_to_virtual_gift_card.rb +1 -1
  38. data/db/migrate/20151013214647_set_redeemable_true_on_virtual_gift_cards.rb +2 -2
  39. data/db/migrate/20151019190731_add_email_send_time_to_virtual_gift_card.rb +1 -1
  40. data/db/migrate/20151109202300_add_deactivated_at_to_gift_card.rb +1 -1
  41. data/db/migrate/20151110202752_add_inventory_unit_to_gift_card.rb +1 -1
  42. data/db/migrate/20151111211220_backfill_inventory_units_on_gift_card.rb +2 -2
  43. data/lib/generators/solidus_virtual_gift_card/install/install_generator.rb +1 -1
  44. data/lib/solidus_virtual_gift_card.rb +1 -0
  45. data/lib/spree_virtual_gift_card/engine.rb +3 -3
  46. data/lib/spree_virtual_gift_card/factories.rb +7 -7
  47. data/lib/tasks/send_gift_card_emails.rake +1 -1
  48. data/solidus_virtual_gift_card.gemspec +23 -21
  49. data/spec/controllers/spree/admin/gift_cards_controller_spec.rb +33 -34
  50. data/spec/controllers/spree/api/gift_cards_controller_spec.rb +12 -13
  51. data/spec/features/admin/gift_cards_spec.rb +19 -17
  52. data/spec/features/admin/products_card_spec.rb +3 -3
  53. data/spec/lib/tasks/send_gift_card_emails_spec.rb +12 -12
  54. data/spec/mailers/spree/gift_card_mailer_spec.rb +4 -4
  55. data/spec/models/concerns/spree/redemption_code_generator_spec.rb +5 -5
  56. data/spec/models/spree/line_item_spec.rb +2 -2
  57. data/spec/models/spree/order_contents_spec.rb +82 -82
  58. data/spec/models/spree/order_spec.rb +13 -13
  59. data/spec/models/spree/permission_sets/virtual_gift_card_display_spec.rb +2 -2
  60. data/spec/models/spree/permission_sets/virtual_gift_card_management_spec.rb +2 -2
  61. data/spec/models/spree/store_credit_category_spec.rb +6 -6
  62. data/spec/models/spree/virtual_gift_card_spec.rb +23 -23
  63. data/spec/spec_helper.rb +13 -74
  64. data/spec/support/cancan.rb +3 -0
  65. metadata +56 -25
  66. data/.travis.yml +0 -8
  67. data/app/models/concerns/spree/gift_cards/inventory_unit_concerns.rb +0 -10
  68. data/app/models/concerns/spree/gift_cards/line_item_concerns.rb +0 -22
  69. data/app/models/spree/inventory_unit_decorator.rb +0 -1
  70. data/app/models/spree/line_item_decorator.rb +0 -1
  71. data/app/models/spree/order_contents_decorator.rb +0 -1
  72. data/app/models/spree/order_decorator.rb +0 -1
  73. data/circle.yml +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ea5a0d37ac45be094a61721f9f91b948a4713063
4
- data.tar.gz: 31f391fb4340bbded183df0bd9fdd5e828c12de5
2
+ SHA256:
3
+ metadata.gz: cd464b35ade1d8c367f28771b22632bf2dfbd1bffb6ab56b1e4893b40cf74ce2
4
+ data.tar.gz: 41c2ac487d74730c650e4636dd115bb307a3ad065fc900bae37a9453afca7060
5
5
  SHA512:
6
- metadata.gz: 652d0b4ace4f835d099d5f518bdca86fb708a809198fa08a7cfad4f0c8011f472c8f8a89e9dc0572e8b76f10e36818aae20b875f2ea210440d73e8d839aeef53
7
- data.tar.gz: 78cd94b6aa0d3e4b89b937ffdbabc83af98940ef1b42edff365ff30600c9787e29c5ac9715d3141d65d6c43e22ca65bcedb262af5150c753ae224f2cae4e8873
6
+ metadata.gz: fa2b39045ef1ba5f6d7de759bd523fa5787a2709f3a5bd85950ce8578befc490b5f9c68866517b154a371fdffd4fadfd58b942a2a7b46b2caf4425ddc758555f
7
+ data.tar.gz: 71c837556620b2aaf272a3236f56813f99da6ae48214b725453493b778a80e320c27a5a932ead827b3ca07b4114c96f1f971b15ad22bfc5209e7ee1516fc0f8a
@@ -0,0 +1,35 @@
1
+ version: 2.1
2
+
3
+ orbs:
4
+ # Always take the latest version of the orb, this allows us to
5
+ # run specs against Solidus supported versions only without the need
6
+ # to change this configuration every time a Solidus version is released
7
+ # or goes EOL.
8
+ solidusio_extensions: solidusio/extensions@volatile
9
+
10
+ jobs:
11
+ run-specs-with-postgres:
12
+ executor: solidusio_extensions/postgres
13
+ steps:
14
+ - solidusio_extensions/run-tests
15
+ run-specs-with-mysql:
16
+ executor: solidusio_extensions/mysql
17
+ steps:
18
+ - solidusio_extensions/run-tests
19
+
20
+ workflows:
21
+ "Run specs on supported Solidus versions":
22
+ jobs:
23
+ - run-specs-with-postgres
24
+ - run-specs-with-mysql
25
+ "Weekly run specs against master":
26
+ triggers:
27
+ - schedule:
28
+ cron: "0 0 * * 4" # every Thursday
29
+ filters:
30
+ branches:
31
+ only:
32
+ - master
33
+ jobs:
34
+ - run-specs-with-postgres
35
+ - run-specs-with-mysql
data/Gemfile CHANGED
@@ -1,13 +1,27 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
3
  branch = ENV.fetch('SOLIDUS_BRANCH', 'master')
4
- gem "solidus", github: "solidusio/solidus", branch: branch
5
- gem "solidus_auth_devise"
4
+ gem 'solidus', github: 'solidusio/solidus', branch: branch
5
+ gem 'solidus_auth_devise'
6
6
 
7
- gem 'pg'
8
- gem 'mysql2'
7
+ gem 'rails-controller-testing', group: :test
9
8
 
10
- group :test, :development do
9
+ case ENV['DB']
10
+ when 'postgres'
11
+ gem 'pg'
12
+ when 'mysql'
13
+ gem 'mysql2'
14
+ end
15
+
16
+ # Needed to help Bundler figure out how to resolve dependencies, otherwise it takes forever to
17
+ # resolve them
18
+ if branch == 'master' || Gem::Version.new(branch[1..-1]) >= Gem::Version.new('2.10.0')
19
+ gem 'rails', '~> 6.0'
20
+ else
21
+ gem 'rails', '~> 5.0'
22
+ end
23
+
24
+ group :development, :test do
11
25
  gem 'pry-rails'
12
26
  end
13
27
 
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  Solidus - Virtual Gift Card
2
2
  ===========================
3
+
4
+ [![CircleCI](https://circleci.com/gh/solidusio-contrib/solidus_virtual_gift_card.svg?style=svg)](https://circleci.com/gh/solidusio-contrib/solidus_virtual_gift_card)
5
+
3
6
  A virtual gift card implementation for Solidus.
4
7
 
5
8
  * Allows setup of virtual gift card products that get unique codes upon
@@ -7,8 +10,6 @@ A virtual gift card implementation for Solidus.
7
10
  * The virtual gift card codes can be used to add store credit to a user's
8
11
  account.
9
12
 
10
- [![Build Status](https://travis-ci.org/solidusio/solidus_virtual_gift_card.svg?branch=master)](https://travis-ci.org/solidusio/solidus_virtual_gift_card)
11
-
12
13
  Installation
13
14
  ------------
14
15
 
@@ -44,7 +45,7 @@ bundle
44
45
  bundle exec rake
45
46
  ```
46
47
 
47
- When testing your applications integration with this extension you may use it's
48
+ When testing your application's integration with this extension you may use its
48
49
  factories. Simply add this require statement to your spec_helper:
49
50
 
50
51
  ```ruby
data/Rakefile CHANGED
@@ -7,9 +7,9 @@ require 'spree/testing_support/extension_rake'
7
7
  RSpec::Core::RakeTask.new
8
8
 
9
9
  task :default do
10
- if Dir["spec/dummy"].empty?
10
+ if Dir['spec/dummy'].empty?
11
11
  Rake::Task[:test_app].invoke
12
- Dir.chdir("../../")
12
+ Dir.chdir('../../')
13
13
  end
14
14
  Rake::Task[:spec].invoke
15
15
  end
@@ -1,11 +1,11 @@
1
1
  class Spree::Admin::GiftCardsController < Spree::Admin::BaseController
2
- before_filter :load_user, only: [:lookup, :redeem]
3
- before_filter :load_gift_card_for_redemption, only: [:redeem]
4
- before_filter :load_gift_card_by_id, only: [:edit, :update, :send_email, :deactivate]
5
- before_filter :load_order, only: [:edit, :update, :deactivate]
2
+ before_action :load_user, only: [:lookup, :redeem]
3
+ before_action :load_gift_card_for_redemption, only: [:redeem]
4
+ before_action :load_gift_card_by_id, only: [:edit, :update, :send_email, :deactivate]
5
+ before_action :load_order, only: [:edit, :update, :deactivate]
6
6
 
7
7
  def index
8
- @search = Spree::VirtualGiftCard.purchased.search(params[:q])
8
+ @search = Spree::VirtualGiftCard.purchased.ransack(params[:q])
9
9
  @gift_cards = @search.result.page(params[:page]).per(params[:per_page])
10
10
  end
11
11
 
@@ -16,35 +16,35 @@ class Spree::Admin::GiftCardsController < Spree::Admin::BaseController
16
16
  end
17
17
 
18
18
  def update
19
- if @gift_card.update_attributes(gift_card_params)
20
- flash[:success] = Spree.t("admin.gift_cards.gift_card_updated")
19
+ if @gift_card.update(gift_card_params)
20
+ flash[:success] = I18n.t('spree.admin.gift_cards.gift_card_updated')
21
21
  redirect_to edit_admin_order_path(@order)
22
22
  else
23
- flash[:error] = @gift_card.errors.full_messages.join(", ")
23
+ flash[:error] = @gift_card.errors.full_messages.join(', ')
24
24
  redirect_to :back
25
25
  end
26
26
  end
27
27
 
28
28
  def redeem
29
29
  if @gift_card.redeem(@user)
30
- flash[:success] = Spree.t("admin.gift_cards.redeemed_gift_card")
30
+ flash[:success] = I18n.t('spree.admin.gift_cards.redeemed_gift_card')
31
31
  redirect_to admin_user_store_credits_path(@user)
32
32
  else
33
- flash[:error] = Spree.t("admin.gift_cards.errors.unable_to_redeem_gift_card")
33
+ flash[:error] = I18n.t('spree.admin.gift_cards.errors.unable_to_redeem_gift_card')
34
34
  render :lookup
35
35
  end
36
36
  end
37
37
 
38
38
  def deactivate
39
39
  if @gift_card.deactivate
40
- flash[:success] = Spree.t("admin.gift_cards.deactivated_gift_card")
40
+ flash[:success] = I18n.t('spree.admin.gift_cards.deactivated_gift_card')
41
41
  redirect_to edit_admin_order_path(@order)
42
42
  else
43
- flash[:error] = @gift_card.errors.full_messages.join(", ").presence || Spree.t("admin.gift_cards.errors.unable_to_reimburse_gift_card")
43
+ flash[:error] = @gift_card.errors.full_messages.join(', ').presence || I18n.t('spree.admin.gift_cards.errors.unable_to_reimburse_gift_card')
44
44
  redirect_to edit_admin_order_gift_card_path(@order, @gift_card)
45
45
  end
46
46
  rescue Spree::Reimbursement::IncompleteReimbursementError
47
- flash[:error] = Spree.t("admin.gift_cards.errors.unable_to_reimburse_gift_card")
47
+ flash[:error] = I18n.t('spree.admin.gift_cards.errors.unable_to_reimburse_gift_card')
48
48
  redirect_to edit_admin_order_gift_card_path(@order, @gift_card)
49
49
  end
50
50
 
@@ -60,7 +60,7 @@ class Spree::Admin::GiftCardsController < Spree::Admin::BaseController
60
60
  @gift_card = Spree::VirtualGiftCard.active_by_redemption_code(redemption_code)
61
61
 
62
62
  if @gift_card.blank?
63
- flash[:error] = Spree.t("admin.gift_cards.errors.not_found")
63
+ flash[:error] = I18n.t('spree.admin.gift_cards.errors.not_found')
64
64
  render :lookup
65
65
  end
66
66
  end
@@ -1,7 +1,7 @@
1
1
  class Spree::Api::GiftCardsController < Spree::Api::BaseController
2
2
 
3
3
  def redeem
4
- redemption_code = Spree::RedemptionCodeGenerator.format_redemption_code_for_lookup(params[:redemption_code] || "")
4
+ redemption_code = Spree::RedemptionCodeGenerator.format_redemption_code_for_lookup(params[:redemption_code] || '')
5
5
  @gift_card = Spree::VirtualGiftCard.active_by_redemption_code(redemption_code)
6
6
 
7
7
  if !@gift_card
@@ -17,7 +17,7 @@ class Spree::Api::GiftCardsController < Spree::Api::BaseController
17
17
 
18
18
  def redeem_fail_response
19
19
  {
20
- error_message: "#{Spree.t('admin.gift_cards.errors.not_found')}. #{Spree.t('admin.gift_cards.errors.please_try_again')}"
20
+ error_message: "#{I18n.t('spree.admin.gift_cards.errors.not_found')}. #{I18n.t('spree.admin.gift_cards.errors.please_try_again')}"
21
21
  }
22
22
  end
23
23
  end
@@ -0,0 +1,13 @@
1
+ module SolidusVirtualGiftCard
2
+ module Spree
3
+ module InventoryUnitDecorator
4
+ def self.prepended(base)
5
+ base.class_eval do
6
+ has_one :gift_card, class_name: 'Spree::VirtualGiftCard'
7
+ end
8
+ end
9
+
10
+ ::Spree::InventoryUnit.prepend self
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ module SolidusVirtualGiftCard
2
+ module Spree
3
+ module LineItemDecorator
4
+ def self.prepended(base)
5
+ base.class_eval do
6
+ has_many :gift_cards, class_name: 'Spree::VirtualGiftCard', dependent: :destroy
7
+ delegate :gift_card?, :gift_card, to: :product
8
+
9
+ self.whitelisted_ransackable_associations += %w[order]
10
+ end
11
+ end
12
+
13
+ def redemption_codes
14
+ gift_cards.map {|gc| {amount: gc.formatted_amount, redemption_code: gc.formatted_redemption_code}}
15
+ end
16
+
17
+ def gift_card_details
18
+ gift_cards.map(&:details)
19
+ end
20
+
21
+ ::Spree::LineItem.prepend self
22
+ end
23
+ end
24
+ end
@@ -1,16 +1,9 @@
1
- module Spree
2
- module GiftCards::OrderContentsConcerns
3
- extend ActiveSupport::Concern
4
- class GiftCardDateFormatError < StandardError; end
5
-
6
- included do
7
- prepend(InstanceMethods)
8
- end
9
-
10
- module InstanceMethods
1
+ module SolidusVirtualGiftCard
2
+ module Spree
3
+ module OrderContentsDecorator
11
4
  def add(variant, quantity = 1, options = {})
12
5
  line_item = super
13
- create_gift_cards(line_item, quantity, options["gift_card_details"] || {})
6
+ create_gift_cards(line_item, quantity, options['gift_card_details'] || {})
14
7
  line_item
15
8
  end
16
9
 
@@ -24,7 +17,7 @@ module Spree
24
17
  update_success = super(params)
25
18
 
26
19
  if update_success && params[:line_items_attributes]
27
- line_item = Spree::LineItem.find_by(id: params[:line_items_attributes][:id])
20
+ line_item = ::Spree::LineItem.find_by(id: params[:line_items_attributes][:id])
28
21
  new_quantity = params[:line_items_attributes][:quantity].to_i
29
22
  update_gift_cards(line_item, new_quantity)
30
23
  end
@@ -37,15 +30,15 @@ module Spree
37
30
  def create_gift_cards(line_item, quantity_diff, gift_card_details = {})
38
31
  if line_item.gift_card?
39
32
  quantity_diff.to_i.times do
40
- Spree::VirtualGiftCard.create!(
33
+ ::Spree::VirtualGiftCard.create!(
41
34
  amount: line_item.price,
42
35
  currency: line_item.currency,
43
36
  line_item: line_item,
44
- recipient_name: gift_card_details["recipient_name"],
45
- recipient_email: gift_card_details["recipient_email"],
46
- purchaser_name: gift_card_details["purchaser_name"],
47
- gift_message: gift_card_details["gift_message"],
48
- send_email_at: format_date(gift_card_details["send_email_at"])
37
+ recipient_name: gift_card_details['recipient_name'],
38
+ recipient_email: gift_card_details['recipient_email'],
39
+ purchaser_name: gift_card_details['purchaser_name'],
40
+ gift_message: gift_card_details['gift_message'],
41
+ send_email_at: format_date(gift_card_details['send_email_at'])
49
42
  )
50
43
  end
51
44
  end
@@ -75,10 +68,11 @@ module Spree
75
68
  begin
76
69
  Date.parse(date)
77
70
  rescue ArgumentError
78
- raise GiftCardDateFormatError
71
+ raise ::Spree::GiftCards::GiftCardDateFormatError
79
72
  end
80
73
  end
74
+
75
+ ::Spree::OrderContents.prepend self
81
76
  end
82
77
  end
83
78
  end
84
-
@@ -1,22 +1,20 @@
1
- module Spree
2
- module GiftCards::OrderConcerns
3
- extend ActiveSupport::Concern
1
+ module SolidusVirtualGiftCard
2
+ module Spree
3
+ module OrderDecorator
4
+ def self.prepended(base)
5
+ base.class_eval do
6
+ state_machine.after_transition to: :complete, do: :send_gift_card_emails
4
7
 
5
- included do
6
- Spree::Order.state_machine.after_transition to: :complete, do: :send_gift_card_emails
7
-
8
- has_many :gift_cards, through: :line_items
9
-
10
- prepend(InstanceMethods)
11
- end
8
+ has_many :gift_cards, through: :line_items
9
+ end
10
+ end
12
11
 
13
- module InstanceMethods
14
12
  def gift_card_match(line_item, options)
15
13
  return true unless line_item.gift_card?
16
- return true unless options["gift_card_details"]
14
+ return true unless options['gift_card_details']
17
15
  line_item.gift_cards.any? do |gift_card|
18
- gc_detail_set = gift_card.details.stringify_keys.except("send_email_at").to_set
19
- options_set = options["gift_card_details"].except("send_email_at").to_set
16
+ gc_detail_set = gift_card.details.stringify_keys.except('send_email_at').to_set
17
+ options_set = options['gift_card_details'].except('send_email_at').to_set
20
18
  gc_detail_set.superset?(options_set)
21
19
  end
22
20
  end
@@ -36,6 +34,8 @@ module Spree
36
34
  end
37
35
  end
38
36
  end
37
+
38
+ ::Spree::Order.prepend self
39
39
  end
40
40
  end
41
41
  end
@@ -1,2 +1,2 @@
1
- Spree::StoreCreditCategory::GIFT_CARD_CATEGORY_NAME = "Gift Card".freeze
1
+ Spree::StoreCreditCategory::GIFT_CARD_CATEGORY_NAME = 'Gift Card'.freeze
2
2
  Spree::StoreCreditCategory.non_expiring_credit_types |= [Spree::StoreCreditCategory::GIFT_CARD_CATEGORY_NAME]
@@ -2,8 +2,11 @@ class Spree::GiftCardMailer < Spree::BaseMailer
2
2
  def gift_card_email(gift_card)
3
3
  @gift_card = gift_card.respond_to?(:id) ? gift_card : Spree::VirtualGiftCard.find(gift_card)
4
4
  @order = @gift_card.line_item.order
5
+
6
+ store = @order.store
5
7
  send_to_address = @gift_card.recipient_email.presence || @order.email
6
- subject = "#{Spree::Store.current.name} #{Spree.t('gift_card_mailer.gift_card_email.subject')}"
7
- mail(to: send_to_address, from: from_address(Spree::Store.current), subject: subject)
8
+ subject = "#{store} #{I18n.t('spree.gift_card_mailer.gift_card_email.subject')}"
9
+
10
+ mail(to: send_to_address, from: from_address(store), subject: subject)
8
11
  end
9
12
  end
@@ -0,0 +1,6 @@
1
+ module Spree
2
+ module GiftCards
3
+ class GiftCardDateFormatError < StandardError
4
+ end
5
+ end
6
+ end
@@ -1,11 +1,11 @@
1
- class Spree::VirtualGiftCard < ActiveRecord::Base
1
+ class Spree::VirtualGiftCard < Spree::Base
2
2
  include ActionView::Helpers::NumberHelper
3
3
 
4
- belongs_to :store_credit, class_name: 'Spree::StoreCredit'
5
- belongs_to :purchaser, class_name: 'Spree::User'
6
- belongs_to :redeemer, class_name: 'Spree::User'
7
- belongs_to :line_item, class_name: 'Spree::LineItem'
8
- belongs_to :inventory_unit, class_name: 'Spree::InventoryUnit'
4
+ belongs_to :store_credit, class_name: 'Spree::StoreCredit', optional: true
5
+ belongs_to :purchaser, class_name: 'Spree::User', optional: true
6
+ belongs_to :redeemer, class_name: 'Spree::User', optional: true
7
+ belongs_to :line_item, class_name: 'Spree::LineItem', optional: true
8
+ belongs_to :inventory_unit, class_name: 'Spree::InventoryUnit', optional: true
9
9
  has_one :order, through: :line_item
10
10
 
11
11
  validates :amount, numericality: { greater_than: 0 }
@@ -16,6 +16,9 @@ class Spree::VirtualGiftCard < ActiveRecord::Base
16
16
  scope :by_redemption_code, -> (redemption_code) { where(redemption_code: redemption_code) }
17
17
  scope :purchased, -> { where(redeemable: true) }
18
18
 
19
+ self.whitelisted_ransackable_associations = %w[line_item order]
20
+ self.whitelisted_ransackable_attributes = %w[redemption_code recipient_email sent_at send_email_at]
21
+
19
22
  ransacker :sent_at do
20
23
  Arel.sql('date(sent_at)')
21
24
  end
@@ -39,15 +42,15 @@ class Spree::VirtualGiftCard < ActiveRecord::Base
39
42
  action_originator: self,
40
43
  category: store_credit_category,
41
44
  })
42
- self.update_attributes( redeemed_at: Time.now, redeemer: redeemer )
45
+ self.update( redeemed_at: Time.now, redeemer: redeemer )
43
46
  end
44
47
 
45
48
  def make_redeemable!(purchaser:, inventory_unit:)
46
- update_attributes!(redeemable: true, purchaser: purchaser, inventory_unit: inventory_unit, redemption_code: (self.redemption_code || generate_unique_redemption_code))
49
+ update!(redeemable: true, purchaser: purchaser, inventory_unit: inventory_unit, redemption_code: (self.redemption_code || generate_unique_redemption_code))
47
50
  end
48
51
 
49
52
  def deactivate
50
- update_attributes(redeemable: false, deactivated_at: Time.now) &&
53
+ update(redeemable: false, deactivated_at: Time.now) &&
51
54
  cancel_and_reimburse_inventory_unit
52
55
  end
53
56
 
@@ -73,7 +76,7 @@ class Spree::VirtualGiftCard < ActiveRecord::Base
73
76
  end
74
77
 
75
78
  def formatted_redemption_code
76
- redemption_code.present? ? redemption_code.scan(/.{4}/).join('-') : ""
79
+ redemption_code.present? ? redemption_code.scan(/.{4}/).join('-') : ''
77
80
  end
78
81
 
79
82
  def formatted_amount
@@ -81,23 +84,23 @@ class Spree::VirtualGiftCard < ActiveRecord::Base
81
84
  end
82
85
 
83
86
  def formatted_send_email_at
84
- send_email_at.strftime("%-m/%-d/%y") if send_email_at
87
+ send_email_at.strftime('%-m/%-d/%y') if send_email_at
85
88
  end
86
89
 
87
90
  def formatted_sent_at
88
- sent_at.strftime("%-m/%-d/%y") if sent_at
91
+ sent_at.strftime('%-m/%-d/%y') if sent_at
89
92
  end
90
93
 
91
94
  def formatted_created_at
92
- created_at.localtime.strftime("%F %I:%M%p")
95
+ created_at.localtime.strftime('%F %I:%M%p')
93
96
  end
94
97
 
95
98
  def formatted_redeemed_at
96
- redeemed_at.localtime.strftime("%F %I:%M%p") if redeemed_at
99
+ redeemed_at.localtime.strftime('%F %I:%M%p') if redeemed_at
97
100
  end
98
101
 
99
102
  def formatted_deactivated_at
100
- deactivated_at.localtime.strftime("%F %I:%M%p") if deactivated_at
103
+ deactivated_at.localtime.strftime('%F %I:%M%p') if deactivated_at
101
104
  end
102
105
 
103
106
  def store_credit_category
@@ -110,7 +113,7 @@ class Spree::VirtualGiftCard < ActiveRecord::Base
110
113
 
111
114
  def send_email
112
115
  Spree::GiftCardMailer.gift_card_email(self).deliver_later
113
- update_attributes!(sent_at: DateTime.now)
116
+ update!(sent_at: DateTime.now)
114
117
  end
115
118
 
116
119
  private