cyrax 0.5.1.beta5 → 0.5.2

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDVkZDFkMDI4MWM3YWUwNjUzMDcwNGYxOTQ4ZjAxNjIyNjIyMDkxNA==
4
+ MjFlNjRkMDcxZjQ4NDZhNjA1YzllYTczNzU0YjUyM2NiMThkMDIwYg==
5
5
  data.tar.gz: !binary |-
6
- MmY0YzA2YWNkMWYxN2NhMzZmOTA1OWRhYzY5ZWEzNGQ3NGMxZmRlYQ==
6
+ MjcwMjAwMWFkM2U3MmI2ZWU1NGExNjE5MDRiNmQ0MDg3YTM3MzM5Nw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YTI3ZTkwOWFlYWNhYTdkMTMyMWRlNDlmNGE5N2RmNmE3NjNmYzc1NmZmYWVi
10
- NThlNjU1NWFkOGZkYzZlYjEyOTE0YjkyZDVkNDFhYzlkNWRhZTU2ZDFhNzVl
11
- NzU2MTI5ZjIxNjA3M2ViNTYxNzBjZGQyODQyZTY5MTA3NGI0MTA=
9
+ ZGNlMDIwYzVmMTZjMzEwMzMwNTU5OGQ0YTY5YjZmMDczMGExZjg3ZWEzY2Fl
10
+ M2E0ODIyMDAyODc3NTY1ODQwMTc5MGMzODdhYjNmMzljMGY0YzU5YTVhMDBh
11
+ MmE4MmUwMDllMTg3ZWM1ZDBkMzkwNTEzYWU5NTE3N2Y3NjNhNGE=
12
12
  data.tar.gz: !binary |-
13
- ZThlOTFiNDQyZjM0YTVkYTc2MzIzYmI3ZjI1NzU4MDJmZGZiZmE1MGQyOWM5
14
- Y2QxNDBhNjdkOTc5MzYxYzMwMjE3MzFkOGZkYTMzODQ0MTk3ZWM1OThkZDI5
15
- ZWQyN2ZiZjJmYjBkMmEwZWJlMzNhMTk5MTEwN2MxYmRkNTQ5MWU=
13
+ ODkxM2RmNWQ4ZGZlZGNjNWYwZDEwN2FkOTJmY2RhNDhkNTVjOTk3M2VkODRk
14
+ OGQxNDFjMzJmZjI3NzdlM2Y0ZjBmNGY2MDk5YTA5MjBkYWU0ZTZlOTBjMDkx
15
+ MjI2MTZhODExNDFmYjg0MGUxYjNkZWU0ZWI5YWM2OTVhNzVlNWU=
@@ -35,10 +35,12 @@ module Cyrax::Extensions
35
35
 
36
36
  def sync_errors_with(model)
37
37
  model = model.to_model if model.respond_to?(:to_model)
38
- if model && model.errors.messages.present?
39
- (@_errors || {}).each do |key, value|
40
- model.errors.add key, value unless model.errors.include?(key)
41
- end
38
+ return unless model
39
+ (@_errors || {}).each do |key, value|
40
+ next unless model.respond_to?(key)
41
+ model.errors.add key, value unless model.errors.include?(key)
42
+ end
43
+ if model.errors.messages.present?
42
44
  model.errors.messages.each do |key, value|
43
45
  add_error key, value
44
46
  end
@@ -4,19 +4,24 @@ module Cyrax::Extensions
4
4
 
5
5
  # Builds and returns a collection response for Rails
6
6
  # @return [Cyrax::Response] response
7
- def collection
8
- respond_with build_collection, name: collection_name, present: :collection
7
+ def read_all(&block)
8
+ authorize_resource!(:read_all, resource_class)
9
+ collection = build_collection
10
+ block.call(collection) if block_given?
11
+ respond_with collection, name: collection_name, present: :collection
9
12
  end
10
13
  # Overrides collection with read_all
11
- alias_method :read_all, :collection
12
- alias_method :read_all!, :collection
14
+ alias_method :read_all!, :read_all
13
15
 
14
16
  # Builds a new resource without saving to DB
15
17
  # Runs Model.new (before saving)
16
18
  # Used for :new action in controller
17
19
  # @return [Cyrax::Response] response
18
- def build
19
- respond_with build_resource(nil)
20
+ def build(&block)
21
+ resource = build_resource(nil)
22
+ authorize_resource!(:build, resource)
23
+ block.call(resource) if block_given?
24
+ respond_with resource
20
25
  end
21
26
  alias_method :build!, :build
22
27
 
@@ -25,6 +30,7 @@ module Cyrax::Extensions
25
30
  # @return [Cyrax::Response] response
26
31
  def create(custom_attributes = nil, &block)
27
32
  resource = build_resource(nil, custom_attributes||resource_attributes)
33
+ authorize_resource!(:create, resource)
28
34
  transaction do
29
35
  if save_resource(resource)
30
36
  set_message(:created)
@@ -52,6 +58,7 @@ module Cyrax::Extensions
52
58
  # @return [Cyrax::Response] response
53
59
  def update(custom_attributes = nil, &block)
54
60
  resource = build_resource(resource_params_id, custom_attributes||resource_attributes)
61
+ authorize_resource!(:update, resource)
55
62
  transaction do
56
63
  if save_resource(resource)
57
64
  set_message(:updated)
@@ -67,6 +74,7 @@ module Cyrax::Extensions
67
74
  # @return [Cyrax::Response] response
68
75
  def destroy(&block)
69
76
  resource = find_resource(resource_params_id)
77
+ authorize_resource!(:destroy, resource)
70
78
  transaction do
71
79
  delete_resource(resource)
72
80
  block.call(resource) if block_given?
@@ -109,6 +117,15 @@ module Cyrax::Extensions
109
117
  resource.destroy
110
118
  end
111
119
 
120
+ # Authorize a resource
121
+ # Should be called on each service method and should be implemented on each resource.
122
+ # Implementation may raise an exception which may be handled by controller.
123
+ # @param action [Symbol] The action to authorize
124
+ # @param resource [object] The resource to authorize
125
+ def authorize_resource!(action, resource)
126
+ # raise AuthorizationError
127
+ end
128
+
112
129
  # Returns a collection of the resource we are calling.
113
130
  #
114
131
  # If you want your resource to return something interesting, you should override the resource_scope method.
data/lib/cyrax/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cyrax
2
- VERSION = "0.5.1.beta5"
2
+ VERSION = "0.5.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cyrax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1.beta5
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Droidlabs
@@ -132,9 +132,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  required_rubygems_version: !ruby/object:Gem::Requirement
134
134
  requirements:
135
- - - ! '>'
135
+ - - ! '>='
136
136
  - !ruby/object:Gem::Version
137
- version: 1.3.1
137
+ version: '0'
138
138
  requirements: []
139
139
  rubyforge_project:
140
140
  rubygems_version: 2.0.5