make_restful 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,13 +19,7 @@ module MakeRestful::ClassMethods
19
19
  end
20
20
 
21
21
  def resource(klass)
22
-
23
22
  self.resource_class = klass.to_s.classify.constantize
24
-
25
- before_filter :load_resource
26
- before_filter :load_collection, only: [:index]
27
- before_filter :load_instance, only: [:show, :destroy, :update]
28
-
29
23
  end
30
24
 
31
25
  def collection(options={}, &block)
@@ -60,9 +54,4 @@ module MakeRestful::ClassMethods
60
54
 
61
55
  end
62
56
 
63
- def paginates!(opts={})
64
- opts.assert_valid_keys :node
65
- self.pagination = opts
66
- end
67
-
68
57
  end
@@ -3,23 +3,12 @@ module MakeRestful::Loaders
3
3
 
4
4
  included do
5
5
 
6
- before_filter :verify_method!
7
- before_filter :verify_format!
6
+ before_filter :verify_method!, :verify_format!, :load_resource
7
+ before_filter :load_collection, unless: ->(c){ c.params[:id].present? || c.params[:action] == 'new' || request.method.upcase != 'GET' }
8
+ before_filter :load_instance, if: ->(c){ c.params[:id].present? }
8
9
 
9
10
  collection do
10
- if pagination && !params.has_key?(:all)
11
- paginated_collection = @resource.paginate page: params[:page], per_page: (params[:per_page] || pagination[:per_page] || 25)
12
- { resource_name.to_sym => paginated_collection,
13
- pagination: {
14
- current_page: paginated_collection.current_page,
15
- per_page: paginated_collection.per_page,
16
- total_entries: paginated_collection.total_entries,
17
- total_pages: paginated_collection.total_pages
18
- }
19
- }
20
- else
21
- @resource.all
22
- end
11
+ @resource.all
23
12
  end
24
13
 
25
14
  instance do
@@ -46,24 +35,22 @@ module MakeRestful::Loaders
46
35
  def load_collection
47
36
  loader = collection_loader[params[:action].to_sym] || collection_loader[:default]
48
37
 
49
- @collection = instance_eval(&loader)
38
+ self.collection = instance_eval(&loader)
50
39
 
51
40
  unless @collection.present?
52
41
  @response = { error: 'no results found' }
53
42
  @status = :not_found
54
43
  render(load_template: false) and return
55
44
  end
56
-
57
- eval("@#{resource_name} = @collection")
58
45
  end
59
46
 
60
47
  def load_instance
61
48
  loader = instance_loader[params[:action].to_sym] || instance_loader[:default]
62
49
 
63
- @instance = instance_eval(&loader)
50
+ self.instance = instance_eval(&loader)
64
51
  eval "@#{instance_name} = @instance"
65
52
 
66
- unless @instance
53
+ unless @instance.present?
67
54
  @response = { error: "Record Not Found" }
68
55
  @status = :not_found
69
56
  render(load_template: false) and return
@@ -71,6 +58,16 @@ module MakeRestful::Loaders
71
58
 
72
59
  end
73
60
 
61
+ def instance= (instance)
62
+ @instance = instance
63
+ eval "@#{instance_name} = @instance"
64
+ end
65
+
66
+ def collection=(collection)
67
+ @collection = collection
68
+ eval("@#{resource_name} = @collection")
69
+ end
70
+
74
71
  def verify_method!
75
72
  return if allowed_actions.allowed?(params[:action]) || !%w{GET POST PUT PATCH}.include?(request.method)
76
73
  @response = { error: "Method not allowed" }
@@ -1,31 +1,38 @@
1
1
  module MakeRestful::Rendering
2
+ extend ActiveSupport::Concern
2
3
 
3
4
  private
4
5
 
5
6
  ## Rendering
6
7
  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 if options[:load_template].nil?
10
- cleaned_options = options.reject{ |k,v| [:load_template, :status].include?(k) }
8
+ options = args.extract_options!
11
9
 
12
- super and return if (( template_present && options[:load_template]) || cleaned_options.present? )
10
+ if handle_errors
11
+ options[:load_template] = false
12
+ end
13
+
14
+ template_present = template_exists? [params[:controller], params[:action]].join('/')
15
+
16
+ options[:status] ||= @status
17
+
18
+ load_template = options.delete(:load_template) == true
19
+
20
+ super(*args, options, &block) and return if ( template_present && load_template ) || options.except(:status).present?
13
21
 
14
22
  # Set the response from the chain
15
23
  response = if @response.present?
16
24
  @response
17
25
  elsif @instance.present?
18
- @instance
26
+ @instance
19
27
  elsif @collection.present?
20
28
  wrap_collection
21
29
  end
22
30
 
23
31
  respond_to do |format|
24
- format.json { super json: response, status: @status, callback: ( params[:callback] if allowed_formats.allowed? :jsonp )}
25
- format.xml { super xml: response, status: @status }
32
+ format.json { super json: response, status: options[:status], callback: ( params[:callback] if allowed_formats.allowed? :jsonp )}
33
+ format.xml { super xml: response, status: options[:status] }
26
34
  format.any do
27
- head @status and return if @status
28
- super
35
+ super(*args, options, &block)
29
36
  end
30
37
  end
31
38
  end
@@ -55,4 +62,11 @@ module MakeRestful::Rendering
55
62
  response
56
63
  end
57
64
 
65
+ def handle_errors
66
+ if @instance.try(:errors).present?
67
+ @status = :unprocessable_entity
68
+ @response = { errors: @instance.errors }
69
+ end
70
+ end
71
+
58
72
  end
@@ -12,7 +12,7 @@ module MakeRestful::RestMethods
12
12
  # POST /resources
13
13
  # Verified by :create
14
14
  def create
15
- @instance = @resource.create(params[@resource.model_name.downcase])
15
+ self.instance = @resource.create(params[@resource.model_name.param_key])
16
16
  end
17
17
 
18
18
  # GET /resources/:id
@@ -21,12 +21,12 @@ module MakeRestful::RestMethods
21
21
 
22
22
  # PUT /resources/:id
23
23
  def update
24
- @instance.update_attributes(params[@resource.model_name.downcase])
24
+ self.instance.update_attributes(params[@resource.model_name.param_key])
25
25
  end
26
26
 
27
27
  # DELETE /resources/:id
28
28
  def destroy
29
- @response = { success: @instance.destroy }
29
+ @response = { success: !!@instance.destroy }
30
30
  end
31
31
 
32
32
  end
@@ -1,3 +1,3 @@
1
1
  module MakeRestful
2
- VERSION = "0.1.8"
2
+ VERSION = "0.1.9"
3
3
  end
data/lib/make_restful.rb CHANGED
@@ -8,11 +8,9 @@ module MakeRestful
8
8
  autoload :Loaders
9
9
  autoload :RestMethods
10
10
  autoload :Rendering
11
- autoload :Callbacks
12
11
  autoload :PermissibleSet
13
12
  autoload :PermissibleActions
14
13
 
15
- extend Callbacks
16
14
  include RestMethods
17
15
  include Rendering
18
16
 
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.8
4
+ version: 0.1.9
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-11-16 00:00:00.000000000 Z
12
+ date: 2012-11-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -72,7 +72,6 @@ files:
72
72
  - README.md
73
73
  - Rakefile
74
74
  - lib/make_restful.rb
75
- - lib/make_restful/callbacks.rb
76
75
  - lib/make_restful/class_methods.rb
77
76
  - lib/make_restful/loaders.rb
78
77
  - lib/make_restful/permissible_actions.rb
@@ -1,15 +0,0 @@
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