convenient-actionpack 0.0.1

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.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ convenient-actionpack
2
+ =====
3
+
4
+ *Under construction... Watch your head.*
5
+
6
+ Making common things easier for ActionController, etc.
7
+
8
+ ### Setup
9
+
10
+ In your Rails 3.1+ project, add this to your Gemfile:
11
+
12
+ gem 'convenient-actionpack', :git => 'git://github.com/garysweaver/convenient-actionpack.git'
13
+
14
+ Then run:
15
+
16
+ bundle install
17
+
18
+ ### Usage
19
+
20
+ Provides a wrap method to rescue StandardError to return an informative JSON error from JSON services instead of HTML when there is an error:
21
+
22
+ def create
23
+ wrap do
24
+ @my_model.save
25
+ respond_with @my_model
26
+ end
27
+ end
28
+
29
+ If you don't like the standard functionality, you can implement a single error handling method in your ApplicationController to handle errors from all wrapped action methods:
30
+
31
+ def on_action_error(error)
32
+ case params[:format].to_sym
33
+ when :json
34
+ respond_with({errors: [error.message]}, location: nil, status: :internal_server_error)
35
+ else
36
+ raise error # should include prior backtrace
37
+ end
38
+ end
39
+
40
+ ### Future
41
+
42
+ Originally was meant to provide a respond method that would skirt some of the unintuitive behavior of respond_with like [#2798][rails2798], and provide additional functionality, but that is commented for now. Maybe will get to it later. Feel free to fork/submit pull requests.
43
+
44
+ ### License
45
+
46
+ Copyright (c) 2012 Gary S. Weaver, released under the [MIT license][lic].
47
+
48
+ [rails2798]: https://github.com/rails/rails/issues/2798
49
+ [lic]: http://github.com/garysweaver/convenient-actionpack/blob/master/LICENSE
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new(:test) do |t|
4
+ t.libs << 'test'
5
+ t.test_files = FileList['test/**/*_test.rb']
6
+ t.verbose = true
7
+ end
8
+
9
+ desc "Run tests"
10
+ task :default => :test
@@ -0,0 +1,3 @@
1
+ require 'convenient-actionpack/version'
2
+ require 'convenient-actionpack/controller'
3
+ require 'convenient-actionpack/railtie' if defined?(Rails)
@@ -0,0 +1,62 @@
1
+ module Convenient
2
+ module Controller
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ end
7
+
8
+ def wrap
9
+ begin
10
+ yield
11
+ rescue
12
+ on_action_error($!)
13
+ end
14
+ end
15
+
16
+ # This is problematic and respond_with seems to function mostly normally.
17
+ # TODO: how to get around needing to specify location, as mentioned here, without interfering with existing functionality for html format, etc.: https://github.com/rails/rails/issues/2798
18
+ #def respond(*args)
19
+ # #puts "respond args=#{args.inspect}"
20
+ # options = args.extract_options!
21
+ # raise ArgumentError.new 'respond takes one resource and an optional array of options' if args.size != 1
22
+ # data = args[0]
23
+ # if data.respond_to?(:errors) && data.try(:errors) && !(data.errors.respond_to?(:size) && data.errors.size == 0)
24
+ # # was setting status: :unprocessable_entity but that is done by respond_with already
25
+ # respond_with({errors: data.errors}, {location: nil}.merge!(options)) do
26
+ # yield if block_given?
27
+ # end
28
+ # else
29
+ # # respond_with requires location to be nil if not required, or it can be specified in options, in which case, we'll take it.
30
+ # case params[:format].to_sym
31
+ # when :json
32
+ # options[:location] = (options[:status] == :accepted || params[:action] == :create) && !options[:location] ? show_uri(data) : options[:location]
33
+ # end
34
+ #
35
+ # # respond_with should do this already
36
+ # #options[:status] = :created if params[:action] == :create && !options[:status]
37
+ #
38
+ # #puts "GOING TO respond_with #{data.inspect}, #{options.inspect}"
39
+ #
40
+ # respond_with data, options do
41
+ # # respond_with should do this already
42
+ # #head :no_content if params[:action] == :destroy && !block_given?
43
+ #
44
+ # yield if block_given?
45
+ # end
46
+ # end
47
+ #end
48
+ #
49
+ #def show_uri(data)
50
+ # send("show_#{data.class.name.underscore}_path", data)
51
+ #end
52
+
53
+ def on_action_error(error)
54
+ case params[:format].to_sym
55
+ when :json
56
+ respond_with({errors: [error.message]}, {location: nil, status: :internal_server_error})
57
+ else
58
+ raise error # should include prior backtrace
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,11 @@
1
+ require 'convenient-actionpack'
2
+
3
+ module Convenient
4
+ class Railtie < Rails::Railtie
5
+ initializer "convenient-actionpack.action_controller" do
6
+ ActiveSupport.on_load(:action_controller) do
7
+ include Convenient::Controller
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Convenient
2
+ VERSION = '0.0.1'
3
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: convenient-actionpack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gary S. Weaver
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.0
30
+ description: Convenient functionality for actionpack.
31
+ email:
32
+ - garysweaver@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/convenient-actionpack/controller.rb
38
+ - lib/convenient-actionpack/railtie.rb
39
+ - lib/convenient-actionpack/version.rb
40
+ - lib/convenient-actionpack.rb
41
+ - Rakefile
42
+ - README.md
43
+ homepage: https://github.com/garysweaver/convenient-actionpack
44
+ licenses:
45
+ - MIT
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.8.24
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Convenient functionality for actionpack.
68
+ test_files: []