cyrax 0.5.15 → 0.6.0.beta
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +14 -6
- data/lib/cyrax/decorator.rb +0 -19
- data/lib/cyrax/extensions/has_decorator.rb +9 -9
- data/lib/cyrax/extensions/has_repository.rb +43 -0
- data/lib/cyrax/extensions/has_resource.rb +23 -32
- data/lib/cyrax/extensions/has_response.rb +0 -1
- data/lib/cyrax/extensions/has_serializer.rb +9 -9
- data/lib/cyrax/extensions/has_service.rb +26 -59
- data/lib/cyrax/helpers/controller.rb +2 -6
- data/lib/cyrax/presenter.rb +1 -1
- data/lib/cyrax/repository.rb +102 -0
- data/lib/cyrax/resource.rb +1 -0
- data/lib/cyrax/response.rb +3 -8
- data/lib/cyrax/serializer.rb +0 -27
- data/lib/cyrax/serializers/scope.rb +4 -18
- data/lib/cyrax/version.rb +1 -1
- data/lib/cyrax.rb +2 -9
- data/spec/cyrax/extensions/has_decorator_spec.rb +5 -5
- data/spec/cyrax/extensions/has_repository_spec.rb +28 -0
- data/spec/cyrax/extensions/has_resource_spec.rb +6 -15
- data/spec/cyrax/extensions/has_response_spec.rb +1 -1
- data/spec/cyrax/extensions/has_service_spec.rb +0 -43
- data/spec/cyrax/repository_spec.rb +72 -0
- data/spec/cyrax/resource_spec.rb +21 -35
- data/spec/cyrax/serializer_spec.rb +2 -13
- metadata +37 -44
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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/decorator.rb
CHANGED
@@ -1,6 +1,4 @@
|
|
1
1
|
require 'active_model'
|
2
|
-
# require 'active_model/serializers/xml'
|
3
|
-
|
4
2
|
class Cyrax::Decorator < Cyrax::Wrapper
|
5
3
|
include ActiveModel::Serialization
|
6
4
|
include ActiveModel::Serializers::JSON
|
@@ -23,28 +21,11 @@ class Cyrax::Decorator < Cyrax::Wrapper
|
|
23
21
|
resource.send(method, *args, &block)
|
24
22
|
end
|
25
23
|
|
26
|
-
def respond_to?(method_sym, include_private = false)
|
27
|
-
super || resource.respond_to?(method_sym, include_private)
|
28
|
-
end
|
29
|
-
|
30
24
|
class << self
|
31
25
|
alias_method :decorate, :new
|
32
26
|
|
33
27
|
def decorate_collection(resource)
|
34
28
|
Cyrax::Presenters::DecoratedCollection.decorate(resource, decorator: self)
|
35
29
|
end
|
36
|
-
|
37
|
-
def respond_to?(method_sym, include_private = false)
|
38
|
-
super || @@decorated_class.respond_to?(method_sym, include_private)
|
39
|
-
end
|
40
|
-
|
41
|
-
def method_missing(method, *args, &block)
|
42
|
-
return super unless @@decorated_class.respond_to?(method)
|
43
|
-
@@decorated_class.send(method, *args, &block)
|
44
|
-
end
|
45
|
-
|
46
|
-
def decorated_class(klass)
|
47
|
-
@@decorated_class = klass
|
48
|
-
end
|
49
30
|
end
|
50
31
|
end
|
@@ -3,24 +3,24 @@ module Cyrax::Extensions
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
6
|
-
class_attribute :
|
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
|
-
!
|
10
|
+
!decorator_class.nil?
|
15
11
|
end
|
16
12
|
|
17
13
|
def decorator_class
|
18
|
-
|
14
|
+
options[:decorator] || self.class._decorator_class
|
19
15
|
end
|
20
16
|
|
21
17
|
module ClassMethods
|
22
|
-
def decorator(
|
23
|
-
|
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 :
|
8
|
-
class_attribute :
|
9
|
-
class_attribute :
|
7
|
+
class_attribute :_resource_name
|
8
|
+
class_attribute :_collection_name
|
9
|
+
class_attribute :_resource_class
|
10
10
|
end
|
11
11
|
|
12
|
-
|
13
|
-
|
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
|
-
|
22
|
-
|
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
|
-
|
35
|
-
|
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
|
39
|
-
|
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
|
-
|
68
|
-
|
69
|
-
|
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 :
|
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
|
-
!
|
10
|
+
!serializer_class.nil?
|
15
11
|
end
|
16
12
|
|
17
13
|
def serializer_class
|
18
|
-
|
14
|
+
options[:serializer] || self.class._serializer_class
|
19
15
|
end
|
20
16
|
|
21
17
|
module ClassMethods
|
22
|
-
def serializer(
|
23
|
-
|
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 =
|
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 =
|
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 =
|
31
|
+
resource = repository.build(nil, custom_attributes||resource_attributes)
|
34
32
|
authorize_resource!(:create, resource)
|
35
33
|
transaction do
|
36
|
-
if
|
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 =
|
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 =
|
59
|
+
resource = repository.build(resource_params_id, custom_attributes||resource_attributes)
|
64
60
|
authorize_resource!(:update, resource)
|
65
61
|
transaction do
|
66
|
-
if
|
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 =
|
75
|
+
resource = repository.find(resource_params_id)
|
82
76
|
authorize_resource!(:destroy, resource)
|
83
77
|
transaction do
|
84
|
-
|
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
|
@@ -15,7 +15,7 @@ module Cyrax::ControllerHelper
|
|
15
15
|
result = result.to_model if result.respond_to?(:to_model)
|
16
16
|
|
17
17
|
respond_to do |format|
|
18
|
-
format.
|
18
|
+
format.any do
|
19
19
|
# set flashes
|
20
20
|
if response.success?
|
21
21
|
flash[:notice] = options[:notice] if options[:notice].present?
|
@@ -27,11 +27,7 @@ module Cyrax::ControllerHelper
|
|
27
27
|
super(result, options, &block)
|
28
28
|
end
|
29
29
|
format.json do
|
30
|
-
render json:
|
31
|
-
end
|
32
|
-
format.any do
|
33
|
-
set_resource_from(response)
|
34
|
-
super(response, options, &block)
|
30
|
+
render json: response.as_json
|
35
31
|
end
|
36
32
|
end
|
37
33
|
else
|
data/lib/cyrax/presenter.rb
CHANGED
@@ -20,7 +20,7 @@ class Cyrax::Presenter < Cyrax::Wrapper
|
|
20
20
|
if options[:present] == :collection
|
21
21
|
Cyrax::Presenters::DecoratedCollection.new(resource, options)
|
22
22
|
else
|
23
|
-
options[:decorator].decorate(resource
|
23
|
+
options[:decorator].decorate(resource)
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -0,0 +1,102 @@
|
|
1
|
+
class Cyrax::Repository
|
2
|
+
include HasActiveLogger::Mixin
|
3
|
+
attr_accessor :options
|
4
|
+
attr_accessor :accessor
|
5
|
+
attr_accessor :params
|
6
|
+
attr_accessor :resource_class
|
7
|
+
attr_accessor :finder_blocks
|
8
|
+
|
9
|
+
def initialize(options = {})
|
10
|
+
@options = options
|
11
|
+
@accessor = options[:as]
|
12
|
+
@params = options[:params]
|
13
|
+
@resource_class = options[:resource_class]
|
14
|
+
@finder_blocks = options[:finder_blocks] || {}
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns the resource class - e.g. Product by default.
|
18
|
+
# If you want your repository to scope with something interesting,
|
19
|
+
# you should override this in your repository by defining the method and returning your own scope
|
20
|
+
#
|
21
|
+
# @example Overriding scope
|
22
|
+
# class Products::Repository < Cyrax::Repository
|
23
|
+
# def scope
|
24
|
+
# accessor.products
|
25
|
+
# end
|
26
|
+
# end
|
27
|
+
def scope
|
28
|
+
if block = finder_blocks[:scope]
|
29
|
+
instance_exec(&block)
|
30
|
+
else
|
31
|
+
resource_class
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# Instantiates the resource
|
36
|
+
# @param id [int] ID or nil if you want a new object
|
37
|
+
# @param attributes [hash] Attributes you want to add to the resource
|
38
|
+
# @return [object] The object
|
39
|
+
def build(id, attributes = {})
|
40
|
+
if block = finder_blocks[:build]
|
41
|
+
instance_exec(id, attributes, &block)
|
42
|
+
else
|
43
|
+
if id.present?
|
44
|
+
resource = find(id)
|
45
|
+
resource.attributes = attributes
|
46
|
+
resource
|
47
|
+
else
|
48
|
+
scope.new(default_attributes.merge(attributes))
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Finds and returns a multiple items within the scope from the DB
|
54
|
+
# @return [Array] Array of objects
|
55
|
+
def find_all
|
56
|
+
if block = finder_blocks[:find_all]
|
57
|
+
instance_exec(&block)
|
58
|
+
else
|
59
|
+
scope.is_a?(ActiveRecord::Relation) ? scope.load : scope.all
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Finds and returns a single item from the DB
|
64
|
+
# @param id [int] ID of item
|
65
|
+
# @return [object] The object
|
66
|
+
def find(id)
|
67
|
+
if block = finder_blocks[:find]
|
68
|
+
instance_exec(id, &block)
|
69
|
+
else
|
70
|
+
scope.find(id)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Saves a resource
|
75
|
+
# @param resource [object] The resource to save
|
76
|
+
def save(resource)
|
77
|
+
if block = finder_blocks[:save]
|
78
|
+
instance_exec(resource, &block)
|
79
|
+
else
|
80
|
+
resource.save
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Removes a resource
|
85
|
+
# Calls destroy method on resource
|
86
|
+
# @param resource [object] The resource to destroy
|
87
|
+
def delete(resource)
|
88
|
+
if block = finder_blocks[:delete]
|
89
|
+
instance_exec(resource, &block)
|
90
|
+
else
|
91
|
+
resource.destroy
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def default_attributes
|
96
|
+
if block = finder_blocks[:default_attributes]
|
97
|
+
instance_exec(&block)
|
98
|
+
else
|
99
|
+
{}
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
data/lib/cyrax/resource.rb
CHANGED
data/lib/cyrax/response.rb
CHANGED
@@ -55,19 +55,14 @@ class Cyrax::Response
|
|
55
55
|
if failure?
|
56
56
|
{errors: @errors}
|
57
57
|
elsif options[:serializer]
|
58
|
-
options[:serializer].new(result,
|
58
|
+
options[:serializer].new(result, as: accessor).serialize
|
59
59
|
else
|
60
60
|
result.as_json
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
64
|
def method_missing(method, *args, &block)
|
65
|
-
|
66
|
-
|
67
|
-
elsif assignments.has_key?(method)
|
68
|
-
assignments[method]
|
69
|
-
else
|
70
|
-
super
|
71
|
-
end
|
65
|
+
super unless assignments.has_key?(method)
|
66
|
+
assignments[method]
|
72
67
|
end
|
73
68
|
end
|
data/lib/cyrax/serializer.rb
CHANGED
@@ -1,45 +1,18 @@
|
|
1
1
|
class Cyrax::Serializer < Cyrax::Wrapper
|
2
2
|
def serialize
|
3
3
|
options[:serializer] = self
|
4
|
-
|
5
|
-
if block = self.class.total_count_block
|
6
|
-
serialize_wrapped(options, &block)
|
7
|
-
else
|
8
|
-
serialize_simple(options)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def serialize_simple(options = {})
|
13
4
|
self.class.scope.serialize(resource, options)
|
14
5
|
end
|
15
6
|
|
16
|
-
def serialize_wrapped(options = {}, &counter_block)
|
17
|
-
total_count = self.instance_eval(&counter_block)
|
18
|
-
data = self.class.scope.serialize(resource, options)
|
19
|
-
{total_count: total_count, data: data}
|
20
|
-
end
|
21
|
-
|
22
7
|
class << self
|
23
8
|
def scope
|
24
9
|
@scope ||= Cyrax::Serializers::Scope.new()
|
25
10
|
end
|
26
11
|
|
27
|
-
def total_count(&block)
|
28
|
-
@total_count_block = block
|
29
|
-
end
|
30
|
-
|
31
|
-
def total_count_block
|
32
|
-
@total_count_block
|
33
|
-
end
|
34
|
-
|
35
12
|
def namespace(name, &block)
|
36
13
|
scope.namespace(name, &block)
|
37
14
|
end
|
38
15
|
|
39
|
-
def assigned(name, &block)
|
40
|
-
scope.assigned(name, &block)
|
41
|
-
end
|
42
|
-
|
43
16
|
def relation(name, &block)
|
44
17
|
scope.relation(name, &block)
|
45
18
|
end
|