resourcy-rails 1.0.2 → 1.1.0

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.
@@ -1,3 +1,3 @@
1
1
  module Resourcy
2
- VERSION = '1.0.2'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -14,8 +14,8 @@ handleRequest = Resourcy.handleRequest
14
14
  Resourcy.noConflict()
15
15
 
16
16
  # Define an options handler, that properly merges options for the Ajax request.
17
- optionsHandler = (opts1, opts2) ->
18
- options = jQuery.extend(true, {}, opts1, opts2)
17
+ optionsHandler = (opts1, opts2, defaults = {}) ->
18
+ options = jQuery.extend(true, {}, opts1, opts2, defaults)
19
19
  for method in ['beforeSend', 'error', 'dataFilter', 'success', 'complete']
20
20
  if opts1[method] && opts2[method]
21
21
  c1 = opts1[method]
@@ -1,3 +1,16 @@
1
+ ###!
2
+ Resourcy v1.1.0
3
+
4
+ Resourcy is an unobtrusive RESTful adapter for jquery-ujs and Rails.
5
+
6
+ Documentation and other useful information can be found at
7
+ https://github.com/jejacks0n/resourcy
8
+
9
+ Copyright (c) 2012 Jeremy Jackson
10
+
11
+ https://raw.github.com/jejacks0n/resourcy/master/LICENSE
12
+ ###
13
+
1
14
  # resource list
2
15
  resources = {}
3
16
 
@@ -13,14 +26,18 @@ parseUrl = (url) ->
13
26
  path = url[9]?.match(/(\/.*)\/+(\w+)$/i) or []
14
27
  return scheme: url[2], credentials: url[5], host: url[6], port: url[8], path: url[9], action: path[2] or '', format: url[11], query: url[13], hash: url[15]
15
28
 
16
- createResource = (path, singular = false) =>
29
+ createResource = (path, singular = false, defaults = {}) =>
17
30
  return resources[path] if resources[path]
18
31
  return resources[path] =
19
32
  path: new RegExp("^#{(if path[0] is '/' then path else "/#{path}").replace(/:\w+/ig, '(\\w+)')}/?(\\w+)?/?(\\w+)?/?($|\\?|\\.|#)", 'i')
20
33
  pathvars: (path.match(/:(\w+)/ig) or []).join('|').replace(/:/g, '').split('|') # todo: this seems wonky
21
34
  actions: {}
35
+ defaults: defaults
22
36
  singular: singular
23
37
  name: path.substr(path.lastIndexOf('/') + 1)
38
+ options: (defaults = {}) ->
39
+ @defaults = defaults
40
+ return @
24
41
  add: (action, callback) ->
25
42
  object = {}
26
43
  if typeof(action) is 'string' then object[action] = callback else object = action
@@ -60,15 +77,17 @@ addCallback = (method, action, callback) ->
60
77
  handleRequest = (method, url, options, original, optionsHandler) ->
61
78
  method = method.toLowerCase()
62
79
  {path, action} = urlParts = parseUrl(url)
80
+ defaults = {}
63
81
 
64
82
  proceeded = false
65
83
  proceed = (opts) ->
66
84
  proceeded = true
67
- return original(url, optionsHandler(options or {}, opts or {}))
85
+ return original(url, optionsHandler(options or {}, opts or {}, defaults))
68
86
 
69
87
  for key, resource of resources
70
88
  continue unless matches = path.match(resource.path)
71
89
  if callback = determineCallback(resource, action, method, matches[matches.length - 2], matches[matches.length - 3])
90
+ defaults = resource.defaults
72
91
  vars = {}
73
92
  vars[pathvar] = matches[index + 1] for pathvar, index in resource.pathvars
74
93
  result = callback(proceed, vars, urlParts)
@@ -96,8 +115,8 @@ determineCallback = (resource, action, method, matchAction, matchIdOrAction) ->
96
115
  handleRequest: handleRequest
97
116
  noConflict: @Resourcy?.noConflict or -> delete(Resourcy)
98
117
 
99
- resources: (path, actions = {}) -> return createResource(path).add(actions)
100
- resource: (path, actions = {}) -> return createResource(path, true).add(actions)
118
+ resources: (path, defaults = {}, actions = {}) -> return createResource(path, false, defaults).add(actions)
119
+ resource: (path, defaults = {}, actions = {}) -> return createResource(path, true, defaults).add(actions)
101
120
  routes: ->
102
121
  routes = {}
103
122
  routes[resource.name] = resource.describe() for path, resource of resources
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resourcy-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
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-22 00:00:00.000000000 Z
12
+ date: 2012-10-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -177,7 +177,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
177
177
  version: '0'
178
178
  segments:
179
179
  - 0
180
- hash: -1412880833074942890
180
+ hash: 585668097187791136
181
181
  required_rubygems_version: !ruby/object:Gem::Requirement
182
182
  none: false
183
183
  requirements:
@@ -186,7 +186,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  version: '0'
187
187
  segments:
188
188
  - 0
189
- hash: -1412880833074942890
189
+ hash: 585668097187791136
190
190
  requirements: []
191
191
  rubyforge_project:
192
192
  rubygems_version: 1.8.24