Class: Droom::User

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#confirmObject

Returns the value of attribute confirm.



29
30
31
# File 'app/models/droom/user.rb', line 29

def confirm
  @confirm
end

#newly_activatedObject

Returns the value of attribute newly_activated.



29
30
31
# File 'app/models/droom/user.rb', line 29

def newly_activated
  @newly_activated
end

#remove_personObject

Returns the value of attribute remove_person.



29
30
31
# File 'app/models/droom/user.rb', line 29

def remove_person
  @remove_person
end

#update_person_emailObject

Returns the value of attribute update_person_email.



29
30
31
# File 'app/models/droom/user.rb', line 29

def update_person_email
  @update_person_email
end

Class Method Details

.currentObject

Current user is pushed into here to make it available in models such as the UserActionObserver that sets ownership before save.



104
105
106
# File 'app/models/droom/user.rb', line 104

def self.current
  Thread.current[:user]
end

.current=(user) ⇒ Object



107
108
109
# File 'app/models/droom/user.rb', line 107

def self.current=(user)
  Thread.current[:user] = user
end

.messaging_groupsObject

Messaging groups are normally scopes passed through when receives_messages is called, but anything will work that can be called on the class and return a set of instances. Here we’re overriding the getter so as to offer sending by group membership as well as the usual scoping.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/droom/user.rb', line 55

def self.messaging_groups
  unless @messaging_groups
    @messaging_groups = {
      :unconfirmed => lambda { Droom::User.unconfirmed},
      :personed => lambda { Droom::User.personed },
      :administrative => lambda { Droom::User.administrative }
    }
    Droom::Group.all.each do |group|
      @messaging_groups[group.slug.to_sym] = lambda { Droom::User.with_person_in_group(group.id) }
    end
  end
  @messaging_groups
end

Instance Method Details

#credentials(options = {}) ⇒ Object

Omniauth package

This is returned to the client application in the final stage of oauth authentication, and may be used to create a new local account.



219
220
221
222
223
224
225
226
227
228
229
# File 'app/models/droom/user.rb', line 219

def credentials(options={})
  {
    id: id,
    title: title,
    name: name,
    forename: forename,
    email: email,
    admin: admin?,
    image: thumbnail
  }
end

#dav_rootObject

Personal DAV repository is accessed via a DAV4rack endpoint but we have to take care of its creation and population.



113
114
115
116
117
# File 'app/models/droom/user.rb', line 113

def dav_root
  dav_path = Rails.root + "webdav/#{id}"
  Dir.mkdir(dav_path, 0600) unless File.exist?(dav_path)
  dav_path
end

#dropbox_clientObject



142
143
144
# File 'app/models/droom/user.rb', line 142

def dropbox_client
  dropbox_token.dropbox_client if dropbox_token
end

#dropbox_tokenObject



135
136
137
138
139
140
# File 'app/models/droom/user.rb', line 135

def dropbox_token
  unless @dropbox_token
    @dropbox_token = dropbox_tokens.by_date.last || 'nope'
  end
  @dropbox_token unless @dropbox_token == 'nope'
end

#for_emailObject

Email

If using ‘msg`, this defines the variables available in message templates.



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/models/droom/user.rb', line 200

def for_email
  generate_confirmation_token! unless confirmation_token?
  {
    :informal_name => informal_name,
    :formal_name => formal_name,
    :forename => forename,
    :name => name,
    :email => email,
    :confirmation_url => Droom::Engine.routes.url_helpers.welcome_url(:id => self.id, :confirmation_token => self.confirmation_token, :host => ActionMailer::Base.default_url_options[:host]),
    :sign_in_url => Droom::Engine.routes.url_helpers.new_user_session_path(:host => ActionMailer::Base.default_url_options[:host]),
    :password_reset_url => Droom::Engine.routes.url_helpers.edit_user_password_url(:reset_password_token => self.reset_password_token, :host => ActionMailer::Base.default_url_options[:host])
  }
end

#formal_nameObject



123
124
125
# File 'app/models/droom/user.rb', line 123

def formal_name
  [title, forename, name].compact.join(' ').strip
end

#full_nameObject



119
120
121
# File 'app/models/droom/user.rb', line 119

def full_name
  [forename, name].compact.join(' ').strip
end

#informal_nameObject



127
128
129
130
131
132
133
# File 'app/models/droom/user.rb', line 127

def informal_name
  if Droom.use_forenames
    forename
  else
    name
  end
end

#is_person?(person) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'app/models/droom/user.rb', line 93

def is_person?(person)
  person == self.person
end

#organisationObject



97
98
99
# File 'app/models/droom/user.rb', line 97

def organisation
  person.organisation if person
end

#password_match?Boolean

Returns:

  • (Boolean)


86
87
88
89
90
91
# File 'app/models/droom/user.rb', line 86

def password_match?
  self.errors[:password] << "can't be blank" if password.blank?
  self.errors[:password_confirmation] << "can't be blank" if password_confirmation.blank?
  self.errors[:password_confirmation] << "does not match password" if password != password_confirmation
  password == password_confirmation && !password.blank?
end

#password_required?Boolean

Password is not required on creation, contrary to the devise defaults.

Returns:

  • (Boolean)


82
83
84
# File 'app/models/droom/user.rb', line 82

def password_required?
  confirmed? && (!password.blank?)
end

#pref(key) ⇒ Object

Preferences

User settings are held as an association with Preference objects, which are simple key:value pairs. The keys are usually colon:separated for namespacing purposes, eg:

current_user.pref("email:enabled?")
current_user.pref("dropbox:enabled?")

Default settings are defined in Droom.user_defaults and can be defined in an initializer if the default droom defaults are not right for your application.

‘User#pref(key)` returns the value of the preference (whether set or default) for the given key. It is intended for use in views:

- if current_user.pref("dropbox:enabled?")
  = link_to "copy to dropbox", dropbox_folder_url(folder)


168
169
170
171
172
173
174
# File 'app/models/droom/user.rb', line 168

def pref(key)
  if pref = preferences.find_by_key(key)
    pref.value
  else
    Droom.user_default(key)
  end
end

#preference(key) ⇒ Object

‘User#preference(key)` always returns a preference object and is used to build control panels. If no preference is saved for the given key, we return a new (unsaved) one with that key and the default value.



179
180
181
182
183
# File 'app/models/droom/user.rb', line 179

def preference(key)
  pref = preferences.find_or_initialize_by_key(key)
  pref.value = Droom.user_default(key) unless pref.persisted?
  pref
end

#privileged?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'app/models/droom/user.rb', line 146

def privileged?
  admin? || person && person.privileged?
end

#set_pref(key, value) ⇒ Object

Setting preferences is normally handled either by the PreferencesController or by nesting preferences in a user form. ‘User#set_pref` is a convenient console method but not otherwise used much.

Preferences are set in a simple key:value way, where key usually includes some namespacing prefixes:

user.set_pref("email:enabled", true)


192
193
194
# File 'app/models/droom/user.rb', line 192

def set_pref(key, value)
  preferences.find_or_create_by_key(key).set(value)
end

#thumbnailObject



231
232
233
# File 'app/models/droom/user.rb', line 231

def thumbnail
  person.image.url(:icon) if person
end