Class: User

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/user.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loginObject

Setup accessible (or protected) attributes for your model :login is a virtual attribute for authenticating by either username or email This is in addition to a real persisted field like ‘username’



15
16
17
# File 'app/models/user.rb', line 15

def 
  @login
end

Class Method Details

.find_for_database_authentication(conditions) ⇒ Object



23
24
25
26
# File 'app/models/user.rb', line 23

def find_for_database_authentication(conditions)
  value = conditions[authentication_keys.first]
  where(["username = :value OR email = :value", { :value => value }]).first
end

Instance Method Details

#add_role(title) ⇒ Object

Raises:

  • (ArgumentException)


50
51
52
53
# File 'app/models/user.rb', line 50

def add_role(title)
  raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(Role)
  roles << Role[title] unless has_role?(title)
end

#authorized_pluginsObject



39
40
41
# File 'app/models/user.rb', line 39

def authorized_plugins
  plugins.collect { |p| p.name } | Refinery::Plugins.always_allowed.names
end

#can_delete?(user_to_delete = self) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
# File 'app/models/user.rb', line 43

def can_delete?(user_to_delete = self)
  user_to_delete.persisted? and
  !user_to_delete.has_role?(:superuser) and
  Role[:refinery].users.count > 1 and
  id != user_to_delete.id
end

#has_role?(title) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentException)


55
56
57
58
# File 'app/models/user.rb', line 55

def has_role?(title)
  raise ArgumentException, "Role should be the title of the role not a role object." if title.is_a?(Role)
  roles.any?{|r| r.title == title.to_s.camelize}
end

#plugins=(plugin_names) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'app/models/user.rb', line 29

def plugins=(plugin_names)
  if persisted? # don't add plugins when the user_id is nil.
    UserPlugin.delete_all(:user_id => id)

    plugin_names.each_with_index do |plugin_name, index|
      plugins.create(:name => plugin_name, :position => index) if plugin_name.is_a?(String)
    end
  end
end