Class: GroupsController

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

Instance Method Summary collapse

Instance Method Details

#create ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/groups_controller.rb', line 38

def create
  @group = @creator.groups.create group_params
  @new_memberships = @group.reload.memberships if @group.valid?
  authorize @group
  respond_to do |format|
    format.html do
      if @group.valid?
        redirect_to creator_groups_path(@creator), notice: t(".success")
      else
        render Views::Groups::New.new(group: @group, creator: @creator), status: :unprocessable_content
      end
    end
    format.manyfold_api_v0 do
      if @group.valid?
        render json: ManyfoldApi::V0::GroupSerializer.new(@group).serialize, status: :created, location: creator_group_path(@creator, @group)
      else
        render json: @group.errors.to_json, status: :unprocessable_content
      end
    end
  end
end

#destroy ⇒ Object



82
83
84
85
86
87
88
# File 'app/controllers/groups_controller.rb', line 82

def destroy
  @group.destroy!
  respond_to do |format|
    format.html { redirect_to creator_groups_path(@creator) }
    format.manyfold_api_v0 { head :no_content }
  end
end

#edit ⇒ Object



32
33
34
35
36
# File 'app/controllers/groups_controller.rb', line 32

def edit
  respond_to do |format|
    format.html { render Views::Groups::Edit.new(group: @group, creator: @creator) }
  end
end

#index ⇒ Object



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

def index
  authorize Group.new(creator: @creator) # stub group to check authorization
  page = params[:page] || 1
  groups = policy_scope(@creator.groups).page(page).per(100)
  respond_to do |format|
    format.html { render Views::Groups::Index.new(groups: groups, creator: @creator) }
    format.manyfold_api_v0 { render json: ManyfoldApi::V0::GroupListSerializer.new(@creator, groups).serialize }
  end
end

#new ⇒ Object



24
25
26
27
28
29
30
# File 'app/controllers/groups_controller.rb', line 24

def new
  group = @creator.groups.new
  authorize group
  respond_to do |format|
    format.html { render Views::Groups::New.new(group: group, creator: @creator) }
  end
end

#show ⇒ Object



17
18
19
20
21
22
# File 'app/controllers/groups_controller.rb', line 17

def show
  authorize @group
  respond_to do |format|
    format.manyfold_api_v0 { render json: ManyfoldApi::V0::GroupSerializer.new(@group).serialize }
  end
end

#update ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/groups_controller.rb', line 60

def update
  previous_membership_ids = @group.memberships.pluck(:id)
  @group.update group_params
  @new_memberships = @group.reload.memberships.where.not(id: previous_membership_ids) if @group.valid?
  respond_to do |format|
    format.html do
      if @group.valid?
        redirect_to creator_groups_path(@creator), notice: t(".success")
      else
        render Views::Groups::Edit.new(group: @group, creator: @creator), status: :unprocessable_content
      end
    end
    format.manyfold_api_v0 do
      if @group.valid?
        render json: ManyfoldApi::V0::GroupSerializer.new(@group).serialize
      else
        render json: @group.errors.to_json, status: :unprocessable_content
      end
    end
  end
end