Pārlūkot izejas kodu

Scaffold activities inside group

Maarten van den Berg 8 gadi atpakaļ
vecāks
revīzija
e01158df73

+ 81 - 0
app/controllers/activities_controller.rb

@@ -0,0 +1,81 @@
1
+class ActivitiesController < ApplicationController
2
+  include GroupsHelper
3
+  before_action :set_activity, only: [:show, :edit, :update, :destroy]
4
+  before_action :set_group
5
+  before_action :require_membership!
6
+
7
+  # GET /groups/:id/activities
8
+  # GET /activities.json
9
+  def index
10
+    @activities = @group.activities
11
+  end
12
+
13
+  # GET /activities/1
14
+  # GET /activities/1.json
15
+  def show
16
+  end
17
+
18
+  # GET /activities/new
19
+  def new
20
+    @activity = Activity.new
21
+  end
22
+
23
+  # GET /activities/1/edit
24
+  def edit
25
+  end
26
+
27
+  # POST /activities
28
+  # POST /activities.json
29
+  def create
30
+    @activity = Activity.new(activity_params)
31
+
32
+    respond_to do |format|
33
+      if @activity.save
34
+        format.html { redirect_to @activity, notice: 'Activity was successfully created.' }
35
+        format.json { render :show, status: :created, location: @activity }
36
+      else
37
+        format.html { render :new }
38
+        format.json { render json: @activity.errors, status: :unprocessable_entity }
39
+      end
40
+    end
41
+  end
42
+
43
+  # PATCH/PUT /activities/1
44
+  # PATCH/PUT /activities/1.json
45
+  def update
46
+    respond_to do |format|
47
+      if @activity.update(activity_params)
48
+        format.html { redirect_to @activity, notice: 'Activity was successfully updated.' }
49
+        format.json { render :show, status: :ok, location: @activity }
50
+      else
51
+        format.html { render :edit }
52
+        format.json { render json: @activity.errors, status: :unprocessable_entity }
53
+      end
54
+    end
55
+  end
56
+
57
+  # DELETE /activities/1
58
+  # DELETE /activities/1.json
59
+  def destroy
60
+    @activity.destroy
61
+    respond_to do |format|
62
+      format.html { redirect_to activities_url, notice: 'Activity was successfully destroyed.' }
63
+      format.json { head :no_content }
64
+    end
65
+  end
66
+
67
+  private
68
+    # Use callbacks to share common setup or constraints between actions.
69
+    def set_activity
70
+      @activity = Activity.find(params[:id])
71
+    end
72
+
73
+    def set_group
74
+      @group = Group.find(params[:group_id])
75
+    end
76
+
77
+    # Never trust parameters from the scary internet, only allow the white list through.
78
+    def activity_params
79
+      params.fetch(:activity, {})
80
+    end
81
+end

+ 1 - 1
app/controllers/members_controller.rb

@@ -7,7 +7,7 @@ class MembersController < ApplicationController
7 7
   # GET /members
8 8
   # GET /members.json
9 9
   def index
10
-    @members = Member.all
10
+    @members = @group.members
11 11
   end
12 12
 
13 13
   # GET /members/1

+ 2 - 0
app/helpers/activities_helper.rb

@@ -0,0 +1,2 @@
1
+module ActivitiesHelper
2
+end

+ 2 - 0
app/views/activities/_activity.json.jbuilder

@@ -0,0 +1,2 @@
1
+json.extract! activity, :id, :created_at, :updated_at
2
+json.url activity_url(activity, format: :json)

+ 17 - 0
app/views/activities/_form.html.erb

@@ -0,0 +1,17 @@
1
+<%= form_for([@group, activity]) do |f| %>
2
+  <% if activity.errors.any? %>
3
+    <div id="error_explanation">
4
+      <h2><%= pluralize(activity.errors.count, "error") %> prohibited this activity from being saved:</h2>
5
+
6
+      <ul>
7
+      <% activity.errors.full_messages.each do |message| %>
8
+        <li><%= message %></li>
9
+      <% end %>
10
+      </ul>
11
+    </div>
12
+  <% end %>
13
+
14
+  <div class="actions">
15
+    <%= f.submit %>
16
+  </div>
17
+<% end %>

+ 6 - 0
app/views/activities/edit.html.erb

@@ -0,0 +1,6 @@
1
+<h1>Editing Activity</h1>
2
+
3
+<%= render 'form', activity: @activity %>
4
+
5
+<%= link_to 'Show', group_activity(@group, @activity) %> |
6
+<%= link_to 'Back', group_activities_path(@group) %>

+ 25 - 0
app/views/activities/index.html.erb

@@ -0,0 +1,25 @@
1
+<p id="notice"><%= notice %></p>
2
+
3
+<h1>Activities</h1>
4
+
5
+<table class="table">
6
+  <thead>
7
+    <tr>
8
+      <th colspan="3"></th>
9
+    </tr>
10
+  </thead>
11
+
12
+  <tbody>
13
+    <% @activities.each do |activity| %>
14
+      <tr>
15
+        <td><%= link_to 'Show', group_activity_path(@group, activity) %></td>
16
+        <td><%= link_to 'Edit', edit_group_activity_path(@group, activity) %></td>
17
+        <td><%= link_to 'Destroy', group_activity_path(@group, activity), method: :delete, data: { confirm: 'Are you sure?' } %></td>
18
+      </tr>
19
+    <% end %>
20
+  </tbody>
21
+</table>
22
+
23
+<br>
24
+
25
+<%= link_to 'New Activity', new_group_activity_path(@group) %>

+ 1 - 0
app/views/activities/index.json.jbuilder

@@ -0,0 +1 @@
1
+json.array! @activities, partial: 'activities/activity', as: :activity

+ 5 - 0
app/views/activities/new.html.erb

@@ -0,0 +1,5 @@
1
+<h1>New Activity</h1>
2
+
3
+<%= render 'form', activity: @activity %>
4
+
5
+<%= link_to 'Back', group_activities_path(@group) %>

+ 4 - 0
app/views/activities/show.html.erb

@@ -0,0 +1,4 @@
1
+<p id="notice"><%= notice %></p>
2
+
3
+<%= link_to 'Edit', edit_group_activity_path(@group, @activity) %> |
4
+<%= link_to 'Back', group_activities_path(@group) %>

+ 1 - 0
app/views/activities/show.json.jbuilder

@@ -0,0 +1 @@
1
+json.partial! "activities/activity", activity: @activity

+ 1 - 0
config/routes.rb

@@ -19,6 +19,7 @@ Rails.application.routes.draw do
19 19
 
20 20
   resources :groups do
21 21
     resources :members
22
+    resources :activities
22 23
   end
23 24
   get 'my_groups', to: 'groups#user_groups', as: :user_groups
24 25
 

+ 48 - 0
test/controllers/activities_controller_test.rb

@@ -0,0 +1,48 @@
1
+require 'test_helper'
2
+
3
+class ActivitiesControllerTest < ActionDispatch::IntegrationTest
4
+  setup do
5
+    @activity = activities(:one)
6
+  end
7
+
8
+  test "should get index" do
9
+    get activities_url
10
+    assert_response :success
11
+  end
12
+
13
+  test "should get new" do
14
+    get new_activity_url
15
+    assert_response :success
16
+  end
17
+
18
+  test "should create activity" do
19
+    assert_difference('Activity.count') do
20
+      post activities_url, params: { activity: {  } }
21
+    end
22
+
23
+    assert_redirected_to activity_url(Activity.last)
24
+  end
25
+
26
+  test "should show activity" do
27
+    get activity_url(@activity)
28
+    assert_response :success
29
+  end
30
+
31
+  test "should get edit" do
32
+    get edit_activity_url(@activity)
33
+    assert_response :success
34
+  end
35
+
36
+  test "should update activity" do
37
+    patch activity_url(@activity), params: { activity: {  } }
38
+    assert_redirected_to activity_url(@activity)
39
+  end
40
+
41
+  test "should destroy activity" do
42
+    assert_difference('Activity.count', -1) do
43
+      delete activity_url(@activity)
44
+    end
45
+
46
+    assert_redirected_to activities_url
47
+  end
48
+end