rescue-dog 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea1931c553f09ed1cf0e12f65479dbbc0a949d76
4
+ data.tar.gz: 49b18603085618a2120933598e512d0e9a3c1491
5
+ SHA512:
6
+ metadata.gz: 27f9b874868af6234c7a1ca9cdb4d7e0b7d597e100303991b554e2ece7d20c0441db504593c2f2b647fea7414caab95d64d22349d9a9c41d577f27798a553a6f
7
+ data.tar.gz: 788415d867e62f4083d66675aba84476ae5458c3da8024851c3b991b0b9215d56edb66a1e14e89b493f173f52697d26a00342b7d85fc7d5d1f3321a32407feff
data/build_gem.sh ADDED
@@ -0,0 +1,48 @@
1
+ #!/bin/sh
2
+ LIB_NAME="rescue-dog"
3
+ if [ $# -ne 2 ]
4
+ then
5
+ echo "Usage: $0 {version} {target}"
6
+ exit 1
7
+ fi
8
+ GEMSPEC="${LIB_NAME}.gemspec"
9
+ PKG_FILE="${LIB_NAME}-$1.gem"
10
+
11
+ echo "[RUN] gem install"
12
+ bundle install
13
+ echo "[RUN] rake"
14
+ bundle exec rake
15
+
16
+ if [ $? -eq 1 ]
17
+ then
18
+ exit 1
19
+ fi
20
+ echo "[RUN] gem build ${GEMSPEC}"
21
+ gem build ${GEMSPEC}
22
+ if [ $? -eq 1 ]
23
+ then
24
+ exit 1
25
+ fi
26
+
27
+ echo "[RUN] mv ${PKG_FILE} pkg/"
28
+ mv ${PKG_FILE} ./pkg
29
+
30
+ case "$2" in
31
+ "install")
32
+ echo "[RUN] rake install pkg/${PKG_FILE}"
33
+ bundle exec rake install --trace
34
+ ;;
35
+ "deploy")
36
+ echo "[RUN] gem push pkg/${PKG_FILE}"
37
+ gem push pkg/${PKG_FILE}
38
+
39
+ echo "[RUN] git tag -a version-$1"
40
+ git tag -a version-$1 -m ""
41
+ git push --tags
42
+ ;;
43
+ *)
44
+ echo "FATAL: invalid target"
45
+ exit 1
46
+ ;;
47
+ esac
48
+
@@ -0,0 +1,24 @@
1
+ en:
2
+ default:
3
+ flash:
4
+ success: "default_controller success"
5
+ error: "default_controller error"
6
+
7
+ views:
8
+ rescue:
9
+ action:
10
+ flash:
11
+ success: "rescue_controller success"
12
+ error: "rescue_controller error"
13
+ dog:
14
+ action:
15
+ flash:
16
+ success: "success"
17
+ error: "error"
18
+
19
+ rescue_dog:
20
+ action:
21
+ flash:
22
+ success: "rescue_dog_controller success"
23
+ error: "rescue_dog_controller error"
24
+
data/lib/rescue/config.rb CHANGED
@@ -12,6 +12,7 @@ module Rescue
12
12
 
13
13
  class Configuration #:nodoc:
14
14
  include ActiveSupport::Configurable
15
+ config_accessor :primary_key
15
16
  config_accessor :respond_name
16
17
  config_accessor :suppress_response_codes
17
18
 
@@ -26,6 +27,7 @@ module Rescue
26
27
  end
27
28
 
28
29
  configure do |config|
30
+ config.primary_key = :id
29
31
  config.respond_name = :respond_status
30
32
  config.suppress_response_codes = false
31
33
  end
@@ -1,5 +1,4 @@
1
1
  # coding: UTF-8
2
-
3
2
  module Rescue
4
3
  module Controller
5
4
 
@@ -37,6 +36,54 @@ module Rescue
37
36
  end
38
37
  end
39
38
 
39
+ 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)
47
+
48
+ [:new].each do |type|
49
+ args = options.delete(type) || (actions.delete(type) ? {} : nil)
50
+ define_action_method(type, :new_call, args) if args
51
+ end
52
+
53
+ [:show, :edit].each do |type|
54
+ args = options.delete(type) || (actions.delete(type) ? {} : nil)
55
+ define_action_method(type, :find_call, args) if args
56
+ end
57
+
58
+ [:create, :update, :delete].each do |type|
59
+ args = options.delete(type) || (actions.delete(type) ? {} : nil)
60
+ if args
61
+ args[:flash] ||= true
62
+ define_action_method(type, :"#{type}_call", args) if args
63
+ end
64
+ end
65
+ end
66
+
67
+ def define_action_method name, call_method, options = {}
68
+ success_message = options[:success]
69
+ error_message = options[:error]
70
+ if options[:flash]
71
+ success_message ||= Flash.message(self, name, :success)
72
+ error_message ||= Flash.message(self, name, :error)
73
+ end
74
+
75
+ define_method name do
76
+ begin
77
+ send(call_method)
78
+ flash[:success] = success_message unless success_message.blank?
79
+ instance_exec(&options[:render]) if options[:render]
80
+ rescue
81
+ flash.now[:error] = error_message unless error_message.blank?
82
+ instance_exec(&options[:rescue]) if options[:rescue]
83
+ end
84
+ end
85
+ end
86
+
40
87
  end
41
88
  end
42
89
  end
@@ -0,0 +1,32 @@
1
+ # coding: UTF-8
2
+ module Rescue
3
+ module Controller
4
+ class Action
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)
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))
13
+ end
14
+ object.send(:define_method, :create_call) do
15
+ instance_variable_set(var_sym, clazz.new(send(params_sym)))
16
+ instance_variable_get(var_sym).save!
17
+ end
18
+ object.send(:define_method, :update_call) do
19
+ find_call
20
+ instance_variable_get(var_sym).attributes = send(params_sym)
21
+ instance_variable_get(var_sym).save!
22
+ end
23
+ object.send(:define_method, :delete_call) do
24
+ find_call
25
+ instance_variable_get(var_sym).destroy!
26
+ end
27
+ object.send(:private, :new_call, :find_call, :create_call, :update_call, :delete_call)
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,24 @@
1
+ # coding: UTF-8
2
+ module Rescue
3
+ module Controller
4
+ class Flash
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
9
+ text(key, scope)
10
+ end
11
+
12
+ def self.default key
13
+ s = I18n.t(key, scope: [:default, :flash], default: '')
14
+ s.blank? ? nil : s
15
+ end
16
+
17
+ def self.text key, scope = []
18
+ s = I18n.t(key, scope: scope, default: '')
19
+ s.blank? ? default(key) : s
20
+ end
21
+ private_class_method :default, :text
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,17 @@
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
@@ -1,3 +1,3 @@
1
1
  module Rescue
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/rescue-dog.rb CHANGED
@@ -2,6 +2,9 @@ require 'rescue/config.rb'
2
2
  require 'rescue/controller.rb'
3
3
  require "rescue/controllers/static.rb"
4
4
  require "rescue/controllers/dynamic.rb"
5
+ require "rescue/controllers/action.rb"
6
+ require "rescue/controllers/parameter.rb"
7
+ require "rescue/controllers/flash.rb"
5
8
  require 'rescue/exceptions/application_error.rb'
6
9
  require 'rescue/exceptions/respond_error.rb'
7
10
 
data/rescue-dog.gemspec CHANGED
@@ -11,11 +11,12 @@ Gem::Specification.new do |gem|
11
11
  gem.description = %q{respond to an exception raised in Rails}
12
12
  gem.summary = %q{define respond methods}
13
13
  gem.homepage = "https://github.com/yulii/rescue-dog"
14
+ gem.license = 'MIT'
14
15
 
15
16
  gem.files = `git ls-files`.split($/)
16
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
19
  gem.require_paths = ["lib"]
19
20
 
20
- gem.add_dependency 'rails', '>= 3.2.11'
21
+ gem.add_dependency 'rails', '>= 4.0.0'
21
22
  end
@@ -38,8 +38,8 @@ app.initialize!
38
38
  # routing
39
39
  app.routes.draw do
40
40
  STATUSES.each do |name, code|
41
- get "/static/#{name}" =>"static##{name}" ,as: name
42
- get "/dynamic/#{name}" =>"dynamic##{name}" ,as: name
41
+ get "/static/#{name}" =>"static##{name}" ,as: "static_#{name}"
42
+ get "/dynamic/#{name}" =>"dynamic##{name}" ,as: "dynamic_#{name}"
43
43
  end
44
44
 
45
45
  Rescue::ApplicationError::STATUS_CODES.each do |code, e|
@@ -91,4 +91,7 @@ class StatusController < ApplicationController
91
91
  end
92
92
  end
93
93
 
94
+ # models
95
+ class RescueModel ; end
96
+
94
97
  Object.const_set(:ApplicationHelper,Module.new)
@@ -3,16 +3,74 @@ require 'spec_helper'
3
3
 
4
4
  describe Rescue::Controller do
5
5
 
6
- let(:object) do
7
- clazz = Class.new ApplicationController do
8
- include Rescue::Controller::ClassMethods
6
+ describe Rescue::Controller::ClassMethods do
7
+ let(:object) do
8
+ Class.new ApplicationController do
9
+ extend Rescue::Controller::ClassMethods
10
+ end
11
+ end
12
+
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
16
+ end
9
17
  end
10
- clazz.new
11
18
  end
12
19
 
13
- describe "include ClassMethods module" do
14
- it "expects to be call `rescue_associate` method" do
15
- expect(object.methods.include? :rescue_associate).to be_true
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: {} }
25
+ end
26
+ end
27
+
28
+ it "should be defined private method `find_params`" do
29
+ expect(object.private_instance_methods.include? :find_params).to be_true
30
+ end
31
+
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
35
+ end
36
+ end
37
+
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
42
+ end
43
+
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
48
+
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
55
+
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
60
+ end
61
+
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
72
+ end
73
+ end
16
74
  end
17
75
  end
18
76
 
@@ -0,0 +1,30 @@
1
+ # coding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe Rescue::Controller::Flash do
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
13
+
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}")
19
+ end
20
+ end
21
+ end
22
+
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)
26
+ end
27
+ end
28
+ end
29
+
30
+ end
data/spec/spec_helper.rb CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  require 'rescue-dog'
4
4
  require 'coveralls'
5
5
 
6
- require File.join(File.dirname(__FILE__), 'rails_spec_app')
6
+ require File.join(File.dirname(__FILE__), 'fake_rails')
7
7
  require File.join(File.dirname(__FILE__), 'test_case')
8
8
  require 'capybara/rails'
9
9
 
data/spec/test_case.rb CHANGED
@@ -3,6 +3,16 @@ class TestCase
3
3
  class Controller
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
+
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
+ FLASHS = [:DefaultController, :RescueController, :RescueDogController]
6
16
  end
7
17
 
8
18
  end
metadata CHANGED
@@ -1,32 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rescue-dog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - yulii
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-29 00:00:00.000000000 Z
11
+ date: 2013-09-08 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rails
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
- version: 3.2.11
19
+ version: 4.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
- version: 3.2.11
26
+ version: 4.0.0
30
27
  description: respond to an exception raised in Rails
31
28
  email:
32
29
  - yuliinfo@gmail.com
@@ -46,11 +43,15 @@ files:
46
43
  - app/views/errors/404.html.erb
47
44
  - app/views/errors/500.html.erb
48
45
  - app/views/layouts/application.html.erb
49
- - deploy_gem.sh
46
+ - build_gem.sh
47
+ - config/locales/rescue.yml
50
48
  - lib/rescue-dog.rb
51
49
  - lib/rescue/config.rb
52
50
  - lib/rescue/controller.rb
51
+ - lib/rescue/controllers/action.rb
53
52
  - lib/rescue/controllers/dynamic.rb
53
+ - lib/rescue/controllers/flash.rb
54
+ - lib/rescue/controllers/parameter.rb
54
55
  - lib/rescue/controllers/static.rb
55
56
  - lib/rescue/exceptions/application_error.rb
56
57
  - lib/rescue/exceptions/respond_error.rb
@@ -64,42 +65,44 @@ files:
64
65
  - public/500.html
65
66
  - public/500.json
66
67
  - rescue-dog.gemspec
67
- - spec/rails_spec_app.rb
68
+ - spec/fake_rails.rb
68
69
  - spec/rescue/controller_spec.rb
69
70
  - spec/rescue/controllers/dynamic_spec.rb
71
+ - spec/rescue/controllers/flash_spec.rb
70
72
  - spec/rescue/controllers/static_spec.rb
71
73
  - spec/rescue/exceptions/respond_error_spec.rb
72
74
  - spec/rescue_spec.rb
73
75
  - spec/spec_helper.rb
74
76
  - spec/test_case.rb
75
77
  homepage: https://github.com/yulii/rescue-dog
76
- licenses: []
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
77
81
  post_install_message:
78
82
  rdoc_options: []
79
83
  require_paths:
80
84
  - lib
81
85
  required_ruby_version: !ruby/object:Gem::Requirement
82
- none: false
83
86
  requirements:
84
- - - ! '>='
87
+ - - '>='
85
88
  - !ruby/object:Gem::Version
86
89
  version: '0'
87
90
  required_rubygems_version: !ruby/object:Gem::Requirement
88
- none: false
89
91
  requirements:
90
- - - ! '>='
92
+ - - '>='
91
93
  - !ruby/object:Gem::Version
92
94
  version: '0'
93
95
  requirements: []
94
96
  rubyforge_project:
95
- rubygems_version: 1.8.24
97
+ rubygems_version: 2.0.7
96
98
  signing_key:
97
- specification_version: 3
99
+ specification_version: 4
98
100
  summary: define respond methods
99
101
  test_files:
100
- - spec/rails_spec_app.rb
102
+ - spec/fake_rails.rb
101
103
  - spec/rescue/controller_spec.rb
102
104
  - spec/rescue/controllers/dynamic_spec.rb
105
+ - spec/rescue/controllers/flash_spec.rb
103
106
  - spec/rescue/controllers/static_spec.rb
104
107
  - spec/rescue/exceptions/respond_error_spec.rb
105
108
  - spec/rescue_spec.rb
data/deploy_gem.sh DELETED
@@ -1,34 +0,0 @@
1
- #!/bin/sh
2
- LIB_NAME="rescue-dog"
3
- if [ $# -ne 1 ]
4
- then
5
- echo "invalid VERSION"
6
- exit 1
7
- fi
8
- GEMSPEC="${LIB_NAME}.gemspec"
9
- PKG_FILE="${LIB_NAME}-$1.gem"
10
-
11
- echo "[RUN] gem install"
12
- bundle install
13
- echo "[RUN] rake"
14
- bundle exec rake
15
-
16
- if [ $? -eq 1 ]
17
- then
18
- exit 1
19
- fi
20
- echo "[RUN] gem build ${GEMSPEC}"
21
- gem build ${GEMSPEC}
22
- if [ $? -eq 1 ]
23
- then
24
- exit 1
25
- fi
26
-
27
- echo "[RUN] mv ${PKG_FILE} pkg/"
28
- mv ${PKG_FILE} ./pkg
29
- echo "[RUN] gem push pkg/${PKG_FILE}"
30
- gem push pkg/${PKG_FILE}
31
-
32
- echo "[RUN] git tag -a version-$1"
33
- git tag -a version-$1 -m ""
34
- git push --tags