Class: Reader

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
RFC822
Defined in:
app/models/reader.rb

Constant Summary collapse

@@user_columns =
%w{name email created_at password password_confirmation notes}

Constants included from RFC822

RFC822::RFC822_valid

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#email_fieldObject

Returns the value of attribute email_field.



11
12
13
# File 'app/models/reader.rb', line 11

def email_field
  @email_field
end

#newly_activatedObject

Returns the value of attribute newly_activated.



11
12
13
# File 'app/models/reader.rb', line 11

def newly_activated
  @newly_activated
end

#skip_user_updateObject

Returns the value of attribute skip_user_update.



11
12
13
# File 'app/models/reader.rb', line 11

def skip_user_update
  @skip_user_update
end

Class Method Details

.csv_for(readers = []) ⇒ Object

Generates a csv file listing the supplied group of readers. No access checks are performed here.



248
249
250
251
252
253
254
# File 'app/models/reader.rb', line 248

def self.csv_for(readers=[])
  columns = %w{forename surname email phone mobile postal_address}
  FasterCSV.generate do |csv|
    csv << columns.map { |f| I18n.t("activerecord.attributes.reader.#{f}") }
    readers.each { |r| csv << columns.map{ |f| r.send(f.to_sym) } }
  end
end

.find_by_nickname_or_email(nickname_or_email) ⇒ Object



70
71
72
# File 'app/models/reader.rb', line 70

def self.find_by_nickname_or_email(nickname_or_email)
  reader = find(:first, :conditions => ["nickname = ? OR email = ?", nickname_or_email, nickname_or_email])
end

.for_user(user) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/reader.rb', line 74

def self.for_user(user)
  if user.respond_to?(:site) && site = Page.current_site
    reader = self.find_or_create_by_site_id_and_user_id(site.id, user.id)
  else
    reader = self.find_or_create_by_user_id(user.id)
  end
  if reader.new_record?
    user_columns.each { |att| reader.send("#{att.to_s}=", user.send(att)) }
    reader.crypted_password = user.password
    reader.password_salt = user.salt
    reader.activated_at = reader.created_at
    reader.save(false)
  end
  reader
end

.vcards_for(readers = []) ⇒ Object

Generates a vcard file containing the supplied group of readers. No access checks are performed here.



259
260
261
# File 'app/models/reader.rb', line 259

def self.vcards_for(readers=[])
  readers.map(&:vcard).join("\n")
end

.visible_to(reader = nil) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/reader.rb', line 90

def self.visible_to(reader=nil)
  case Radiant.config['reader.directory_visibility']
  when 'public'
    self.all
  when 'private'
    reader ? self.all : self.none
  when 'grouped'
    reader ? self.in_groups(reader.group_ids) : self.none
  else
    self.none
  end
end

Instance Method Details

#activate!Object



176
177
178
179
180
181
182
# File 'app/models/reader.rb', line 176

def activate!
  self.activated_at = Time.now.utc
  self.newly_activated = true
  self.save!
  send_welcome_message
  send_group_welcomes
end

#activated?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'app/models/reader.rb', line 184

def activated?
  !inactive?
end

#can_see?(this) ⇒ Boolean

Returns:

  • (Boolean)


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

def can_see? (this)
  this.groups.empty? or in_any_of_these_groups?(this.groups)
end

#create_password!Object



225
226
227
228
229
# File 'app/models/reader.rb', line 225

def create_password!
  self.clear_password = self.password_confirmation = self.randomize_password # randomize_password is provided by authlogic
  self.save! unless self.new_record?
  self.clear_password
end

#disable_perishable_token_maintenance?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'app/models/reader.rb', line 196

def disable_perishable_token_maintenance?
  inactive? && !new_record?
end

#filenameObject



172
173
174
# File 'app/models/reader.rb', line 172

def filename
  name.downcase.gsub(/\W/, '_')
end

#find_homepageObject



231
232
233
234
235
# File 'app/models/reader.rb', line 231

def find_homepage
  if homegroup = groups.with_home_page.first
    homegroup.homepage
  end
end

#generate_email_field_nameObject



213
214
215
# File 'app/models/reader.rb', line 213

def generate_email_field_name
  self.email_field = Authlogic::Random.friendly_token
end

#has_group?(group) ⇒ Boolean Also known as: is_in?

Returns:

  • (Boolean)


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

def has_group? (group)
  !!membership_of(group)
end

#home_urlObject



237
238
239
240
241
242
243
# File 'app/models/reader.rb', line 237

def home_url
  if homepage = self.find_homepage
    homepage.url
  else
    nil
  end
end

#in_any_of_these_groups?(grouplist) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/reader.rb', line 111

def in_any_of_these_groups? (grouplist)
  (grouplist & groups).any?
end

#inactive?Boolean

Returns:

  • (Boolean)


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

def inactive?
  self.activated_at.nil?
end

#is_admin?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'app/models/reader.rb', line 221

def is_admin?
  is_user? && self.user.admin?
end

#is_grouped?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/reader.rb', line 124

def is_grouped?
  groups.any?
end

#is_user?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'app/models/reader.rb', line 217

def is_user?
  !!self.user
end

#membership_of(group) ⇒ Object



115
116
117
# File 'app/models/reader.rb', line 115

def membership_of(group)
  memberships.of(group).first
end

#newly_activated?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'app/models/reader.rb', line 188

def newly_activated?
  !!newly_activated
end

#postal_addressObject



140
141
142
143
144
145
146
147
148
149
# File 'app/models/reader.rb', line 140

def postal_address
  Snail.new(
    :line_1 => post_line1,
    :line_2 => post_line2,
    :city => post_city,
    :region => post_province,
    :postal_code => postcode,
    :country => post_country
  )
end

#postal_address?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/models/reader.rb', line 136

def postal_address?
  !post_line1.blank? && !post_city.blank?
end

#preferred_informal_nameObject



132
133
134
# File 'app/models/reader.rb', line 132

def preferred_informal_name
  nickname? ? nickname : forename
end

#preferred_nameObject



128
129
130
# File 'app/models/reader.rb', line 128

def preferred_name
  nickname? ? nickname : name
end

#send_functional_message(function, group = nil) ⇒ Object

Raises:

  • (StandardError)


206
207
208
209
210
211
# File 'app/models/reader.rb', line 206

def send_functional_message(function, group=nil)
  reset_perishable_token!
  message = Message.functional(function, group)   # returns the standard functional message if no group is supplied, or no group message exists
  raise StandardError, "No #{function} message could be found" unless message
  message.deliver_to(self)
end

#send_group_invitation_message(group = nil) ⇒ Object



263
264
265
# File 'app/models/reader.rb', line 263

def send_group_invitation_message(group=nil)
  send_functional_message('group_invitation', group)
end

#vcardObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/models/reader.rb', line 151

def vcard
	@vcard ||= Vpim::Vcard::Maker.make2 do |maker|
		maker.add_name do |n|
		  n.prefix = honorific || ""
		  n.given = forename || ""
		  n.family = surname || ""
  end
		maker.add_addr {|a| 
		  a.location = 'home' # until we do this properly with multiple contact sets
      a.country = post_country || ""
      a.region = post_province || ""
      a.locality = post_city || ""
      a.street = [post_line1, post_line2].compact.join("\n")
      a.postalcode = postcode || ""
		}
		maker.add_tel phone { |t| t.location = 'home' } unless phone.blank?
		maker.add_tel mobile { |t| t.location = 'cell' } unless mobile.blank?
		maker.add_email email { |e| t.location = 'home' }
	end
end

#visible_to?(reader = nil) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/reader.rb', line 103

def visible_to?(reader=nil)
  (reader && (reader == self)) || self.class.visible_to(reader).map(&:id).include?(self.id)
end