undercarriage 0.1.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (25) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +42 -4
  3. data/lib/undercarriage.rb +15 -3
  4. data/lib/undercarriage/controllers/action_concern.rb +18 -0
  5. data/lib/undercarriage/controllers/kaminari_concern.rb +11 -0
  6. data/lib/undercarriage/controllers/locale_concern.rb +7 -1
  7. data/lib/undercarriage/controllers/restful/actions/base_concern.rb +120 -0
  8. data/lib/undercarriage/controllers/restful/actions/create_concern.rb +107 -0
  9. data/lib/undercarriage/controllers/restful/actions/destroy_concern.rb +97 -0
  10. data/lib/undercarriage/controllers/restful/actions/edit_concern.rb +94 -0
  11. data/lib/undercarriage/controllers/restful/actions/index_concern.rb +85 -0
  12. data/lib/undercarriage/controllers/restful/actions/new_concern.rb +96 -0
  13. data/lib/undercarriage/controllers/restful/actions/show_concern.rb +91 -0
  14. data/lib/undercarriage/controllers/restful/actions/update_concern.rb +105 -0
  15. data/lib/undercarriage/controllers/restful/flash_concern.rb +112 -0
  16. data/lib/undercarriage/controllers/restful/location_after_concern.rb +79 -0
  17. data/lib/undercarriage/controllers/restful/namespace_concern.rb +41 -0
  18. data/lib/undercarriage/controllers/restful/permitted_attributes_concern.rb +109 -0
  19. data/lib/undercarriage/controllers/restful/utility_concern.rb +74 -0
  20. data/lib/undercarriage/controllers/restful_concern.rb +34 -0
  21. data/lib/undercarriage/models/published_concern.rb +120 -0
  22. data/lib/undercarriage/version.rb +4 -1
  23. metadata +17 -4
  24. data/lib/tasks/undercarriage_tasks.rake +0 -6
  25. data/lib/undercarriage/railtie.rb +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ec3241ab6e053eb78aa66e3aed56473b299ebef9c48c08c415a7f9c04b0eb70
4
- data.tar.gz: c0e0393b7f2d1c383eadb22af21ac440fe5651eea335f71241289918f754b547
3
+ metadata.gz: b12f7949572ff816f2b669ca0a6d94bfe8bce39ba6a744092b16ceedfda4cdac
4
+ data.tar.gz: 651b54c29ed119c043ca0542dccadfc7e6f60ffd471d86cbe1988c7f377d7c90
5
5
  SHA512:
6
- metadata.gz: 423946c74cd8e1f5d313627933fd3f6a27d663e0e580d78ae5d56f202117d8348d56eb42315498e32878e0bdb39e30f7e48c021e4207b2221197c0d0edf7c73b
7
- data.tar.gz: 5613f293beb7bac032d58f7aed7f686f67e6c8219ba52708690e591e874246e4fd86a60c91dc98c04a518a739b2e883dc7fc8f1ed1e47f8955bef20e955cc270
6
+ metadata.gz: de197de22b4c1cada28489687edeea986d628a7dc3245932e0b33565e9a4061c74dea37b7f5981e24e4348262d22d8df1df026b204edb008c86665299098b8fc
7
+ data.tar.gz: da32a07d87b9eccfc756de524410800fc36fa1b63814bb1ea5defa95bf870f854824c98ccae3fc3f8dbbd0192bb97f56e9a3f7921e5448a9a90746dd06ef7e81
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  **\*Undercarriage is currently under development. It is not ready for production use.\***
4
4
 
5
+ [![CircleCI](https://circleci.com/gh/dfreerksen/undercarriage.svg?style=shield)](https://circleci.com/gh/dfreerksen/undercarriage)
6
+ [![Maintainability](https://api.codeclimate.com/v1/badges/d15980a534f8db936469/maintainability)](https://codeclimate.com/github/dfreerksen/undercarriage/maintainability)
7
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/d15980a534f8db936469/test_coverage)](https://codeclimate.com/github/dfreerksen/undercarriage/test_coverage)
8
+
5
9
  Undercarriage is a set of concerns to add to your application to trim some of the fat from controllers and models.
6
10
 
7
11
  ## Requirements
@@ -13,13 +17,13 @@ Undercarriage is a set of concerns to add to your application to trim some of th
13
17
 
14
18
  Add to your application's Gemfile
15
19
 
16
- ```ruby
17
- gem 'undercarriage', '~> 0.1'
20
+ ```
21
+ gem 'undercarriage', '~> 0.4'
18
22
  ```
19
23
 
20
24
  Run the bundle command
21
25
 
22
- ```bash
26
+ ```
23
27
  $ bundle install
24
28
  ```
25
29
 
@@ -31,9 +35,43 @@ TODO
31
35
 
32
36
  * [ ] Allow a way to set locale instead of relying on browser preferred language in `Undercarriage::Controllers::LocaleConcern`
33
37
 
38
+ ## Testing
39
+
40
+ Run tests with one of the following
41
+
42
+ ```
43
+ $ bin/test
44
+ $ bundle exec rspec spec
45
+ ```
46
+
34
47
  ## Code Analysis
35
48
 
36
- TODO
49
+ Various tools are used to ensure code is linted and formatted correctly.
50
+
51
+ ### RuboCop
52
+
53
+ [RuboCop](https://github.com/bbatsov/rubocop) is a Ruby static code analyzer.
54
+
55
+ ```
56
+ $ rubocop
57
+ ```
58
+
59
+ ## Documentation
60
+
61
+ [Yard](https://github.com/lsegal/yard) is used to generate documentation. [Online documentation is available](http://www.rubydoc.info/github/dfreerksen/undercarriage/master)
62
+
63
+ Build the documentation with one of the following
64
+
65
+ ```
66
+ $ yard
67
+ $ yard doc
68
+ ```
69
+
70
+ Build the documentation and list all undocumented objects
71
+
72
+ ```
73
+ $ yard stats --list-undoc
74
+ ```
37
75
 
38
76
  ## License
39
77
 
data/lib/undercarriage.rb CHANGED
@@ -3,8 +3,21 @@
3
3
  require 'undercarriage/controllers/action_concern'
4
4
  require 'undercarriage/controllers/kaminari_concern'
5
5
  require 'undercarriage/controllers/locale_concern'
6
-
7
- require 'undercarriage/railtie'
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/flash_concern'
16
+ require 'undercarriage/controllers/restful/location_after_concern'
17
+ require 'undercarriage/controllers/restful/namespace_concern'
18
+ require 'undercarriage/controllers/restful/permitted_attributes_concern'
19
+ require 'undercarriage/controllers/restful/utility_concern'
20
+ require 'undercarriage/models/published_concern'
8
21
 
9
22
  ##
10
23
  # Undercarriage
@@ -12,5 +25,4 @@ require 'undercarriage/railtie'
12
25
  # Undercarriage is a set of concerns to add to your application to trim some of the fat from controllers and models.
13
26
  #
14
27
  module Undercarriage
15
- # Your code goes here...
16
28
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Undercarriage
4
+ # :nodoc:
4
5
  module Controllers
5
6
  ##
6
7
  # Action helpers
@@ -209,22 +210,39 @@ module Undercarriage
209
210
 
210
211
  protected
211
212
 
213
+ ##
214
+ # Action symbol
215
+ #
216
+ # Take `action_name` (string) and turn it into a symbol
217
+ #
212
218
  def action
213
219
  action_name.to_sym
214
220
  end
215
221
 
222
+ ##
223
+ # Collection actions
224
+ #
216
225
  def collection_actions
217
226
  %i[index]
218
227
  end
219
228
 
229
+ ##
230
+ # Member actions
231
+ #
220
232
  def member_actions
221
233
  %i[edit show update]
222
234
  end
223
235
 
236
+ ##
237
+ # Create actions
238
+ #
224
239
  def create_actions
225
240
  %i[create new]
226
241
  end
227
242
 
243
+ ##
244
+ # Update actions
245
+ #
228
246
  def update_actions
229
247
  %i[edit update]
230
248
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Undercarriage
4
+ # :nodoc:
4
5
  module Controllers
5
6
  ##
6
7
  # Kaminari pagination
@@ -61,10 +62,20 @@ module Undercarriage
61
62
 
62
63
  protected
63
64
 
65
+ ##
66
+ # Items per page key
67
+ #
68
+ # Query param to be used to identify count to be returned
69
+ #
64
70
  def per_page_key
65
71
  :per
66
72
  end
67
73
 
74
+ ##
75
+ # Page numberkey
76
+ #
77
+ # Query param to be used to identify page offset
78
+ #
68
79
  def page_num_key
69
80
  Kaminari.config.param_name
70
81
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Undercarriage
4
+ # :nodoc:
4
5
  module Controllers
5
6
  ##
6
7
  # Identify locale for translations
@@ -65,6 +66,11 @@ module Undercarriage
65
66
 
66
67
  protected
67
68
 
69
+ ##
70
+ # Set I18n locale
71
+ #
72
+ # Set I18n locale for the request
73
+ #
68
74
  def identify_locale(&action)
69
75
  I18n.with_locale(first_available_locale, &action)
70
76
  end
@@ -75,7 +81,7 @@ module Undercarriage
75
81
  preferred_locales = (accepted_languages_header << I18n.default_locale.to_s).uniq
76
82
  available_locales = I18n.available_locales.map(&:to_s)
77
83
 
78
- preferred_locales.intersection(available_locales).first
84
+ (preferred_locales & available_locales).first
79
85
  end
80
86
 
81
87
  def accepted_languages_header
@@ -0,0 +1,120 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ # :nodoc:
5
+ module Controllers
6
+ # :nodoc:
7
+ module Restful
8
+ # :nodoc:
9
+ module Actions
10
+ ##
11
+ # Base restful action
12
+ #
13
+ # Usage
14
+ # class ExamplesController < ApplicationController
15
+ # include Undercarriage::Controllers::Restful::Actions::BaseConcern
16
+ # end
17
+ #
18
+ module BaseConcern
19
+ extend ActiveSupport::Concern
20
+
21
+ protected
22
+
23
+ ##
24
+ # New content action
25
+ #
26
+ # Decide what content to load based on action name
27
+ #
28
+ def resource_new_content
29
+ action_name == 'new' ? new_resource_content : create_resource_content
30
+ end
31
+
32
+ ##
33
+ # Resource action
34
+ #
35
+ # Used for `show`, `edit`, `update` and `destroy` actions unless overwritten
36
+ #
37
+ # Usage
38
+ # class ExamplesController < ApplicationController
39
+ # include Undercarriage::Controllers::RestfulConcern
40
+ #
41
+ # ##
42
+ # # This method is only needed if you want to override the query entirely. Otherwise, it is not needed.
43
+ # # Database resources can be accessed as `@example`
44
+ # #
45
+ # # def resource_content
46
+ # # ...
47
+ # # end
48
+ #
49
+ # ##
50
+ # # To add authorization through something like Pundit, the following could be used
51
+ # #
52
+ # # def resource_content
53
+ # # super
54
+ # #
55
+ # # authorize @example
56
+ # # end
57
+ # end
58
+ #
59
+ def resource_content
60
+ resource_id = params.fetch(:id)
61
+ resource_query = model_class.find(resource_id)
62
+
63
+ instance_variable_set("@#{instance_name}", resource_query)
64
+ end
65
+
66
+ ##
67
+ # Build nested association before action
68
+ #
69
+ # Called first thing from `new`, `create`, `edit` and `update` actions. Meant to build a basic resource before
70
+ # the action is evaluated
71
+ #
72
+ # Usage
73
+ # nested_resource_pre_build
74
+ # @example.build_image if @example.image.blank?
75
+ # end
76
+ #
77
+ def nested_resource_pre_build; end
78
+
79
+ ##
80
+ # Build nested association for action
81
+ #
82
+ # Similar to `nested_resource_pre_build` but called in different places. For the `new` and `edit` actions, it
83
+ # is called right after `nested_resource_pre_build`. For the `create` and `update` actions, it is only called
84
+ # after validation has failed and before the view is rendered.
85
+ #
86
+ # Usage
87
+ # nested_resource_build
88
+ # @example.build_image if @example.image.blank?
89
+ # end
90
+ #
91
+ def nested_resource_build; end
92
+
93
+ ##
94
+ # After create callback
95
+ #
96
+ # Callback after `create` action has created the record.
97
+ #
98
+ # Usage
99
+ # after_create_action
100
+ # ExampleJob.perform_later(@example.id)
101
+ # end
102
+ #
103
+ def after_create_action; end
104
+
105
+ ##
106
+ # After update callback
107
+ #
108
+ # Callback after `update` action has updated the record.
109
+ #
110
+ # Usage
111
+ # after_update_action
112
+ # ExampleJob.perform_later(@example.id)
113
+ # end
114
+ #
115
+ def after_update_action; end
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Undercarriage
4
+ # :nodoc:
5
+ module Controllers
6
+ # :nodoc:
7
+ module Restful
8
+ # :nodoc:
9
+ module Actions
10
+ ##
11
+ # Create restful action
12
+ #
13
+ # Usage
14
+ # class ExamplesController < ApplicationController
15
+ # include Undercarriage::Controllers::RestfulConcern
16
+ # end
17
+ #
18
+ module CreateConcern
19
+ extend ActiveSupport::Concern
20
+
21
+ included do
22
+ before_action :create_resource, only: %i[create]
23
+ end
24
+
25
+ ##
26
+ # Create action
27
+ #
28
+ # Usage
29
+ # class ExamplesController < ApplicationController
30
+ # include Undercarriage::Controllers::RestfulConcern
31
+ #
32
+ # ##
33
+ # # This method is only needed if you want to override the action entirely. Otherwise, it is not needed.
34
+ # # Database resources can be accessed as `@create_resource` or `@example`
35
+ # #
36
+ # # def create
37
+ # # ...
38
+ # # end
39
+ # end
40
+ #
41
+ def create
42
+ nested_resource_pre_build
43
+
44
+ if @create_resource.save
45
+ after_create_action
46
+
47
+ flash[flash_status_type] = flash_created_message
48
+
49
+ redirect_to location_after_create
50
+ else
51
+ nested_resource_build
52
+
53
+ render :new, status: :unprocessable_entity
54
+ end
55
+ end
56
+
57
+ protected
58
+
59
+ ##
60
+ # Create restful action
61
+ #
62
+ # Usage
63
+ # class ExamplesController < ApplicationController
64
+ # include Undercarriage::Controllers::RestfulConcern
65
+ #
66
+ # ##
67
+ # # This method is only needed if you want to override the query entirely. Otherwise, it is not needed.
68
+ # # Database resources can be accessed as `@example`
69
+ # #
70
+ # # def create_resource_content
71
+ # # ...
72
+ # # end
73
+ #
74
+ # ##
75
+ # # To add authorization through something like Pundit, the following could be used
76
+ # #
77
+ # # def create_resource_content
78
+ # # super
79
+ # #
80
+ # # authorize @example
81
+ # # end
82
+ #
83
+ # ##
84
+ # # The `resource_new_content` method can also be overwritten. This method is meant to share content with
85
+ # # the `new` action
86
+ # #
87
+ # # def resource_new_content
88
+ # # ...
89
+ # # end
90
+ # end
91
+ #
92
+ def create_resource_content
93
+ resource_query = model_class.new(create_resource_params)
94
+
95
+ instance_variable_set("@#{instance_name}", resource_query)
96
+ end
97
+
98
+ private
99
+
100
+ def create_resource
101
+ @create_resource ||= resource_new_content
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end