Skip to content

Instantly share code, notes, and snippets.

@neidiom
Forked from serg2801/Admin Coupon
Created November 27, 2018 16:59
Show Gist options
  • Save neidiom/ec9d68ff7859e63aa00a6cabf95ae7c9 to your computer and use it in GitHub Desktop.
Save neidiom/ec9d68ff7859e63aa00a6cabf95ae7c9 to your computer and use it in GitHub Desktop.
ActiveAdmin.register Coupon do
menu label: proc { current_user.admin? ? 'Coupons' : 'My coupons' }
actions :all, except: [:destroy]
permit_params :name, :coupon_type, :picture, :note, :for_deleting, :business_owner_id
# ===== scopes =====
scope_to :current_user, if: proc { current_user.business_owner? }
scope :all, default: true do |coupons|
coupons
end
scope :not_approved do |coupons|
coupons.not_approved
end
scope :approved do |coupons|
coupons.approved
end
scope :declined do |coupons|
coupons.declined
end
scope :for_deleting do |coupons|
coupons.waiting_for_destroy
end
scope :deleted do |coupons|
coupons.only_deleted
end
# ===== scopes =====
# ===== actions =====
member_action :delete, method: :put do
Coupon.find(params[:id]).destroy_coupon
flash[:notice] = "Coupon successfully deleted!"
redirect_to collection_path
end
member_action :confirm_deleting, method: :put do
Coupon.find(params[:id]).destroy
flash[:notice] = "Coupon successfully deleted!"
redirect_to collection_path
end
member_action :approve, method: :put do
Coupon.find(params[:id]).approve!
flash[:notice] = "Coupon successfully approved!"
redirect_to collection_path
end
member_action :decline, method: :put do
Coupon.find(params[:id]).decline!
flash[:notice] = "Coupon successfully declined!"
redirect_to collection_path
end
# ===== actions =====
# ===== index =====
index title: proc { current_user.admin? ? 'Coupons' : 'My coupons' } do
selectable_column
id_column
column :status do |coupon|
case coupon.status
when /not_approved/
status_tag(coupon.status, :gray)
when /approved/
status_tag(coupon.status, :green)
when /declined/
status_tag(coupon.status, :red)
else
status_tag(coupon.status, :no)
end
end
column :coupon_type do |coupon|
case coupon.coupon_type
when /standard/
status_tag(coupon.coupon_type, :yes)
when /hot/
status_tag(coupon.coupon_type, :red)
else
status_tag(coupon.coupon_type, :no)
end
end
column :name
column :picture do |coupon|
if coupon.picture.url.blank?
image_tag 'default_image.png', class: 'default_image'
else
image_tag coupon.picture.url(:thumb), class: ''
end
end
column :note
column :for_deleting if params['scope'].in? [nil, 'all']
if params['scope'] == 'not_approved' && current_user.admin?
actions defaults: false do |coupon|
item 'Approve', approve_admin_coupon_path(coupon), method: :put, data: {confirm: 'Are you sure?'}, class: 'member_link'
item 'Decline', decline_admin_coupon_path(coupon), method: :put, data: {confirm: 'Are you sure?'}, class: 'member_link'
end
end
actions
if params['scope'] == 'for_deleting' && current_user.admin?
actions defaults: false do |coupon|
item 'Confirm deleting', confirm_deleting_admin_coupon_path(coupon), method: :put, data: {confirm: 'Are you sure?'}, class: 'member_link'
end
elsif params['scope'].in?([nil, 'all', 'not_approved', 'approved', 'declined']) && current_user.business_owner?
actions defaults: false do |coupon|
item 'Delete', delete_admin_coupon_path(coupon), method: :put, data: {confirm: 'Are you sure?'}, class: 'member_link'
end
end
end
# ===== index =====
# ===== show =====
show do
attributes_table do
row :id
row :business_owner
row :status
row :coupon_type
row :name
row :picture
row :note
row :for_deleting
row :created_at
row :updated_at
end
end
# ===== show =====
# ===== form =====
form do |f|
f.inputs do
if current_user.admin?
f.input :business_owner_id, input_html: {min: 1}
f.input :coupon_type if object.new_record?
else
f.input :business_owner_id, input_html: {readonly: true, disabled: true}
f.input :coupon_type, input_html: {readonly: true, disabled: true, value: '1'}, selected: 'hot'
end
f.input :picture
f.input :name
f.input :note
f.input :for_deleting if object.persisted?
end
f.actions
end
# ===== form =====
# ===== filters =====
filter :business_owner
filter :name
filter :status, as: :select, collection: Coupon.statuses.to_a.map { |i| [i.first.humanize, i.second] }
filter :coupon_type, as: :select, collection: Coupon.coupon_types.to_a.map { |i| [i.first.humanize, i.second] }
# ===== filters =====
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment