make_restful 0.1.6 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,24 +20,24 @@ module MakeRestful::ClassMethods
20
20
 
21
21
  def resource(klass)
22
22
 
23
+ self.resource_class = klass.to_s.classify.constantize
24
+
23
25
  before_filter :load_resource
24
26
  before_filter :load_collection, only: [:index]
25
27
  before_filter :load_instance, only: [:show, :destroy, :update]
26
28
 
27
- self.resource_class = klass.to_s.classify.constantize
28
-
29
- end
30
-
31
- def find_by(key)
32
- self.finder = key.to_sym
33
29
  end
34
30
 
35
- def collection(&block)
36
- self.collection_loader = block if block_given?
31
+ def collection(options={}, &block)
32
+ options.assert_valid_keys(:for)
33
+ key = options[:for].try(:to_sym) || :default
34
+ self.collection_loader[key] = block if block_given?
37
35
  end
38
36
 
39
- def instance(&block)
40
- self.collection_loader = block if block_given?
37
+ def instance(options={}, &block)
38
+ options.assert_valid_keys(:for)
39
+ key = options[:for].try(:to_sym) || :default
40
+ self.instance_loader[key] = block if block_given?
41
41
  end
42
42
 
43
43
  def actions(*actions)
@@ -23,11 +23,7 @@ module MakeRestful::Loaders
23
23
  end
24
24
 
25
25
  instance do
26
- if finder && @resource.respond_to?(:where)
27
- @resource.where(finder.to_sym => finder_value).first
28
- else
29
- @resource.find(finder_value)
30
- end
26
+ @resource.find(params[:id])
31
27
  end
32
28
 
33
29
  end
@@ -48,7 +44,9 @@ module MakeRestful::Loaders
48
44
  end
49
45
 
50
46
  def load_collection
51
- @collection = instance_eval(&collection_loader)
47
+ loader = collection_loader[params[:action].to_sym] || collection_loader[:default]
48
+
49
+ @collection = instance_eval(&loader)
52
50
 
53
51
  unless @collection.present?
54
52
  @response = { error: 'no results found' }
@@ -59,15 +57,14 @@ module MakeRestful::Loaders
59
57
  eval("@#{resource_name} = @collection")
60
58
  end
61
59
 
62
- def load_instance(opts={})
63
- finder = opts[:finder] || send(:finder)
64
- finder_value = params[finder]
60
+ def load_instance
61
+ loader = instance_loader[params[:action].to_sym] || instance_loader[:default]
65
62
 
66
- @instance = instance_eval(&instance_loader)
63
+ @instance = instance_eval(&loader)
67
64
  eval "@#{instance_name} = @instance"
68
65
 
69
66
  unless @instance
70
- @response = { error: "Could not find record with #{finder}: #{finder_value}" }
67
+ @response = { error: "Record Not Found" }
71
68
  @status = :not_found
72
69
  render(load_template: false) and return
73
70
  end
@@ -1,3 +1,3 @@
1
1
  module MakeRestful
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.8"
3
3
  end
data/lib/make_restful.rb CHANGED
@@ -9,33 +9,34 @@ module MakeRestful
9
9
  autoload :RestMethods
10
10
  autoload :Rendering
11
11
  autoload :Callbacks
12
- autoload :Sugar
13
12
  autoload :PermissibleSet
14
13
  autoload :PermissibleActions
15
14
 
16
15
  extend Callbacks
17
16
  include RestMethods
18
17
  include Rendering
19
- include Sugar
20
18
 
21
19
  delegate :allow_method?, :allow_format?, :resource_name, :instance_name, to: :klass
22
20
 
23
21
  included do
22
+
24
23
  class_attribute :json_root_node, :collection_loader, :instance_loader, instance_writer: false
25
24
 
25
+ self.instance_loader = {}
26
+ self.collection_loader = {}
27
+
26
28
  extend ClassMethods
27
29
 
28
- class_attribute :allowed_actions, :allowed_formats, :finder, :pagination, :resource_class, instance_writer: false
30
+ class_attribute :allowed_actions, :allowed_formats, :pagination, :resource_class, instance_writer: false
29
31
  attr_reader :resource, :instance, :collection
30
32
 
31
33
  self.allowed_actions = PermissibleActions.new(self)
32
34
  self.allowed_formats = PermissibleSet.new([:json, :jsonp, :xml])
33
35
 
34
- private :klass, :allowed_actions, :allowed_formats, :pagination, :resource_class, :finder, :allow_method?, :allow_format?, :allowed_actions?, :allowed_formats?, :finder?, :pagination?, :resource_class?
36
+ private :klass, :allowed_actions, :allowed_formats, :pagination, :resource_class, :allow_method?, :allow_format?, :allowed_actions?, :allowed_formats?, :pagination?, :resource_class?
35
37
  helper_method :resource, :instance, :collection
36
38
 
37
39
  resource name.underscore.gsub(/(.*)_controller/,'\1').singularize rescue nil
38
- find_by :id
39
40
 
40
41
  include Loaders
41
42
 
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.6
4
+ version: 0.1.8
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-14 00:00:00.000000000 Z
12
+ date: 2012-11-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -79,7 +79,6 @@ files:
79
79
  - lib/make_restful/permissible_set.rb
80
80
  - lib/make_restful/rendering.rb
81
81
  - lib/make_restful/rest_methods.rb
82
- - lib/make_restful/sugar.rb
83
82
  - lib/make_restful/version.rb
84
83
  - make_restful.gemspec
85
84
  homepage: http://github.com/jwaldrip/make_restful
@@ -1,22 +0,0 @@
1
- module MakeRestful::Sugar
2
- extend ActiveSupport::Concern
3
-
4
- private
5
-
6
- def method_missing(m, *args, &block)
7
- if /load_instance_by_(?<finder>[a-z_]+)/ =~ m.to_s
8
- load_instance(finder: finder.to_sym)
9
- else
10
- super
11
- end
12
- end
13
-
14
- def respond_to_missing?(method_name, include_private = false)
15
- if /load_instance_by_(?<finder>[a-z_]+)/ =~ method_name.to_s
16
- true
17
- else
18
- false
19
- end
20
- end
21
-
22
- end