cirandas.net

ref: master

app/models/enterprise.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
 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
class Enterprise < Organization

  attr_accessible :business_name, :address_reference, :district,
    :organization_website, :historic_and_current_context,
    :activities_short_description

  SEARCH_FILTERS = {
    :order => %w[more_recent more_popular more_active],
    :display => %w[compact full map]
  }

  def self.type_name
    _('Enterprise')
  end

  N_('enterprise')

  acts_as_trackable after_add: proc{ |p, t| notify_activity t }

  has_many :favorite_enterprise_people
  has_many :fans, source: :person, through: :favorite_enterprise_people

  N_('Organization website'); N_('Historic and current context'); N_('Activities short description'); N_('City'); N_('State'); N_('Country'); N_('ZIP code')

  settings_items :organization_website, :historic_and_current_context, :activities_short_description

  extend SetProfileRegionFromCityState::ClassMethods
  set_profile_region_from_city_state

  before_save do |enterprise|
    enterprise.organization_website = enterprise.maybe_add_http(enterprise.organization_website)
  end
  include MaybeAddHttp

  def business_name
    self.nickname
  end
  def business_name=(value)
    self.nickname = value
  end
  N_('Business name')

  FIELDS = %w[
    business_name
    organization_website
    historic_and_current_context
    activities_short_description
    acronym
    foundation_year
  ]

  def self.fields
    super + FIELDS
  end

  def active_fields
    environment ? environment.active_enterprise_fields : []
  end

  def required_fields
    environment ? environment.required_enterprise_fields : []
  end

  def signup_fields
    environment ? environment.signup_enterprise_fields : []
  end

  def closed?
    true
  end

  def blocked?
    data[:blocked]
  end

  def block
    data[:blocked] = true
    save
  end

  def unblock
    data[:blocked] = false
    save
  end

  def activation_task
    self.tasks.where(:type => 'EnterpriseActivation').first
  end

  def enable(owner = nil)
    return if self.enabled
    # must be set first for the following to work
    self.enabled = true

    self.affiliate owner, Profile::Roles.all_roles(self.environment.id) if owner
    if self.environment.replace_enterprise_template_when_enable
      self.articles.destroy_all
      self.apply_template template
      self.home_page = self.articles(true).where(type: 'EnterpriseHomepage').first
    end
    self.activation_task.update_attribute :status, Task::Status::FINISHED rescue nil
    self.save(:validate => false)
  end

  def question
    if !self.foundation_year.blank?
      :foundation_year
    elsif !self.cnpj.blank?
      :cnpj
    else
      nil
    end
  end

  # Use to create an enterprise manually (via console) that is not enabled
  def create_activation_task
    return if self.enabled
    EnterpriseActivation.create! :enterprise => self, :code_length => 7
  end
  def activation_task
    self.tasks.where(:type => 'EnterpriseActivation').first
  end

  def default_set_of_blocks
    links = [
      {:name => _("Enterprises's profile"), :address => '/profile/{profile}', :icon => 'ok'},
      {:name => _('Blog'), :address => '/{profile}/blog', :icon => 'edit'},
    ]
    blocks = [
      [MainBlock.new],
      [ ProfileImageBlock.new,
        LinkListBlock.new(:links => links),
      ],
      [LocationBlock.new]
    ]
    blocks
  end

  def default_set_of_articles
    [
      Blog.new(:name => _('Blog')),
    ]
  end

  before_create do |enterprise|
    enterprise.validated = enterprise.environment.enabled?('enterprises_are_validated_when_created')
    if enterprise.environment.enabled?('enterprises_are_disabled_when_created')
      enterprise.enabled = false
    end
    true
  end

  def default_template
    environment.enterprise_default_template
  end

  def template_with_inactive_enterprise
    !enabled? ? environment.inactive_enterprise_template : template_without_inactive_enterprise
  end
  alias_method_chain :template, :inactive_enterprise

  def control_panel_settings_button
    {:title => _('Enterprise Info and settings'), :icon => 'edit-profile-enterprise'}
  end

  settings_items :enable_contact_us, :type => :boolean, :default => true

  def enable_contact?
    enable_contact_us
  end

  def control_panel_settings_button
    {:title => _('Enterprise Info and settings'), :icon => 'edit-profile-enterprise'}
  end

  def more_recent_label
    ''
  end

  def available_blocks(person)
    super(person) + [DisabledEnterpriseMessageBlock, HighlightsBlock, FansBlock]
  end
end