Last active
December 30, 2015 17:59
-
-
Save joseph-ravenwolfe/7864692 to your computer and use it in GitHub Desktop.
Adding Dynamic Roles to Users
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db/migrate/20131208171336_create_roles.rb | |
class CreateRoles < ActiveRecord::Migration | |
def change | |
create_table :roles do |t| | |
t.string :name | |
t.timestamps | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# db/migrate/20131208171457_add_role_id_to_users.rb | |
class AddRoleIdToUsers < ActiveRecord::Migration | |
def change | |
add_column :users, :role_id, :integer | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/views/admin/users/new.html.erb | |
<%= # form_for ... %> | |
<div class="field"> | |
<%= f.label :role_id %> | |
<%= f.collection_select :role_id, Role.all, :id, :to_s %> | |
<a data-action=add-role href="#">Add New</a> | |
</div> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/role.rb | |
class Role < ActiveRecord::Base | |
# They don't really need to have_many :users, unless that is useful. | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/views/users/show.html.erb | |
<p>You are an <%= @user.role_name %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UserForm = do -> | |
initialize = -> | |
$('[data-action=add-role]').on 'click', -> addRole() | |
addRole = -> | |
# Here you can hide the select box and show a text | |
# field, then have the user model accept that text | |
# field and create a role before save. | |
# | |
# Another thing you could do is provide a little | |
# modal that allows them to type in a role name, | |
# perform an AJAX request to save that role, and | |
# add that role to the select box with it automatically | |
# selected. | |
# | |
# Or of course, you could also have a separate UI | |
# for managing roles. | |
return { | |
initialize: initialize | |
addRole: addRole | |
} | |
UserForm.initialize() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/your_user_model.rb | |
class YourUserModel < ActiveRecord::Base | |
belongs_to :role | |
# This will allow you to call "user.role_name" and retrieve | |
# the role without it raising an exception if the associated | |
# object does not exist. | |
# | |
delegate :name, to: :role, prefix: true, allow_nil: :true | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment