cyrax 0.5.13 → 0.6.0.beta

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,15 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4a305051a8e89e95a2fccd98ef8c2ac05679189c
4
- data.tar.gz: f9f3cb1822a1f850ff9a55e1f0d492579ff73ad8
5
- SHA512:
6
- metadata.gz: d8e50444c96e653ab43c86463816cc5d7ab4593ef7f27c08b211c1193a87fb2e773ba620ff26d60c91c6e297857f76ea2e8d8d8631626660b7f77f57e4dbfea2
7
- data.tar.gz: dcbfc8d93cfdd24c5a4d080be8a4db16776e9e49bc45524f74fe5dce820473c744f29513479296b304ebe7b80f08a7880126d2215f67899d234319b3974666a2
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MzJlYTg3MDkzYjVjMzJhODVmODAwNTllMzVlM2Q2Y2Q4MDQ2NjUyNQ==
5
+ data.tar.gz: !binary |-
6
+ ODdlMjE1MGIxMzRkZTc1MmExMTFkZWUyYThlZGViM2RiNjEwYjZmZQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YzE2M2IzNjRmZTAzOGY2MjNlMmJhYzg0NzM2YjFhYjFhZTljNTZlYTMyMTE5
10
+ YjE0OGQ2ZjE3Y2IzYWIxMTg2MTA2MjFmMzdlYjhhNGFkN2IxOGQ1YzFlNWVi
11
+ YmY1ODAyM2FkNjdlODcxYTNmYjk3NjcyOGRhYWQxYjdlYjZlMjc=
12
+ data.tar.gz: !binary |-
13
+ NWM1NzBjNDEwM2E2NDc4ZGEyNmZhNGY3MTRiN2YyNThmYzQ3NzU1OWY5Mjg3
14
+ NzE2ZTEyZWEyMDAyNWU3NDE4MzRhYjNkOTI2NDRiMDFjZTQxNjBkMjU4NmQy
15
+ ZjA4ZWMzZmUwMjQ4MjJiNzgxYTY2NmZkNDYzMjhjNThkODcyNjQ=
data/lib/cyrax.rb CHANGED
@@ -6,6 +6,7 @@ require "cyrax/extensions/has_response.rb"
6
6
  require "cyrax/extensions/has_service.rb"
7
7
  require "cyrax/extensions/has_decorator.rb"
8
8
  require "cyrax/extensions/has_serializer.rb"
9
+ require "cyrax/extensions/has_repository.rb"
9
10
  require "cyrax/presenters/base_collection.rb"
10
11
  require "cyrax/presenters/decorated_collection.rb"
11
12
  require "cyrax/serializers/scope.rb"
@@ -17,10 +18,10 @@ require "cyrax/presenter.rb"
17
18
  require "cyrax/response.rb"
18
19
  require "cyrax/decorator.rb"
19
20
  require "cyrax/serializer.rb"
21
+ require "cyrax/repository.rb"
20
22
 
21
23
  module Cyrax
22
24
  @@strong_parameters = true
23
- @@automatically_set_invalid_status = true
24
25
 
25
26
  def self.strong_parameters
26
27
  @@strong_parameters
@@ -29,12 +30,4 @@ module Cyrax
29
30
  def self.strong_parameters=(value)
30
31
  @@strong_parameters = value
31
32
  end
32
-
33
- def self.automatically_set_invalid_status
34
- @@automatically_set_invalid_status
35
- end
36
-
37
- def self.automatically_set_invalid_status=(value)
38
- @@automatically_set_invalid_status = value
39
- end
40
33
  end
@@ -21,28 +21,11 @@ class Cyrax::Decorator < Cyrax::Wrapper
21
21
  resource.send(method, *args, &block)
22
22
  end
23
23
 
24
- def respond_to?(method_sym, include_private = false)
25
- super || resource.respond_to?(method_sym, include_private)
26
- end
27
-
28
24
  class << self
29
25
  alias_method :decorate, :new
30
26
 
31
27
  def decorate_collection(resource)
32
28
  Cyrax::Presenters::DecoratedCollection.decorate(resource, decorator: self)
33
29
  end
34
-
35
- def respond_to?(method_sym, include_private = false)
36
- super || @@decorated_class.respond_to?(method_sym, include_private)
37
- end
38
-
39
- def method_missing(method, *args, &block)
40
- return super unless @@decorated_class.respond_to?(method)
41
- @@decorated_class.send(method, *args, &block)
42
- end
43
-
44
- def decorated_class(klass)
45
- @@decorated_class = klass
46
- end
47
30
  end
48
31
  end
@@ -3,24 +3,24 @@ module Cyrax::Extensions
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- class_attribute :decorator_class_name
7
- end
8
-
9
- def decorator_class_name
10
- options[:decorator] || self.class.decorator_class_name
6
+ class_attribute :_decorator_class
11
7
  end
12
8
 
13
9
  def decorable?
14
- !decorator_class_name.nil?
10
+ !decorator_class.nil?
15
11
  end
16
12
 
17
13
  def decorator_class
18
- decorator_class_name.to_s.classify.constantize
14
+ options[:decorator] || self.class._decorator_class
19
15
  end
20
16
 
21
17
  module ClassMethods
22
- def decorator(name)
23
- self.decorator_class_name = name ? name.to_s : nil
18
+ def decorator(klass)
19
+ if klass.is_a?(String)
20
+ ActiveSupport::Deprecation.warn "sending String in #decorator method is deprecated. send Class instead"
21
+ klass = klass.constantize
22
+ end
23
+ self._decorator_class = klass
24
24
  end
25
25
  end
26
26
  end
@@ -0,0 +1,43 @@
1
+ module Cyrax::Extensions
2
+ module HasRepository
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :_repository_class
7
+ class_attribute :_repository_options
8
+ end
9
+
10
+ def repository_class
11
+ options[:repository] || self.class._repository_class || Cyrax::Repository
12
+ end
13
+
14
+ def repository
15
+ options = (self.class._repository_options || {}).merge(
16
+ as: accessor, resource_class: resource_class, params: params
17
+ )
18
+ repository_class.new(options)
19
+ end
20
+
21
+ module ClassMethods
22
+ def repository(name = nil, &block)
23
+ if name.is_a?(Symbol)
24
+ klass, finder_name = nil, name
25
+ elsif name.is_a?(String)
26
+ ActiveSupport::Deprecation.warn "sending String in #decorator method is deprecated. send Class instead"
27
+ klass, finder_name = name.constantize, nil
28
+ elsif name.present?
29
+ klass, finder_name = name, nil
30
+ end
31
+
32
+ if klass.present?
33
+ self._repository_class = klass
34
+ end
35
+ if block_given?
36
+ self._repository_options ||= {}
37
+ self._repository_options[:finder_blocks] = {}
38
+ self._repository_options[:finder_blocks][finder_name || :scope] = block
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -4,39 +4,26 @@ module Cyrax::Extensions
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- class_attribute :resource_name
8
- class_attribute :resource_class_name
9
- class_attribute :collection_name
7
+ class_attribute :_resource_name
8
+ class_attribute :_collection_name
9
+ class_attribute :_resource_class
10
10
  end
11
11
 
12
- # Returns the resource class as a constant - e.g. Product
13
- def resource_class
14
- if self.resource_class_name
15
- self.resource_class_name.constantize
16
- else
17
- resource_name.classify.constantize
18
- end
12
+ def resource_name
13
+ self.class._resource_name
19
14
  end
20
15
 
21
- # Returns the resource class - e.g. Product
22
- # If you want your resource to return something interesting, you should override this in your resource by defining the method and returning your own scope
23
- #
24
- # @example Overriding resource_scope
25
- # class Products::UserResource < Products::BaseResource
26
- # def resource_scope
27
- # accessor.products
28
- # end
29
- # end
30
- def resource_scope
31
- resource_class
16
+ def collection_name
17
+ self.class._collection_name
32
18
  end
33
19
 
34
- def resource_attributes
35
- filter_attributes(dirty_resource_attributes)
20
+ # Returns the resource class as a constant - e.g. Product
21
+ def resource_class
22
+ self.class._resource_class || resource_name.classify.constantize
36
23
  end
37
24
 
38
- def response_name
39
- resource_name
25
+ def resource_attributes
26
+ filter_attributes(dirty_resource_attributes)
40
27
  end
41
28
 
42
29
  module ClassMethods
@@ -64,13 +51,21 @@ module Cyrax::Extensions
64
51
  # @param name [Symbol] The name of the resource
65
52
  # @param options Hash [Hash] Options
66
53
  def resource(name, options = {})
67
- self.resource_name = name.to_s
68
- self.resource_class_name = options[:class_name]
69
- self.collection_name = name.to_s.pluralize
54
+ if options[:class_name].present?
55
+ ActiveSupport::Deprecation.warn "sending :class_name in #resource method is deprecated. send :class instead"
56
+ options[:class] = options[:class_name].to_s.constantize
57
+ end
58
+ self._resource_name = name.to_s
59
+ self._resource_class = options[:class]
60
+ self._collection_name = name.to_s.pluralize
70
61
  end
71
62
  end
72
63
 
73
64
  private
65
+ def response_name
66
+ resource_name
67
+ end
68
+
74
69
  def dirty_resource_attributes
75
70
  if Cyrax.strong_parameters
76
71
  params.require(resource_name)
@@ -79,10 +74,6 @@ module Cyrax::Extensions
79
74
  end
80
75
  end
81
76
 
82
- def default_resource_attributes
83
- {}
84
- end
85
-
86
77
  def filter_attributes(attributes)
87
78
  if Cyrax.strong_parameters
88
79
  attributes.permit(self.class.accessible_attributes)
@@ -82,7 +82,6 @@ module Cyrax::Extensions
82
82
  # @param options [Hash] Options
83
83
  def respond_with(result, options = {})
84
84
  options[:as] ||= accessor
85
- options[:assignments] = @_assignments
86
85
  name = options[:name] || response_name
87
86
  result = result.result.to_model if result.is_a?(Cyrax::Response)
88
87
  if sync_errors_with?(result)
@@ -3,24 +3,24 @@ module Cyrax::Extensions
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- class_attribute :serializer_class_name
7
- end
8
-
9
- def serializer_class_name
10
- options[:serializer] || self.class.serializer_class_name
6
+ class_attribute :_serializer_class
11
7
  end
12
8
 
13
9
  def serializable?
14
- !serializer_class_name.nil?
10
+ !serializer_class.nil?
15
11
  end
16
12
 
17
13
  def serializer_class
18
- serializer_class_name.to_s.classify.constantize
14
+ options[:serializer] || self.class._serializer_class
19
15
  end
20
16
 
21
17
  module ClassMethods
22
- def serializer(name)
23
- self.serializer_class_name = name ? name.to_s : nil
18
+ def serializer(klass)
19
+ if klass.is_a?(String)
20
+ ActiveSupport::Deprecation.warn "sending String in #serializer method is deprecated. send Class instead"
21
+ klass = klass.constantize
22
+ end
23
+ self._serializer_class = klass
24
24
  end
25
25
  end
26
26
  end
@@ -1,17 +1,15 @@
1
1
  module Cyrax::Extensions
2
2
  module HasService
3
- VALIDATION_ERROR_STATUS = 422
4
3
  extend ActiveSupport::Concern
5
4
 
6
5
  # Builds and returns a collection response for Rails
7
6
  # @return [Cyrax::Response] response
8
7
  def read_all(&block)
9
8
  authorize_resource!(:read_all, resource_class)
10
- collection = build_collection
9
+ collection = repository.find_all
11
10
  block.call(collection) if block_given?
12
11
  respond_with collection, name: collection_name, present: :collection
13
12
  end
14
- # Overrides collection with read_all
15
13
  alias_method :read_all!, :read_all
16
14
 
17
15
  # Builds a new resource without saving to DB
@@ -19,7 +17,7 @@ module Cyrax::Extensions
19
17
  # Used for :new action in controller
20
18
  # @return [Cyrax::Response] response
21
19
  def build(&block)
22
- resource = build_resource(nil)
20
+ resource = repository.build(nil)
23
21
  authorize_resource!(:build, resource)
24
22
  block.call(resource) if block_given?
25
23
  respond_with resource
@@ -30,14 +28,12 @@ module Cyrax::Extensions
30
28
  # Used for :create action in controller
31
29
  # @return [Cyrax::Response] response
32
30
  def create(custom_attributes = nil, &block)
33
- resource = build_resource(nil, custom_attributes||resource_attributes)
31
+ resource = repository.build(nil, custom_attributes||resource_attributes)
34
32
  authorize_resource!(:create, resource)
35
33
  transaction do
36
- if save_resource(resource)
34
+ if repository.save(resource)
37
35
  set_message(:created)
38
36
  block.call(resource) if block_given?
39
- elsif Cyrax.automatically_set_invalid_status
40
- set_status VALIDATION_ERROR_STATUS
41
37
  end
42
38
  end
43
39
  respond_with(resource)
@@ -48,7 +44,7 @@ module Cyrax::Extensions
48
44
  # Used for :show action in controller
49
45
  # @return [Cyrax::Response] response
50
46
  def read(&block)
51
- resource = find_resource(resource_params_id)
47
+ resource = repository.find(resource_params_id)
52
48
  block.call(resource) if block_given?
53
49
  respond_with resource
54
50
  end
@@ -60,14 +56,12 @@ module Cyrax::Extensions
60
56
  # Used for :update action in controller
61
57
  # @return [Cyrax::Response] response
62
58
  def update(custom_attributes = nil, &block)
63
- resource = build_resource(resource_params_id, custom_attributes||resource_attributes)
59
+ resource = repository.build(resource_params_id, custom_attributes||resource_attributes)
64
60
  authorize_resource!(:update, resource)
65
61
  transaction do
66
- if save_resource(resource)
62
+ if repository.save(resource)
67
63
  set_message(:updated)
68
64
  block.call(resource) if block_given?
69
- elsif Cyrax.automatically_set_invalid_status
70
- set_status VALIDATION_ERROR_STATUS
71
65
  end
72
66
  end
73
67
  respond_with(resource)
@@ -78,50 +72,16 @@ module Cyrax::Extensions
78
72
  # Used for :destroy action in controller
79
73
  # @return [Cyrax::Response] response
80
74
  def destroy(&block)
81
- resource = find_resource(resource_params_id)
75
+ resource = repository.find(resource_params_id)
82
76
  authorize_resource!(:destroy, resource)
83
77
  transaction do
84
- delete_resource(resource)
78
+ repository.delete(resource)
85
79
  block.call(resource) if block_given?
86
80
  end
87
81
  respond_with(resource)
88
82
  end
89
83
  alias_method :destroy!, :destroy
90
84
 
91
- # Finds and returns a single item from the DB
92
- # @param id [int] ID of item
93
- # @return [object] The object
94
- def find_resource(id)
95
- resource_scope.find(id)
96
- end
97
-
98
- # Instantiates the resource
99
- # @param id [int] ID or nil if you want a new object
100
- # @param attributes [hash] Attributes you want to add to the resource
101
- # @return [object] The object
102
- def build_resource(id, attributes = {})
103
- if id.present?
104
- resource = find_resource(id)
105
- resource.attributes = attributes
106
- resource
107
- else
108
- resource_scope.new(default_resource_attributes.merge(attributes))
109
- end
110
- end
111
-
112
- # Saves a resource
113
- # @param resource [object] The resource to save
114
- def save_resource(resource)
115
- resource.save
116
- end
117
-
118
- # Remove a resource
119
- # Calls destroy method on resource
120
- # @param resource [object] The resource to destroy
121
- def delete_resource(resource)
122
- resource.destroy
123
- end
124
-
125
85
  # Authorize a resource
126
86
  # Should be called on each service method and should be implemented on each resource.
127
87
  # Implementation may raise an exception which may be handled by controller.
@@ -131,16 +91,6 @@ module Cyrax::Extensions
131
91
  # raise AuthorizationError
132
92
  end
133
93
 
134
- # Returns a collection of the resource we are calling.
135
- #
136
- # If you want your resource to return something interesting, you should override the resource_scope method.
137
- # Otherwise by default it will return the constantized model name and it will call .all on it during presentation.
138
- #
139
- # @return [type] The collection
140
- def build_collection
141
- resource_scope
142
- end
143
-
144
94
  def transaction(&block)
145
95
  if defined?(ActiveRecord::Base)
146
96
  ActiveRecord::Base.transaction do
@@ -154,5 +104,22 @@ module Cyrax::Extensions
154
104
  def resource_params_id
155
105
  params[:id]
156
106
  end
107
+
108
+ # DEPRECATED METHODS
109
+ def resource_scope
110
+ respository.scope
111
+ end
112
+ def build_collection
113
+ respository.find_all
114
+ end
115
+ def find_resource(id)
116
+ respository.find(id)
117
+ end
118
+ def save_resource(resource)
119
+ respository.save(resource)
120
+ end
121
+ def delete_resource(resource)
122
+ respository.delete(resource)
123
+ end
157
124
  end
158
125
  end