rescue-dog 0.3.2 → 0.3.4

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: cafa21df2d504ab347d4e9164a227d692dc94096
4
- data.tar.gz: 23d90492c087617ea7241c15a9635f7d585d86f0
3
+ metadata.gz: 0e2a7f7faf7befcd8fb279f982baa669fcf66964
4
+ data.tar.gz: 21ea12236b212cc34badb3351fe6892bff346f42
5
5
  SHA512:
6
- metadata.gz: ddf72573ce9f3deab8ee9f0d08ee65b7eea1f94ed6d0d17cc2fc0c8c499b2ae5f540118e386134c803b96c42b61ce1bdc4fa6525863adc61dca1a165d750f5f4
7
- data.tar.gz: bbc8b7508ad09d9818d918c690f979f6220efa0d901d3f5dc2126d21330f77317255313d383c7c89b225186bc112912c720f4365e759f42a5c67e46ae1733400
6
+ metadata.gz: cbfc2e1db3316d00f46c99ebf4a79c2e5b24e0e4cfc16512e2ce095da21b3c2927de1d0caa3ce556e590b3a16845a402782b44ab6be160729c3452654bcbbd89
7
+ data.tar.gz: b43644434d7c85cc0e5c1835b8a92c3769443a5f4bcafc610640a77b894f2e149ac09efde1d77c5ed32a93a86e27cf160cada1104be73239dc47f26258ad285c
data/README.md CHANGED
@@ -28,7 +28,7 @@ $ gem install rescue-dog
28
28
 
29
29
  ## Simple CRUD Actions
30
30
 
31
- 1. Include `Rescue::Controller::Static` (or `Rescue::Controller::Dynamic`).
31
+ 1. Include `Rescue::Controller` (Rescue::Controller::Static` or `Rescue::Controller::Dynamic`).
32
32
  2. Call `rescue_controller` method.
33
33
 
34
34
  ### Define Actions
@@ -1,24 +1,24 @@
1
1
  en:
2
2
  default:
3
3
  flash:
4
- success: "default_controller success"
5
- error: "default_controller error"
4
+ success: "default success"
5
+ error: "default error"
6
6
 
7
7
  views:
8
8
  rescue:
9
9
  action:
10
10
  flash:
11
- success: "rescue_controller success"
12
- error: "rescue_controller error"
11
+ success: "rescue success"
12
+ error: "rescue error"
13
13
  dog:
14
14
  action:
15
15
  flash:
16
- success: "success"
17
- error: "error"
16
+ success: "rescue/dog success"
17
+ error: "rescue/dog error"
18
18
 
19
19
  rescue_dog:
20
20
  action:
21
21
  flash:
22
- success: "rescue_dog_controller success"
23
- error: "rescue_dog_controller error"
22
+ success: "rescue_dog success"
23
+ error: "rescue_dog error"
24
24
 
@@ -2,6 +2,24 @@
2
2
  module Rescue
3
3
  module Controller
4
4
 
5
+ def self.included(base)
6
+ base.extend Rescue::Controller::ClassMethods
7
+ end
8
+
9
+ def rescue_respond call, params, options = {}
10
+ begin
11
+ send(call, params)
12
+ success_message = options[:success]||Flash.message(self, :success)
13
+ flash[:success] = success_message unless success_message.blank?
14
+ instance_exec(&options[:render])
15
+ rescue => e
16
+ Rails.logger.debug ([e.message] + e.backtrace).join("\n\t")
17
+ error_message = options[:error]||Flash.message(self, :error)
18
+ flash.now[:error] = error_message unless error_message.blank?
19
+ instance_exec(&options[:rescue])
20
+ end
21
+ end
22
+
5
23
  module ClassMethods
6
24
 
7
25
  def rescue_associate *klasses, &block
@@ -37,54 +55,24 @@ module Rescue
37
55
  end
38
56
 
39
57
  def rescue_controller clazz, *actions
40
- options = actions.extract_options!
41
- name = clazz.name.downcase
42
- var_sym = :"@#{name}"
43
- params_sym = :"#{name}_params"
44
-
45
- Parameter.define(self)
46
- Action.define(self, clazz, var_sym, params_sym)
58
+ options = actions.extract_options!
59
+ method_names = actions + options.keys
60
+ name = clazz.name.downcase
47
61
 
48
- [:new, :edit, :show, :create, :update, :delete].each do |type|
49
- args = options.delete(type) || (actions.delete(type) ? {} : nil)
50
- define_action_method(type, args) if args
62
+ [:show, :edit, :new].each do |type|
63
+ (options.delete(type)||[]).each do |name|
64
+ method_names << name
65
+ options[name] = { type: type }
66
+ end
51
67
  end
52
- end
53
68
 
54
- def define_action_method name, options = {}
55
- raise RuntimeError "`name` is already defined." if method_defined?(name)
56
- options[:type] ||= name
57
- options[:flash] ||= [:create, :update, :delete].include?(options[:type])
58
- call_method = case options[:type]
59
- when :show, :edit ; :find_call
60
- else ; :"#{options[:type]}_call"
61
- end
62
- rescue_given = options[:rescue]||options[:error]||options[:flash]
63
- success_message = options[:success]
64
- error_message = options[:error]
65
- if options[:flash]
66
- success_message ||= Flash.message(self, name, :success)
67
- error_message ||= Flash.message(self, name, :error)
69
+ [:create, :update, :destroy].each do |type|
70
+ options[type] ||= {}
71
+ options[type][:params] ||= :"#{type}_params"
68
72
  end
69
73
 
70
- if rescue_given
71
- define_method name do
72
- begin
73
- send(call_method, &options[:intercept])
74
- flash[:success] = success_message unless success_message.blank?
75
- instance_exec(&options[:render]) if options[:render]
76
- rescue => e
77
- Rails.logger.debug(e.backtrace.unshift(e.message).join("\n\t"))
78
- flash.now[:error] = error_message unless error_message.blank?
79
- instance_exec(&options[:rescue]) if options[:rescue]
80
- end
81
- end
82
- else
83
- define_method name do
84
- send(call_method)
85
- instance_exec(&options[:render]) if options[:render]
86
- end
87
- end
74
+ Action.define_call(self, clazz, :"@#{name}")
75
+ Action.define(self, method_names, options)
88
76
  end
89
77
 
90
78
  end
@@ -3,31 +3,56 @@ module Rescue
3
3
  module Controller
4
4
  class Action
5
5
 
6
- def self.define object, clazz, var_sym, params_sym
7
- object.send(:define_method, :new_call) do
8
- instance_variable_set(var_sym, clazz.new)
6
+ def self.define_call object, clazz, var_sym
7
+ object.send(:define_method, :new_call) do |params = {}|
8
+ instance_variable_set(var_sym, clazz.new(params))
9
9
  end
10
- object.send(:define_method, :find_call) do
11
- id = find_params[Rescue.config.primary_key]
12
- instance_variable_set(var_sym, clazz.find(id))
10
+ object.send(:define_method, :find_call) do |params = {}|
11
+ id = (params.empty? ? send(:params) : params).delete(Rescue.config.primary_key)
12
+ instance_variable_set(var_sym, clazz.where(params).find(id))
13
13
  end
14
- object.send(:define_method, :create_call) do |&block|
15
- instance_variable_set(var_sym, clazz.new(send(params_sym)))
16
- instance_exec(&block) if block
14
+ object.send(:define_method, :create_call) do |params|
15
+ new_call(params)
17
16
  instance_variable_get(var_sym).save!
18
17
  end
19
- object.send(:define_method, :update_call) do |&block|
18
+ object.send(:define_method, :update_call) do |params|
20
19
  find_call
21
- instance_variable_get(var_sym).attributes = send(params_sym)
22
- instance_exec(&block) if block
20
+ instance_variable_get(var_sym).attributes = params
23
21
  instance_variable_get(var_sym).save!
24
22
  end
25
- object.send(:define_method, :delete_call) do |&block|
26
- find_call
27
- instance_exec(&block) if block
28
- instance_variable_get(var_sym).destroy!
23
+ object.send(:define_method, :destroy_call) do |params|
24
+ find_call(params)
25
+ instance_variable_get(var_sym).destroy
26
+ end
27
+ object.send(:private, :new_call, :find_call, :create_call, :update_call, :destroy_call)
28
+ end
29
+
30
+ def self.define object, names, args = {}
31
+ names.each do |name|
32
+ options = args[name]||{}
33
+ type = options[:type]||name
34
+ raise RuntimeError "`name` is already defined." if object.private_method_defined?(name)
35
+ case type
36
+ when :show, :edit
37
+ object.send(:define_method, name) do
38
+ send(:find_call)
39
+ instance_exec(&options[:render]) if options[:render]
40
+ end
41
+ when :new
42
+ object.send(:define_method, name) do
43
+ params = options[:params] ? send(options[:params]) : {}
44
+ send(:new_call, params)
45
+ instance_exec(&options[:render]) if options[:render]
46
+ end
47
+ when :create, :update, :destroy
48
+ object.send(:define_method, name) do
49
+ params = send(options[:params]||:"#{name}_params")
50
+ rescue_respond(:"#{type}_call", params, options)
51
+ end
52
+ else
53
+ raise RuntimeError "Undefined action type `#{type}`."
54
+ end
29
55
  end
30
- object.send(:private, :new_call, :find_call, :create_call, :update_call, :delete_call)
31
56
  end
32
57
 
33
58
  end
@@ -23,7 +23,7 @@ module Rescue
23
23
  end
24
24
  end
25
25
  end
26
- base.extend Rescue::Controller::ClassMethods
26
+ base.send(:include, Rescue::Controller)
27
27
  end
28
28
 
29
29
  end
@@ -3,9 +3,11 @@ module Rescue
3
3
  module Controller
4
4
  class Flash
5
5
 
6
- def self.message object, action, key
7
- controllers = object.class.name.gsub('Controller', '').split('::').map {|e| e.underscore }
8
- scope = ([:views] + controllers) << action << :flash
6
+ def self.message object, key
7
+ scope = [:views]
8
+ scope += object.controller_path.split('/')
9
+ scope << object.action_name
10
+ scope << :flash
9
11
  text(key, scope)
10
12
  end
11
13
 
@@ -10,7 +10,7 @@ module Rescue
10
10
  render status: code, file: "#{Rails.root}/public/#{code}", layout: false and return
11
11
  end
12
12
  end
13
- base.extend Rescue::Controller::ClassMethods
13
+ base.send(:include, Rescue::Controller)
14
14
  end
15
15
 
16
16
  end
@@ -1,3 +1,3 @@
1
1
  module Rescue
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.4"
3
3
  end
data/lib/rescue-dog.rb CHANGED
@@ -3,7 +3,6 @@ require 'rescue/controller.rb'
3
3
  require "rescue/controllers/static.rb"
4
4
  require "rescue/controllers/dynamic.rb"
5
5
  require "rescue/controllers/action.rb"
6
- require "rescue/controllers/parameter.rb"
7
6
  require "rescue/controllers/flash.rb"
8
7
  require 'rescue/exceptions/application_error.rb'
9
8
  require 'rescue/exceptions/respond_error.rb'
@@ -3,74 +3,110 @@ require 'spec_helper'
3
3
 
4
4
  describe Rescue::Controller do
5
5
 
6
- describe Rescue::Controller::ClassMethods do
7
- let(:object) do
8
- Class.new ApplicationController do
9
- extend Rescue::Controller::ClassMethods
6
+ let(:object) do
7
+ Class.new ApplicationController do
8
+ include Rescue::Controller
9
+ rescue_controller RescueModel,
10
+ show: [:show_action],
11
+ edit: [:edit_action],
12
+ new: [:new_action],
13
+ create: { render: lambda { } ,rescue: lambda { } },
14
+ update: { render: lambda { } ,rescue: lambda { } },
15
+ destroy: { render: lambda { } ,rescue: lambda { } },
16
+ create_action: { type: :create ,render: lambda { } ,rescue: lambda { } },
17
+ update_action: { type: :update ,render: lambda { } ,rescue: lambda { } },
18
+ destroy_action: { type: :destroy ,render: lambda { } ,rescue: lambda { } }
19
+
20
+ def customized_create_action
21
+ rescue_respond(:create_call, create_params,
22
+ render: lambda { },
23
+ rescue: lambda { }
24
+ )
10
25
  end
11
- end
12
26
 
13
- [:rescue_associate, :rescue_controller, :define_action_method].each do |name|
14
- it "should be able to call method `#{name}`" do
15
- expect(object.public_methods.include? name).to be_true
27
+ def customized_update_action
28
+ rescue_respond(:update_call, update_params,
29
+ render: lambda { return true },
30
+ rescue: lambda { }
31
+ )
16
32
  end
17
- end
18
- end
19
33
 
20
- describe "Rescue::Controller#rescue_controller" do
21
- let(:object) do
22
- Class.new ApplicationController do
23
- extend Rescue::Controller::ClassMethods
24
- rescue_controller RescueModel, :show, :new, :edit, { create: {}, update: {}, delete: {} }
34
+ def customized_destroy_action
35
+ rescue_respond(:destroy_call, destroy_params,
36
+ render: lambda { },
37
+ rescue: lambda { }
38
+ )
25
39
  end
26
40
  end
41
+ end
27
42
 
28
- it "should be defined private method `find_params`" do
29
- expect(object.private_instance_methods.include? :find_params).to be_true
43
+ it "should be defined `rescue_respond`" do
44
+ expect(object.method_defined? :rescue_respond).to be_true
45
+ end
46
+
47
+ [:rescue_associate, :rescue_controller].each do |name|
48
+ it "should be able to call method `#{name}`" do
49
+ expect(object.public_methods.include? name).to be_true
30
50
  end
51
+ end
31
52
 
32
- [:find_call, :new_call, :create_call, :update_call, :delete_call].each do |name|
33
- it "should be defined private method `#{name}`" do
34
- expect(object.private_instance_methods.include? name).to be_true
53
+
54
+ describe "#rescue_respond" do
55
+ let(:object) do
56
+ clazz = Class.new ApplicationController do
57
+ include Rescue::Controller
35
58
  end
59
+ object = clazz.new
60
+ object.stub(:flash).and_return({})
61
+ object.stub(:controller_path).and_return('rescue')
62
+ object.stub(:action_name).and_return('action')
63
+ object.stub(:stub_call).with({}).and_return(nil)
64
+ object
36
65
  end
37
66
 
38
- [:new, :edit, :show, :create, :update, :delete].each do |name|
39
- it "should be defined public method `#{name}`" do
40
- expect(object.public_instance_methods.include? name).to be_true
41
- end
67
+ it "should be able to render" do
68
+ expect(
69
+ object.send(:rescue_respond, :stub_call, {},
70
+ render: lambda { return 'render' },
71
+ rescue: lambda { return 'rescue' })
72
+ ).to eq('render')
42
73
  end
43
74
 
44
- TestCase::Controller::RESCUE_OPTIONS.each do |args|
45
- context "when `rescue_controller` is called #{args}" do
46
- options = args.extract_options!
47
- methods = args + options.keys
75
+ end
48
76
 
49
- let(:object) do
50
- Class.new ApplicationController do
51
- extend Rescue::Controller::ClassMethods
52
- rescue_controller RescueModel, *args.dup, options.dup
53
- end
54
- end
77
+ describe Rescue::Controller::ClassMethods do
78
+ describe "#rescue_controller" do
55
79
 
56
- [:find_call, :new_call, :create_call, :update_call, :delete_call].each do |name|
57
- it "should be defined private method `#{name}`" do
58
- expect(object.private_instance_methods.include? name).to be_true
59
- end
80
+ let(:object) do
81
+ Class.new ApplicationController do
82
+ extend Rescue::Controller::ClassMethods
83
+ rescue_controller RescueModel,
84
+ show: [:show_action],
85
+ edit: [:edit_action],
86
+ new: [:new_action],
87
+ create: { render: lambda { } ,rescue: lambda { } },
88
+ update: { render: lambda { } ,rescue: lambda { } },
89
+ destroy: { render: lambda { } ,rescue: lambda { } },
90
+ create_action: { type: :create ,render: lambda { } ,rescue: lambda { } },
91
+ update_action: { type: :update ,render: lambda { } ,rescue: lambda { } },
92
+ destroy_action: { type: :destroy ,render: lambda { } ,rescue: lambda { } }
60
93
  end
94
+ end
61
95
 
62
- [:index, :new, :edit, :show, :create, :update, :delete].each do |name|
63
- if methods.include? name
64
- it "should be defined public method `#{name}`" do
65
- expect(object.public_instance_methods.include? name).to be_true
66
- end
67
- else
68
- it "should not be defined method `#{name}`" do
69
- expect(object.instance_methods.include? name).to be_false
70
- end
71
- end
96
+ [:find_call, :new_call, :create_call, :update_call, :destroy_call].each do |name|
97
+ it "should be defined private method `#{name}`" do
98
+ expect(object.private_instance_methods.include? name).to be_true
72
99
  end
73
100
  end
101
+
102
+ [ :new, :edit, :show, :create, :update, :destroy,
103
+ :show_action, :edit_action, :new_action,
104
+ :create_action, :update_action, :destroy_action ].each do |name|
105
+ it "should be defined public method `#{name}`" do
106
+ expect(object.public_instance_methods.include? name).to be_true
107
+ end
108
+ end
109
+
74
110
  end
75
111
  end
76
112
 
@@ -9,58 +9,41 @@ describe Rescue::Controller::Action do
9
9
  object = Object.new
10
10
  object.stub(:attributes=).and_return(true)
11
11
  object.stub(:save!).and_return(true)
12
- object.stub(:destroy!).and_return(true)
12
+ object.stub(:destroy).and_return(true)
13
13
  object
14
14
  end
15
+ clazz.stub(:where).and_return(clazz)
15
16
  clazz.stub(:find).and_return(clazz.new)
16
17
  clazz
17
18
  end
18
19
 
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
20
  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
21
+ let(:controller) do # Fake Controller
22
+ clazz = model
23
+ Class.new do
24
+ Rescue::Controller::Action.define_call self, clazz, :@rescue
41
25
  end
42
26
  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 }
27
+
28
+ let(:object) do
29
+ object = controller.new
30
+ object.stub(:rescuemodel_params).and_return({})
31
+ object.stub(:params).and_return({})
32
+ object
48
33
  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
34
+
35
+ let(:params) { {} }
36
+
37
+ [:find_call, :new_call, :create_call, :update_call, :destroy_call].each do |name|
38
+ it "should be defined private method `#{name}`" do
39
+ expect(controller.private_instance_methods.include? name).to be_true
40
+ end
41
+ it "should not raise error" do
42
+ expect { object.send(name, params) }.not_to raise_error
61
43
  end
62
-
63
44
  end
45
+
64
46
  end
65
47
 
66
48
  end
49
+
@@ -3,26 +3,23 @@ require 'spec_helper'
3
3
 
4
4
  describe Rescue::Controller::Flash do
5
5
 
6
- before(:all) do
7
- TestCase::Controller::FLASHS.each do |name|
8
- Object.const_set name, Class.new {}
9
- end
10
-
11
- Rescue.const_set :DogController, Class.new {}
12
- end
6
+ describe "#message" do
7
+ ['default', 'rescue', 'rescue/dog'].each do |name|
8
+ context "when #{{ controller: name, action: :action }}" do
13
9
 
14
- describe "Rescue::Controller::Flash#message" do
15
- TestCase::Controller::FLASHS.each do |name|
16
- [:success, :error].each do |status|
17
- it do
18
- expect(Rescue::Controller::Flash.message(Object.const_get(name).new, :action, status)).to eq("#{name.to_s.underscore} #{status}")
10
+ let(:controller) do
11
+ clazz = Class.new
12
+ clazz.stub(:controller_path).and_return(name)
13
+ clazz.stub(:controller_name).and_return(name)
14
+ clazz.stub(:action_name).and_return(:action)
15
+ clazz
19
16
  end
20
- end
21
- end
22
17
 
23
- [:success, :error].each do |status|
24
- it do
25
- expect(Rescue::Controller::Flash.message(Rescue::DogController.new, :action, status)).to eq(status.to_s)
18
+ [:success, :error].each do |status|
19
+ it do
20
+ expect(Rescue::Controller::Flash.message(controller, status)).to eq("#{name} #{status}")
21
+ end
22
+ end
26
23
  end
27
24
  end
28
25
  end
data/spec/test_case.rb CHANGED
@@ -4,14 +4,6 @@ class TestCase
4
4
  ERRORS = { BadRequest: 400, Unauthorized: 401, NotFound: 404, ServerError: 500 }
5
5
  FORMATS = [Mime::Type.new("text/html", :html), Mime::Type.new("application/json", :json)]
6
6
 
7
- RESCUE_OPTIONS = [
8
- [],
9
- [:show],
10
- [:new, :show, :create],
11
- [:new, :show, :edit, :create, :update],
12
- [:new, :show, :edit, { create: {}, update: {}, delete: {} }],
13
- ]
14
-
15
7
  FLASHS = [:DefaultController, :RescueController, :RescueDogController]
16
8
  end
17
9
 
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.2
4
+ version: 0.3.4
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-16 00:00:00.000000000 Z
11
+ date: 2013-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -51,7 +51,6 @@ files:
51
51
  - lib/rescue/controllers/action.rb
52
52
  - lib/rescue/controllers/dynamic.rb
53
53
  - lib/rescue/controllers/flash.rb
54
- - lib/rescue/controllers/parameter.rb
55
54
  - lib/rescue/controllers/static.rb
56
55
  - lib/rescue/exceptions/application_error.rb
57
56
  - lib/rescue/exceptions/respond_error.rb
@@ -1,17 +0,0 @@
1
- # coding: UTF-8
2
- module Rescue
3
- module Controller
4
- class Parameter
5
-
6
- def self.define object
7
- id = Rescue.config.primary_key
8
- object.send(:define_method, :find_params) do
9
- params.require(id)
10
- params.permit(id)
11
- end
12
- object.send(:private, :find_params)
13
- end
14
-
15
- end
16
- end
17
- end