Skip to content

Instantly share code, notes, and snippets.

@cdesch
Last active March 29, 2016 12:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdesch/7874594 to your computer and use it in GitHub Desktop.
Save cdesch/7874594 to your computer and use it in GitHub Desktop.
#ActiveAdmin User
ActiveAdmin.register User do
index do
column :email
column :alias
column :current_sign_in_at
column :last_sign_in_at
column :sign_in_count
default_actions
end
filter :email
show do
h3 user.alias
attributes_table do
row :email
row :first_name
row :last_name
row :alias
row :bio
end
table_for user.roles do
column "Role" do |role|
role.name
end
end
end
form do |f|
#Debugging
if f.object.errors.size >= 1
f.inputs "Errors" do
f.object.errors.full_messages.join('|')
end
end
f.inputs "Admin Details" do
f.input :email
f.input :first_name
f.input :last_name
f.input :alias
f.input :bio
f.input :password, :hint => "Not Required if not changing the password" , :required => false
f.input :password_confirmation
f.input :roles, :as => :check_boxes
f.input :photo_link
end
f.actions
end
controller do
def permitted_params
params.permit(:user => [:email,:first_name, :last_name, :alias,:bio, :password, :password_confirmation, :photo_link, :roles, :role_id, :role_ids])
params.permit(:user_roles => [:user_id, :role_id])
end
end
end
#User Model
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
if Rails.env.production?
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable
else
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable
end
validates_presence_of :first_name, :last_name, :alias, :email, :case_sensitive => true
validates_uniqueness_of :email, :case_sensitive => false
validates_uniqueness_of :alias, :case_sensitive => false
has_many :entries
belongs_to :role
before_create :assign_role
def assign_role
# assign a default role if no role is assigned
self.add_role :user if self.roles.first.nil?
end
=begin
after_create { |admin| admin.send_reset_password_instructions }
def password_required?
new_record? ? false : super
end
=end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment