rescue-dog 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 04a759ce546669df119ad51af2b97b93a417db88
4
- data.tar.gz: 6ec7e0fef288002684edc508f39baadf6fd1ba39
3
+ metadata.gz: cafa21df2d504ab347d4e9164a227d692dc94096
4
+ data.tar.gz: 23d90492c087617ea7241c15a9635f7d585d86f0
5
5
  SHA512:
6
- metadata.gz: 3c506070a73c70ef41bfb5049e0f217b16b2c424a37c268e9656d3b730196a9db5b42e1bf92f92cb99702695350e92b5007148de909769fa0aedfe2f5d8b2334
7
- data.tar.gz: 618a85f1e3f2b7f1f4f1df03e776c54072746bea2e80a00b83485e52da43253e1b658aa641b32bdcf0023ec3a5fc3062df940e6533a7385c7a4fa66d834e5b6d
6
+ metadata.gz: ddf72573ce9f3deab8ee9f0d08ee65b7eea1f94ed6d0d17cc2fc0c8c499b2ae5f540118e386134c803b96c42b61ce1bdc4fa6525863adc61dca1a165d750f5f4
7
+ data.tar.gz: bbc8b7508ad09d9818d918c690f979f6220efa0d901d3f5dc2126d21330f77317255313d383c7c89b225186bc112912c720f4365e759f42a5c67e46ae1733400
data/README.md CHANGED
@@ -26,7 +26,22 @@ Or install it yourself as:
26
26
  $ gem install rescue-dog
27
27
  ```
28
28
 
29
- ## Usage
29
+ ## Simple CRUD Actions
30
+
31
+ 1. Include `Rescue::Controller::Static` (or `Rescue::Controller::Dynamic`).
32
+ 2. Call `rescue_controller` method.
33
+
34
+ ### Define Actions
35
+
36
+ ```ruby
37
+ class UsersController < ApplicationController
38
+ rescue_controller User, :new, :edit, :show,
39
+ create: { render: lambda { redirect_to edit_user_path(@user) } ,rescue: lambda { render :new } },
40
+ update: { render: lambda { redirect_to edit_user_path(@user) } ,rescue: lambda { render :edit } },
41
+ delete: { render: lambda { redirect_to root_path } ,rescue: lambda { render :edit } }
42
+ ```
43
+
44
+ ## Render Errors
30
45
 
31
46
  1. Include `Rescue::Controller::Static` or `Rescue::Controller::Dynamic`.
32
47
  2. Call `rescue_associate` method. And then, the exception class is defined and added to `rescue_handlers`.
@@ -70,10 +70,11 @@ module Rescue
70
70
  if rescue_given
71
71
  define_method name do
72
72
  begin
73
- send(call_method)
73
+ send(call_method, &options[:intercept])
74
74
  flash[:success] = success_message unless success_message.blank?
75
75
  instance_exec(&options[:render]) if options[:render]
76
- rescue
76
+ rescue => e
77
+ Rails.logger.debug(e.backtrace.unshift(e.message).join("\n\t"))
77
78
  flash.now[:error] = error_message unless error_message.blank?
78
79
  instance_exec(&options[:rescue]) if options[:rescue]
79
80
  end
@@ -11,17 +11,20 @@ module Rescue
11
11
  id = find_params[Rescue.config.primary_key]
12
12
  instance_variable_set(var_sym, clazz.find(id))
13
13
  end
14
- object.send(:define_method, :create_call) do
14
+ object.send(:define_method, :create_call) do |&block|
15
15
  instance_variable_set(var_sym, clazz.new(send(params_sym)))
16
+ instance_exec(&block) if block
16
17
  instance_variable_get(var_sym).save!
17
18
  end
18
- object.send(:define_method, :update_call) do
19
+ object.send(:define_method, :update_call) do |&block|
19
20
  find_call
20
21
  instance_variable_get(var_sym).attributes = send(params_sym)
22
+ instance_exec(&block) if block
21
23
  instance_variable_get(var_sym).save!
22
24
  end
23
- object.send(:define_method, :delete_call) do
25
+ object.send(:define_method, :delete_call) do |&block|
24
26
  find_call
27
+ instance_exec(&block) if block
25
28
  instance_variable_get(var_sym).destroy!
26
29
  end
27
30
  object.send(:private, :new_call, :find_call, :create_call, :update_call, :delete_call)
@@ -1,3 +1,3 @@
1
1
  module Rescue
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/spec/fake_rails.rb CHANGED
@@ -92,6 +92,7 @@ class StatusController < ApplicationController
92
92
  end
93
93
 
94
94
  # models
95
- class RescueModel ; end
95
+ class RescueModel
96
+ end
96
97
 
97
98
  Object.const_set(:ApplicationHelper,Module.new)
@@ -0,0 +1,66 @@
1
+ # coding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Rescue::Controller::Action do
5
+
6
+ let(:model) do # Stub of Model Class
7
+ clazz = Class.new
8
+ clazz.stub(:new).with(any_args()) do
9
+ object = Object.new
10
+ object.stub(:attributes=).and_return(true)
11
+ object.stub(:save!).and_return(true)
12
+ object.stub(:destroy!).and_return(true)
13
+ object
14
+ end
15
+ clazz.stub(:find).and_return(clazz.new)
16
+ clazz
17
+ end
18
+
19
+ let(:controller) do # Fake Controller
20
+ clazz = model
21
+ Class.new do
22
+ Rescue::Controller::Action.define self, clazz, :@rescue, :rescue_params
23
+ end
24
+ end
25
+
26
+ let(:object) do
27
+ object = controller.new
28
+ object.stub(:find_params).and_return({})
29
+ object.stub(:rescue_params).and_return({})
30
+ object
31
+ end
32
+
33
+ let(:exception) do
34
+ Class.new(RuntimeError)
35
+ end
36
+
37
+ describe "#define" do
38
+ [:find_call, :new_call, :create_call, :update_call, :delete_call].each do |name|
39
+ it "should be defined private method `#{name}`" do
40
+ expect(controller.private_instance_methods.include? name).to be_true
41
+ end
42
+ end
43
+ end
44
+
45
+ [:find_call, :new_call].each do |name|
46
+ describe "##{name}" do
47
+ it { expect { object.send(name) }.not_to raise_error }
48
+ end
49
+ end
50
+
51
+ [:create_call, :update_call, :delete_call].each do |name|
52
+ describe "##{name}" do
53
+ it { expect { object.send(name) }.not_to raise_error }
54
+
55
+ context "with &block" do
56
+ let(:block) { raise exception }
57
+
58
+ it "should execute block-process" do
59
+ expect { object.send(name, &block) }.to raise_error(exception)
60
+ end
61
+ end
62
+
63
+ end
64
+ end
65
+
66
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rescue-dog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - yulii
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-09 00:00:00.000000000 Z
11
+ date: 2013-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -67,6 +67,7 @@ files:
67
67
  - rescue-dog.gemspec
68
68
  - spec/fake_rails.rb
69
69
  - spec/rescue/controller_spec.rb
70
+ - spec/rescue/controllers/action_spec.rb
70
71
  - spec/rescue/controllers/dynamic_spec.rb
71
72
  - spec/rescue/controllers/flash_spec.rb
72
73
  - spec/rescue/controllers/static_spec.rb
@@ -101,6 +102,7 @@ summary: define respond methods
101
102
  test_files:
102
103
  - spec/fake_rails.rb
103
104
  - spec/rescue/controller_spec.rb
105
+ - spec/rescue/controllers/action_spec.rb
104
106
  - spec/rescue/controllers/dynamic_spec.rb
105
107
  - spec/rescue/controllers/flash_spec.rb
106
108
  - spec/rescue/controllers/static_spec.rb