cyrax 0.4.0 → 0.5.0

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
- Yjk1OTdiMmRiYzdhNzMyNzY4MzAxYWUxZjllODEwNDE4NjI2ZGJjZQ==
4
+ NTlmMTE4OTYxZTNlOGJjZWE5ZmY0NjNjNjZlNDRkZDY0YmFkZDY0ZA==
5
5
  data.tar.gz: !binary |-
6
- ZTE5ZWJiMDhlYzFkZWFjMzcxNDE5MTBiNzY4MDI1MzRkOGU5OTg4Nw==
6
+ ODY3YmYxMWQ5NDY4NjAyNzRjYTdjM2Q4ZmFmZGU3YWQzODg1ZmRlZA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NmJkMjBkNDQ3ZTQyNWJiNGZmNTc2ZmU2YjhiZjJkYjRjNTk2ZTc2NDNkY2Q2
10
- YmU2ODI5MjMwMjFjYmI1NzUwMTIzZjhlNDQyNWJmY2FmN2EwMWMwMGRiZjQ2
11
- MjhiNGZlZGVhNDlkOWE4YzRiOGNjMDg0NzMyOGExODNjNGFjOWM=
9
+ YzE5OGIzYzkxNGMzYTEzNmJjY2IxMWRiOTNlNWJiODdkOGFmZTYzYzFhZmQx
10
+ YmZjNTVlOTk3MzBkOTE4NTJjNmZhNGE4NzRhZDhhNzdiMTNmNjcwNGU4ZGU4
11
+ OWE3NmE3NmYxMTc3ZmNiMDhiOWZmZWZlZTNiZTI1NjE4NTk4Yjk=
12
12
  data.tar.gz: !binary |-
13
- MTUzNTE1MDZjNTJlMDQ0NGM4NDY4OGU5YzA5MWQyNzlmOTkzZDlmZWZjNjI2
14
- OWIwNDM3YTM0ZDcwZmUzYWY4NzhlOTBhYjNhY2ZhZTljMDc1YmJiYmE5MGM5
15
- NzcyMGFlMjkwY2QzNmFmOWM0ZTNhZDUzMTJkZDAxMzllYmMzNDI=
13
+ ZGQ1MDhkZmFhZDJiN2FjYTQ0OWM0MDRjYzUzMmI1ZmFiZjViMzRiNzUxMTA1
14
+ OTYwNDI4NGJkYWNjMDQzMTYyZGRjODUxY2I0ZWYzMjcwYTVkZDk0YjBiN2I3
15
+ ODI4ZmUxMzk2NTEzYzQ5Njc5ZGEzZDJlMzQwYmY1NmFmZDdlMTQ=
data/lib/cyrax/base.rb CHANGED
@@ -17,7 +17,16 @@ class Cyrax::Base
17
17
  # Products::UserResource.new(as: current_user, params: params)
18
18
  def initialize(options = {})
19
19
  @accessor = options[:as]
20
- @params = options[:params]
20
+ @params = wrap_params(options[:params])
21
21
  @options = options
22
22
  end
23
+
24
+ def wrap_params(params)
25
+ if Cyrax.strong_parameters && defined?(ActionController) &&
26
+ !params.is_a?(ActionController::Parameters)
27
+ ActionController::Parameters.new(params)
28
+ else
29
+ params
30
+ end
31
+ end
23
32
  end
@@ -1,10 +1,19 @@
1
1
  module Cyrax::Extensions
2
2
  module HasResponse
3
3
  extend ActiveSupport::Concern
4
+ STATUS_VALUES = {
5
+ created: 201,
6
+ no_content: 204,
7
+ bad_request: 400,
8
+ not_allowed: 403
9
+ }
4
10
 
5
- def add_error(error)
6
- @_errors ||= []
7
- @_errors << error
11
+ def add_error(key, value = nil)
12
+ if value.blank?
13
+ raise "Use key-value syntax for adding errors"
14
+ end
15
+ @_errors ||= {}
16
+ @_errors[key.to_sym] = value
8
17
  end
9
18
 
10
19
  def assign_resource(resource_name, resource, options = {})
@@ -15,23 +24,39 @@ module Cyrax::Extensions
15
24
  @_assignments[resource_name.to_sym] = resource
16
25
  end
17
26
 
18
- def add_error_unless(error, condition)
19
- add_error(error) unless condition
27
+ def add_error_unless(key, value, condition)
28
+ add_error(key, value) unless condition
20
29
  end
21
30
 
22
31
  def add_errors_from(model)
23
- @_errors ||= []
24
32
  if model && model.errors.messages.present?
25
33
  model.errors.messages.each do |key, value|
26
- add_error "#{key}: #{value}"
34
+ add_error key, value
27
35
  end
28
36
  end
29
37
  end
30
38
 
39
+ def add_errors_from?(model)
40
+ model = model.to_model if model.respond_to?(:to_model)
41
+ model && model.respond_to?(:errors) &&
42
+ model.errors.respond_to?(:messages)
43
+ end
44
+
45
+ def reset_errors
46
+ @_errors = {}
47
+ end
48
+
31
49
  def set_message(message)
32
50
  @_message = message
33
51
  end
34
52
 
53
+ def set_status(status)
54
+ if status.is_a?(Symbol)
55
+ status = STATUS_VALUES[status]
56
+ end
57
+ @_status = status
58
+ end
59
+
35
60
  def response_name
36
61
  self.class.name.demodulize.underscore
37
62
  end
@@ -43,6 +68,9 @@ module Cyrax::Extensions
43
68
  options[:as] ||= accessor
44
69
  name = options[:name] || response_name
45
70
  result = result.result.to_model if result.is_a?(Cyrax::Response)
71
+ if add_errors_from?(result)
72
+ add_errors_from(result)
73
+ end
46
74
  if respond_to?(:decorable?) && decorable?
47
75
  options = {decorator: decorator_class}.merge(options)
48
76
  end
@@ -54,6 +82,7 @@ module Cyrax::Extensions
54
82
  response.message = @_message
55
83
  response.errors = @_errors
56
84
  response.assignments = @_assignments
85
+ response.status = @_status
57
86
  response
58
87
  end
59
88
  end
@@ -26,15 +26,9 @@ module Cyrax::Extensions
26
26
  def create(custom_attributes = nil, &block)
27
27
  resource = build_resource(nil, custom_attributes||resource_attributes)
28
28
  transaction do
29
- invoke_callback(:before_create, resource)
30
- invoke_callback(:before_save, resource)
31
29
  if save_resource(resource)
32
- invoke_callback(:after_create, resource)
33
- invoke_callback(:after_save, resource)
34
30
  set_message("#{resource_name.titleize} successfully created")
35
31
  block.call(resource) if block_given?
36
- else
37
- add_errors_from(resource)
38
32
  end
39
33
  end
40
34
  respond_with(resource)
@@ -59,15 +53,9 @@ module Cyrax::Extensions
59
53
  def update(custom_attributes = nil, &block)
60
54
  resource = build_resource(params[:id], custom_attributes||resource_attributes)
61
55
  transaction do
62
- invoke_callback(:before_update, resource)
63
- invoke_callback(:before_save, resource)
64
56
  if save_resource(resource)
65
- invoke_callback(:after_update, resource)
66
- invoke_callback(:after_save, resource)
67
57
  set_message("#{resource_name.titleize} successfully updated")
68
58
  block.call(resource) if block_given?
69
- else
70
- add_errors_from(resource)
71
59
  end
72
60
  end
73
61
  respond_with(resource)
@@ -80,9 +68,7 @@ module Cyrax::Extensions
80
68
  def destroy(&block)
81
69
  resource = find_resource(params[:id])
82
70
  transaction do
83
- invoke_callback(:before_destroy, resource)
84
71
  delete_resource(resource)
85
- invoke_callback(:after_destroy, resource)
86
72
  block.call(resource) if block_given?
87
73
  end
88
74
  respond_with(resource)
@@ -7,23 +7,21 @@ module Cyrax::ControllerHelper
7
7
  error: response.error
8
8
  }.merge(options || {})
9
9
 
10
- set_resource_from(response)
11
-
12
- # set flashes
13
- flash[:notice] = options[:notice] if options[:notice].present?
14
- flash[:error] = options[:error] if options[:error].present?
10
+ # override status if needed
11
+ options[:status] = response.status if response.status
15
12
 
16
13
  # convert result to model if possible
17
14
  result = response.result
18
15
  result = result.to_model if result.respond_to?(:to_model)
19
16
 
17
+ # set flashes
18
+ flash[:notice] = options[:notice] if options[:notice].present?
19
+ flash[:error] = options[:error] if options[:error].present?
20
+ set_resource_from(response)
21
+
20
22
  super(result, options) do |format|
21
23
  format.json do
22
- if result.respond_to?(:errors) && result.errors.present?
23
- render json: { errors: result.errors }
24
- else
25
- render json: response.as_json
26
- end
24
+ render json: response.as_json
27
25
  end
28
26
  end
29
27
  else
@@ -1,6 +1,5 @@
1
1
  class Cyrax::Resource < Cyrax::Base
2
2
  include Cyrax::Extensions::HasResource
3
- include Cyrax::Extensions::HasCallbacks
4
3
  include Cyrax::Extensions::HasService
5
4
  include Cyrax::Extensions::HasDecorator
6
5
  include Cyrax::Extensions::HasSerializer
@@ -1,5 +1,5 @@
1
1
  class Cyrax::Response
2
- attr_accessor :message, :errors, :assignments,
2
+ attr_accessor :message, :errors, :status, :assignments,
3
3
  :result, :resource_name, :options
4
4
 
5
5
  def initialize(resource_name, result, options = {})
@@ -7,8 +7,9 @@ class Cyrax::Response
7
7
  @result = result
8
8
  @options = options
9
9
  @message = nil
10
- @errors = []
10
+ @errors = {}
11
11
  @assignments = {}
12
+ @status = nil
12
13
  end
13
14
 
14
15
  def with_errors(errors)
@@ -33,16 +34,24 @@ class Cyrax::Response
33
34
  message if success?
34
35
  end
35
36
 
37
+ def error_messages
38
+ errors.map do |key, value|
39
+ "#{key}: #{value}"
40
+ end
41
+ end
42
+
36
43
  def error
37
- message || errors.first if failure?
44
+ message || error_messages.first if failure?
38
45
  end
39
46
 
40
47
  def has_error?(error)
41
- errors && errors.include?(error)
48
+ errors && errors.has_key?(error)
42
49
  end
43
50
 
44
51
  def as_json(*args)
45
- if options[:serializer]
52
+ if failure?
53
+ {errors: @errors}
54
+ elsif options[:serializer]
46
55
  options[:serializer].new(result).serialize
47
56
  else
48
57
  result.as_json
data/lib/cyrax/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cyrax
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/cyrax.rb CHANGED
@@ -1,7 +1,8 @@
1
+ require 'active_support/core_ext/object'
2
+ require 'active_support/core_ext/class'
1
3
  require "cyrax/version"
2
4
  require "cyrax/extensions/has_resource.rb"
3
5
  require "cyrax/extensions/has_response.rb"
4
- require "cyrax/extensions/has_callbacks.rb"
5
6
  require "cyrax/extensions/has_service.rb"
6
7
  require "cyrax/extensions/has_decorator.rb"
7
8
  require "cyrax/extensions/has_serializer.rb"
@@ -14,7 +15,6 @@ require "cyrax/resource.rb"
14
15
  require "cyrax/wrapper.rb"
15
16
  require "cyrax/presenter.rb"
16
17
  require "cyrax/response.rb"
17
- require "cyrax/callbacks.rb"
18
18
  require "cyrax/decorator.rb"
19
19
  require "cyrax/serializer.rb"
20
20
 
@@ -28,17 +28,4 @@ module Cyrax
28
28
  def self.strong_parameters=(value)
29
29
  @@strong_parameters = value
30
30
  end
31
-
32
- def self.const_missing(const_name)
33
- case const_name
34
- when :BaseResource
35
- warn "`Cyrax::BaseResource` has been deprecated. Use `Cyrax::Resource` instead."
36
- Cyrax::Resource
37
- when :DecoratedCollectionPresenter
38
- warn "`Cyrax:DecoratedCollectionPresenter` has been deprecated. Use `Cyrax::Presenters::DecoratedCollection` instead."
39
- Cyrax::Presenters::DecoratedCollection
40
- else
41
- super
42
- end
43
- end
44
31
  end
@@ -21,19 +21,19 @@ module Cyrax
21
21
 
22
22
  describe '#add_error' do
23
23
  it 'should add error' do
24
- subject.add_error('foo')
25
- subject.instance_variable_get(:@_errors).should include('foo')
24
+ subject.add_error(:foo, 'bar')
25
+ subject.instance_variable_get(:@_errors).has_key?(:foo).should be_true
26
26
  end
27
27
  end
28
28
 
29
29
  describe '#add_error_unless' do
30
30
  it 'should add error when condition false' do
31
- subject.add_error_unless('foo', 1==0)
32
- subject.instance_variable_get(:@_errors).should include('foo')
31
+ subject.add_error_unless(:foo, 'bar', 1==0)
32
+ subject.instance_variable_get(:@_errors).has_key?(:foo).should be_true
33
33
  end
34
34
 
35
35
  it 'should not add error when condition true' do
36
- subject.add_error_unless('foo', 1==1)
36
+ subject.add_error_unless(:foo, 'bar', 1==1)
37
37
  subject.instance_variable_get(:@_errors).should be_nil
38
38
  end
39
39
  end
@@ -45,7 +45,7 @@ module Cyrax
45
45
 
46
46
  it 'should add errors from model error messages' do
47
47
  subject.add_errors_from(model)
48
- subject.instance_variable_get(:@_errors).should eq(['foo: bar', 'bar: bazz'])
48
+ subject.instance_variable_get(:@_errors).should eq({foo: 'bar', bar: 'bazz'})
49
49
  end
50
50
  end
51
51
 
@@ -73,6 +73,7 @@ module Cyrax
73
73
  response.should_receive(:message=)
74
74
  response.should_receive(:errors=)
75
75
  response.should_receive(:assignments=)
76
+ response.should_receive(:status=)
76
77
  subject.respond_with('bar')
77
78
  end
78
79
  end
@@ -21,7 +21,6 @@ module Cyrax
21
21
  it{ should respond_to(:add_error) }
22
22
  it{ should respond_to(:add_error_unless) }
23
23
  it{ should respond_to(:add_errors_from) }
24
- it{ should respond_to(:invoke_callback) }
25
24
  end
26
25
  end
27
26
 
@@ -77,12 +76,6 @@ module Cyrax
77
76
  resource.should_receive(:destroy)
78
77
  subject.destroy
79
78
  end
80
-
81
- it 'invokes callbacks' do
82
- subject.should_receive(:invoke_callback).with(:before_destroy, resource)
83
- subject.should_receive(:invoke_callback).with(:after_destroy, resource)
84
- subject.destroy
85
- end
86
79
  end
87
80
 
88
81
  describe '#create' do
@@ -97,14 +90,6 @@ module Cyrax
97
90
  context 'when resource successfully saved' do
98
91
  before { resource.stub(:save).and_return(true) }
99
92
 
100
- it 'invokes callbacks' do
101
- subject.should_receive(:invoke_callback).with(:before_save, resource)
102
- subject.should_receive(:invoke_callback).with(:before_create, resource)
103
- subject.should_receive(:invoke_callback).with(:after_save, resource)
104
- subject.should_receive(:invoke_callback).with(:after_create, resource)
105
- subject.create(params)
106
- end
107
-
108
93
  it 'sets message' do
109
94
  subject.should_receive(:set_message).with('Foo successfully created')
110
95
  subject.create(params)
@@ -114,14 +99,6 @@ module Cyrax
114
99
  context 'when resource could not be saved' do
115
100
  before { resource.stub(:save).and_return(false) }
116
101
 
117
- it 'invokes callbacks' do
118
- subject.should_receive(:invoke_callback).with(:before_save, resource)
119
- subject.should_receive(:invoke_callback).with(:before_create, resource)
120
- subject.should_not_receive(:invoke_callback).with(:after_save, resource)
121
- subject.should_not_receive(:invoke_callback).with(:after_create, resource)
122
- subject.create(params)
123
- end
124
-
125
102
  it 'sets error messages' do
126
103
  subject.should_receive(:add_errors_from).with(resource)
127
104
  subject.create(params)
@@ -141,14 +118,6 @@ module Cyrax
141
118
  context 'when resource successfully saved' do
142
119
  before { resource.stub(:save).and_return(true) }
143
120
 
144
- it 'invokes callbacks' do
145
- subject.should_receive(:invoke_callback).with(:before_save, resource)
146
- subject.should_receive(:invoke_callback).with(:before_update, resource)
147
- subject.should_receive(:invoke_callback).with(:after_save, resource)
148
- subject.should_receive(:invoke_callback).with(:after_update, resource)
149
- subject.update(params)
150
- end
151
-
152
121
  it 'sets message' do
153
122
  subject.should_receive(:set_message).with('Foo successfully updated')
154
123
  subject.update(params)
@@ -158,14 +127,6 @@ module Cyrax
158
127
  context 'when resource could not be saved' do
159
128
  before { resource.stub(:save).and_return(false) }
160
129
 
161
- it 'invokes callbacks' do
162
- subject.should_receive(:invoke_callback).with(:before_save, resource)
163
- subject.should_receive(:invoke_callback).with(:before_update, resource)
164
- subject.should_not_receive(:invoke_callback).with(:after_save, resource)
165
- subject.should_not_receive(:invoke_callback).with(:after_update, resource)
166
- subject.update(params)
167
- end
168
-
169
130
  it 'sets error messages' do
170
131
  subject.should_receive(:add_errors_from).with(resource)
171
132
  subject.update(params)
@@ -12,27 +12,27 @@ module Cyrax
12
12
  end
13
13
 
14
14
  describe '#with_errors' do
15
- before { subject.with_errors(['some', 'errors']) }
15
+ before { subject.with_errors({foo: 'some', bar: 'errors'}) }
16
16
  it { should be_kind_of(Cyrax::Response) }
17
17
  its(:errors) { should be }
18
- its(:errors) { should eq(['some', 'errors']) }
18
+ its(:errors) { should eq({foo: 'some', bar: 'errors'}) }
19
19
  end
20
20
 
21
21
  describe '#success?' do
22
22
  context 'when there are no errors' do
23
- before { subject.with_errors([]) }
23
+ before { subject.with_errors({}) }
24
24
  its(:success?) { should be_true }
25
25
  end
26
26
 
27
27
  context 'when there are errors' do
28
- before { subject.with_errors(['some', 'errors']) }
28
+ before { subject.with_errors({foo: 'some', bar: 'errors'}) }
29
29
  its(:success?) { should be_false }
30
30
  end
31
31
  end
32
32
 
33
33
  describe '#failure?' do
34
34
  context 'when there are no errors' do
35
- before { subject.with_errors([]) }
35
+ before { subject.with_errors({}) }
36
36
  its(:failure?) { should be_false }
37
37
  end
38
38
 
@@ -51,7 +51,7 @@ module Cyrax
51
51
  end
52
52
 
53
53
  context 'when there are errors' do
54
- before { subject.with_errors(['some', 'errors']) }
54
+ before { subject.with_errors({foo: 'some', bar: 'errors'}) }
55
55
  its(:notice) { should be_nil }
56
56
  end
57
57
  end
@@ -63,13 +63,13 @@ module Cyrax
63
63
  end
64
64
 
65
65
  context 'when there are errors' do
66
- before { subject.with_errors(['some', 'message']) }
66
+ before { subject.with_errors({foo: 'some', bar: 'errors'}) }
67
67
  its(:error) { should be }
68
- its(:error) { should eq('some') }
68
+ its(:error) { should eq('foo: some') }
69
69
  end
70
70
 
71
71
  context 'when message is present' do
72
- before { subject.with_message('some message').with_errors(['some', 'message']) }
72
+ before { subject.with_message('some message').with_errors({foo: 'some', bar: 'errors'}) }
73
73
  its(:error) { should be }
74
74
  its(:error) { should eq('some message') }
75
75
  end
@@ -77,14 +77,14 @@ module Cyrax
77
77
 
78
78
  describe '#has_error?' do
79
79
  context 'when there are no errors' do
80
- before { subject.with_errors([]) }
81
- specify { subject.has_error?('foo').should be_false }
80
+ before { subject.with_errors({}) }
81
+ specify { subject.has_error?(:foo).should be_false }
82
82
  end
83
83
 
84
84
  context 'when there are errors' do
85
- before { subject.with_errors(['some', 'message']) }
86
- specify { subject.has_error?('some').should be_true }
87
- specify { subject.has_error?('foo').should be_false }
85
+ before { subject.with_errors({foo: 'some', bar: 'errors'}) }
86
+ specify { subject.has_error?(:foo).should be_true }
87
+ specify { subject.has_error?(:foo1).should be_false }
88
88
  end
89
89
  end
90
90
 
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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Droidlabs
@@ -87,9 +87,7 @@ extensions: []
87
87
  extra_rdoc_files: []
88
88
  files:
89
89
  - lib/cyrax/base.rb
90
- - lib/cyrax/callbacks.rb
91
90
  - lib/cyrax/decorator.rb
92
- - lib/cyrax/extensions/has_callbacks.rb
93
91
  - lib/cyrax/extensions/has_decorator.rb
94
92
  - lib/cyrax/extensions/has_resource.rb
95
93
  - lib/cyrax/extensions/has_response.rb
@@ -110,9 +108,7 @@ files:
110
108
  - Rakefile
111
109
  - README.md
112
110
  - spec/cyrax/base_spec.rb
113
- - spec/cyrax/callbacks_spec.rb
114
111
  - spec/cyrax/decorator_spec.rb
115
- - spec/cyrax/extensions/has_callbacks_spec.rb
116
112
  - spec/cyrax/extensions/has_decorator_spec.rb
117
113
  - spec/cyrax/extensions/has_resource_spec.rb
118
114
  - spec/cyrax/extensions/has_response_spec.rb
@@ -147,9 +143,7 @@ specification_version: 4
147
143
  summary: Small library for adding service layer to Rails projects
148
144
  test_files:
149
145
  - spec/cyrax/base_spec.rb
150
- - spec/cyrax/callbacks_spec.rb
151
146
  - spec/cyrax/decorator_spec.rb
152
- - spec/cyrax/extensions/has_callbacks_spec.rb
153
147
  - spec/cyrax/extensions/has_decorator_spec.rb
154
148
  - spec/cyrax/extensions/has_resource_spec.rb
155
149
  - spec/cyrax/extensions/has_response_spec.rb
@@ -1,17 +0,0 @@
1
- class Cyrax::Callbacks
2
- attr_accessor :resource
3
-
4
- def initialize(resource)
5
- @resource = resource
6
- end
7
-
8
- def before_save; end
9
- def before_create; end
10
- def before_update; end
11
- def before_destroy; end
12
-
13
- def after_save; end
14
- def after_create; end
15
- def after_update; end
16
- def after_destroy; end
17
- end
@@ -1,26 +0,0 @@
1
- require 'active_support/core_ext/object'
2
- require 'active_support/core_ext/class'
3
- require 'active_support/deprecation'
4
-
5
- module Cyrax::Extensions
6
- module HasCallbacks
7
- extend ActiveSupport::Concern
8
-
9
- included do
10
- class_attribute :resource_callbacks_handler_class
11
- end
12
-
13
- def invoke_callback(name, resource)
14
- if resource_callbacks_handler_class
15
- resource_callbacks_handler_class.new(resource).send(name)
16
- end
17
- end
18
-
19
- module ClassMethods
20
- def callbacks_handler(name, options = {})
21
- ActiveSupport::Deprecation.warn "#callbacks_handler is deprecated. Redefine method and use short alias instead"
22
- self.resource_callbacks_handler_class = name
23
- end
24
- end
25
- end
26
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Cyrax
4
- describe Callbacks do
5
- subject { Cyrax::Callbacks.new(double) }
6
-
7
- it { should respond_to(:before_create) }
8
- it { should respond_to(:after_create) }
9
-
10
- it { should respond_to(:before_save) }
11
- it { should respond_to(:after_save) }
12
-
13
- it { should respond_to(:before_update) }
14
- it { should respond_to(:after_update) }
15
-
16
- it { should respond_to(:before_destroy) }
17
- it { should respond_to(:after_destroy) }
18
- end
19
- end
@@ -1,46 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Cyrax
4
- class BaseWithCallbacks < Cyrax::Base
5
- include Cyrax::Extensions::HasCallbacks
6
- end
7
-
8
- describe Cyrax::Extensions::HasCallbacks do
9
- subject { BaseWithCallbacks.new }
10
-
11
- describe '#invoke_callback' do
12
- context 'when callback handler class is not defined' do
13
- let(:name) { double }
14
- let(:resource) { double }
15
- before { subject.class.stub(:resource_callbacks_handler_class) }
16
- it { subject.invoke_callback(name, resource).should be_nil }
17
- end
18
-
19
- context 'when callback handler class is defined' do
20
- let(:name) { "foo" }
21
- let(:resource) { double }
22
- let(:handle_class) { double.as_null_object }
23
- before { subject.class.stub(:resource_callbacks_handler_class).and_return(handle_class) }
24
-
25
- it 'initializes handle class instance' do
26
- handle_class.should_receive(:new).with(resource)
27
- subject.invoke_callback(name, resource)
28
- end
29
-
30
- it 'calls corresponding method on instance' do
31
- instance = double.as_null_object
32
- handle_class.stub(:new).and_return(instance)
33
- instance.should_receive(:foo)
34
- subject.invoke_callback(name, resource)
35
- end
36
- end
37
- end
38
-
39
- describe '#callbacks_handler' do
40
- it 'should define callbacks handler class' do
41
- subject.class.callbacks_handler('Foo')
42
- subject.class.resource_callbacks_handler_class.should eq('Foo')
43
- end
44
- end
45
- end
46
- end