Class: Atreides::User

Inherits:
Base
  • Object
show all
Includes:
Extendable
Defined in:
app/models/atreides/user.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#dom_id, #to_param

Class Method Details

.available_roles(user) ⇒ Object



73
74
75
76
# File 'app/models/atreides/user.rb', line 73

def available_roles(user)
  idx = roles.find_index(user.role.to_sym)
  idx == nil ? [] : roles[idx..-1]
end

.build_admin(props) ⇒ Object



60
61
62
# File 'app/models/atreides/user.rb', line 60

def build_admin props
  self.new (props || {}).merge :role => :admin
end

.can_destroy_admin?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/atreides/user.rb', line 64

def can_destroy_admin?
  admins.count > 1
end

.rolesObject

Sort by descending order of ability



69
70
71
# File 'app/models/atreides/user.rb', line 69

def roles
  [:admin, :editor, :writer]
end

Instance Method Details

#add_to_cart(opts = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/atreides/user.rb', line 103

def add_to_cart(opts = {})
  product = opts[:product]
  size = opts[:size].to_s
  qty = opts[:qty].to_i
  return if !product or !product.is_a?(Product)

  # Look for exisiting item
  item = cart_items.detect{|i|i.product==product and i.product.size_for_name(size) }

  # Create if not found
  item = cart_items.build(:product => product, :size => size) if item.nil?

  # Update qty
  item.qty += qty

  item.save
  item
end

#admin?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/models/atreides/user.rb', line 88

def admin?
  role && role.to_sym == :admin
end

#destroyable?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'app/models/atreides/user.rb', line 84

def destroyable?
  !admin? || self.class.admins.count > 1
end

#full_nameObject

Instance Methods



99
100
101
# File 'app/models/atreides/user.rb', line 99

def full_name
  [first_name, last_name].join(' ')
end

#prevent_destroying_last_adminObject



79
80
81
82
# File 'app/models/atreides/user.rb', line 79

def prevent_destroying_last_admin
  errors.add(:base, "not allowed destroying the last administrator") unless destroyable?
  destroyable?
end

#roleObject



92
93
94
# File 'app/models/atreides/user.rb', line 92

def role
  @role ||= (self.attributes['role'] && self.attributes['role'].to_sym)
end