Skip to content

Instantly share code, notes, and snippets.

@slim1979
Created May 22, 2020 08:33
Show Gist options
  • Select an option

  • Save slim1979/6b029b10cad640aea843dc5dd08564d2 to your computer and use it in GitHub Desktop.

Select an option

Save slim1979/6b029b10cad640aea843dc5dd08564d2 to your computer and use it in GitHub Desktop.
expected structure
class User
enum tariff: %i[free standart unlimited]
end
class Album < ApplicationRecord
extend tariff
def self.tariff
user.tariff.camelize.constantize
end
module Free
extend News
extend Blog
extend SomeThingFree
end
module Standard
# standard modules
end
module Unlimited
# unlimited modules
end
end
# models, controllers
class AlbumsController < ApplicationController
extend current_user.tariff.camelize.constantize
end
# concern
class AlbumsController
module Free
extend News
extend Blog
extend SomeThingFree
end
module Standard
# standard modules
end
module Unlimited
# unlimited modules
end
end
class Ability
include CanCan::Ability
def case_user_tariff
send @user.tariff + '_abilities'
rescue NoMethodError => e
puts e
end
def free_abilities
end
def standard_abilities
end
def custom_abilities
@user.custom_tariff.keys do |k|
can :manage, k.to_s.camelize.constantize, @user.custom_tariff[k] == true
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment