Browse Source

ClassandModuleChildren

Maarten van den Berg 6 years ago
parent
commit
8d56cd7f40

+ 0 - 20
.rubocop.yml

@@ -68,26 +68,6 @@ Style/BracesAroundHashParameters:
68 68
     - 'app/models/person.rb'
69 69
     - 'config/environments/production.rb'
70 70
 
71
-# Offense count: 13
72
-# Cop supports --auto-correct.
73
-# Configuration parameters: AutoCorrect, EnforcedStyle.
74
-# SupportedStyles: nested, compact
75
-Style/ClassAndModuleChildren:
76
-  Exclude:
77
-    - 'app/controllers/api/activities_controller.rb'
78
-    - 'app/controllers/api/groups_controller.rb'
79
-    - 'app/controllers/api/me_controller.rb'
80
-    - 'app/controllers/api/people_controller.rb'
81
-    - 'app/helpers/api/activities_helper.rb'
82
-    - 'app/helpers/api/groups_helper.rb'
83
-    - 'app/helpers/api/me_helper.rb'
84
-    - 'app/helpers/api/people_helper.rb'
85
-    - 'test/controllers/api/activities_controller_test.rb'
86
-    - 'test/controllers/api/groups_controller_test.rb'
87
-    - 'test/controllers/api/me_controller_test.rb'
88
-    - 'test/controllers/api/people_controller_test.rb'
89
-    - 'test/test_helper.rb'
90
-
91 71
 # Offense count: 2
92 72
 # Cop supports --auto-correct.
93 73
 Style/ClassMethods:

+ 96 - 94
app/controllers/api/activities_controller.rb

@@ -1,107 +1,109 @@
1 1
 # Provides read-only access to Activities.
2
-class Api::ActivitiesController < ApiController
3
-  has_no_activity = [:index]
4
-
5
-  # Session-based authentication/authorization
6
-  before_action :set_activity,        except: has_no_activity
7
-  before_action :require_membership!, except: has_no_activity
8
-  before_action :api_require_admin!,  only: has_no_activity
9
-  skip_before_action :api_require_authentication!, :set_activity, :require_membership!, if: 'request.authorization'
10
-
11
-  # Group API-key-based authentication/authorization
12
-  before_action :api_auth_group_token,    if: 'request.authorization'
13
-  before_action :set_activity_with_group, if: 'request.authorization'
14
-
15
-  # GET /api/activities
16
-  def index
17
-    @activities = Activity.all
18
-  end
19
-
20
-  # GET /api/activities/1
21
-  def show; end
22
-
23
-  # GET /api/activities/1/response_summary
24
-  def response_summary
25
-    as = @activity
26
-         .participants
27
-         .joins(:person)
28
-         .order('people.first_name ASC')
29
-
30
-    present = as.where(attending: true)
31
-
32
-    unknown = as.where(attending: nil)
33
-
34
-    absent = as.where(attending: false)
35
-
36
-    presentnames = present.map { |p| p.person.first_name }
37
-
38
-    unknownnames = unknown.map { |p| p.person.first_name }
39
-
40
-    absentnames = absent.map { |p| p.person.first_name }
41
-
42
-    if presentnames.positive?
43
-      present_mess = I18n.t('activities.participant.these_present', count: present.count, names: presentnames.join(', '))
44
-    else
45
-      present_mess = I18n.t('activities.participant.none_present')
2
+module Api
3
+  class ActivitiesController < ApiController
4
+    has_no_activity = [:index]
5
+
6
+    # Session-based authentication/authorization
7
+    before_action :set_activity,        except: has_no_activity
8
+    before_action :require_membership!, except: has_no_activity
9
+    before_action :api_require_admin!,  only: has_no_activity
10
+    skip_before_action :api_require_authentication!, :set_activity, :require_membership!, if: 'request.authorization'
11
+
12
+    # Group API-key-based authentication/authorization
13
+    before_action :api_auth_group_token,    if: 'request.authorization'
14
+    before_action :set_activity_with_group, if: 'request.authorization'
15
+
16
+    # GET /api/activities
17
+    def index
18
+      @activities = Activity.all
46 19
     end
47 20
 
48
-    if unknownnames.positive?
49
-      unknown_mess = I18n.t('activities.participant.these_unknown', count: unknown.count, names: unknownnames.join(', '))
50
-    else
51
-      unknown_mess = I18n.t('activities.participant.none_unknown')
52
-    end
53
-
54
-    if absentnames.positive?
55
-      absent_mess = I18n.t('activities.participant.these_absent', count: absent.count, names: absentnames.join(', '))
56
-    else
57
-      absent_mess = I18n.t('activities.participant.none_absent')
21
+    # GET /api/activities/1
22
+    def show; end
23
+
24
+    # GET /api/activities/1/response_summary
25
+    def response_summary
26
+      as = @activity
27
+           .participants
28
+           .joins(:person)
29
+           .order('people.first_name ASC')
30
+
31
+      present = as.where(attending: true)
32
+
33
+      unknown = as.where(attending: nil)
34
+
35
+      absent = as.where(attending: false)
36
+
37
+      presentnames = present.map { |p| p.person.first_name }
38
+
39
+      unknownnames = unknown.map { |p| p.person.first_name }
40
+
41
+      absentnames = absent.map { |p| p.person.first_name }
42
+
43
+      if presentnames.positive?
44
+        present_mess = I18n.t('activities.participant.these_present', count: present.count, names: presentnames.join(', '))
45
+      else
46
+        present_mess = I18n.t('activities.participant.none_present')
47
+      end
48
+
49
+      if unknownnames.positive?
50
+        unknown_mess = I18n.t('activities.participant.these_unknown', count: unknown.count, names: unknownnames.join(', '))
51
+      else
52
+        unknown_mess = I18n.t('activities.participant.none_unknown')
53
+      end
54
+
55
+      if absentnames.positive?
56
+        absent_mess = I18n.t('activities.participant.these_absent', count: absent.count, names: absentnames.join(', '))
57
+      else
58
+        absent_mess = I18n.t('activities.participant.none_absent')
59
+      end
60
+
61
+      @summary = {
62
+        present: {
63
+          count: present.count,
64
+          names: presentnames,
65
+          message: present_mess
66
+        },
67
+        unknown: {
68
+          count: unknown.count,
69
+          names: unknownnames,
70
+          message: unknown_mess
71
+        },
72
+        absent: {
73
+          count: absent.count,
74
+          names: absentnames,
75
+          message: absent_mess
76
+        }
77
+      }
58 78
     end
59 79
 
60
-    @summary = {
61
-      present: {
62
-        count: present.count,
63
-        names: presentnames,
64
-        message: present_mess
65
-      },
66
-      unknown: {
67
-        count: unknown.count,
68
-        names: unknownnames,
69
-        message: unknown_mess
70
-      },
71
-      absent: {
72
-        count: absent.count,
73
-        names: absentnames,
74
-        message: absent_mess
75
-      }
76
-    }
77
-  end
80
+    def presence
81
+      participant = Participant.find_by(
82
+        person_id: params[:person_id],
83
+        activity: @activity
84
+      )
78 85
 
79
-  def presence
80
-    participant = Participant.find_by(
81
-      person_id: params[:person_id],
82
-      activity: @activity
83
-    )
86
+      participant.update_attributes(params.permit(:attending))
87
+      head :no_content
88
+    end
84 89
 
85
-    participant.update_attributes(params.permit(:attending))
86
-    head :no_content
87
-  end
90
+    private
88 91
 
89
-  private
92
+    # Set activity from the :id-parameter
93
+    def set_activity
94
+      @activity = Activity.find(params[:id])
95
+      @group = @activity.group
96
+    end
90 97
 
91
-  # Set activity from the :id-parameter
92
-  def set_activity
93
-    @activity = Activity.find(params[:id])
94
-    @group = @activity.group
95
-  end
98
+    # Set activity from the :id-parameter, and assert that it belongs to the set @group.
99
+    def set_activity_with_group
100
+      @activity = Activity.find_by id: params[:id]
101
+      unless @activity
102
+        head :not_found
103
+        return
104
+      end
96 105
 
97
-  # Set activity from the :id-parameter, and assert that it belongs to the set @group.
98
-  def set_activity_with_group
99
-    @activity = Activity.find_by id: params[:id]
100
-    unless @activity
101
-      head :not_found
102
-      return
106
+      head :unauthorized unless @activity.group == @group
103 107
     end
104
-
105
-    head :unauthorized unless @activity.group == @group
106 108
   end
107 109
 end

+ 47 - 45
app/controllers/api/groups_controller.rb

@@ -4,65 +4,67 @@
4 4
 #   - By passing a custom Authorization:-header of the form 'Group :api_key'.
5 5
 #
6 6
 # If the API key method is used, the :id parameter is ignored, but still required in the URL.
7
-class Api::GroupsController < ApiController
8
-  has_no_group = [:index]
7
+module Api
8
+  class GroupsController < ApiController
9
+    has_no_group = [:index]
9 10
 
10
-  # Session-based authentication / authorization filters
11
-  before_action :set_group,           except: has_no_group
12
-  before_action :require_membership!, except: has_no_group
13
-  before_action :api_require_admin!,  only:   has_no_group
14
-  skip_before_action :set_group, :require_membership!, :api_require_authentication!, if: 'request.authorization'
11
+    # Session-based authentication / authorization filters
12
+    before_action :set_group,           except: has_no_group
13
+    before_action :require_membership!, except: has_no_group
14
+    before_action :api_require_admin!,  only:   has_no_group
15
+    skip_before_action :set_group, :require_membership!, :api_require_authentication!, if: 'request.authorization'
15 16
 
16
-  # API key based filter (both authenticates and authorizes)
17
-  before_action :api_auth_group_token, if: 'request.authorization'
17
+    # API key based filter (both authenticates and authorizes)
18
+    before_action :api_auth_group_token, if: 'request.authorization'
18 19
 
19
-  # GET /api/groups
20
-  def index
21
-    @groups = Group.all
22
-  end
20
+    # GET /api/groups
21
+    def index
22
+      @groups = Group.all
23
+    end
23 24
 
24
-  # GET /api/groups/1
25
-  def show; end
25
+    # GET /api/groups/1
26
+    def show; end
26 27
 
27
-  # GET /api/groups/1/current_activities
28
-  def current_activities
29
-    reference = try_parse_datetime params[:reference]
30
-    @activities = @group.current_activities reference
28
+    # GET /api/groups/1/current_activities
29
+    def current_activities
30
+      reference = try_parse_datetime params[:reference]
31
+      @activities = @group.current_activities reference
31 32
 
32
-    render 'api/activities/index'
33
-  end
33
+      render 'api/activities/index'
34
+    end
34 35
 
35
-  # GET /api/groups/1/upcoming_activities
36
-  def upcoming_activities
37
-    reference = try_parse_datetime params[:reference]
38
-    @activities = @group.upcoming_activities reference
36
+    # GET /api/groups/1/upcoming_activities
37
+    def upcoming_activities
38
+      reference = try_parse_datetime params[:reference]
39
+      @activities = @group.upcoming_activities reference
39 40
 
40
-    render 'api/activities/index'
41
-  end
41
+      render 'api/activities/index'
42
+    end
42 43
 
43
-  # GET /api/groups/1/previous_activities
44
-  def previous_activities
45
-    reference = try_parse_datetime params[:reference]
46
-    @activities = @group.previous_activities reference
44
+    # GET /api/groups/1/previous_activities
45
+    def previous_activities
46
+      reference = try_parse_datetime params[:reference]
47
+      @activities = @group.previous_activities reference
47 48
 
48
-    render 'api/activities/index'
49
-  end
49
+      render 'api/activities/index'
50
+    end
50 51
 
51
-  private
52
+    private
52 53
 
53
-  # Set group from the :id parameter.
54
-  def set_group
55
-    @group = Group.find(params[:id])
56
-  end
54
+    # Set group from the :id parameter.
55
+    def set_group
56
+      @group = Group.find(params[:id])
57
+    end
57 58
 
58
-  # @return [DateTime] the parsed input.
59
-  def try_parse_datetime(input = nil)
60
-    return unless input
59
+    # @return [DateTime] the parsed input.
60
+    def try_parse_datetime(input = nil)
61
+      return unless input
61 62
 
62
-    begin
63
-      DateTime.parse input
64
-    rescue ArgumentError
65
-      nil
63
+      begin
64
+        DateTime.parse input
65
+      rescue ArgumentError
66
+        nil
67
+      end
66 68
     end
67 69
   end
68 70
 end

+ 10 - 8
app/controllers/api/me_controller.rb

@@ -1,11 +1,13 @@
1
-class Api::MeController < ApiController
2
-  def index
3
-    @person = current_person
4
-    render 'api/people/show'
5
-  end
1
+module Api
2
+  class MeController < ApiController
3
+    def index
4
+      @person = current_person
5
+      render 'api/people/show'
6
+    end
6 7
 
7
-  def groups
8
-    @groups = current_person.groups
9
-    render 'api/groups/index'
8
+    def groups
9
+      @groups = current_person.groups
10
+      render 'api/groups/index'
11
+    end
10 12
   end
11 13
 end

+ 17 - 15
app/controllers/api/people_controller.rb

@@ -1,21 +1,23 @@
1
-class Api::PeopleController < ApiController
2
-  before_action :set_person, only: [:show]
3
-  before_action :api_require_admin! # Normal people should use /me
1
+module Api
2
+  class PeopleController < ApiController
3
+    before_action :set_person, only: [:show]
4
+    before_action :api_require_admin! # Normal people should use /me
4 5
 
5
-  # GET /api/people
6
-  # GET /api/people.json
7
-  def index
8
-    @people = Person.all
9
-  end
6
+    # GET /api/people
7
+    # GET /api/people.json
8
+    def index
9
+      @people = Person.all
10
+    end
10 11
 
11
-  # GET /api/people/1
12
-  # GET /api/people/1.json
13
-  def show; end
12
+    # GET /api/people/1
13
+    # GET /api/people/1.json
14
+    def show; end
14 15
 
15
-  private
16
+    private
16 17
 
17
-  # Use callbacks to share common setup or constraints between actions.
18
-  def set_person
19
-    @person = Person.find(params[:id])
18
+    # Use callbacks to share common setup or constraints between actions.
19
+    def set_person
20
+      @person = Person.find(params[:id])
21
+    end
20 22
   end
21 23
 end

+ 3 - 1
app/helpers/api/activities_helper.rb

@@ -1,2 +1,4 @@
1
-module Api::ActivitiesHelper
1
+module Api
2
+  class ActivitiesHelper
3
+  end
2 4
 end

+ 3 - 1
app/helpers/api/groups_helper.rb

@@ -1,2 +1,4 @@
1
-module Api::GroupsHelper
1
+module Api
2
+  class GroupsHelper
3
+  end
2 4
 end

+ 3 - 1
app/helpers/api/me_helper.rb

@@ -1,2 +1,4 @@
1
-module Api::MeHelper
1
+module Api
2
+  class MeHelper
3
+  end
2 4
 end

+ 3 - 1
app/helpers/api/people_helper.rb

@@ -1,2 +1,4 @@
1
-module Api::PeopleHelper
1
+module Api
2
+  class PeopleHelper
3
+  end
2 4
 end

+ 36 - 34
test/controllers/api/activities_controller_test.rb

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

+ 36 - 34
test/controllers/api/groups_controller_test.rb

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

+ 6 - 4
test/controllers/api/me_controller_test.rb

@@ -1,7 +1,9 @@
1 1
 require 'test_helper'
2 2
 
3
-class Api::MeControllerTest < ActionDispatch::IntegrationTest
4
-  # test "the truth" do
5
-  #   assert true
6
-  # end
3
+module Api
4
+  class MeControllerTest < ActionDispatch::IntegrationTest
5
+    # test "the truth" do
6
+    #   assert true
7
+    # end
8
+  end
7 9
 end

+ 36 - 34
test/controllers/api/people_controller_test.rb

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

+ 6 - 4
test/test_helper.rb

@@ -2,9 +2,11 @@ ENV['RAILS_ENV'] ||= 'test'
2 2
 require File.expand_path('../config/environment', __dir__)
3 3
 require 'rails/test_help'
4 4
 
5
-class ActiveSupport::TestCase
6
-  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
7
-  fixtures :all
5
+module ActiveSupport
6
+  class TestCase
7
+    # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
8
+    fixtures :all
8 9
 
9
-  # Add more helper methods to be used by all tests here...
10
+    # Add more helper methods to be used by all tests here...
11
+  end
10 12
 end