Skip to content

Instantly share code, notes, and snippets.

@joshuaclayton
Created December 9, 2008 03:53
Show Gist options
  • Save joshuaclayton/33753 to your computer and use it in GitHub Desktop.
Save joshuaclayton/33753 to your computer and use it in GitHub Desktop.
class Generate
def self.user(attributes = {})
key = ActiveSupport::SecureRandom.hex(16)
user = User.new(attributes.reverse_merge({
:first_name => "John",
:last_name => "Doe",
:password => "password",
:password_confirmation => "password",
:active => true,
:username => key,
:email_address => "#{key}@test.com"
}))
default_privileges = (["has_account"] << attributes.delete(:privileges)).compact.uniq
role = Role.find_or_create_by_name(attributes.delete(:role) || "user")
privs = []
default_privileges.each {|p| privs << Privilege.find_or_create_by_name(p)}
user.role = role
role.privileges << privs
user.save!
user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment