Class: Contact
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Contact
- Includes:
- E9::ActiveRecord::AttributeSearchable, E9Rails::ActiveRecord::Initialization, E9Tags::Model
- Defined in:
- app/models/contact.rb
Defined Under Namespace
Class Method Summary collapse
- .available_to_deal(deal) ⇒ Object
-
.users_build_parameters ⇒ Object
The parameters for building the JS template for associated users.
Instance Method Summary collapse
-
#company ⇒ Object
Associations.
-
#company_name=(value) ⇒ Object
Setting company name will attempt to find a Company or create a new one.
-
#first_name ⇒ Object
Validations.
-
#merge_and_destroy!(other) ⇒ Object
NOTE there’s no non-destructive ‘merge`.
- #merge_destructive_and_destroy!(other) ⇒ Object
- #merge_tags(other) ⇒ Object
-
#name ⇒ Object
Helper to concatenate a Contact’s full name.
- #name_with_email ⇒ Object
- #page_views ⇒ Object
- #primary_user ⇒ Object
- #thumb(options = {}) ⇒ Object
- #to_liquid ⇒ Object
- #valid?(context = nil) ⇒ Boolean
Class Method Details
.available_to_deal(deal) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'app/models/contact.rb', line 170 def self.available_to_deal(deal) return all unless deal.persisted? sql = <<-SQL SELECT distinct contacts.* FROM `contacts` LEFT OUTER JOIN `contacts_deals` ON `contacts_deals`.`contact_id` = `contacts`.`id` WHERE (`contacts_deals`.`deal_id` IS NULL OR `contacts_deals`.`deal_id` != #{deal.id}) SQL find_by_sql(sql) end |
.users_build_parameters ⇒ Object
The parameters for building the JS template for associated users
191 192 193 |
# File 'app/models/contact.rb', line 191 def self.users_build_parameters # :nodoc: { :role => :prospect } end |
Instance Method Details
#company ⇒ Object
Associations
17 |
# File 'app/models/contact.rb', line 17 belongs_to :company |
#company_name=(value) ⇒ Object
Setting company name will attempt to find a Company or create a new one
198 199 200 201 202 203 204 205 206 207 208 |
# File 'app/models/contact.rb', line 198 def company_name=(value) if value.present? if existing_company = Company.find_by_name(value) self.company = existing_company else self.build_company(:name => value) end else self.company = nil end end |
#first_name ⇒ Object
Validations
111 |
# File 'app/models/contact.rb', line 111 validates :first_name, :presence => true, :length => { :maximum => 25 } |
#merge_and_destroy!(other) ⇒ Object
NOTE there’s no non-destructive ‘merge`
225 226 227 228 229 230 231 232 233 234 235 |
# File 'app/models/contact.rb', line 225 def merge_and_destroy!(other) (other) self.info = "#{self.info}\n\n#{other.info}" if success = save merge_destructive_and_destroy!(other) end success end |
#merge_destructive_and_destroy!(other) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'app/models/contact.rb', line 243 def merge_destructive_and_destroy!(other) other.users.clear_primary! self.owned_deals |= other.owned_deals self.associated_deals |= other.associated_deals self.campaigns_as_salesperson |= other.campaigns_as_salesperson self.campaigns_as_affiliate |= other.campaigns_as_affiliate self.users |= other.users self.website_attributes |= other.website_attributes self.address_attributes |= other.address_attributes self.phone_number_attributes |= other.phone_number_attributes self.instant_messaging_handle_attributes |= other.instant_messaging_handle_attributes other.associated_deals.clear other.reload other.destroy end |
#merge_tags(other) ⇒ Object
237 238 239 240 241 |
# File 'app/models/contact.rb', line 237 def (other) = self.tag_list_on('users__h__') = other.tag_list_on('users__h__') set_tag_list_on('users__h__', | ) end |
#name ⇒ Object
Helper to concatenate a Contact’s full name
220 221 222 |
# File 'app/models/contact.rb', line 220 def name [first_name, last_name].join(' ').to_s.strip end |
#name_with_email ⇒ Object
211 212 213 214 215 |
# File 'app/models/contact.rb', line 211 def name_with_email retv = name.dup retv << " (#{email})" if email.present? retv end |
#page_views ⇒ Object
24 25 26 |
# File 'app/models/contact.rb', line 24 def page_views PageView.by_users(user_ids) end |
#primary_user ⇒ Object
95 96 97 |
# File 'app/models/contact.rb', line 95 def primary_user users.primary.first end |
#thumb(options = {}) ⇒ Object
188 |
# File 'app/models/contact.rb', line 188 def thumb( = {}); self.avatar end |
#to_liquid ⇒ Object
322 323 324 |
# File 'app/models/contact.rb', line 322 def to_liquid Drop.new(self) end |
#valid?(context = nil) ⇒ Boolean
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'app/models/contact.rb', line 260 def valid?(context = nil) # # NOTE #valid? manages duplicate users and is a destructive process! # TODO move the logic that deletes/manages duplicate email'd users out of # #valid?, which probably should not be destructive # # When checking for validity, we're also checking to see if Users are being added # which have emails that already exist in the database. If one is found, one of # two things will happen, depending on whether or not that User already has a # Contact record. # # A.) If it does, the validation will return false immediately and add an error # suggesting a Contact merge. # # B.) If it does not, no error will be added, but the offending "user" association # will be deleted and the Contact will be related to the pre-existing User with # that email. # # If more than one user associations are passed with the same email, it will be treated # as a normal uniqueness error, until all emails passed are unique. At which time we # go back to the A/B scenario above. # super || begin unless errors.delete(:"users.email").blank? users.dup.each_with_index do |user, i| user.errors[:email].each do |error| if error.taken? && users.select {|u| u.email == user.email }.length == 1 existing_user = User.find_by_email(user.email) if contact = existing_user.contact args = if new_record? [contact, 'new', {:contact => self.attributes}] else [contact, self] end errors.add(:users, :merge_required, { :email => user.email, :merge_path => new_contact_merge_path(*args) }) return false else self.users.delete(user) self.users << existing_user end else if error.label errors.add(:users, error.label.to_sym, :email => user.email) else errors.add(:users, nil, :message => error, :email => user.email) end end end end errors[:users].uniq! errors[:users].empty? end end end |