Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mzaragoza/5fe97d69792c268ed531 to your computer and use it in GitHub Desktop.
Save mzaragoza/5fe97d69792c268ed531 to your computer and use it in GitHub Desktop.
Wallaby Sample
class Users::RegistrationsController < Devise::RegistrationsController
layout :choose_layout
expose(:selected_plan) { Plan.find_by_slug(params[:plan]) }
after_filter :after_registration, :only => [:create]
def choose_layout
if params[:action] == 'edit' or params[:action] =='update' or params[:action] =='change_password'
'users/default'
else
'users/login'
end
end
def new
redirect_to user_root_path if current_user
resource = build_resource({})
resource.build_account
resource.account.cards.build
resource.agree_newsletter = true
#resource.account.build_subscription
#resource.plan_slug = plan.slug if plan
respond_with resource
end
def create
build_resource(sign_up_params)
resource.is_admin = true
resource.is_owner = true
resource.phone = resource.account.phone
resource.account.brand_id = brand.id
if resource.save
yield resource if block_given?
if resource.active_for_authentication?
add_referal
resource.post_sign_up
#set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
message = 'New Account ' + "\n"
message = message + ' ON: ' + Rails.env + "\n"
message = message + ' Acc Name: ' + resource.account.name + "\n"
message = message + ' Owner Name: ' + resource.name + "\n"
message = message + ' Phone: ' + resource.phone + "\n"
alert_managers ({:message => message})
Resque.enqueue(RegistrationMailerQueue, { :user_id => resource.id })
respond_with resource, :location => after_sign_up_path_for(resource)
else
#set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, :location => after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
render :action => 'new'
#respond_with({}, :location => after_sign_up_fails_path_for(resource))
end
end
def after_inactive_sign_up_path_for(resource)
users_root_path
end
def after_sign_up_fails_path_for(resource)
new_user_registration_path
end
def after_sign_up_path_for(resource)
session[:just_registered] = true
sign_in(resource)
users_root_path
end
private
def sign_up_params
allow = [:first_name, :last_name, :phone, :email, :password, :password_confirmation, :agree_newsleter, account_attributes: [:name, :phone, :website, :company_size, :sign_up_plan_id, :credit_card_token, :stripe_card_id]]
params.require(resource_name).permit(allow)
end
def after_update_path_for(resource)
users_root_path(resource)
end
def after_registration
unless resource.new_record?
create_subscription
end
end
def create_subscription
Subscription.create(
:account_id => resource.account.id,
:plan_id => resource.account.sign_up_plan_id,
:price => resource.account.sign_up_plan.price,
:number_of_users => resource.account.sign_up_plan.number_of_users,
:number_of_residents => resource.account.sign_up_plan.number_of_residents,
:name => resource.account.sign_up_plan.name,
:minimum_number_of_residents => resource.account.sign_up_plan.minimum_number_of_residents,
:subscribable_id => resource.account.id,
:subscribable_type => 'Account'
)
if params[:user][:account_attributes][:cards_attributes].first[1][:stripe_card_id]
Resque.enqueue(ProcessSubscriptionQueue, { :account_id => resource.account.id, :card_token => params[:user][:account_attributes][:cards_attributes].first[1][:stripe_card_id], :description => brand.name })
else
Resque.enqueue(ProcessSubscriptionQueue, { :account_id => resource.account.id, :description => brand.name })
end
end
def add_referal
if current_guest.utm_partner_id
Referal.create(
:partner_id => current_guest.utm_partner_id,
:account_id => resource.account.id,
:plan_slug => resource.account.sign_up_plan.slug,
:plan_price => resource.account.sign_up_plan.price
)
end
end
end
namespace :billing do
desc "Create invoices for resident"
task :generate_invoices => :environment do
Contract.active.each do |contract|
if contract.end_date.nil? || contract.end_date > Date.today
if contract.resident_invoices.count == 0
create_resident_invoice(contract)
elsif contract.collection_interval == 'weekly'
if contract.resident_invoices.last.created_at.at_beginning_of_day <= DateTime.now.in_time_zone("UTC").at_beginning_of_day - 7.day
create_resident_invoice(contract)
end
elsif contract.collection_interval == 'monthly'
if contract.resident_invoices.last.created_at.at_beginning_of_day <= DateTime.now.in_time_zone("UTC").at_beginning_of_day - 28.day
create_resident_invoice(contract)
end
end
else
contract.status = 'compleated'
contract.save
end
end
end
desc "Update how many residents each account has on ther subscriotion"
task :update_subscription_count => :environment do
set_resident_status
Account.active.each do |a|
residents_count = a.residents.where(:active => true).count
if residents_count <= 3
residents_count = 3
end
stripe_customer_id = a.stripe_customer_id
stripe_subscription_id = a.subscription.stripe_subscription_id
customer = Stripe::Customer.retrieve(stripe_customer_id)
subscription = customer.subscriptions.retrieve(stripe_subscription_id)
subscription.quantity = residents_count
subscription.save
end
end
desc "Send text of active residents per plan"
task :send_text_of_active_residents_per_plan => :environment do
if Date.today.strftime('%A') == 'Monday'
message = Rails.env.upcase
Plan.all.each do |p|
message = message + "\n" + p.name
p.subscriptions.each do |s|
message = message + "\n" + ' - ' + s.name
if s.subscribable
message = message + "\n" + ' -- ' + s.subscribable.name + " (#{s.subscribable_type })"
if s.subscribable_type == 'Account'
message = message + "\n" + ' --- ' + s.subscribable.residents.active.count.to_s + '/' + s.subscribable.residents.count.to_s
else
message = message + "\n" + ' --- ' + s.subscribable.patients.count.to_s
end
else
message = message + "\n" + ' -- DELETE ' + s.id.to_s
end
end
end
Sms.send_message(ENV['phone'], message)
puts message
end
end
def create_resident_invoice(contract)
if contract.collection_interval == 'weekly'
due_date = Date.today + 7.days
period_start = Date.today - 7.days
period_end = Date.today
else
due_date = Date.today + 28.days
period_start = Date.today - 28.days
period_end = Date.today
end
ResidentInvoice.create(
:account_id => contract.account_id ,
:contract_id => contract.id ,
:facility_id => contract.facility_id ,
:resident_id => contract.resident_id ,
:amount => contract.dues ,
:due_date => due_date ,
:period_start => period_start ,
:period_end => period_end ,
:status => 'unpaid' ,
:collection_interval => contract.collection_interval ,
:description => '',
:invoice_type => 'generated'
)
end
def set_resident_status
Resident.all.each do |r|
r.set_status
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment