Friday, March 29, 2024
HomeRuby On RailsRoles from Scratch | Drifting Ruby

Roles from Scratch | Drifting Ruby


# Terminal
rails g migration add_role_to_users function:integer
# AddRoleToUsers Migration
class AddRoleToUsers < ActiveRecord::Migration[7.0]
  def change
   add_column :customers, :function, :integer, default: 0, restrict: 1
  finish
finish
# fashions/person.rb
  enum function: {
    regular: 0,
    admin: 1
  }
# users_controller.rb
def user_params
  allowed_attributes = [:email, :name]
  if user_signed_in? && current_user.admin?
    allowed_attributes << :function
  finish
  params.require(:person).allow(allowed_attributes)
finish
# Utilization
if current_user.admin?
# Terminal
rails g mannequin function identify reference entry:integer
rails g mannequin user_role person:belongs_to function:belongs_to
# CreateRoles Migration
class CreateRoles < ActiveRecord::Migration[7.0]
  def change
    create_table :roles do |t|
      t.string :identify
      t.string :reference
      t.integer :entry, restrict: 1, default: 0

      t.timestamps
    finish
  finish
finish
# db/seeds.rb
admin = Person.create(e-mail: "[email protected]", password: "123456", password_confirmation: "123456")
editor = Person.create(e-mail: "[email protected]", password: "123456", password_confirmation: "123456")
Person.create(e-mail: "[email protected]", password: "123456", password_confirmation: "123456")

admin_user_role = Function.create(identify: "Admin Person", reference: "Person", entry: :createable)
admin_post_role = Function.create(identify: "Admin Put up", reference: "Put up", entry: :createable)
editor_post_role = Function.create(identify: "Editor Put up", reference: "Put up", entry: :editable)

admin.user_roles.create(function: admin_user_role)
admin.user_roles.create(function: admin_post_role)
editor.user_roles.create(function: editor_post_role)
# fashions/function.rb
class Function < ApplicationRecord
  has_many :user_roles, dependent: :destroy

  enum entry: {
    viewable: 0,
    createable: 1,
    editable: 2,
    no_access: 3
  }
finish
# fashions/user_role.rb
class UserRole < ApplicationRecord
  belongs_to :person
  belongs_to :function
finish
# fashions/person.rb
class Person < ApplicationRecord
  # Embody default devise modules. Others accessible are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_many :user_roles, dependent: :destroy
  has_many :roles, via: :user_roles

  def can_edit?(useful resource)
    resource_class = useful resource.class.to_s == "Class" ? useful resource.identify : useful resource.class.to_s
    function = roles.the place(reference: resource_class)
    return false until function

    function.map(&:editable?).any? || function.map(&:createable?).any?
  finish

  def can_create?(useful resource)
    resource_class = useful resource.class.to_s == "Class" ? useful resource.identify : useful resource.class.to_s
    function = roles.the place(reference: resource_class)
    return false until function

    function.map(&:createable?).any?
  finish
finish
# Rails Console
person.can_edit?(put up)
person.can_create?(put up)
person.can_edit?(Put up)
person.can_create?(Put up)
# helpers/application_helper.rb
module ApplicationHelper

  def can_edit?(useful resource)
    return false until user_signed_in?

    current_user.can_edit?(useful resource)
  finish

  def can_create?(useful resource)
    return false until user_signed_in?

    current_user.can_create?(useful resource)
  finish
finish
# views/posts/index.html.erb
<%= link_to "Edit", edit_post_path(put up) if can_edit?(put up) %>
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments