Skip to content

Commit

Permalink
Merge pull request #2 from spree-edge/4-8-stable
Browse files Browse the repository at this point in the history
4 8 stable
  • Loading branch information
rahul-bash authored Dec 2, 2024
2 parents 1e7ba12 + 1b2cc91 commit 619eb88
Show file tree
Hide file tree
Showing 110 changed files with 1,719 additions and 935 deletions.
101 changes: 0 additions & 101 deletions .rubocop.yml

This file was deleted.

1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-2.6.5
13 changes: 12 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# frozen_string_literal: true

source 'https://rubygems.org'
gem 'spree', github: 'spree/spree', branch: '3-3-stable'

git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
"https://github.com/#{repo_name}.git"
end

gem 'rails-controller-testing'
gem 'rubocop-rails', '~> 2.14', '>= 2.14.2'
gem 'spree', '~> 4.8'

group :test do
gem 'timecop', '~> 0.8.1'
end
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ SpreeGiftCard is an extension and one stop solution to integrate gift card funct

* Recipient can then redeem the gift card by entering the unique gift card `Code` during checkout on payment step.

* Removed deface dependency.

## Installation

1. Just add this line to your `Gemfile`:
Expand Down
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'bundler'
Bundler::GemHelper.install_tasks

Expand All @@ -6,7 +8,7 @@ require 'spree/testing_support/common_rake'

RSpec::Core::RakeTask.new

task :default => [:spec]
task default: [:spec]

desc 'Generates a dummy app for testing'
task :test_app do
Expand Down
5 changes: 0 additions & 5 deletions Versionfile

This file was deleted.

40 changes: 35 additions & 5 deletions app/controllers/spree/admin/gift_cards_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# frozen_string_literal: true
module Spree
module Admin
class GiftCardsController < Spree::Admin::ResourceController
before_action :set_gift_card, :find_gift_card_variant, except: :destroy
before_action :find_gift_card, only: %i[edit show update resend]

def create
@object.assign_attributes(gift_card_params)
Expand All @@ -11,18 +13,41 @@ def create
flash[:success] = Spree.t(:successfully_created_gift_card)
redirect_to admin_gift_cards_path
else
render :new
redirect_to new_gift_card_path
end
end

def show; end

def edit; end

def update
flash[:success] = Spree.t(:successfully_updated_gift_card) if @gift_card.update(gift_card_params)
redirect_to admin_gift_cards_path
end

def resend
order = @gift_card.order

if order.present?
order.update_columns(gift_card_notified: false) # Reset notified flag.
order.gift_card_notification
flash[:success] = Spree.t(:gift_card_email_resent)
else
flash[:error] = Spree.t(:gift_card_order_not_found)
end

redirect_back fallback_location: spree.edit_admin_gift_card_url(@gift_card)
end

private

def collection
super.order(created_at: :desc).page(params[:page]).per(Spree::Config[:admin_orders_per_page])
super.order(created_at: :desc).page(params[:page]).per(Spree::Backend::Config[:admin_orders_per_page])
end

def set_gift_card
@is_e_gift_card = request.path.include?('new-digital') || (params[:gift_card] && params[:gift_card][:e_gift_card] == 'true')
@is_e_gift_card = request.path.include?('new-digital') || (params[:gift_card].present? && params[:gift_card][:e_gift_card] == 'true')
end

def find_gift_card_variant
Expand All @@ -32,7 +57,7 @@ def find_gift_card_variant
else
products.not_e_gift_cards
end
@gift_card_variant_id = products.first.master.id
@gift_card_variant_id = products&.first&.master&.id
end

def gift_card_params
Expand All @@ -43,9 +68,14 @@ def gift_card_params
:original_value,
:sender_name,
:sender_email,
:delivery_on
:delivery_on,
:state
)
end

def find_gift_card
@gift_card = Spree::GiftCard.find_by(id: params[:id])
end
end
end
end
54 changes: 0 additions & 54 deletions app/controllers/spree/checkout_controller_decorator.rb

This file was deleted.

69 changes: 39 additions & 30 deletions app/controllers/spree/gift_cards_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module Spree
class GiftCardsController < Spree::StoreController
before_action :load_master_variant, only: :new
Expand All @@ -23,32 +25,31 @@ def new
end

def create
begin
# Wrap the transaction script in a transaction so it is an atomic operation
Spree::GiftCard.transaction do
@gift_card = GiftCard.new(gift_card_params)
@gift_card.save!
# Create line item
line_item = LineItem.new(quantity: 1)
line_item.gift_card = @gift_card
line_item.variant = @gift_card.variant
line_item.price = @gift_card.variant.price
# Add to order
order = current_order(create_order_if_necessary: true)
order.line_items << line_item
line_item.order = order
order.update_totals
order.updater.update_item_count
order.save!
# Save gift card
@gift_card.line_item = line_item
@gift_card.save!
end
redirect_to cart_path
rescue ActiveRecord::RecordInvalid
find_gift_card_variants
render :new
# Wrap the transaction script in a transaction so it is an atomic operation
Spree::GiftCard.transaction do
@gift_card = GiftCard.new(gift_card_params)
@gift_card.save!
# Create line item
line_item = LineItem.new(quantity: 1)
line_item.gift_card = @gift_card
line_item.variant = @gift_card.variant
line_item.price = @gift_card.variant.price
# Add to order
order = current_order(create_order_if_necessary: true)
order.line_items << line_item
line_item.order = order
order.update_totals
order.updater.update_item_count
order.save!
# Save gift card
@gift_card.line_item = line_item
@gift_card.save!
end
redirect_to cart_path
rescue StandardError => e
flash[:error] = e.message
find_gift_card_variants
redirect_to new_gift_card_path
end

private
Expand All @@ -59,21 +60,29 @@ def redirect_after_redeem

def load_gift_card
@gift_card = Spree::GiftCard.where(code: params[:id]).last
unless @gift_card
redirect_to root_path, flash: { error: Spree.t('gift_code_not_found') }
end
redirect_to root_path, flash: { error: ::Spree.t('gift_code_not_found') } unless @gift_card
end

def find_gift_card_variants
products = Product.not_deleted.gift_cards
products = Spree::Product.not_deleted.gift_cards
products = if @is_e_gift_card
products.e_gift_cards
else
products.not_e_gift_cards
end
@gift_card_product = products.first
gift_card_product_ids = products.pluck(:id)
@gift_card_variants = Variant.joins(:prices).where(["amount > 0 AND product_id IN (?)", gift_card_product_ids]).order("amount")

@gift_card_variants = []

products.each do |product|
@gift_card_variants += if product.variants.present?
product.variants.select(&:non_zero_price?)
else
(product.master.price.positive? ? [product.master] : [])
end
end
@gift_card_variants = @gift_card_variants.sort_by(&:price)
end

def gift_card_params
Expand Down
9 changes: 0 additions & 9 deletions app/controllers/spree/products_controller_decorator.rb

This file was deleted.

Loading

0 comments on commit 619eb88

Please sign in to comment.