sweet_actions 0.1.5 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/.DS_Store +0 -0
  3. data/README.md +216 -175
  4. data/lib/generators/sweet_actions/templates/base_action.rb +4 -0
  5. data/lib/generators/sweet_actions/templates/collect_action.rb +7 -8
  6. data/lib/generators/sweet_actions/templates/create_action.rb +10 -11
  7. data/lib/generators/sweet_actions/templates/destroy_action.rb +10 -11
  8. data/lib/generators/sweet_actions/templates/show_action.rb +7 -8
  9. data/lib/generators/sweet_actions/templates/update_action.rb +11 -12
  10. data/lib/sweet_actions/.DS_Store +0 -0
  11. data/lib/sweet_actions/action.rb +32 -0
  12. data/lib/sweet_actions/action_factory.rb +4 -2
  13. data/lib/sweet_actions/{authorization_concerns.rb → authorization.rb} +2 -2
  14. data/lib/sweet_actions/controller_concerns.rb +0 -1
  15. data/lib/sweet_actions/exceptions.rb +13 -0
  16. data/lib/sweet_actions/json/.DS_Store +0 -0
  17. data/lib/sweet_actions/json/base_action.rb +41 -0
  18. data/lib/sweet_actions/json/collect_action.rb +13 -0
  19. data/lib/sweet_actions/json/create_action.rb +18 -0
  20. data/lib/sweet_actions/json/destroy_action.rb +13 -0
  21. data/lib/sweet_actions/json/show_action.rb +13 -0
  22. data/lib/sweet_actions/json/update_action.rb +18 -0
  23. data/lib/sweet_actions/{rest_concerns.rb → resource.rb} +3 -5
  24. data/lib/sweet_actions/rest/.DS_Store +0 -0
  25. data/lib/sweet_actions/rest/base.rb +8 -0
  26. data/lib/sweet_actions/rest/collect.rb +14 -0
  27. data/lib/sweet_actions/rest/create.rb +14 -0
  28. data/lib/sweet_actions/rest/destroy.rb +25 -0
  29. data/lib/sweet_actions/rest/find.rb +13 -0
  30. data/lib/sweet_actions/rest/multiple.rb +9 -0
  31. data/lib/sweet_actions/rest/read.rb +19 -0
  32. data/lib/sweet_actions/rest/save.rb +54 -0
  33. data/lib/sweet_actions/rest/show.rb +8 -0
  34. data/lib/sweet_actions/rest/singular.rb +9 -0
  35. data/lib/sweet_actions/rest/update.rb +15 -0
  36. data/lib/sweet_actions/{rest_serializer_concerns.rb → serialize.rb} +12 -3
  37. data/lib/sweet_actions/version.rb +1 -1
  38. data/lib/sweet_actions.rb +27 -13
  39. data/sweet_actions.gemspec +3 -0
  40. metadata +55 -14
  41. data/lib/generators/rails/resource_override.rb +0 -10
  42. data/lib/sweet_actions/api_action.rb +0 -63
  43. data/lib/sweet_actions/collect_action.rb +0 -11
  44. data/lib/sweet_actions/create_action.rb +0 -10
  45. data/lib/sweet_actions/destroy_action.rb +0 -19
  46. data/lib/sweet_actions/read_concerns.rb +0 -14
  47. data/lib/sweet_actions/save_concerns.rb +0 -51
  48. data/lib/sweet_actions/show_action.rb +0 -9
  49. data/lib/sweet_actions/update_action.rb +0 -11
@@ -0,0 +1,41 @@
1
+ module SweetActions
2
+ module JSON
3
+ class BaseAction < Action
4
+ include Serialize
5
+
6
+ attr_reader :response_data, :response_code
7
+
8
+ def perform_action
9
+ run_action = Proc.new do
10
+ @response_data = action
11
+ end
12
+
13
+ debug_mode? ? run_action.call : fail_gracefully(run_action)
14
+ controller.render status: response_code, json: response_data
15
+ end
16
+
17
+ private
18
+
19
+ def fail_gracefully(proc)
20
+ @response_code = '200'
21
+ begin
22
+ proc.call
23
+ rescue Exceptions::NotAuthorized => e
24
+ @response_code = '401'
25
+ @response_data = {
26
+ message: 'not authorized'
27
+ }
28
+ rescue StandardError => e
29
+ @response_code = '500'
30
+ @response_data = {
31
+ server_error: e.message
32
+ }
33
+ end
34
+ end
35
+
36
+ def debug_mode?
37
+ ENV['SWEET_ACTION_DEBUG_MODE'] == true
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ module SweetActions
2
+ module JSON
3
+ class CollectAction < BaseAction
4
+ include REST::Collect
5
+
6
+ private
7
+
8
+ def respond
9
+ serialize
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module SweetActions
2
+ module JSON
3
+ class CreateAction < BaseAction
4
+ include REST::Create
5
+
6
+ private
7
+
8
+ def respond_with_success
9
+ serialize
10
+ end
11
+
12
+ def respond_with_error
13
+ @response_code = '422'
14
+ serialize_errors
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ module SweetActions
2
+ module JSON
3
+ class DestroyAction < BaseAction
4
+ include REST::Destroy
5
+
6
+ private
7
+
8
+ def respond
9
+ serialize
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module SweetActions
2
+ module JSON
3
+ class ShowAction < BaseAction
4
+ include REST::Show
5
+
6
+ private
7
+
8
+ def respond
9
+ serialize
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module SweetActions
2
+ module JSON
3
+ class UpdateAction < BaseAction
4
+ include REST::Update
5
+
6
+ private
7
+
8
+ def respond_with_success
9
+ serialize
10
+ end
11
+
12
+ def respond_with_error
13
+ @response_code = '422'
14
+ serialize_errors
15
+ end
16
+ end
17
+ end
18
+ end
@@ -1,7 +1,5 @@
1
1
  module SweetActions
2
- module RestConcerns
3
- include RestSerializerConcerns
4
-
2
+ module Resource
5
3
  attr_reader :resource_name, :resource
6
4
 
7
5
  private
@@ -11,12 +9,12 @@ module SweetActions
11
9
  end
12
10
 
13
11
  def set_resource
14
- raise "set_resource method must be implemented in #{self.class.name} class since it includes RestConcerns"
12
+ raise "set_resource method must be implemented in #{self.class.name} class since it includes SweetActions::Resource"
15
13
  end
16
14
 
17
15
  def resource
18
16
  return @resource if defined?("@resource")
19
- raise "@resource must be set for #{self.class.name} since it includes RestConcerns"
17
+ raise "@resource must be set for #{self.class.name} since it includes SweetActions::Resource"
20
18
  end
21
19
 
22
20
  def resource_class
Binary file
@@ -0,0 +1,8 @@
1
+ module SweetActions
2
+ module REST
3
+ module Base
4
+ include SweetActions::Authorization
5
+ include SweetActions::Resource
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,14 @@
1
+ module SweetActions
2
+ module REST
3
+ module Collect
4
+ include Read
5
+ include Multiple
6
+
7
+ private
8
+
9
+ def set_resource
10
+ resource_class.all
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ module SweetActions
2
+ module REST
3
+ module Create
4
+ include Save
5
+ include Singular
6
+
7
+ private
8
+
9
+ def set_resource
10
+ resource_class.new(resource_params)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,25 @@
1
+ module SweetActions
2
+ module REST
3
+ module Destroy
4
+ include Base
5
+ include Find
6
+
7
+ private
8
+
9
+ def action
10
+ @resource = set_resource
11
+ authorize
12
+ destroy
13
+ respond
14
+ end
15
+
16
+ def destroy
17
+ resource.destroy
18
+ end
19
+
20
+ def respond
21
+ raise "respond method must be implemented by #{self.class.name} since it includes SweetActions::REST::Read"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module SweetActions
2
+ module REST
3
+ module Find
4
+ include Singular
5
+
6
+ private
7
+
8
+ def set_resource
9
+ resource_class.find(params[:id])
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ module SweetActions
2
+ module REST
3
+ module Multiple
4
+ def root_key
5
+ plural_key
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,19 @@
1
+ module SweetActions
2
+ module REST
3
+ module Read
4
+ include Base
5
+
6
+ private
7
+
8
+ def action
9
+ @resource = set_resource
10
+ authorize
11
+ respond
12
+ end
13
+
14
+ def respond
15
+ raise "respond method must be implemented by #{self.class.name} since it includes SweetActions::REST::Read"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,54 @@
1
+ module SweetActions
2
+ module REST
3
+ module Save
4
+ include Base
5
+
6
+ private
7
+
8
+ def action
9
+ @resource = set_resource
10
+ authorize
11
+ if validate_and_save
12
+ after_save
13
+ respond_with_success
14
+ else
15
+ after_fail
16
+ respond_with_error
17
+ end
18
+ end
19
+
20
+ def validate_and_save
21
+ valid? && save
22
+ end
23
+
24
+ def respond_with_success
25
+ raise "respond_with_success method must be implemented by #{self.class.name} since it includes SweetActions::REST::SaveConcerns"
26
+ end
27
+
28
+ def respond_with_error
29
+ raise "respond_with_error method must be implemented by #{self.class.name} since it includes SaveConcerns"
30
+ end
31
+
32
+ def valid?
33
+ # optional hook for subclasses
34
+ true
35
+ end
36
+
37
+ def save
38
+ resource.save
39
+ end
40
+
41
+ def save
42
+ raise "save method must be implemented by #{self.class.name} since it includes SaveConcerns"
43
+ end
44
+
45
+ def after_save
46
+ # hook
47
+ end
48
+
49
+ def after_fail
50
+ # hook
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ module SweetActions
2
+ module REST
3
+ module Show
4
+ include Read
5
+ include Find
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module SweetActions
2
+ module REST
3
+ module Singular
4
+ def root_key
5
+ singular_key
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ module SweetActions
2
+ module REST
3
+ module Update
4
+ include Save
5
+ include Find
6
+
7
+ private
8
+
9
+ def save
10
+ resource.attributes = resource_params
11
+ resource.save
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,9 +1,13 @@
1
1
  module SweetActions
2
- module RestSerializerConcerns
2
+ module Serialize
3
3
  private
4
4
 
5
5
  def root_key
6
- raise "root_key method must be implemented in #{self.class.name} since it includes RestSerializerConcerns"
6
+ raise "root_key method must be implemented in #{self.class.name} since it includes SweetActions::Serialize"
7
+ end
8
+
9
+ def resource
10
+ raise "resource method must be implemented in #{self.class.name} since it includes SweetActions::Serialize"
7
11
  end
8
12
 
9
13
  def serialize
@@ -27,12 +31,17 @@ module SweetActions
27
31
  }
28
32
  end
29
33
 
34
+ def map_base_to__error(error_obj)
35
+ error_obj[:_error] = error_obj.delete(:base) if error_obj.key? :base
36
+ error_obj
37
+ end
38
+
30
39
  def errors
31
40
  resource.respond_to?(:errors) && resource.errors
32
41
  end
33
42
 
34
43
  def serialized_attributes
35
- return resource unless serialized_resource
44
+ return resource.as_json unless serialized_resource
36
45
  serialized_resource.serializer_instance
37
46
  end
38
47
 
@@ -1,3 +1,3 @@
1
1
  module SweetActions
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.1"
3
3
  end
data/lib/sweet_actions.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'active_support/all'
2
+
1
3
  module SweetActions
2
4
  class << self
3
5
  def configuration
@@ -19,21 +21,33 @@ require 'sweet_actions/exceptions'
19
21
 
20
22
  # base classes
21
23
  require 'sweet_actions/action_factory'
22
- require 'sweet_actions/api_action'
24
+ require 'sweet_actions/action'
23
25
 
24
26
  # concerns
25
- require 'sweet_actions/rest_serializer_concerns'
26
- require 'sweet_actions/rest_concerns'
27
- require 'sweet_actions/authorization_concerns'
28
- require 'sweet_actions/save_concerns'
29
- require 'sweet_actions/read_concerns'
30
-
31
- # actions
32
- require 'sweet_actions/collect_action'
33
- require 'sweet_actions/create_action'
34
- require 'sweet_actions/update_action'
35
- require 'sweet_actions/show_action'
36
- require 'sweet_actions/destroy_action'
27
+ require 'sweet_actions/authorization'
28
+ require 'sweet_actions/resource'
29
+ require 'sweet_actions/serialize'
30
+
31
+ # rest
32
+ require 'sweet_actions/rest/base'
33
+ require 'sweet_actions/rest/singular'
34
+ require 'sweet_actions/rest/multiple'
35
+ require 'sweet_actions/rest/find'
36
+ require 'sweet_actions/rest/save'
37
+ require 'sweet_actions/rest/read'
38
+ require 'sweet_actions/rest/collect'
39
+ require 'sweet_actions/rest/create'
40
+ require 'sweet_actions/rest/update'
41
+ require 'sweet_actions/rest/show'
42
+ require 'sweet_actions/rest/destroy'
43
+
44
+ # json
45
+ require 'sweet_actions/json/base_action'
46
+ require 'sweet_actions/json/collect_action'
47
+ require 'sweet_actions/json/create_action'
48
+ require 'sweet_actions/json/update_action'
49
+ require 'sweet_actions/json/show_action'
50
+ require 'sweet_actions/json/destroy_action'
37
51
 
38
52
  # helpers
39
53
  require 'sweet_actions/controller_concerns'
@@ -30,7 +30,10 @@ Gem::Specification.new do |spec|
30
30
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
31
  spec.require_paths = ["lib"]
32
32
 
33
+ spec.add_dependency "activesupport"
34
+
33
35
  spec.add_development_dependency "bundler", "~> 1.14"
34
36
  spec.add_development_dependency "rake", "~> 10.0"
35
37
  spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency "pry"
36
39
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sweet_actions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Francis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-30 00:00:00.000000000 Z
11
+ date: 2017-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  description: Each controller action is its own class, making it possible to abstract
56
84
  tasks like authorization, deserialization, serialization, and callbacks.
57
85
  email:
@@ -60,6 +88,7 @@ executables: []
60
88
  extensions: []
61
89
  extra_rdoc_files: []
62
90
  files:
91
+ - ".DS_Store"
63
92
  - ".gitignore"
64
93
  - ".rspec"
65
94
  - ".travis.yml"
@@ -75,7 +104,6 @@ files:
75
104
  - lib/generators/.DS_Store
76
105
  - lib/generators/rails/.DS_Store
77
106
  - lib/generators/rails/actions_generator.rb
78
- - lib/generators/rails/resource_override.rb
79
107
  - lib/generators/rails/templates/.DS_Store
80
108
  - lib/generators/rails/templates/collect.rb.erb
81
109
  - lib/generators/rails/templates/create.rb.erb
@@ -83,6 +111,7 @@ files:
83
111
  - lib/generators/rails/templates/show.rb.erb
84
112
  - lib/generators/rails/templates/update.rb.erb
85
113
  - lib/generators/sweet_actions/install_generator.rb
114
+ - lib/generators/sweet_actions/templates/base_action.rb
86
115
  - lib/generators/sweet_actions/templates/collect_action.rb
87
116
  - lib/generators/sweet_actions/templates/create_action.rb
88
117
  - lib/generators/sweet_actions/templates/destroy_action.rb
@@ -91,23 +120,35 @@ files:
91
120
  - lib/generators/sweet_actions/templates/update_action.rb
92
121
  - lib/sweet_actions.rb
93
122
  - lib/sweet_actions/.DS_Store
123
+ - lib/sweet_actions/action.rb
94
124
  - lib/sweet_actions/action_factory.rb
95
- - lib/sweet_actions/api_action.rb
96
- - lib/sweet_actions/authorization_concerns.rb
97
- - lib/sweet_actions/collect_action.rb
125
+ - lib/sweet_actions/authorization.rb
98
126
  - lib/sweet_actions/configuration.rb
99
127
  - lib/sweet_actions/controller_concerns.rb
100
- - lib/sweet_actions/create_action.rb
101
- - lib/sweet_actions/destroy_action.rb
102
128
  - lib/sweet_actions/exceptions.rb
129
+ - lib/sweet_actions/json/.DS_Store
130
+ - lib/sweet_actions/json/base_action.rb
131
+ - lib/sweet_actions/json/collect_action.rb
132
+ - lib/sweet_actions/json/create_action.rb
133
+ - lib/sweet_actions/json/destroy_action.rb
134
+ - lib/sweet_actions/json/show_action.rb
135
+ - lib/sweet_actions/json/update_action.rb
103
136
  - lib/sweet_actions/railtie.rb
104
- - lib/sweet_actions/read_concerns.rb
105
- - lib/sweet_actions/rest_concerns.rb
106
- - lib/sweet_actions/rest_serializer_concerns.rb
137
+ - lib/sweet_actions/resource.rb
138
+ - lib/sweet_actions/rest/.DS_Store
139
+ - lib/sweet_actions/rest/base.rb
140
+ - lib/sweet_actions/rest/collect.rb
141
+ - lib/sweet_actions/rest/create.rb
142
+ - lib/sweet_actions/rest/destroy.rb
143
+ - lib/sweet_actions/rest/find.rb
144
+ - lib/sweet_actions/rest/multiple.rb
145
+ - lib/sweet_actions/rest/read.rb
146
+ - lib/sweet_actions/rest/save.rb
147
+ - lib/sweet_actions/rest/show.rb
148
+ - lib/sweet_actions/rest/singular.rb
149
+ - lib/sweet_actions/rest/update.rb
107
150
  - lib/sweet_actions/routes_helpers.rb
108
- - lib/sweet_actions/save_concerns.rb
109
- - lib/sweet_actions/show_action.rb
110
- - lib/sweet_actions/update_action.rb
151
+ - lib/sweet_actions/serialize.rb
111
152
  - lib/sweet_actions/version.rb
112
153
  - sweet_actions-0.1.0.gem
113
154
  - sweet_actions.gemspec
@@ -1,10 +0,0 @@
1
- require 'rails/generators'
2
- require 'rails/generators/rails/resource/resource_generator'
3
-
4
- module Rails
5
- module Generators
6
- class ResourceGenerator
7
- hook_for :decanter, default: true, boolean: true
8
- end
9
- end
10
- end
@@ -1,63 +0,0 @@
1
- module SweetActions
2
- class ApiAction
3
- attr_reader :controller, :response_data, :response_code
4
-
5
- def initialize(controller, options = {})
6
- @controller = controller
7
- after_init(options)
8
- end
9
-
10
- def perform_action
11
- run_action = Proc.new do
12
- @response_data = action
13
- end
14
-
15
- debug_mode? ? run_action.call : fail_gracefully(run_action)
16
- end
17
-
18
- private
19
-
20
- def after_init(options); end
21
-
22
- def fail_gracefully(proc)
23
- @response_code = '200'
24
- begin
25
- proc.call
26
- rescue Exceptions::NotAuthorized => e
27
- @response_code = '401'
28
- @response_data = {
29
- message: 'not authorized'
30
- }
31
- rescue StandardError => e
32
- @response_code = '500'
33
- @response_data = {
34
- server_error: e.message
35
- }
36
- end
37
- end
38
-
39
- def debug_mode?
40
- ENV['SWEET_ACTION_DEBUG_MODE'] == true
41
- end
42
-
43
- def action
44
- raise "action method is required for #{self.class.name} because it inherits from ApiAction"
45
- end
46
-
47
- def path_parameters
48
- @path_parameters ||= env['action_dispatch.request.path_parameters']
49
- end
50
-
51
- def request
52
- @request ||= controller.request
53
- end
54
-
55
- def params
56
- @params ||= controller.params
57
- end
58
-
59
- def env
60
- request.env
61
- end
62
- end
63
- end
@@ -1,11 +0,0 @@
1
- module SweetActions
2
- class CollectAction < ApiAction
3
- include ReadConcerns
4
-
5
- private
6
-
7
- def root_key
8
- plural_key
9
- end
10
- end
11
- end
@@ -1,10 +0,0 @@
1
- module SweetActions
2
- class CreateAction < ApiAction
3
- include RestConcerns
4
- include SaveConcerns
5
-
6
- def save
7
- resource.save
8
- end
9
- end
10
- end