-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorganization.rb
68 lines (58 loc) · 1.67 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true
# typed: true
# == Schema Information
#
# Table name: organizations
#
# id :integer not null, primary key
# icon_path :string
# name :string not null
# created_at :datetime not null
# updated_at :datetime not null
# sway_locale_id :integer not null
#
# Indexes
#
# index_organizations_on_name_and_sway_locale_id (name,sway_locale_id) UNIQUE
# index_organizations_on_sway_locale_id (sway_locale_id)
#
# Foreign Keys
#
# sway_locale_id (sway_locale_id => sway_locales.id)
#
class Organization < ApplicationRecord
extend T::Sig
include SwayGoogleCloudStorage
belongs_to :sway_locale
has_many :organization_bill_positions, inverse_of: :organization, dependent: :destroy
has_many :bills, through: :organization_bill_positions
validates :name, uniqueness: {scope: :sway_locale_id, allow_nil: true}
def positions
organization_bill_positions
end
sig { params(current_icon_path: T.nilable(String)).void }
def remove_icon(current_icon_path)
return if current_icon_path.blank?
return unless icon_path != current_icon_path
delete_file(bucket_name: SwayGoogleCloudStorage::BUCKETS[:ASSETS], file_name: current_icon_path)
end
sig { returns(Jbuilder) }
def to_simple_builder
Jbuilder.new do |o|
o.id id
o.sway_locale_id sway_locale_id
o.name name
o.icon_path icon_path
end
end
sig { returns(Jbuilder) }
def to_builder
Jbuilder.new do |o|
o.id id
o.sway_locale_id sway_locale_id
o.name name
o.icon_path icon_path
o.positions positions.map(&:to_sway_json)
end
end
end