Module: Humanity::Base
- Defined in:
- lib/humanity/base.rb
Class Method Summary collapse
Instance Method Summary collapse
- #admin? ⇒ Boolean
- #developer? ⇒ Boolean
- #has_role?(role) ⇒ Boolean
- #name ⇒ Object
- #role_symbols ⇒ Object
- #to_s ⇒ Object
- #update_login_info! ⇒ Object
- #update_roles!(role_names, source) ⇒ Object
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/humanity/base.rb', line 3 def self.included(base) base.has_many :assignments, as: :human, dependent: :destroy, class_name: 'Humanity::Assignment' base.has_many :roles, through: :assignments, class_name: 'Humanity::Role' base.validates_presence_of :username base.validates_uniqueness_of :username end |
Instance Method Details
#admin? ⇒ Boolean
23 24 25 |
# File 'lib/humanity/base.rb', line 23 def admin? has_role? :admin end |
#developer? ⇒ Boolean
27 28 29 |
# File 'lib/humanity/base.rb', line 27 def developer? has_role? :developer end |
#has_role?(role) ⇒ Boolean
19 20 21 |
# File 'lib/humanity/base.rb', line 19 def has_role?(role) roles.where(name: role.to_s).first.present? end |
#name ⇒ Object
11 12 13 |
# File 'lib/humanity/base.rb', line 11 def name "#{first_name} #{last_name}".strip end |
#role_symbols ⇒ Object
15 16 17 |
# File 'lib/humanity/base.rb', line 15 def role_symbols roles.map(&:to_sym) end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/humanity/base.rb', line 58 def to_s name end |
#update_login_info! ⇒ Object
51 52 53 54 55 56 |
# File 'lib/humanity/base.rb', line 51 def update_login_info! self.last_login_at = current_login_at self.current_login_at = Time.now self.login_count = login_count.to_i + 1 self.save end |
#update_roles!(role_names, source) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/humanity/base.rb', line 31 def update_roles!(role_names, source) prev_roles = self.roles new_roles = role_names.map { |n| Role.find_or_create_by_name(n) } # Create new roles and/or update source new_roles.each do |role| assignment = Assignment.find_or_initialize_by_human_id_and_human_type_and_role_id(self.id, self.class.to_s, role.id) assignment.source = source assignments << assignment if assignment.changed? end # Remove roles we had but don't anymore old_roles = (prev_roles - new_roles) assignments.each do |a| a.destroy if old_roles.include?(a.role) && a.source == source end roles.reload end |