-
Notifications
You must be signed in to change notification settings - Fork 0
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
wip: paid subscriptions system and articles #467
base: master
Are you sure you want to change the base?
Changes from 38 commits
01e20d1
5a8e0df
f76fb69
fda8cd9
9f884e5
472b9a8
c82f46e
8956ebc
09e8c50
a27935a
97ffc49
adbc98c
ffbccc5
6681798
ca250bd
66907ce
b64f9ec
0e731a6
6d236ec
6ba8bd1
85f996d
4d14f17
518d6c6
152a8b5
2d9083e
5e49aef
63a2e1d
9692009
7c24259
defe95b
434a130
58dff5c
b1997a0
48abb03
368a182
28bd61c
4590d97
c956e56
eb89235
a0d683b
b9db1cd
d2391e9
e112313
4e2e840
fee752c
06ad62e
12e32fd
6cae8a0
012f6bd
5b1bff1
8a5a714
6d3196f
74232b0
ef231da
a85b030
d7bbf2c
604b731
470c527
1768f59
e5b05ec
8894741
720c7d2
1a799a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class ArticlesController < ApplicationController | ||
def new | ||
@article = Article.new | ||
authorize! :new, @article | ||
end | ||
|
||
def create | ||
@article = Article.new(article_params) | ||
authorize! :create, @article | ||
if @article.save | ||
flash[:success] = "Article #{article_params[:name]} created!" | ||
redirect_to admin_path | ||
else | ||
render 'new', status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
@article = Article.find(params[:id]) | ||
authorize! :destroy, @article | ||
@article.soft_delete unless @article.destroy | ||
redirect_to admin_path | ||
end | ||
|
||
def article_params | ||
params.require(:article).permit(:price, :name) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
class DashboardController < ApplicationController | ||
def index | ||
authorize! :manage, :all | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a temporary permission and we will check more granularly, or is the admin dashboard only intended for superadmins? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is temporary permission for now |
||
@articles = Article.where(deleted_at: nil).order(created_at: :desc) | ||
@subscription_offers = SubscriptionOffer.where(deleted_at: nil).order(created_at: :desc) | ||
@payment_methods = PaymentMethod.where(deleted_at: nil).order(created_at: :desc) | ||
DioFun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||||
# frozen_string_literal: true | ||||||
|
||||||
module Admin | ||||||
class PaymentMethodsController < ApplicationController | ||||||
def new | ||||||
@payment_method = PaymentMethod.new | ||||||
authorize! :new, @payment_method | ||||||
end | ||||||
|
||||||
def create | ||||||
@payment_method = PaymentMethod.new(payment_method_params) | ||||||
authorize! :create, @payment_method | ||||||
if @payment_method.save | ||||||
flash[:success] = "Payment method #{payment_method_params[:name]} created!" | ||||||
redirect_to admin_path | ||||||
else | ||||||
render 'new', status: :unprocessable_entity | ||||||
end | ||||||
end | ||||||
|
||||||
def destroy | ||||||
@payment_method = PaymentMethod.find(params[:id]) | ||||||
authorize! :destroy, @payment_method | ||||||
@payment_method.soft_delete unless @payment_method.destroy | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here, maybe comment, maybe write it differently
Suggested change
|
||||||
redirect_to admin_path | ||||||
end | ||||||
|
||||||
def payment_method_params | ||||||
params.require(:payment_method).permit(:name, :auto_verify) | ||||||
end | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||||
# frozen_string_literal: true | ||||||
|
||||||
module Admin | ||||||
class SubscriptionOffersController < ApplicationController | ||||||
def new | ||||||
@subscription_offer = SubscriptionOffer.new | ||||||
authorize! :new, @subscription_offer | ||||||
end | ||||||
|
||||||
def create | ||||||
@subscription_offer = SubscriptionOffer.new(subscription_offer_params) | ||||||
authorize! :create, @subscription_offer | ||||||
if @subscription_offer.save | ||||||
flash[:success] = "Subscription offer for #{subscription_offer_params[:duration]} months created!" | ||||||
redirect_to admin_path | ||||||
else | ||||||
render 'new', status: :unprocessable_entity | ||||||
end | ||||||
end | ||||||
|
||||||
def destroy | ||||||
@subscription_offer = SubscriptionOffer.find(params[:id]) | ||||||
authorize! :destroy, @subscription_offer | ||||||
@subscription_offer.soft_delete unless @subscription_offer.destroy | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same, comment or
Suggested change
|
||||||
redirect_to admin_path | ||||||
end | ||||||
|
||||||
def subscription_offer_params | ||||||
params.require(:subscription_offer).permit(:price, :duration) | ||||||
end | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
class RefundsController < ApplicationController | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
class SalesController < ApplicationController | ||
before_action :owner, only: [:new, :create] | ||
|
||
def new | ||
@sale = @owner.sales_as_client.new | ||
@sale.articles_sales.new | ||
@articles = Article.where(deleted_at: nil) | ||
@subscription_offers = SubscriptionOffer.where(deleted_at: nil).order(duration: :desc) | ||
@payment_methods = PaymentMethod.where(deleted_at: nil) | ||
authorize! :new, @sale | ||
end | ||
|
||
# rubocop:disable Metrics/AbcSize | ||
def create | ||
@sale = @owner.sales_as_client.new(reformated_params) | ||
@sale.update_total_price | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooooh, it's dangerous to have to think to do this before saving, can this be moved to a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Has been moved but not sure if it's enough |
||
@sale.seller = current_user | ||
if @sale.total_price.zero? | ||
flash.now[:error] = "You can't process an empty sale!" | ||
return render :new, status: :unprocessable_entity | ||
end | ||
@sale.verify if @sale.payment_method.auto_verify | ||
@sale.gen_temp_invoice | ||
authorize! :create, @sale | ||
if @sale.save | ||
name = @sale.generate_invoice_id | ||
@sale.invoice.pdf.attach(io: @sale.invoice.generate, filename: name, content_type: 'application/pdf') | ||
@sale.save! | ||
DioFun marked this conversation as resolved.
Show resolved
Hide resolved
|
||
flash[:success] = 'Sale was successfully created.' | ||
redirect_to @owner | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
# rubocop:enable Metrics/AbcSize | ||
|
||
private | ||
|
||
def owner | ||
@owner = User.find(params[:user_id]) | ||
end | ||
|
||
def sales_params | ||
params.require(:sale).permit(:duration, :payment_method_id, articles_sales_attributes: [:article_id, :quantity]) | ||
end | ||
|
||
def reformated_params | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The params methods could probably do with some comments and vertical space to separate blocks ^^' |
||
par = sales_params | ||
par[:sales_subscription_offers_attributes] = duration_to_subscription_offers sales_params[:duration].to_i | ||
par.delete(:duration) | ||
par[:articles_sales_attributes]&.each do |rec| | ||
par[:articles_sales_attributes].delete(rec[0]) if rec[1][:quantity].to_i.zero? | ||
end | ||
par | ||
end | ||
|
||
def duration_to_subscription_offers(duration) | ||
tab = [] | ||
subscription_offers = SubscriptionOffer.where(deleted_at: nil).order(duration: :desc) | ||
if subscription_offers.empty? | ||
flash[:error] = 'There are no subscription offers registered!' | ||
return false | ||
end | ||
subscription_offers.each do |offer| | ||
break if duration.zero? | ||
|
||
quantity = duration / offer.duration | ||
if quantity.positive? | ||
tab << { subscription_offer_id: offer.id, quantity: quantity } | ||
duration -= quantity * offer.duration | ||
end | ||
end | ||
unless duration.zero? | ||
flash[:error] = 'Subscription offers are not exhaustive!' | ||
return false | ||
end | ||
tab | ||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
module ArticlesHelper | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
module DashboardHelper | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
module PaymentMethodsHelper | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
module Admin | ||
module SubscriptionOffersHelper | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
module SubscriptionsHelper | ||
module RefundsHelper | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
module SalesHelper | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a comment to explain why we do this.
Alternative way to write it, not really convinced by it but at least it reads from left to right (and there isn't a side-effect in the conditional):