undercarriage 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ec3241ab6e053eb78aa66e3aed56473b299ebef9c48c08c415a7f9c04b0eb70
4
- data.tar.gz: c0e0393b7f2d1c383eadb22af21ac440fe5651eea335f71241289918f754b547
3
+ metadata.gz: fccd3ba65ccfd908ef199e41930f736cd2f9022ca3113ee90186a96230a7cf8c
4
+ data.tar.gz: a92a0518846b2d4996ba806f55e1a4f00df34ad6dcdd0501a3e4cd7292915bf5
5
5
  SHA512:
6
- metadata.gz: 423946c74cd8e1f5d313627933fd3f6a27d663e0e580d78ae5d56f202117d8348d56eb42315498e32878e0bdb39e30f7e48c021e4207b2221197c0d0edf7c73b
7
- data.tar.gz: 5613f293beb7bac032d58f7aed7f686f67e6c8219ba52708690e591e874246e4fd86a60c91dc98c04a518a739b2e883dc7fc8f1ed1e47f8955bef20e955cc270
6
+ metadata.gz: 45773941e2a9fd2775e2e75c736083072e2193f90b49e92711638e2776d9d2058f18649050ce97ad663e2eb0d7b82d929d0db23de859fd7ff96912c886f228fd
7
+ data.tar.gz: 3d5cf0b390504b5cf2d342d8e92ecce6738798466579af5562f145d0c11866a924950d955e506e699c4af4d8c25c30ca8d4d186391482ce39dd23f469b3bd5a6
data/README.md CHANGED
@@ -14,7 +14,7 @@ Undercarriage is a set of concerns to add to your application to trim some of th
14
14
  Add to your application's Gemfile
15
15
 
16
16
  ```ruby
17
- gem 'undercarriage', '~> 0.1'
17
+ gem 'undercarriage', '~> 0.2'
18
18
  ```
19
19
 
20
20
  Run the bundle command
data/lib/undercarriage.rb CHANGED
@@ -3,6 +3,19 @@
3
3
  require 'undercarriage/controllers/action_concern'
4
4
  require 'undercarriage/controllers/kaminari_concern'
5
5
  require 'undercarriage/controllers/locale_concern'
6
+ require 'undercarriage/controllers/restful_concern'
7
+ require 'undercarriage/controllers/restful/actions/base_concern'
8
+ require 'undercarriage/controllers/restful/actions/index_concern'
9
+ require 'undercarriage/controllers/restful/actions/show_concern'
10
+ require 'undercarriage/controllers/restful/actions/new_concern'
11
+ require 'undercarriage/controllers/restful/actions/create_concern'
12
+ require 'undercarriage/controllers/restful/actions/edit_concern'
13
+ require 'undercarriage/controllers/restful/actions/update_concern'
14
+ require 'undercarriage/controllers/restful/actions/destroy_concern'
15
+ require 'undercarriage/controllers/restful/location_after_concern'
16
+ require 'undercarriage/controllers/restful/namespace_concern'
17
+ require 'undercarriage/controllers/restful/permitted_attributes_concern'
18
+ require 'undercarriage/controllers/restful/utility_concern'
6
19
 
7
20
  require 'undercarriage/railtie'
8
21
 
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module Actions
7
+ ##
8
+ # Base restful action
9
+ #
10
+ # Usage
11
+ # class ExamplesController < ApplicationController
12
+ # include Undercarriage::Controllers::RestfulConcern
13
+ # end
14
+ #
15
+ module BaseConcern
16
+ extend ActiveSupport::Concern
17
+
18
+ protected
19
+
20
+ def resource_new_content
21
+ action_name == 'new' ? new_resource_content : create_resource_content
22
+ end
23
+
24
+ def resource_content
25
+ resource_id = params.fetch(:id)
26
+ resource_query = model_class.find(resource_id)
27
+
28
+ instance_variable_set("@#{instance_name}", resource_query)
29
+ end
30
+
31
+ def nested_resource_pre_build; end
32
+
33
+ def nested_resource_build; end
34
+
35
+ def after_create_action; end
36
+
37
+ def after_update_action; end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module Actions
7
+ ##
8
+ # Create restful action
9
+ #
10
+ # Usage
11
+ # class ExamplesController < ApplicationController
12
+ # include Undercarriage::Controllers::RestfulConcern
13
+ # end
14
+ #
15
+ module CreateConcern
16
+ extend ActiveSupport::Concern
17
+
18
+ included do
19
+ before_action :create_resource, only: %i[create]
20
+ end
21
+
22
+ ##
23
+ # Create action
24
+ #
25
+ # Usage
26
+ # class ExamplesController < ApplicationController
27
+ # include Undercarriage::Controllers::RestfulConcern
28
+ #
29
+ # ##
30
+ # # This method is only needed if you want to override the action entirely. Otherwise, it is not needed.
31
+ # # Database resources can be accessed as `@create_resource` or `@example`
32
+ # #
33
+ # # def create
34
+ # # ...
35
+ # # end
36
+ # end
37
+ #
38
+ def create
39
+ nested_resource_pre_build
40
+
41
+ if @create_resource.save
42
+ after_create_action
43
+
44
+ respond_with(@create_resource) do |format|
45
+ format.html { redirect_to location_after_create }
46
+ end
47
+ else
48
+ nested_resource_build
49
+
50
+ respond_with(@create_resource) do |format|
51
+ format.html { render action: :new }
52
+ end
53
+ end
54
+ end
55
+
56
+ protected
57
+
58
+ ##
59
+ # Create restful action
60
+ #
61
+ # Usage
62
+ # class ExamplesController < ApplicationController
63
+ # include Undercarriage::Controllers::RestfulConcern
64
+ #
65
+ # ##
66
+ # # This method is only needed if you want to override the query entirely. Otherwise, it is not needed.
67
+ # # Database resources can be accessed as `@example`
68
+ # #
69
+ # # def create_resource_content
70
+ # # ...
71
+ # # end
72
+ #
73
+ # ##
74
+ # # To add authorization through something like Pundit, the following could be used
75
+ # #
76
+ # # def create_resource_content
77
+ # # super
78
+ # #
79
+ # # authorize @example
80
+ # # end
81
+ #
82
+ # ##
83
+ # # The `resource_new_content` method can also be overwritten. This method is meant to share content with
84
+ # # the `new` action
85
+ # #
86
+ # # def resource_new_content
87
+ # # ...
88
+ # # end
89
+ # end
90
+ #
91
+ def create_resource_content
92
+ resource_query = model_class.new(create_resource_params)
93
+
94
+ instance_variable_set("@#{instance_name}", resource_query)
95
+ end
96
+
97
+ private
98
+
99
+ def create_resource
100
+ @create_resource ||= resource_new_content
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module Actions
7
+ ##
8
+ # Destroy restful action
9
+ #
10
+ # Usage
11
+ # class ExamplesController < ApplicationController
12
+ # include Undercarriage::Controllers::RestfulConcern
13
+ # end
14
+ #
15
+ module DestroyConcern
16
+ extend ActiveSupport::Concern
17
+
18
+ included do
19
+ before_action :destroy_resource, only: %i[destroy]
20
+ end
21
+
22
+ ##
23
+ # Destroy action
24
+ #
25
+ # Usage
26
+ # class ExamplesController < ApplicationController
27
+ # include Undercarriage::Controllers::RestfulConcern
28
+ #
29
+ # ##
30
+ # # This method is only needed if you want to override the action entirely. Otherwise, it is not needed.
31
+ # # Database resources can be accessed as `@destroy_resource` or `@example`
32
+ # #
33
+ # # def destroy
34
+ # # ...
35
+ # # end
36
+ # end
37
+ #
38
+ def destroy
39
+ @destroy_resource.destroy
40
+
41
+ respond_with(@destroy_resource) do |format|
42
+ format.html { redirect_to location_after_destroy }
43
+ end
44
+ end
45
+
46
+ protected
47
+
48
+ ##
49
+ # Destroy restful action
50
+ #
51
+ # Usage
52
+ # class ExamplesController < ApplicationController
53
+ # include Undercarriage::Controllers::RestfulConcern
54
+ #
55
+ # ##
56
+ # # This method is only needed if you want to override the query entirely. Otherwise, it is not needed.
57
+ # # Database resources can be accessed as `@example`
58
+ # #
59
+ # # def destroy_resource_content
60
+ # # ...
61
+ # # end
62
+ #
63
+ # ##
64
+ # # To add authorization through something like Pundit, the following could be used
65
+ # #
66
+ # # def destroy_resource_content
67
+ # # super
68
+ # #
69
+ # # authorize @example
70
+ # # end
71
+ #
72
+ # ##
73
+ # # The `resource_content` method can also be overwritten. Be careful with this because the `show`,
74
+ # # `edit` and `update` actions will also use this method
75
+ # #
76
+ # # def resource_content
77
+ # # ...
78
+ # # end
79
+ # end
80
+ #
81
+ def destroy_resource_content
82
+ resource_content
83
+ end
84
+
85
+ private
86
+
87
+ def destroy_resource
88
+ @destroy_resource ||= destroy_resource_content
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module Actions
7
+ ##
8
+ # Edit restful action
9
+ #
10
+ # Usage
11
+ # class ExamplesController < ApplicationController
12
+ # include Undercarriage::Controllers::RestfulConcern
13
+ # end
14
+ #
15
+ module EditConcern
16
+ extend ActiveSupport::Concern
17
+
18
+ included do
19
+ before_action :edit_resource, only: %i[edit]
20
+ end
21
+
22
+ ##
23
+ # Edit action
24
+ #
25
+ # Usage
26
+ # class ExamplesController < ApplicationController
27
+ # include Undercarriage::Controllers::RestfulConcern
28
+ #
29
+ # ##
30
+ # # This method is only needed if you want to override the action entirely. Otherwise, it is not needed.
31
+ # # Database resources can be accessed as `@edit_resource` or `@example`
32
+ # #
33
+ # # def edit
34
+ # # ...
35
+ # # end
36
+ # end
37
+ #
38
+ def edit
39
+ nested_resource_pre_build
40
+ nested_resource_build
41
+
42
+ respond_with(@edit_resource) do |format|
43
+ format.html { render layout: !request.xhr? }
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ ##
50
+ # Edit restful action
51
+ #
52
+ # Usage
53
+ # class ExamplesController < ApplicationController
54
+ # include Undercarriage::Controllers::RestfulConcern
55
+ #
56
+ # ##
57
+ # # This method is only needed if you want to override the query entirely. Otherwise, it is not needed.
58
+ # # Database resources can be accessed as `@example`
59
+ # #
60
+ # # def edit_resource_content
61
+ # # ...
62
+ # # end
63
+ #
64
+ # ##
65
+ # # To add authorization through something like Pundit, the following could be used
66
+ # #
67
+ # # def edit_resource_content
68
+ # # super
69
+ # #
70
+ # # authorize @example
71
+ # # end
72
+ #
73
+ # ##
74
+ # # The `resource_content` method can also be overwritten. Be careful with this because the `show`,
75
+ # # `update` and `destroy` actions will also use this method
76
+ # #
77
+ # # def resource_content
78
+ # # ...
79
+ # # end
80
+ # end
81
+ #
82
+ def edit_resource_content
83
+ resource_content
84
+ end
85
+
86
+ private
87
+
88
+ def edit_resource
89
+ @edit_resource ||= edit_resource_content
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module Actions
7
+ ##
8
+ # Index restful action
9
+ #
10
+ # Usage
11
+ # class ExamplesController < ApplicationController
12
+ # include Undercarriage::Controllers::RestfulConcern
13
+ # end
14
+ #
15
+ module IndexConcern
16
+ extend ActiveSupport::Concern
17
+
18
+ included do
19
+ before_action :index_resources, only: %i[index]
20
+ end
21
+
22
+ ##
23
+ # Index action
24
+ #
25
+ # Usage
26
+ # class ExamplesController < ApplicationController
27
+ # include Undercarriage::Controllers::RestfulConcern
28
+ #
29
+ # ##
30
+ # # This method is only needed if you want to override the action entirely. Otherwise, it is not needed.
31
+ # # Database resources can be accessed as `@index_resources` or `@examples`
32
+ # #
33
+ # # def index
34
+ # # ...
35
+ # # end
36
+ # end
37
+ #
38
+ def index
39
+ respond_with(@index_resources) do |format|
40
+ format.html { render layout: !request.xhr? }
41
+ format.json { render layout: false }
42
+ end
43
+ end
44
+
45
+ protected
46
+
47
+ ##
48
+ # Index restful action
49
+ #
50
+ # Usage
51
+ # class ExamplesController < ApplicationController
52
+ # include Undercarriage::Controllers::RestfulConcern
53
+ #
54
+ # ##
55
+ # # This method is only needed if you want to override the query entirely. Otherwise, it is not needed.
56
+ # # Database resources can be accessed as `@examples`
57
+ # #
58
+ # # def resources_content
59
+ # # ...
60
+ # # end
61
+ #
62
+ # ##
63
+ # # To add authorization through something like Pundit, the following could be used
64
+ # #
65
+ # # def resources_content
66
+ # # super
67
+ # #
68
+ # # authorize @examples
69
+ # # end
70
+ # end
71
+ #
72
+ def resources_content
73
+ resources_query = model_class.all
74
+
75
+ instance_variable_set("@#{instances_name}", resources_query)
76
+ end
77
+
78
+ private
79
+
80
+ def index_resources
81
+ @index_resources ||= resources_content
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,97 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module Actions
7
+ ##
8
+ # New restful action
9
+ #
10
+ # Usage
11
+ # class ExamplesController < ApplicationController
12
+ # include Undercarriage::Controllers::RestfulConcern
13
+ # end
14
+ #
15
+ module NewConcern
16
+ extend ActiveSupport::Concern
17
+
18
+ included do
19
+ before_action :new_resource, only: %i[new]
20
+ end
21
+
22
+ ##
23
+ # New action
24
+ #
25
+ # Usage
26
+ # class ExamplesController < ApplicationController
27
+ # include Undercarriage::Controllers::RestfulConcern
28
+ #
29
+ # ##
30
+ # # This method is only needed if you want to override the action entirely. Otherwise, it is not needed.
31
+ # # Database resources can be accessed as `@new_resource` or `@example`
32
+ # #
33
+ # # def new
34
+ # # ...
35
+ # # end
36
+ # end
37
+ #
38
+ def new
39
+ nested_resource_pre_build
40
+ nested_resource_build
41
+
42
+ respond_with(@new_resource) do |format|
43
+ format.html { render layout: !request.xhr? }
44
+ end
45
+ end
46
+
47
+ protected
48
+
49
+ ##
50
+ # New restful action
51
+ #
52
+ # Usage
53
+ # class ExamplesController < ApplicationController
54
+ # include Undercarriage::Controllers::RestfulConcern
55
+ #
56
+ # ##
57
+ # # This method is only needed if you want to override the query entirely. Otherwise, it is not needed.
58
+ # # Database resources can be accessed as `@example`
59
+ # #
60
+ # # def new_resource_content
61
+ # # ...
62
+ # # end
63
+ #
64
+ # ##
65
+ # # To add authorization through something like Pundit, the following could be used
66
+ # #
67
+ # # def new_resource_content
68
+ # # super
69
+ # #
70
+ # # authorize @example
71
+ # # end
72
+ #
73
+ # ##
74
+ # # The `resource_new_content` method can also be overwritten. This method is meant to share content with
75
+ # # the `create` action
76
+ # #
77
+ # # def resource_new_content
78
+ # # ...
79
+ # # end
80
+ # end
81
+ #
82
+ def new_resource_content
83
+ resource_query = model_class.new
84
+
85
+ instance_variable_set("@#{instance_name}", resource_query)
86
+ end
87
+
88
+ private
89
+
90
+ def new_resource
91
+ @new_resource ||= resource_new_content
92
+ end
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module Actions
7
+ ##
8
+ # Show restful action
9
+ #
10
+ # Usage
11
+ # class ExamplesController < ApplicationController
12
+ # include Undercarriage::Controllers::RestfulConcern
13
+ # end
14
+ #
15
+ module ShowConcern
16
+ extend ActiveSupport::Concern
17
+
18
+ included do
19
+ before_action :show_resource, only: %i[show]
20
+ end
21
+
22
+ ##
23
+ # Show action
24
+ #
25
+ # Usage
26
+ # class ExamplesController < ApplicationController
27
+ # include Undercarriage::Controllers::RestfulConcern
28
+ #
29
+ # ##
30
+ # # This method is only needed if you want to override the action entirely. Otherwise, it is not needed.
31
+ # # Database resources can be accessed as `@show_resource` or `@example`
32
+ # #
33
+ # # def show
34
+ # # ...
35
+ # # end
36
+ # end
37
+ #
38
+ def show
39
+ respond_with(@show_resource) do |format|
40
+ format.html { render layout: !request.xhr? }
41
+ format.json { render layout: false }
42
+ end
43
+ end
44
+
45
+ protected
46
+
47
+ ##
48
+ # Show restful action
49
+ #
50
+ # Usage
51
+ # class ExamplesController < ApplicationController
52
+ # include Undercarriage::Controllers::RestfulConcern
53
+ #
54
+ # ##
55
+ # # This method is only needed if you want to override the query entirely. Otherwise, it is not needed.
56
+ # # Database resources can be accessed as `@example`
57
+ # #
58
+ # # def show_resource_content
59
+ # # ...
60
+ # # end
61
+ #
62
+ # ##
63
+ # # To add authorization through something like Pundit, the following could be used
64
+ # #
65
+ # # def show_resource_content
66
+ # # super
67
+ # #
68
+ # # authorize @example
69
+ # # end
70
+ #
71
+ # ##
72
+ # # The `resource_content` method can also be overwritten. Be careful with this because the `edit`,
73
+ # # `update`, and `destroy` actions will also use this method
74
+ # #
75
+ # # def resource_content
76
+ # # ...
77
+ # # end
78
+ # end
79
+ #
80
+ def show_resource_content
81
+ resource_content
82
+ end
83
+
84
+ private
85
+
86
+ def show_resource
87
+ @show_resource ||= show_resource_content
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module Actions
7
+ ##
8
+ # Update restful action
9
+ #
10
+ # Usage
11
+ # class ExamplesController < ApplicationController
12
+ # include Undercarriage::Controllers::RestfulConcern
13
+ # end
14
+ #
15
+ module UpdateConcern
16
+ extend ActiveSupport::Concern
17
+
18
+ included do
19
+ before_action :update_resource, only: %i[update]
20
+ end
21
+
22
+ ##
23
+ # Update action
24
+ #
25
+ # Usage
26
+ # class ExamplesController < ApplicationController
27
+ # include Undercarriage::Controllers::RestfulConcern
28
+ #
29
+ # ##
30
+ # # This method is only needed if you want to override the action entirely. Otherwise, it is not needed.
31
+ # # Database resources can be accessed as `@update_resource` or `@example`
32
+ # #
33
+ # # def update
34
+ # # ...
35
+ # # end
36
+ # end
37
+ #
38
+ def update
39
+ nested_resource_pre_build
40
+
41
+ if @update_resource.update(update_resource_params)
42
+ after_update_action
43
+
44
+ respond_with(@update_resource) do |format|
45
+ format.html { redirect_to location_after_update }
46
+ end
47
+ else
48
+ nested_resource_build
49
+
50
+ respond_with(@update_resource) do |format|
51
+ format.html { render action: :edit }
52
+ end
53
+ end
54
+ end
55
+
56
+ protected
57
+
58
+ ##
59
+ # Update restful action
60
+ #
61
+ # Usage
62
+ # class ExamplesController < ApplicationController
63
+ # include Undercarriage::Controllers::RestfulConcern
64
+ #
65
+ # ##
66
+ # # This method is only needed if you want to override the query entirely. Otherwise, it is not needed.
67
+ # # Database resources can be accessed as `@example`
68
+ # #
69
+ # # def update_resource_content
70
+ # # ...
71
+ # # end
72
+ #
73
+ # ##
74
+ # # To add authorization through something like Pundit, the following could be used
75
+ # #
76
+ # # def update_resource_content
77
+ # # super
78
+ # #
79
+ # # authorize @example
80
+ # # end
81
+ #
82
+ # ##
83
+ # # The `resource_content` method can also be overwritten. Be careful with this because the `show`,
84
+ # # `edit` and `destroy` actions will also use this method
85
+ # #
86
+ # # def resource_content
87
+ # # ...
88
+ # # end
89
+ # end
90
+ #
91
+ def update_resource_content
92
+ resource_content
93
+ end
94
+
95
+ private
96
+
97
+ def update_resource
98
+ @update_resource ||= update_resource_content
99
+ end
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module LocationAfterConcern
7
+ extend ActiveSupport::Concern
8
+
9
+ protected
10
+
11
+ def location_after_create
12
+ resource_id = @create_resource
13
+
14
+ resource_path(resource_id)
15
+ end
16
+
17
+ def location_after_update
18
+ resource_id = @update_resource
19
+
20
+ resource_path(resource_id)
21
+ end
22
+
23
+ def location_after_destroy
24
+ location_after_save
25
+ end
26
+
27
+ def location_after_save
28
+ resources_path
29
+ end
30
+
31
+ private
32
+
33
+ def resource_path(resource, options = {})
34
+ location_path = [resource_namespace, controller_name_singular].compact
35
+
36
+ send("#{location_path.join('_')}_path", resource, options)
37
+ end
38
+
39
+ def resources_path(options = {})
40
+ location_path = [resource_namespace, controller_name].compact
41
+
42
+ polymorphic_path(location_path, options)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module NamespaceConcern
7
+ extend ActiveSupport::Concern
8
+
9
+ protected
10
+
11
+ ##
12
+ # Resource namespace
13
+ #
14
+ # Best guess for namespace. Take `controller_path` and if there is more than one segment, assume the first is
15
+ # the namespace. When there is one segment, the namespace is `nil`
16
+ #
17
+ # Example
18
+ # # Override method that builds namespace
19
+ # def resource_namespace
20
+ # :admin
21
+ # end
22
+ #
23
+ def resource_namespace
24
+ segments = controller_path.split('/')
25
+
26
+ segments.length > 1 ? segments.first : nil
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module PermittedAttributesConcern
7
+ extend ActiveSupport::Concern
8
+
9
+ protected
10
+
11
+ def permitted_create_attributes
12
+ permitted_attributes_fallback
13
+ end
14
+
15
+ def permitted_update_attributes
16
+ permitted_attributes_fallback
17
+ end
18
+
19
+ def resource_new_params
20
+ action_name == 'new' ? nil : create_resource_params
21
+ end
22
+
23
+ def create_resource_params
24
+ permitted = permitted_create_attributes
25
+
26
+ params.require(resource_scope).permit(permitted)
27
+ end
28
+
29
+ def update_resource_params
30
+ permitted = permitted_update_attributes
31
+
32
+ params.require(resource_scope).permit(permitted)
33
+ end
34
+
35
+ private
36
+
37
+ def permitted_attributes_fallback
38
+ with_method = self.class.instance_methods(false)
39
+ .include?(:permitted_attributes)
40
+
41
+ with_method ? permitted_attributes : []
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ module Restful
6
+ module UtilityConcern
7
+ extend ActiveSupport::Concern
8
+
9
+ protected
10
+
11
+ def controller_name_singular
12
+ controller_name.to_s.singularize
13
+ end
14
+
15
+ def model_name
16
+ controller_name_singular
17
+ end
18
+
19
+ def model_class
20
+ model_name.classify.constantize
21
+ end
22
+
23
+ def instances_name
24
+ controller_name.to_s
25
+ end
26
+
27
+ def instance_name
28
+ model_name
29
+ end
30
+
31
+ def resource_scope
32
+ model_name.to_sym
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ module Controllers
5
+ ##
6
+ # Restful actions
7
+ #
8
+ # Usage
9
+ # class ExamplesController < ApplicationController
10
+ # include Undercarriage::Controllers::RestfulConcern
11
+ # end
12
+ #
13
+ module RestfulConcern
14
+ extend ActiveSupport::Concern
15
+
16
+ included do
17
+ include Undercarriage::Controllers::Restful::LocationAfterConcern
18
+ include Undercarriage::Controllers::Restful::NamespaceConcern
19
+ include Undercarriage::Controllers::Restful::PermittedAttributesConcern
20
+ include Undercarriage::Controllers::Restful::UtilityConcern
21
+ include Undercarriage::Controllers::Restful::Actions::BaseConcern
22
+ include Undercarriage::Controllers::Restful::Actions::IndexConcern
23
+ include Undercarriage::Controllers::Restful::Actions::ShowConcern
24
+ include Undercarriage::Controllers::Restful::Actions::NewConcern
25
+ include Undercarriage::Controllers::Restful::Actions::CreateConcern
26
+ include Undercarriage::Controllers::Restful::Actions::EditConcern
27
+ include Undercarriage::Controllers::Restful::Actions::UpdateConcern
28
+ include Undercarriage::Controllers::Restful::Actions::DestroyConcern
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Undercarriage
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: undercarriage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Freerksen
@@ -40,6 +40,19 @@ files:
40
40
  - lib/undercarriage/controllers/action_concern.rb
41
41
  - lib/undercarriage/controllers/kaminari_concern.rb
42
42
  - lib/undercarriage/controllers/locale_concern.rb
43
+ - lib/undercarriage/controllers/restful/actions/base_concern.rb
44
+ - lib/undercarriage/controllers/restful/actions/create_concern.rb
45
+ - lib/undercarriage/controllers/restful/actions/destroy_concern.rb
46
+ - lib/undercarriage/controllers/restful/actions/edit_concern.rb
47
+ - lib/undercarriage/controllers/restful/actions/index_concern.rb
48
+ - lib/undercarriage/controllers/restful/actions/new_concern.rb
49
+ - lib/undercarriage/controllers/restful/actions/show_concern.rb
50
+ - lib/undercarriage/controllers/restful/actions/update_concern.rb
51
+ - lib/undercarriage/controllers/restful/location_after_concern.rb
52
+ - lib/undercarriage/controllers/restful/namespace_concern.rb
53
+ - lib/undercarriage/controllers/restful/permitted_attributes_concern.rb
54
+ - lib/undercarriage/controllers/restful/utility_concern.rb
55
+ - lib/undercarriage/controllers/restful_concern.rb
43
56
  - lib/undercarriage/railtie.rb
44
57
  - lib/undercarriage/version.rb
45
58
  homepage: https://github.com/dfreerksen/undercarriage