organization.rb
5.24 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Represents any organization of the system
class Organization < Profile
attr_accessible :moderated_articles, :foundation_year, :contact_person, :acronym, :legal_form, :economic_activity, :management_information, :cnpj, :display_name, :enable_contact_us
SEARCH_FILTERS = {
:order => %w[more_recent more_popular more_active],
:display => %w[compact]
}
settings_items :closed, :type => :boolean, :default => false
def closed?
closed
end
before_save do |organization|
organization.closed = true if !organization.public_profile?
end
settings_items :moderated_articles, :type => :boolean, :default => false
def moderated_articles?
moderated_articles
end
has_one :validation_info
has_many :validations, :class_name => 'CreateEnterprise', :foreign_key => :target_id
has_many :mailings, :class_name => 'OrganizationMailing', :foreign_key => :source_id, :as => 'source'
has_many :custom_roles, :class_name => 'Role', :foreign_key => :profile_id
scope :more_popular, :order => 'members_count DESC'
validate :presence_of_required_fieds, :unless => :is_template
def self.notify_activity tracked_action
Delayed::Job.enqueue NotifyActivityToProfilesJob.new(tracked_action.id)
end
def presence_of_required_fieds
self.required_fields.each do |field|
if self.send(field).blank?
self.errors.add_on_blank(field)
end
end
end
def validation_methodology
self.validation_info ? self.validation_info.validation_methodology : nil
end
def validation_restrictions
self.validation_info ? self.validation_info.restrictions : nil
end
def pending_validations
validations.pending
end
def find_pending_validation(code)
validations.pending.find(:first, :conditions => {:code => code})
end
def processed_validations
validations.finished
end
def find_processed_validation(code)
validations.finished.find(:first, :conditions => {:code => code})
end
def is_validation_entity?
!self.validation_info.nil?
end
FIELDS = %w[
display_name
description
contact_person
contact_email
contact_phone
legal_form
economic_activity
management_information
address
zip_code
city
state
country
tag_list
template_id
district
address_reference
]
def self.fields
FIELDS
end
def required_fields
[]
end
def active_fields
[]
end
def signup_fields
[]
end
N_('Display name'); N_('Description'); N_('Contact person'); N_('Contact email'); N_('Acronym'); N_('Foundation year'); N_('Legal form'); N_('Economic activity'); N_('Management information'); N_('Tag list'); N_('District'); N_('Address reference')
settings_items :display_name, :description, :contact_person, :contact_email, :acronym, :foundation_year, :legal_form, :economic_activity, :management_information, :district, :address_reference
settings_items :zip_code, :city, :state, :country
validates_format_of :foundation_year, :with => Noosfero::Constants::INTEGER_FORMAT
validates_format_of :contact_email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda { |org| !org.contact_email.blank? })
validates_as_cnpj :cnpj
xss_terminate :only => [ :acronym, :contact_person, :contact_email, :legal_form, :economic_activity, :management_information ], :on => 'validation'
# Yes, organizations have members.
#
# Returns <tt>true</tt>.
def has_members?
true
end
def default_set_of_blocks
links = [
{:name => _("Community's profile"), :address => '/profile/{profile}', :icon => 'ok'},
{:name => _('Invite Friends'), :address => '/profile/{profile}/invite/friends', :icon => 'send'},
{:name => _('Agenda'), :address => '/profile/{profile}/events', :icon => 'event'},
{:name => _('Image gallery'), :address => '/{profile}/gallery', :icon => 'photos'},
{:name => _('Blog'), :address => '/{profile}/blog', :icon => 'edit'},
]
[
[MainBlock.new],
[ProfileImageBlock.new, LinkListBlock.new(:links => links)],
[RecentDocumentsBlock.new]
]
end
def default_set_of_articles
[
Blog.new(:name => _('Blog')),
Gallery.new(:name => _('Gallery')),
]
end
def short_name chars = 40
s = self.display_name
s = super(chars) if s.blank?
s
end
def notification_emails
emails = [contact_email].select(&:present?) + admins.map(&:email)
if emails.empty?
emails << environment.contact_email
end
emails
end
def already_request_membership?(person)
self.tasks.pending.find_by_requestor_id(person.id, :conditions => { :type => 'AddMember' })
end
def jid(options = {})
super({:domain => "conference.#{environment.default_hostname}"}.merge(options))
end
def receives_scrap_notification?
false
end
def members_to_json
members.map { |member| {:id => member.id, :name => member.name} }.to_json
end
def members_by_role_to_json(role)
members_by_role(role).map { |member| {:id => member.id, :name => member.name} }.to_json
end
def disable
self.visible = false
save!
end
def allow_invitation_from?(person)
(followed_by?(person) && self.allow_members_to_invite) || person.has_permission?('invite-members', self)
end
def is_admin?(user)
self.admins.where(:id => user.id).exists?
end
end