Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgraded controllers and models to modules also upgraded overrides an… #66

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Try Spree Product Subscriptions for Spree master with direct deployment on Herok

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/vinsol-spree-contrib/spree-demo-heroku/tree/spree-product-subscriptions-master)

Try Spree Product Subscriptions for Spree 4 with direct deployment on Heroku:

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/vinsol-spree-contrib/spree-demo-heroku/tree/spree-product-subscriptions-4-1)

Try Spree Product Subscriptions for Spree 3-4 with direct deployment on Heroku:

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/vinsol-spree-contrib/spree-demo-heroku/tree/spree-product-subscriptions-3-4)
Expand Down
4 changes: 2 additions & 2 deletions app/assets/javascripts/spree/backend/subscribable.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ Subscribable.prototype.bindParentCheckboxEvent = function() {
};

Subscribable.prototype.enableChildCheckboxes = function() {
this.$childCheckboxesDiv.removeClass("hidden");
this.$childCheckboxesDiv.removeClass("is-hidden");
this.$childCheckboxesDiv.find("input").removeAttr("disabled");
};

Subscribable.prototype.disableChildCheckboxes = function() {
this.$childCheckboxesDiv.addClass("hidden");
this.$childCheckboxesDiv.addClass("is-hidden");
this.$childCheckboxesDiv.find("input").attr("disabled", "disabled");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Spree.ready(function($) {
Spree.addToCartFormSubmissionOptions = function() {
$cartForm = $(ADD_TO_CART_FORM_SELECTOR);
if($cartForm.find('.cart_radio_button:checked').val() == ".subscription_options") {
options = {
subscribe: true,
subscription_frequency_id: $cartForm.find('#subscription_subscription_frequency_id option:selected').val(),
delivery_number: parseInt($cartForm.find('#subscription_delivery_number').val())
};
return options;
}
return {};
}
})
19 changes: 8 additions & 11 deletions app/assets/javascripts/spree/frontend/cart_radio_button.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ CartRadioButton.prototype.bindEvents = function() {
};

CartRadioButton.prototype.toggleDiv = function($checkBox) {
$($checkBox.val()).show();
this.hideOtherDivs();
};

CartRadioButton.prototype.hideOtherDivs = function() {
$.each(this.$radioButtons, function(index, value) {
$checkBox = $(value);
if (!$checkBox.prop("checked")) {
$($checkBox.val()).hide();
}
});
let value = $checkBox.val();
$(value).show();
if (value.includes('subscription_options')) {
$('#add-to-cart-button').text('SUBSCRIBE NOW');
} else {
$('#add-to-cart-button').text('ADD TO CART');
$(value).siblings('.subscription_options').hide();
}
};

$(function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Placeholder manifest file.
//= require spree/frontend/cart_radio_button.js
//= require spree/frontend/datepicker
//= require spree/frontend/cart_form_subscription_button
//= require spree/frontend/ajax_handler.js
//= require spree/frontend/update_quantity_in_cart
// the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/frontend/all.js'

This file was deleted.

3 changes: 0 additions & 3 deletions app/controllers/spree/base_controller_decorator.rb

This file was deleted.

12 changes: 0 additions & 12 deletions app/controllers/spree/users_controller_decorator.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module SpreeProductSubscriptions
module BaseControllerDecorator

def self.prepended(base)
base.add_flash_types :success, :error
end

end
end

Spree::BaseController.prepend SpreeProductSubscriptions::BaseControllerDecorator
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module SpreeProductSubscriptions::LineItemsControllerDecorator

def self.prepended(base)
base.line_item_options += [:subscribe, :delivery_number, :subscription_frequency_id]
end


end

Spree::Api::V1::LineItemsController.prepend SpreeProductSubscriptions::LineItemsControllerDecorator
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Spree::OrdersController.class_eval do
module SpreeProductSubscriptions::OrdersControllerDecorator

before_action :add_subscription_fields, only: :populate, if: -> { params[:subscribe].present? }
before_action :restrict_guest_subscription, only: :update, unless: :spree_current_user
def self.prepended(base)
# base.before_action :add_subscription_fields, only: :populate, if: -> { params[:subscribe].present? }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets discuss

base.before_action :restrict_guest_subscription, only: :update, unless: :spree_current_user
end

private

Expand All @@ -20,3 +22,5 @@ def add_subscription_fields
end

end

Spree::OrdersController.prepend SpreeProductSubscriptions::OrdersControllerDecorator
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Spree::Admin::PaymentsController.class_eval do
module SpreeProductSubscriptions::PaymentsControllerDecorator

private

Expand All @@ -13,3 +13,5 @@ def available_payment_methods
end

end

Spree::Admin::PaymentsController.prepend SpreeProductSubscriptions::PaymentsControllerDecorator
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module SpreeProductSubscriptions::UsersControllerDecorator

def self.prepended(base)
base.before_action :load_subscriptions, only: :show
end

private

def load_subscriptions
@orders = @user.orders.complete.order(completed_at: :desc)
@subscriptions = Spree::Subscription.active.order(created_at: :desc).with_parent_orders(@orders)
end

end

Spree::UsersController.prepend SpreeProductSubscriptions::UsersControllerDecorator
46 changes: 0 additions & 46 deletions app/models/spree/order_decorator.rb

This file was deleted.

15 changes: 0 additions & 15 deletions app/models/spree/product_decorator.rb

This file was deleted.

15 changes: 10 additions & 5 deletions app/models/spree/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,20 @@ def number_of_deliveries_left

def pause
run_callbacks :pause do
update_attributes(paused: true)
update(paused: true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets discuss

end
end

def unpause
run_callbacks :unpause do
update_attributes(paused: false)
update(paused: false)
end
end

def cancel
self.cancelled = true
run_callbacks :cancel do
update_attributes(cancelled_at: Time.current)
update(cancelled_at: Time.current)
end
end

Expand Down Expand Up @@ -189,7 +189,12 @@ def make_new_order
end

def add_variant_to_order(order)
order.contents.add(variant, quantity)
# order.contents.add(variant, quantity) Contents removed from Spree 4
Spree::Cart::AddItem.call(
order: order,
variant: variant,
quantity: quantity
)
order.next
end

Expand Down Expand Up @@ -236,7 +241,7 @@ def confirm_order(order)
def order_attributes
{
currency: parent_order.currency,
guest_token: parent_order.guest_token,
token: parent_order.token,
store: parent_order.store,
user: parent_order.user,
created_by: parent_order.user,
Expand Down
7 changes: 0 additions & 7 deletions app/models/spree/variant_decorator.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
Spree::LineItem.class_eval do
module SpreeProductSubscriptions::LineItemDecorator

attr_accessor :subscription_frequency_id, :delivery_number, :subscribe

after_create :create_subscription!, if: :subscribable?
after_update :update_subscription_quantity, if: :can_update_subscription_quantity?
after_update :update_subscription_attributes, if: :can_update_subscription_attributes?
after_destroy :destroy_associated_subscription!, if: :subscription?
def self.prepended(base)
base.after_create :create_subscription!, if: :subscribable?
base.after_update :update_subscription_quantity, if: :can_update_subscription_quantity?
base.after_update :update_subscription_attributes, if: :can_update_subscription_attributes?
base.after_destroy :destroy_associated_subscription!, if: :subscription?
base.attr_accessor :subscription_frequency_id, :delivery_number, :subscribe
end

def subscription_attributes_present?
subscription_frequency_id.present? || delivery_number.present?
Expand Down Expand Up @@ -67,3 +69,5 @@ def can_update_subscription_quantity?
end

end

Spree::LineItem.prepend SpreeProductSubscriptions::LineItemDecorator
40 changes: 40 additions & 0 deletions app/models/spree_product_subscriptions/order_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module SpreeProductSubscriptions::OrderDecorator

def self.prepended(base)
base.has_one :order_subscription, class_name: "Spree::OrderSubscription", dependent: :destroy
base.has_one :parent_subscription, through: :order_subscription, source: :subscription
base.has_many :subscriptions, class_name: "Spree::Subscription",
foreign_key: :parent_order_id,
dependent: :restrict_with_error
base.after_update :update_subscriptions
base.state_machine.after_transition to: :complete, do: :enable_subscriptions, if: :any_disabled_subscription?
base.alias_attribute :guest_token, :token
end

private

def enable_subscriptions
subscriptions.each do |subscription|
subscription.update(
source: payments.from_credit_card.first.source,
enabled: true,
ship_address: ship_address.clone,
bill_address: bill_address.clone
)
end
end

def any_disabled_subscription?
subscriptions.disabled.any?
end

def update_subscriptions
line_items.each do |line_item|
if line_item.subscription_attributes_present?
subscriptions.find_by(variant: line_item.variant).update(line_item.updatable_subscription_attributes)
end
end
end
end

Spree::Order.prepend SpreeProductSubscriptions::OrderDecorator
15 changes: 15 additions & 0 deletions app/models/spree_product_subscriptions/product_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module SpreeProductSubscriptions::ProductDecorator

def self.prepended(base)
base.has_many :subscriptions, through: :variants_including_master, source: :subscriptions, dependent: :restrict_with_error
base.has_many :product_subscription_frequencies, class_name: "Spree::ProductSubscriptionFrequency", dependent: :destroy
base.has_many :subscription_frequencies, through: :product_subscription_frequencies, dependent: :destroy
base.validates :subscription_frequencies, presence: true, if: :subscribable?
base.scope :subscribable, -> { where(subscribable: true) }
base.whitelisted_ransackable_attributes += %w( is_subscribable )
base.alias_attribute :subscribable, :is_subscribable
end

end

Spree::Product.prepend SpreeProductSubscriptions::ProductDecorator
12 changes: 12 additions & 0 deletions app/models/spree_product_subscriptions/variant_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module SpreeProductSubscriptions::VariantDecorator


def self.prepended(base)
base.has_many :subscriptions, class_name: "Spree::Subscription", dependent: :restrict_with_error
base.delegate :variants_including_master, to: :product, prefix: true
base.alias_method :product_variants, :product_variants_including_master
end

end

Spree::Variant.prepend SpreeProductSubscriptions::VariantDecorator
2 changes: 1 addition & 1 deletion app/overrides/add_checkboxes_to_product_show_page.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Deface::Override.new(
virtual_path: "spree/products/_cart_form",
name: "add_checkboxes_to_cart_form",
insert_before: ".add-to-cart",
insert_after: "hr:last",
partial: "spree/products/cart_checkboxes"
)
Empty file.
Loading