Class: GroupsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/groups_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_navable, #current_navable=, #current_user, #point_navigation_to, #redirect_www_subdomain, #set_locale

Instance Method Details

#createObject



139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/groups_controller.rb', line 139

def create
  if secure_parent_type.present? && params[:parent_id].present?
    @parent = secure_parent_type.constantize.find(params[:parent_id]).child_groups
  else
    @parent = Group
  end
  if can? :manage, @parent
    @new_group = @parent.create(name: I18n.t(:new_group))
  end
  respond_with @new_group
end

#destroyObject



151
152
153
154
155
156
# File 'app/controllers/groups_controller.rb', line 151

def destroy
  @group.destroy
  respond_to do |format|
    format.json { head :no_content }
  end
end

#indexObject



5
6
7
8
# File 'app/controllers/groups_controller.rb', line 5

def index
  point_navigation_to Page.intranet_root
  respond_with @groups
end

#index_mineObject



10
11
12
13
14
# File 'app/controllers/groups_controller.rb', line 10

def index_mine
  point_navigation_to current_user
  @groups = current_user.groups
  respond_with @groups
end

#showObject



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
# File 'app/controllers/groups_controller.rb', line 16

def show
  if @group

    if request.format.html? || request.format.xls? || request.format.csv? || request.format.json?
      Rack::MiniProfiler.step('groups#show controller: fetch memberships') do 
        # If this is a collection group, e.g. the corporations_parent group, 
        # do not list the single members.
        #
        if @group.group_of_groups?
          @memberships = []
          @child_groups = @group.child_groups - [@group.find_officers_parent_group]

        # This is a regular group.
        #
        else
          @memberships = @group.memberships_for_member_list
        end
      end
      
      # The user might provide a `valid_from` option as constraint on the validity range.
      # 
      if params[:valid_from].present?
        @memberships = @memberships.started_after(params[:valid_from].to_datetime)
      end
      
      Rack::MiniProfiler.step('groups#show controller: cancan') do
        # Make sure only members that are allowed to be seen are in this array!
        #
        allowed_members = @group.members.accessible_by(current_ability)
        allowed_memberships = @group.memberships.where(descendant_id: allowed_members.map(&:id))
        @memberships = @memberships & allowed_memberships
      end
      
      Rack::MiniProfiler.step('groups#show controller: fetch members') do
        # Fill also the members into a separate variable.
        #
        @members = @group.members.includes(:links_as_child).where(dag_links: {id: @memberships.map(&:id)})
        
        # For some special groups, the first method of retreiving the members does not work.
        # Fallback to these slower methods:
        @members = User.includes(:links_as_child).where(dag_links: {id: @memberships.map(&:id)}) if @members.empty?
        @members = @memberships.collect { |membership| membership.user } if @members.empty?
      end
      
      # for performance reasons deactivated for the moment.
      # fill_map_address_fields
      @large_map_address_fields = []
      
      # @posts = @group.posts.order("sent_at DESC").limit(10)
      
      @new_user_group_membership = @group.build_membership
    end
  end
  
  if request.format.csv? || request.format.xls?
    list_preset = params[:list]
    list_preset_i18n = I18n.translate(list_preset) if list_preset.present?
    @file_title = "#{@group.name} #{list_preset_i18n} #{Time.zone.now}".parameterize

    if list_preset.in? ['member_development', 'join_statistics']
      @list_export = ListExport.new(@group, list_preset)
    else
      @list_export = ListExport.new(@members, list_preset)
    end
  end
  
  respond_to do |format|
    format.html do
      authorize! :read, @group
      point_navigation_to @group
      current_user.try(:update_last_seen_activity, "sieht sich Mitgliederlisten an: #{@group.title}", @group)
    end
    format.json do
      authorize! :read, @group
      render json: @group.serializable_hash.merge({member_count: @memberships.count})
    end
    format.csv do
      authorize! :read, @group
      authorize! :export_member_list, @group
      
      # See: http://railscasts.com/episodes/362-exporting-csv-and-excel
      #bom = "\377\376".force_encoding('utf-16le')
      bom = "\xEF\xBB\xBF".force_encoding('utf-8') # UTF-8
      
      csv_data = bom + @list_export.to_csv
      send_data csv_data, filename: "#{@file_title}.csv"
    end
    format.xls do
      authorize! :read, @group
      authorize! :export_member_list, @group

      send_data(@list_export.to_xls, type: 'application/xls; charset=utf-8; header=present', filename: "#{@file_title}.xls")
    end  
    format.pdf do
      authorize! :read, @group
      authorize! :export_member_list, @group
      
      if params[:sender].present?
        # TODO: This should not be inside a GET request; but I wasn't sure how to do it properly.
        session[:address_labels_pdf_sender] = params[:sender]
      end
      options = {sender: params[:sender], book_rate: params[:book_rate]}
      file_title = "#{I18n.t(:address_labels)} #{@group.name} #{Time.zone.now}".parameterize
      
      # Possible dispositions: attachment, inline.
      # Windows users with the Adobe Reader browser plugin can't print with 100% scale from the browser plugin.
      # They would need to download and open the file in Adobe Reader standalone to print at 100% scale.
      # Therefore, we use 'attachment' here in order to prevent the use of the browser plugin.
      #
      send_data(@group.members_to_pdf(options), filename: "#{file_title}.pdf", type: 'application/pdf', disposition: 'attachment')
    end
  end
  
  metric_logger.log_event @group.try(:attributes), type: :show_group
end

#updateObject



132
133
134
135
136
137
# File 'app/controllers/groups_controller.rb', line 132

def update
  @group.update_attributes(group_params)
  respond_to do |format|
    format.json { respond_with_bip @group.reload }
  end
end