Class: Contact
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Contact
- Includes:
- E9Rails::ActiveRecord::AttributeSearchable, E9Rails::ActiveRecord::Initialization, E9Tags::Model
- Defined in:
- app/models/contact.rb
Defined Under Namespace
Constant Summary collapse
- RECORD_ATTRIBUTES =
%w[users phone_number_attributes instant_messaging_handle_attributes website_attributes address_attributes]
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
- #build_all_record_attributes ⇒ Object
-
#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_contact) ⇒ Object
-
#name ⇒ Object
Helper to concatenate a Contact’s full name.
- #page_views ⇒ Object
- #primary_user ⇒ Object
- #thumb(options = {}) ⇒ Object
- #to_liquid ⇒ Object
- #valid?(context = nil) ⇒ Boolean
Class Method Details
.available_to_deal(deal) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'app/models/contact.rb', line 175 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
196 197 198 |
# File 'app/models/contact.rb', line 196 def self.users_build_parameters # :nodoc: { :status => User::Status::Prospect } end |
Instance Method Details
#build_all_record_attributes ⇒ Object
106 107 108 109 110 111 112 |
# File 'app/models/contact.rb', line 106 def build_all_record_attributes RECORD_ATTRIBUTES.each do |attr| params_method = "#{attr}_build_parameters" build_params = self.class.send(params_method) if self.class.respond_to?(params_method) send(attr).send(:build, build_params || {}) end end |
#company ⇒ Object
Associations
16 |
# File 'app/models/contact.rb', line 16 belongs_to :company |
#company_name=(value) ⇒ Object
Setting company name will attempt to find a Company or create a new one
203 204 205 206 207 208 209 210 211 212 213 |
# File 'app/models/contact.rb', line 203 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
117 |
# File 'app/models/contact.rb', line 117 validates :first_name, :presence => true, :length => { :maximum => 25 } |
#merge_and_destroy!(other_contact) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 |
# File 'app/models/contact.rb', line 223 def merge_and_destroy!(other_contact) other_contact.users.clear_primary! self.users |= other_contact.users self.website_attributes |= other_contact.website_attributes self.address_attributes |= other_contact.address_attributes self.phone_number_attributes |= other_contact.phone_number_attributes self.instant_messaging_handle_attributes |= other_contact.instant_messaging_handle_attributes other_contact.destroy end |
#name ⇒ Object
Helper to concatenate a Contact’s full name
219 220 221 |
# File 'app/models/contact.rb', line 219 def name [first_name, last_name].join(' ').to_s.strip end |
#page_views ⇒ Object
93 94 95 |
# File 'app/models/contact.rb', line 93 def page_views PageView.by_user(users) end |
#primary_user ⇒ Object
87 88 89 |
# File 'app/models/contact.rb', line 87 def primary_user users.primary.first end |
#thumb(options = {}) ⇒ Object
193 |
# File 'app/models/contact.rb', line 193 def thumb( = {}); self.avatar end |
#to_liquid ⇒ Object
297 298 299 |
# File 'app/models/contact.rb', line 297 def to_liquid Drop.new(self) end |
#valid?(context = nil) ⇒ Boolean
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 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 |
# File 'app/models/contact.rb', line 235 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 |