Skip to content

Instantly share code, notes, and snippets.

@gabeodess
Created October 19, 2015 17:50
Show Gist options
  • Save gabeodess/bd9aa8ddae104f82357b to your computer and use it in GitHub Desktop.
Save gabeodess/bd9aa8ddae104f82357b to your computer and use it in GitHub Desktop.
ActiveAdmin.register User do
scope("all", :default => true)
scope("setup a subscription"){ |scope| scope.where("(SELECT COUNT(*) FROM subscriptions WHERE subscriptions.user_id = users.id LIMIT 1) = 1") }
scope("canceled"){ |scope| scope.where("
(SELECT COUNT(*) FROM subscriptions WHERE subscriptions.user_id = users.id LIMIT 1) = 1 AND
(SELECT COUNT(*) FROM subscriptions WHERE subscriptions.user_id = users.id AND subscriptions.archived_at IS NULL) = 0
") }
scope("active"){ |scope| scope.where(:id => Subscription.find_active.select(:user_id)) }
# scope("30 days +"){ |scope| scope.where(:id => Subscription.find_active.where("subscriptions.created_at < ?", 30.days.ago).select(:user_id)) }
# scope("60 days +"){ |scope| scope.where(:id => Subscription.find_active.where("subscriptions.created_at < ?", 60.days.ago).select(:user_id)) }
# scope("90 days +"){ |scope| scope.where(:id => Subscription.find_active.where("subscriptions.created_at < ?", 90.days.ago).select(:user_id)) }
scope("1 order"){ |scope| scope.where(:orders_count => 1) }
(2..5).to_a.each do |i|
scope("#{i}+ orders"){ |scope| scope.where("users.orders_count >= #{i}") }
end
menu :priority => 10
actions :all#, except: [:destroy]
permit_params :name, :email, :password, :referrer_id, :referral_code
# ==================
# = Custom Actions =
# ==================
member_action :adjust_balance, :method => :post do
@user = User.find(params[:id])
if (amount = params[:amount].to_f) != 0
@user.adjust_balance!(params[:amount])
bal_was = view_context.number_to_currency((@user.store_was || {})['account_balance'].to_f/100)
bal_is = view_context.number_to_currency(@user.store['account_balance'].to_f/100)
comment = ActiveAdmin::Comment.new(params[:active_admin_comment].permit(:body))
comment.body = [comment.body, view_context.number_to_currency(amount.to_f), bal_was, bal_is].join(" | ")
comment.resource = @user
comment.namespace = :admin
comment.author = current_admin_user
comment.save!
end
redirect_to [:admin, @user]
end
member_action :log_in, :method => :post do
@user = User.find(params[:id])
sign_in(@user)
redirect_to :subscriptions
end
member_action :approve, :method => :put do
resource.approve!
Mailer.account_approved(resource).deliver_later
redirect_to :admin_users
end
# ================
# = Action Items =
# ================
action_item :orders, :only => [:show] do
link_to('Orders', admin_orders_path(:q => {:user_email_eq => resource.email}))
end
action_item :subscriptions, :only => [:show] do
link_to('Subscriptions', admin_subscriptions_path(:q => {:user_email_eq => resource.email}))
end
action_item :invoices, :only => [:show] do
link_to('Invoices', admin_invoices_path(:q => {:user_email_eq => resource.email}))
end
action_item :sign_in, :only => [:show] do
link_to('Sign In', [:log_in, :admin, resource], :method => :post)
end
action_item :new, :only => [:show] do
link_to('New User', [:new, :admin, :user])
end
# =================
# = Batch Actions =
# =================
batch_action :message do |ids|
@message = Message.new(:extra_emails => User.where(:id => ids).map{ |i| %Q{"#{i.name}" <#{i.email}>} }.join(', '))
redirect_to new_admin_message_path(:message => {:extra_emails => @message.extra_emails})
# render "active_admin/resource/new", :layout => false, :resource => @message, :message => @message
end
filter :subscriptions_count, :as => :numeric
filter :name
filter :email
filter :id
filter :created_at
filter :last_sign_in_at
filter :points
filter :status
filter :approved_at
show do
present(user) do |user|
attributes_table do
row(:balance) do
div{user.balance}
div do
form_for ActiveAdmin::Comment.new, :url => [:adjust_balance, :admin, user] do |f|
fieldset do
div{label_tag :amount, "Notice: a negative balance corresponds to a customer credit and a positive balance corresponds to an outstanding balance."}
div(:class => 'form-group'){number_field_tag :amount, nil, :required => true, :placeholder => 'Amount', :step => "0.01"}
div{f.text_area :body, :placeholder => 'Why?', :required => true}
div{f.submit("Adjust balance", :data => {:disable_with => '...'})}
end
end
end
end
user.attributes.each do |key,value|
next if key.in?(%w( encrypted_password ))
row(key)
end
end
panel "Coupons" do
semantic_form_for [:admin, resource.coupons.build], builder: ActiveAdmin::FormBuilder do |f|
f.inputs "Add Coupon" do
f.input :dollars_off
f.input :percent_off
end
f.actions
end
end
end
active_admin_comments
end
index do
selectable_column
id_column
column(:email)
column(:name)
column(:referrer)
column(:active_subscriptions){ |i| link_to([i.subscriptions.find_active.count, i.subscriptions.count].join('/'), admin_subscriptions_path({:q => {:user_id_eq => i.id}})) }
column(:orders_count)
column(:created_at)
actions :defaults => false do |resource|
item 'View', admin_user_path(resource), :class => 'member_link'
item 'Edit', edit_admin_user_path(resource), :class => 'member_link'
item "Delete", [:admin, resource], :class => 'member_link', :method => :delete if resource.destroyable?
item "Sign In", [:log_in, :admin, resource], :method => :post
end
end
form do |f|
f.inputs do
f.input :referrer_id
f.input :referral_code
f.input :name
f.input :email
# f.input :password
end
f.actions
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment