-
Notifications
You must be signed in to change notification settings - Fork 342
/
organization.rb
48 lines (34 loc) · 1.34 KB
/
organization.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Organization < ApplicationRecord
default_scope { order('id DESC') }
has_many :locations, dependent: :destroy
has_many :programs, dependent: :destroy
has_many :contacts, dependent: :destroy
has_many :phones, dependent: :destroy, inverse_of: :organization
accepts_nested_attributes_for :phones,
allow_destroy: true, reject_if: :all_blank
validates :name,
presence: { message: I18n.t('errors.messages.blank_for_org') },
uniqueness: { case_sensitive: false }
validates :description,
presence: { message: I18n.t('errors.messages.blank_for_org') }
validates :email, email: true, allow_blank: true
validates :website, url: true, allow_blank: true
validates :date_incorporated, date: true
validates :accreditations, :funding_sources, :licenses, pg_array: true
auto_strip_attributes :alternate_name, :description, :email, :legal_status,
:name, :tax_id, :tax_status, :website
def self.with_locations(ids)
joins(:locations).where(locations: { id: ids }).distinct
end
extend FriendlyId
friendly_id :name, use: [:history]
after_save :touch_locations, if: :needs_touch?
private
def needs_touch?
return false if locations.count.zero?
saved_change_to_name?
end
def touch_locations
locations.find_each(&:touch)
end
end