cirandas.net

ref: master

app/models/community.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
class Community < Organization

  attr_accessible :accessor_id, :accessor_type, :role_id, :resource_id,
    :resource_type, :address_reference, :district, :language, :description

  attr_accessible :requires_email, :address_line2

  after_destroy :check_invite_member_for_destroy

  def self.type_name
    _('Community')
  end

  N_('community')
  N_('Language')

  settings_items :language

  extend SetProfileRegionFromCityState::ClassMethods
  set_profile_region_from_city_state

  before_create do |community|
    community.moderated_articles = true if community.environment.enabled?('organizations_are_moderated_by_default')
  end

  def check_invite_member_for_destroy
      InviteMember.pending.select { |task| task.community_id == self.id }.map(&:destroy)
  end

  # Since it's not a good idea to add the environment as accessible through
  # mass-assignment, we set it manually here. Note that this requires that the
  # places that call this method are safe from mass-assignment by setting the
  # environment key themselves.
  def self.create_after_moderation(requestor, attributes = {})
    environment = attributes[:environment]
    community = Community.new(attributes)
    community.environment = environment
    if community.environment.enabled?('admin_must_approve_new_communities')
      CreateCommunity.create!(attributes.merge(:requestor => requestor, :environment => environment))
    else
      community.save!
      community.add_admin(requestor)
    end
    community
  end

  xss_terminate :only => [ :name, :address, :contact_phone, :description ], :on => 'validation'

  FIELDS = %w[
    language
  ]

  def self.fields
    super + FIELDS
  end

  def active_fields
    environment ? environment.active_community_fields : []
  end

  def required_fields
    environment ? environment.required_community_fields : []
  end

  def signup_fields
    environment ? environment.signup_community_fields : []
  end

  def name=(value)
    super(value)
    self.identifier ||= value.to_slug
  end

  def default_template
    environment.community_default_template
  end

  def news(limit = 30, highlight = false)
    recent_documents(limit, ["articles.type != ? AND articles.highlighted = ?", 'Folder', highlight])
  end

  def each_member(offset=0)
    while member = self.members.order(:id).offset(offset).first
      yield member
      offset = offset + 1
    end
  end

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

  def exclude_verbs_on_activities
    %w[join_community leave_scrap]
  end

  def default_set_of_blocks
    return angular_theme_default_set_of_blocks if Theme.angular_theme?(environment.theme)
    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(:show_name => true), LinkListBlock.new(:links => links), RecentDocumentsBlock.new]
    ]
  end

  def angular_theme_default_set_of_blocks
    @boxes_limit = 2
    self.layout_template = 'rightbar'
    [
      [MenuBlock.new, MainBlock.new],
      [CommunitiesBlock.new, TagsCloudBlock.new]
    ]
  end

end