resourcy-rails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/LICENSE +25 -0
  2. data/lib/resourcy/engine.rb +4 -0
  3. data/lib/resourcy/rails.rb +4 -0
  4. data/lib/resourcy/version.rb +3 -0
  5. data/lib/resourcy-rails.rb +1 -0
  6. data/spec/dummy/Rakefile +7 -0
  7. data/spec/dummy/app/assets/javascripts/jquery-1.7.1.js +9266 -0
  8. data/spec/dummy/app/assets/javascripts/jquery-1.8.0.js +9227 -0
  9. data/spec/dummy/app/controllers/posts_controller.rb +85 -0
  10. data/spec/dummy/app/models/post.rb +3 -0
  11. data/spec/dummy/app/views/posts/_form.html.erb +25 -0
  12. data/spec/dummy/app/views/posts/edit.html.erb +6 -0
  13. data/spec/dummy/app/views/posts/index.html.erb +25 -0
  14. data/spec/dummy/app/views/posts/new.html.erb +5 -0
  15. data/spec/dummy/app/views/posts/show.html.erb +15 -0
  16. data/spec/dummy/config/application.rb +63 -0
  17. data/spec/dummy/config/boot.rb +9 -0
  18. data/spec/dummy/config/database.yml +10 -0
  19. data/spec/dummy/config/environment.rb +5 -0
  20. data/spec/dummy/config/environments/development.rb +37 -0
  21. data/spec/dummy/config/environments/test.rb +37 -0
  22. data/spec/dummy/config/evergreen.rb +47 -0
  23. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  24. data/spec/dummy/config/initializers/inflections.rb +15 -0
  25. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  26. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  27. data/spec/dummy/config/initializers/session_store.rb +8 -0
  28. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  29. data/spec/dummy/config/locales/en.yml +5 -0
  30. data/spec/dummy/config/routes.rb +64 -0
  31. data/spec/dummy/config.ru +4 -0
  32. data/spec/dummy/db/development.sqlite3 +0 -0
  33. data/spec/dummy/db/migrate/20120825050219_create_posts.rb +10 -0
  34. data/spec/dummy/db/structure.sql +4 -0
  35. data/spec/dummy/log/.gitkeep +0 -0
  36. data/spec/dummy/public/index.html +12 -0
  37. data/spec/dummy/script/rails +6 -0
  38. data/spec/javascripts/jquery.resourcy_spec.js.coffee +399 -0
  39. data/spec/javascripts/resourcy_spec.js.coffee +177 -0
  40. data/spec/javascripts/spec_helper.js +37 -0
  41. data/spec/javascripts/templates/ujs.html +3 -0
  42. data/vendor/assets/javascripts/jquery.resourcy.js.coffee +37 -0
  43. data/vendor/assets/javascripts/resourcy.js.coffee +104 -0
  44. metadata +229 -0
@@ -0,0 +1,104 @@
1
+ # resource list
2
+ resources = {}
3
+
4
+ # used to describe routes
5
+ methods = ['get', 'put', 'post', 'delete']
6
+ actions = ['index', 'create', 'new', 'edit', 'show', 'update', 'delete']
7
+ descriptions =
8
+ plural: [['get', ''], ['post', ''], ['get', '/new'], ['get', '/:id/edit'], ['get', '/:id'], ['put', '/:id'], ['delete', '/:id']]
9
+ singular: [null, ['post', ''], ['get', '/new'], ['get', '/edit'], ['get', ''], ['put', ''], ['delete', '']]
10
+
11
+ parseUrl = (url) ->
12
+ url = url.match(/^((http[s]?|ftp):\/\/)?(((.+)@)?([^:\/\?#\s]+)(:(\d+))?)?(\/?[^\?#\.]+)?(\.([^\?#]+))?(\?([^#]*))?(#(.*))?$/i) or []
13
+ path = url[9]?.match(/(\/.*)\/+(\w+)$/i) or []
14
+ 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
+
16
+ createResource = (path, singular = false) =>
17
+ return resources[path] if resources[path]
18
+ return resources[path] =
19
+ path: new RegExp("^#{(if path[0] is '/' then path else "/#{path}").replace(/:\w+/ig, '(\\w+)')}/?(\\w+)?/?(\\w+)?/?($|\\?|\\.|#)", 'i')
20
+ pathvars: (path.match(/:(\w+)/ig) or []).join('|').replace(/:/g, '').split('|') # todo: this seems wonky
21
+ actions: {}
22
+ singular: singular
23
+ name: path.substr(path.lastIndexOf('/') + 1)
24
+ add: (action, callback) ->
25
+ object = {}
26
+ if typeof(action) is 'string' then object[action] = callback else object = action
27
+ for action, callback of object
28
+ [method, action] = action.split(':')
29
+ addCallback.call(@, method, action, callback)
30
+ return @
31
+ remove: (action) ->
32
+ [method, action] = action.split(':')
33
+ if action then delete(@actions[method][action]) else delete(@actions[method])
34
+ return @
35
+ removeAll: ->
36
+ @actions = {}
37
+ return @
38
+ describe: ->
39
+ routes = []
40
+ for method in methods
41
+ for action of @actions[method]
42
+ routes.push("#{path}/#{action} #{method.toUpperCase()} => #{@name}##{action}")
43
+ [start, desc] = if @singular then [1, descriptions.singular] else [0, descriptions.plural]
44
+ for i in [start..actions.length - 1]
45
+ if @actions[actions[i]]
46
+ routes.push("#{path}#{desc[i][1]} #{desc[i][0].toUpperCase()} => #{@name}##{actions[i]}")
47
+ return routes
48
+
49
+ addCallback = (method, action, callback) ->
50
+ errorMsg = "The #{([method, action].join(':').replace(/:$/, ''))} action already exists on the '#{@name}' resource. Try removing it first."
51
+ if action
52
+ @actions[method] ||= {}
53
+ throw errorMsg if @actions[method][action]
54
+ @actions[method][action] = callback
55
+ else
56
+ throw errorMsg if @actions[method]
57
+ throw "Adding index to '#{@name}' isn't possible (singular resource)." if @singular and method is 'index'
58
+ @actions[method] = callback
59
+
60
+ handleRequest = (method, url, options, original, optionsHandler) ->
61
+ method = method.toLowerCase()
62
+ {path, action} = urlParts = parseUrl(url)
63
+
64
+ proceeded = false
65
+ proceed = (opts) ->
66
+ proceeded = true
67
+ return original(url, optionsHandler(options or {}, opts or {}))
68
+
69
+ for key, resource of resources
70
+ continue unless matches = path.match(resource.path)
71
+ if callback = determineCallback(resource, action, method, matches[matches.length - 2], matches[matches.length - 3])
72
+ vars = {}
73
+ vars[pathvar] = matches[index + 1] for pathvar, index in resource.pathvars
74
+ result = callback(proceed, vars, urlParts)
75
+ return proceed(result) if result != false and !proceeded
76
+ return
77
+
78
+ return original(url, options)
79
+
80
+ determineCallback = (resource, action, method, matchAction, matchIdOrAction) ->
81
+ switch method
82
+ when 'get'
83
+ return resource.actions.get[action] if resource.actions.get?[action]
84
+ switch (if matchIdOrAction then action else '')
85
+ when '' then return (if resource.singular then resource.actions.show else resource.actions.index)
86
+ when 'new' then return resource.actions.new
87
+ when 'edit' then return resource.actions.edit
88
+ else return resource.actions.show unless matchAction
89
+ when 'put' then return resource.actions.put?[action] or (resource.actions.update unless matchAction)
90
+ when 'post' then return resource.actions.post?[action] or (resource.actions.create unless matchIdOrAction)
91
+ when 'delete' then return resource.actions.delete?[action] or (resource.actions.destroy unless matchAction)
92
+
93
+
94
+ @Resourcy =
95
+ removeAll: -> resources = {}
96
+ handleRequest: handleRequest
97
+ noConflict: Resourcy.noConflict or -> delete(Resourcy)
98
+
99
+ resources: (path, actions = {}) -> return createResource(path).add(actions)
100
+ resource: (path, actions = {}) -> return createResource(path, true).add(actions)
101
+ routes: ->
102
+ routes = {}
103
+ routes[resource.name] = resource.describe() for path, resource of resources
104
+ return routes
metadata ADDED
@@ -0,0 +1,229 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: resourcy-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy Jackson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: coffee-rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sprockets
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.1'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ - !ruby/object:Gem::Dependency
63
+ name: uglifier
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: sprockets-rails
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: evergreen
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: 1.0.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 1.0.0
110
+ description: A RESTful javascript router that can be used with jquery-ujs and Rails
111
+ for client route handling
112
+ email:
113
+ - jejacks0n@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files:
117
+ - LICENSE
118
+ files:
119
+ - lib/resourcy/engine.rb
120
+ - lib/resourcy/rails.rb
121
+ - lib/resourcy/version.rb
122
+ - lib/resourcy-rails.rb
123
+ - vendor/assets/javascripts/jquery.resourcy.js.coffee
124
+ - vendor/assets/javascripts/resourcy.js.coffee
125
+ - LICENSE
126
+ - spec/dummy/Rakefile
127
+ - spec/dummy/app/assets/javascripts/jquery-1.7.1.js
128
+ - spec/dummy/app/assets/javascripts/jquery-1.8.0.js
129
+ - spec/dummy/app/controllers/posts_controller.rb
130
+ - spec/dummy/app/models/post.rb
131
+ - spec/dummy/app/views/posts/_form.html.erb
132
+ - spec/dummy/app/views/posts/edit.html.erb
133
+ - spec/dummy/app/views/posts/index.html.erb
134
+ - spec/dummy/app/views/posts/new.html.erb
135
+ - spec/dummy/app/views/posts/show.html.erb
136
+ - spec/dummy/config.ru
137
+ - spec/dummy/config/application.rb
138
+ - spec/dummy/config/boot.rb
139
+ - spec/dummy/config/database.yml
140
+ - spec/dummy/config/environment.rb
141
+ - spec/dummy/config/environments/development.rb
142
+ - spec/dummy/config/environments/test.rb
143
+ - spec/dummy/config/evergreen.rb
144
+ - spec/dummy/config/initializers/backtrace_silencers.rb
145
+ - spec/dummy/config/initializers/inflections.rb
146
+ - spec/dummy/config/initializers/mime_types.rb
147
+ - spec/dummy/config/initializers/secret_token.rb
148
+ - spec/dummy/config/initializers/session_store.rb
149
+ - spec/dummy/config/initializers/wrap_parameters.rb
150
+ - spec/dummy/config/locales/en.yml
151
+ - spec/dummy/config/routes.rb
152
+ - spec/dummy/db/development.sqlite3
153
+ - spec/dummy/db/migrate/20120825050219_create_posts.rb
154
+ - spec/dummy/db/structure.sql
155
+ - spec/dummy/log/.gitkeep
156
+ - spec/dummy/public/index.html
157
+ - spec/dummy/script/rails
158
+ - spec/javascripts/jquery.resourcy_spec.js.coffee
159
+ - spec/javascripts/resourcy_spec.js.coffee
160
+ - spec/javascripts/spec_helper.js
161
+ - spec/javascripts/templates/ujs.html
162
+ homepage: http://github.com/jejacks0n/resourcy
163
+ licenses:
164
+ - MIT
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
172
+ - - ! '>='
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ segments:
176
+ - 0
177
+ hash: -1331812618252069276
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ none: false
180
+ requirements:
181
+ - - ! '>='
182
+ - !ruby/object:Gem::Version
183
+ version: '0'
184
+ segments:
185
+ - 0
186
+ hash: -1331812618252069276
187
+ requirements: []
188
+ rubyforge_project:
189
+ rubygems_version: 1.8.24
190
+ signing_key:
191
+ specification_version: 3
192
+ summary: ! 'Resourcy: Awesome RESTful javascript resource routing'
193
+ test_files:
194
+ - spec/dummy/Rakefile
195
+ - spec/dummy/app/assets/javascripts/jquery-1.7.1.js
196
+ - spec/dummy/app/assets/javascripts/jquery-1.8.0.js
197
+ - spec/dummy/app/controllers/posts_controller.rb
198
+ - spec/dummy/app/models/post.rb
199
+ - spec/dummy/app/views/posts/_form.html.erb
200
+ - spec/dummy/app/views/posts/edit.html.erb
201
+ - spec/dummy/app/views/posts/index.html.erb
202
+ - spec/dummy/app/views/posts/new.html.erb
203
+ - spec/dummy/app/views/posts/show.html.erb
204
+ - spec/dummy/config.ru
205
+ - spec/dummy/config/application.rb
206
+ - spec/dummy/config/boot.rb
207
+ - spec/dummy/config/database.yml
208
+ - spec/dummy/config/environment.rb
209
+ - spec/dummy/config/environments/development.rb
210
+ - spec/dummy/config/environments/test.rb
211
+ - spec/dummy/config/evergreen.rb
212
+ - spec/dummy/config/initializers/backtrace_silencers.rb
213
+ - spec/dummy/config/initializers/inflections.rb
214
+ - spec/dummy/config/initializers/mime_types.rb
215
+ - spec/dummy/config/initializers/secret_token.rb
216
+ - spec/dummy/config/initializers/session_store.rb
217
+ - spec/dummy/config/initializers/wrap_parameters.rb
218
+ - spec/dummy/config/locales/en.yml
219
+ - spec/dummy/config/routes.rb
220
+ - spec/dummy/db/development.sqlite3
221
+ - spec/dummy/db/migrate/20120825050219_create_posts.rb
222
+ - spec/dummy/db/structure.sql
223
+ - spec/dummy/log/.gitkeep
224
+ - spec/dummy/public/index.html
225
+ - spec/dummy/script/rails
226
+ - spec/javascripts/jquery.resourcy_spec.js.coffee
227
+ - spec/javascripts/resourcy_spec.js.coffee
228
+ - spec/javascripts/spec_helper.js
229
+ - spec/javascripts/templates/ujs.html