make_restful 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/make_restful.rb CHANGED
@@ -10,6 +10,8 @@ module MakeRestful
10
10
  autoload :Rendering
11
11
  autoload :Callbacks
12
12
  autoload :Sugar
13
+ autoload :PermissibleSet
14
+ autoload :PermissibleActions
13
15
 
14
16
  extend Callbacks
15
17
  include Loaders
@@ -17,21 +19,23 @@ module MakeRestful
17
19
  include Rendering
18
20
  include Sugar
19
21
 
20
- delegate :allow_method?, :allow_format?, to: :klass
22
+ delegate :allow_method?, :allow_format?, :resource_name, :instance_name, to: :klass
21
23
 
22
24
  included do
23
25
 
24
- class_attribute :allowed_methods, :allowed_formats, :finder, :pagination, :resource_class
26
+ class_attribute :allowed_actions, :allowed_formats, :finder, :pagination, :resource_class, instance_writer: false
25
27
  attr_reader :resource, :instance, :collection
26
28
 
27
- resource name.underscore.gsub(/(.*)_controller/,'\1').singularize
28
- self.allowed_formats = [:json, :jsonp, :xml]
29
- self.allowed_methods = [:index, :show, :update, :destroy, :create, :spec]
30
- self.finder = :id
29
+ self.allowed_actions = PermissibleActions.new(self)
30
+ self.allowed_formats = PermissibleSet.new([:json, :jsonp, :xml])
31
31
 
32
- end
32
+ private :klass, :allowed_actions, :allowed_formats, :pagination, :resource_class, :finder, :allow_method?, :allow_format?, :allowed_actions?, :allowed_formats?, :finder?, :pagination?, :resource_class?
33
+ helper_method :resource, :instance, :collection
34
+
35
+ resource name.underscore.gsub(/(.*)_controller/,'\1').singularize rescue nil
36
+ find_by :id
33
37
 
34
- private
38
+ end
35
39
 
36
40
  def klass
37
41
  self.class
@@ -8,18 +8,6 @@ module MakeRestful::ClassMethods
8
8
  resource_name.singularize
9
9
  end
10
10
 
11
- # Is a format allowed?
12
- def allow_format?(*formats)
13
- true if formats.flatten.map(&:to_sym).select { |i| allowed_formats.map(&:to_sym).include? i }.present?
14
- end
15
-
16
- # Is a method allowed?
17
- def allow_method?(*methods)
18
- true if methods.flatten.map(&:to_sym).select { |i| allowed_methods.map(&:to_sym).include? i }.present?
19
- end
20
-
21
- alias :allow_action? :allow_method?
22
-
23
11
  private
24
12
 
25
13
  def resource(klass)
@@ -29,15 +17,7 @@ module MakeRestful::ClassMethods
29
17
  before_filter :load_collection, only: [:index]
30
18
  before_filter :load_instance, only: [:show, :destroy, :update]
31
19
 
32
- begin
33
- klass = klass.to_s.camelize.constantize
34
- rescue NameError
35
- klass = false
36
- end
37
-
38
- if klass
39
- self.resource_class = klass
40
- end
20
+ self.resource_class = klass.to_s.classify.constantize
41
21
 
42
22
  end
43
23
 
@@ -45,25 +25,25 @@ module MakeRestful::ClassMethods
45
25
  self.finder = key.to_sym
46
26
  end
47
27
 
48
- def allow_method(*methods)
49
- self.allowed_methods += methods.flatten
50
- end
51
- alias :allow_methods :allow_method
28
+ def actions(*actions)
29
+
30
+ options = actions.extract_options!
52
31
 
53
- def allow_format(*formats)
54
- self.allowed_formats += formats.flatten
32
+ self.allowed_actions.objects = actions if actions.present?
33
+ self.allowed_actions.only(options[:only])
34
+ self.allowed_actions.except(options[:except])
35
+
55
36
  end
56
- alias :allow_formats :allow_format
57
37
 
58
- def deny_method(*methods)
59
- self.allowed_methods -= methods
60
- end
61
- alias :deny_methods :deny_method
38
+ def formats(*formats)
39
+
40
+ options = formats.extract_options!
62
41
 
63
- def deny_format(*methods)
64
- self.allowed_formats -= methods
42
+ self.allowed_formats.objects = formats if formats.present?
43
+ self.allowed_formats.only(options[:only])
44
+ self.allowed_formats.except(options[:except])
45
+
65
46
  end
66
- alias :deny_formats :deny_format
67
47
 
68
48
  def paginates!(opts={})
69
49
  opts.reverse_merge!({ per_page: 25 })
@@ -3,13 +3,22 @@ module MakeRestful::Loaders
3
3
  private
4
4
 
5
5
  def load_resource
6
- @resource = self.class.resource_class
6
+ if (@resource = resource_class)
7
+ nested_in_resources = params.select{ |param| /_id$/ =~ param }
8
+ @resource = @resource.where(nested_in_resources) if nested_in_resources.present?
9
+
10
+ else
11
+ @response = { error: 'error when loading resource' }
12
+ @status = :failed_dependency
13
+ render(load_template: false) and return
14
+
15
+ end
7
16
  end
8
17
 
9
18
  def load_collection
10
- @collection = if self.class.pagination && !params.has_key?(:all)
11
- paginated_collection = @resource.paginate page: params[:page], per_page: (params[:per_page] || self.class.pagination[:per_page] || 25)
12
- { :"#{self.class.resource_name}" => collection,
19
+ @collection = if pagination && !params.has_key?(:all)
20
+ paginated_collection = @resource.paginate page: params[:page], per_page: (params[:per_page] || pagination[:per_page] || 25)
21
+ { resource_name.to_sym => paginated_collection,
13
22
  pagination: {
14
23
  current_page: paginated_collection.current_page,
15
24
  per_page: paginated_collection.per_page,
@@ -27,12 +36,11 @@ module MakeRestful::Loaders
27
36
  render(load_template: false) and return
28
37
  end
29
38
 
30
- eval("@#{self.class.resource_name} = @collection")
39
+ eval("@#{resource_name} = @collection")
31
40
  end
32
41
 
33
42
  def load_instance(opts={})
34
-
35
- finder = opts[:finder] || self.class.finder
43
+ finder = opts[:finder] || send(:finder)
36
44
  finder_value = params[finder]
37
45
 
38
46
  @instance = if finder && @resource.respond_to?(:where)
@@ -40,7 +48,7 @@ module MakeRestful::Loaders
40
48
  else
41
49
  @resource.find(finder_value)
42
50
  end
43
- eval "@#{self.class.instance_name} = @instance"
51
+ eval "@#{instance_name} = @instance"
44
52
 
45
53
  unless @instance
46
54
  @response = { error: "Could not find record with #{finder}: #{finder_value}" }
@@ -51,8 +59,8 @@ module MakeRestful::Loaders
51
59
  end
52
60
 
53
61
  def verify_method!
54
- return if allow_method? params[:action]
55
- @response = { error: "Method not allowed"}
62
+ return if allowed_actions.allowed?(params[:action]) || !%w{GET POST PUT PATCH}.include?(request.method)
63
+ @response = { error: "Method not allowed" }
56
64
  @status = :method_not_allowed
57
65
  render(load_template: false)
58
66
  end
@@ -0,0 +1,12 @@
1
+ class MakeRestful::PermissibleActions < MakeRestful::PermissibleSet
2
+
3
+ def initialize(controller)
4
+ @controller = controller
5
+ super([])
6
+ end
7
+
8
+ def available_objects
9
+ self.to_a.present? ? super : @controller.action_methods.map(&:to_sym)
10
+ end
11
+
12
+ end
@@ -0,0 +1,33 @@
1
+ class MakeRestful::PermissibleSet < Set
2
+
3
+ def initialize(set=[])
4
+ @only = Set.new
5
+ @except = Set.new
6
+ super(set)
7
+ end
8
+
9
+ def objects=(*objects)
10
+ self.replace objects.flatten.compact
11
+ end
12
+
13
+ def available_objects
14
+ Set.new(self.to_a)
15
+ end
16
+
17
+ def only(*objects)
18
+ @only = objects.flatten.compact.map(&:to_sym)
19
+ end
20
+
21
+ def except(*objects)
22
+ @except += objects.flatten.compact.map(&:to_sym)
23
+ end
24
+
25
+ def allowed?(*objects)
26
+ true if objects.flatten.map(&:to_sym).select { |i| permissible_objects.include? i }.present?
27
+ end
28
+
29
+ def permissible_objects
30
+ Set.new(available_objects).intersection(@only.present? ? @only : available_objects).difference(@except)
31
+ end
32
+
33
+ end
@@ -6,17 +6,21 @@ module MakeRestful::Rendering
6
6
  def render(*args, &block)
7
7
  options = args.last.is_a?(Hash) && args.last.extractable_options? ? args.last : {}
8
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
+ options[:load_template] = true if options[:load_template].nil?
10
+ cleaned_options = options.reject{ |k,v| [:load_template, :status].include?(k) }
11
+
12
+ super and return if (( template_present && options[:load_template]) || cleaned_options.present? ) && allowed_formats.allowed?(formats)
12
13
 
13
14
  # Set the response from the chain
14
15
  response = @response || @instance || @collection
15
16
 
16
17
  respond_to do |format|
17
- format.json { super json: response, status: @status, callback: ( params[:callback] if allow_format? :jsonp )} if allow_format? :json, :jsonp
18
- format.xml { super xml: response, status: @status } if allow_format? :xml
19
- format.any { super }
18
+ format.json { super json: response, status: @status, callback: ( params[:callback] if allowed_formats.allowed? :jsonp )} if allowed_formats.allowed? :json, :jsonp
19
+ format.xml { super xml: response, status: @status } if allowed_formats.allowed? :xml
20
+ format.any do
21
+ head @status and return if @status
22
+ super
23
+ end
20
24
  end
21
25
  end
22
26
 
@@ -12,7 +12,7 @@ module MakeRestful::RestMethods
12
12
  # POST /resources
13
13
  # Verified by :create
14
14
  def create
15
- @instance = @resource.create
15
+ @instance = @resource.create(params[@resource.model_name.downcase])
16
16
  end
17
17
 
18
18
  # GET /resources/:id
@@ -21,7 +21,7 @@ module MakeRestful::RestMethods
21
21
 
22
22
  # PUT /resources/:id
23
23
  def update
24
- @instance.update_attributes(params[@resource.to_s.underscore])
24
+ @instance.update_attributes(params[@resource.model_name.downcase])
25
25
  end
26
26
 
27
27
  # DELETE /resources/:id
@@ -29,9 +29,4 @@ module MakeRestful::RestMethods
29
29
  @response = { success: @instance.destroy }
30
30
  end
31
31
 
32
- # OPTIONS /resources
33
- def spec
34
- @response = specification
35
- end
36
-
37
32
  end
@@ -1,6 +1,8 @@
1
1
  module MakeRestful::Sugar
2
2
  extend ActiveSupport::Concern
3
3
 
4
+ private
5
+
4
6
  def method_missing(m, *args, &block)
5
7
  if /load_instance_by_(?<finder>[a-z_]+)/ =~ m.to_s
6
8
  load_instance(finder: finder.to_sym)
@@ -1,3 +1,3 @@
1
1
  module MakeRestful
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
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.3
4
+ version: 0.1.4
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-09 00:00:00.000000000 Z
12
+ date: 2012-10-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -75,6 +75,8 @@ files:
75
75
  - lib/make_restful/callbacks.rb
76
76
  - lib/make_restful/class_methods.rb
77
77
  - lib/make_restful/loaders.rb
78
+ - lib/make_restful/permissible_actions.rb
79
+ - lib/make_restful/permissible_set.rb
78
80
  - lib/make_restful/rendering.rb
79
81
  - lib/make_restful/rest_methods.rb
80
82
  - lib/make_restful/sugar.rb