make_restful 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/make_restful.rb CHANGED
@@ -8,12 +8,14 @@ module MakeRestful
8
8
  autoload :Loaders
9
9
  autoload :RestMethods
10
10
  autoload :Rendering
11
- autoload :Specification
11
+ autoload :Callbacks
12
+ autoload :Sugar
12
13
 
14
+ extend Callbacks
13
15
  include Loaders
14
16
  include RestMethods
15
17
  include Rendering
16
- include Specification
18
+ include Sugar
17
19
 
18
20
  delegate :allow_method?, :allow_format?, to: :klass
19
21
 
@@ -24,7 +26,7 @@ module MakeRestful
24
26
 
25
27
  resource name.underscore.gsub(/(.*)_controller/,'\1').singularize
26
28
  self.allowed_formats = [:json, :jsonp, :xml]
27
- self.allowed_methods = [:index, :get, :put, :post, :delete]
29
+ self.allowed_methods = [:index, :show, :update, :destroy, :create, :spec]
28
30
  self.finder = :id
29
31
 
30
32
  end
@@ -0,0 +1,15 @@
1
+ module MakeRestful::Callbacks
2
+
3
+ def before_render(options={}, &block)
4
+
5
+ end
6
+
7
+ module ClassMethods
8
+
9
+ def render_callback(options={}, &block)
10
+
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -4,7 +4,6 @@ module MakeRestful::Loaders
4
4
 
5
5
  def load_resource
6
6
  @resource = self.class.resource_class
7
- @resource.params = params.except(:action, :controller, :id) if @resource.respond_to?(:params=)
8
7
  end
9
8
 
10
9
  def load_collection
@@ -25,6 +24,7 @@ module MakeRestful::Loaders
25
24
  if @collection.empty?
26
25
  @response = { error: 'no results found' }
27
26
  @status = :not_found
27
+ render(load_template: false) and return
28
28
  end
29
29
 
30
30
  eval("@#{self.class.resource_name} = @collection")
@@ -35,15 +35,17 @@ module MakeRestful::Loaders
35
35
  finder = opts[:finder] || self.class.finder
36
36
  finder_value = params[finder]
37
37
 
38
- if finder && @resource.respond_to?(:find)
39
- @instance = @resource.where(finder.to_sym => finder_value).first
40
- eval "@#{self.class.instance_name} = @instance"
38
+ @instance = if finder && @resource.respond_to?(:where)
39
+ @resource.where(finder.to_sym => finder_value).first
40
+ else
41
+ @resource.find(finder_value)
41
42
  end
43
+ eval "@#{self.class.instance_name} = @instance"
42
44
 
43
45
  unless @instance
44
46
  @response = { error: "Could not find record with #{finder}: #{finder_value}" }
45
47
  @status = :not_found
46
- render_formats(load_template: false) and return
48
+ render(load_template: false) and return
47
49
  end
48
50
 
49
51
  end
@@ -52,21 +54,9 @@ module MakeRestful::Loaders
52
54
  return if allow_method? params[:action]
53
55
  @response = { error: "Method not allowed"}
54
56
  @status = :method_not_allowed
55
- render
57
+ render(load_template: false)
56
58
  end
57
59
 
58
60
  alias :verify_action! :verify_method!
59
61
 
60
- def method_missing(m, *args, &block)
61
- if /load_instance_by_(?<finder>[a-z_]+)/ =~ m.to_s
62
- load_instance(finder: finder.to_sym)
63
- else
64
- super
65
- end
66
- end
67
-
68
- def respond_to_missing?(method_name, include_private = false)
69
- true if /load_instance_by_(?<finder>[a-z_]+)/ =~ method_name.to_s
70
- end
71
-
72
62
  end
@@ -3,9 +3,12 @@ module MakeRestful::Rendering
3
3
  private
4
4
 
5
5
  ## Rendering
6
- def render(options = {}, deprecated_status = nil, &block)
7
-
8
- super and return if template_exists?([params[:controller], params[:action]].join('/')) && allow_format?(formats) unless options[:load_template] == false
6
+ def render(*args, &block)
7
+ options = args.last.is_a?(Hash) && args.last.extractable_options? ? args.last : {}
8
+ template_present = template_exists?([params[:controller], params[:action]].join('/'))
9
+ options[:load_template] ||= true
10
+ cleaned_options = options.delete_if{ |k,v| [:load_template, :status].include?(k) }
11
+ super and return if (( template_present && options[:load_template]) || cleaned_options.present? ) && allow_format?(formats)
9
12
 
10
13
  # Set the response from the chain
11
14
  response = @response || @instance || @collection
@@ -0,0 +1,20 @@
1
+ module MakeRestful::Sugar
2
+ extend ActiveSupport::Concern
3
+
4
+ def method_missing(m, *args, &block)
5
+ if /load_instance_by_(?<finder>[a-z_]+)/ =~ m.to_s
6
+ load_instance(finder: finder.to_sym)
7
+ else
8
+ super
9
+ end
10
+ end
11
+
12
+ def respond_to_missing?(method_name, include_private = false)
13
+ if /load_instance_by_(?<finder>[a-z_]+)/ =~ method_name.to_s
14
+ true
15
+ else
16
+ false
17
+ end
18
+ end
19
+
20
+ end
@@ -1,3 +1,3 @@
1
1
  module MakeRestful
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: make_restful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-02 00:00:00.000000000 Z
12
+ date: 2012-10-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -72,12 +72,12 @@ files:
72
72
  - README.md
73
73
  - Rakefile
74
74
  - lib/make_restful.rb
75
+ - lib/make_restful/callbacks.rb
75
76
  - lib/make_restful/class_methods.rb
76
77
  - lib/make_restful/loaders.rb
77
78
  - lib/make_restful/rendering.rb
78
79
  - lib/make_restful/rest_methods.rb
79
- - lib/make_restful/specification.rb
80
- - lib/make_restful/specification/builder.rb
80
+ - lib/make_restful/sugar.rb
81
81
  - lib/make_restful/version.rb
82
82
  - make_restful.gemspec
83
83
  homepage: http://github.com/jwaldrip/make_restful
@@ -1,25 +0,0 @@
1
- module MakeRestful::Specification
2
- extend ActiveSupport::Concern
3
- extend ActiveSupport::Autoload
4
-
5
- autoload :Builder
6
- # autoload :MethodBuilder
7
-
8
- def specification
9
- nil
10
- end
11
-
12
- module ClassMethods
13
-
14
- def specification(&block)
15
-
16
- spec_result = MakeRestful::Specification::Builder.new(self, &block).freeze
17
-
18
- define_method :specification do
19
- spec_result.render
20
- end
21
- end
22
-
23
- end
24
-
25
- end
@@ -1,75 +0,0 @@
1
- class MakeRestful::Specification::Builder
2
-
3
- attr_reader :_controller_, :_methods_, :_description_, :_formats_, :_attributes_, :_resource_, :_default_methods_, :_default_attributes_, :_default_formats_
4
-
5
- private
6
-
7
- def initialize(controller, &block)
8
- @_controller_ = controller
9
- @_methods_ = []
10
- @_default_methods_ = default_supported_methods
11
- @_methods_ = []
12
- @_attributes_ = []
13
- @_default_attributes_ = controller.resource_class.new.attributes.keys
14
- @_default_formats_ = controller.allowed_formats
15
- @_formats_ = []
16
- @_description_ = "Api for #{controller.resource_name.titleize}"
17
- @_resource_ = controller.instance_name
18
- instance_eval(&block) if block_given?
19
- end
20
-
21
- def method(method, options={})
22
- method = {
23
- method: method,
24
- path: options[:path],
25
- returns: options[:returns]
26
- }
27
- @_methods_ << method
28
- method
29
- end
30
-
31
- def description(string)
32
- @_description_ = string
33
- end
34
-
35
- def resource(string)
36
- @_resource_ = string
37
- end
38
-
39
- def attribute(attr)
40
- @_attributes_ << attr
41
- attr
42
- end
43
-
44
- def attributes(*args)
45
- @_attributes_ += args.flatten
46
- end
47
-
48
- def format(attr)
49
- @_formats_ << attr
50
- attr
51
- end
52
-
53
- def formats(*args)
54
- @_formats_ += args
55
- end
56
-
57
- def default_supported_methods
58
- method :get, path: "/", returns: "collection" if _controller_.allow_method?(:list) || _controller_.allow_method?(:index)
59
- method :post, path: "/", returns: "resource" if _controller_.allow_method?(:create) || _controller_.allow_method?(:post)
60
- method :get, path: "/:id", returns: "resource" if _controller_.allow_method?(:show) || _controller_.allow_method?(:get)
61
- method :put, path: "/:id", returns: "resource" if _controller_.allow_method?(:update) || _controller_.allow_method?(:put)
62
- method :delete, path: "/:id", returns: "success" if _controller_.allow_method?(:destroy) || _controller_.allow_method?(:delete)
63
- end
64
-
65
- def render
66
- {
67
- resource: _resource_,
68
- formats: _formats_.present? ? _formats_ : _default_formats_,
69
- description: _description_,
70
- attributes: _attributes_.present? ? _attributes_ : _default_attributes_,
71
- methods: _methods_.present? ? _methods_ : _default_methods_
72
- }
73
- end
74
-
75
- end