entangled 0.0.18 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c26b49537871a591ca346338518c72959721f39a
4
- data.tar.gz: b1edc3cd5f06358c30ce85ef8146da53db890704
3
+ metadata.gz: dd3064c1f6e0a2646581f73b82460adadd183021
4
+ data.tar.gz: a7de74b2b66c782d00af12c4c6ca6788e068b37c
5
5
  SHA512:
6
- metadata.gz: 54880c5779d9979ac8f97728b5e939fa3af5ffff4e76a5f330314cf3eb9b1fd7d609cd15e2be8ba6273019c5441db8d59788013763f2c4c15758024bb70c4e31
7
- data.tar.gz: 98ab954df3d7f04be3bd6a8f0662a33f0b88626e5ab06c6b5351d3721a3c7de63350af215da5ead09d6e29c2c61434b2aefe1ff9bc5eb11d2f12de68281da86d
6
+ metadata.gz: 1b5c9a3d4f5b9a6b3706653162c143cad14f308d5293041faf42058a0e960d87d32b04ae6e04f31dc6101bffe15449e9ef217f3c50749fd647bb6edf82f0e9cf
7
+ data.tar.gz: cc0882bc886ac6aca547bb34eb398731ce8fa912acffa270f80b0d97d774b66efb473d18053b2503731c53339d72f885d9654967a5aa9abc471d247416fbb6db
data/README.md CHANGED
@@ -43,24 +43,40 @@ sockets_for :messages
43
43
 
44
44
  Under the hood, this creates the following routes:
45
45
 
46
- ```ruby
47
- get '/messages', to: 'messages#index', as: :messages
48
- get '/messages/create', to: 'messages#create', as: :create_message
49
- get '/messages/:id', to: 'messages#show', as: :message
50
- get '/messages/:id/destroy', to: 'messages#destroy', as: :destroy_message
51
- get '/messages/:id/update', to: 'messages#update', as: :update_message
46
+ ```shell
47
+ Prefix Verb URI Pattern Controller#Action
48
+ messages GET /messages(.:format) messages#index
49
+ message GET /messages/:id(.:format) messages#show
50
+ create_messages GET /messages/create(.:format) messages#create
51
+ update_message GET /messages/:id/update(.:format) messages#update
52
+ destroy_message GET /messages/:id/destroy(.:format) messages#destroy
52
53
  ```
53
54
 
54
- The options `:only` and `:except` are available just like when using `resources`, so you can say something like:
55
+ Note that websockets don't speak HTTP, so only GET requests are available. That's why these routes deviate slightly from restful routes. Also note that there are no `edit` and `new` actions, since an Entangled controller is only concerned with rendering data, not views.
56
+
57
+ You can use `sockets_for` just like `resources`, including the following features:
55
58
 
56
59
  ```ruby
57
- sockets_for :messages, only: :index # or use an array
58
- ```
60
+ # Inclusion/exclusion
61
+ sockets_for :messages, only: :index
62
+ sockets_for :messages, only: [:index, :show]
63
+
64
+ sockets_for :messages, except: :index
65
+ sockets_for :messages, except: [:index, :show]
59
66
 
60
- Note that Websockets don't speak HTTP, so only GET requests are available. That's why these routes deviate slightly from restful routes. Also note that there are no `edit` and `new` actions, since an Entangled controller is only concerned with rendering data, not views.
67
+ # Nesting
68
+ sockets_for :parents do
69
+ sockets_for :children
70
+ end
71
+
72
+ # Multiple routes at once
73
+ sockets_for :foos, :bars
74
+
75
+ # ...etc
76
+ ```
61
77
 
62
78
  ### Models
63
- Add the following to the top inside your model (e.g., a `Message` model):
79
+ Add the following to the top of your model (e.g., a `Message` model):
64
80
 
65
81
  ```ruby
66
82
  class Message < ActiveRecord::Base
@@ -80,7 +96,8 @@ By default, the following callbacks will be added:
80
96
  You can limit this behavior by specifying `:only` or `:except` options. For example, if you don't want to propagate the destruction or update of an object to all connected clients, you can do the following:
81
97
 
82
98
  ```ruby
83
- entangle only: :create # or use an array
99
+ entangle only: :create
100
+ entangled only: [:create, :update]
84
101
  ```
85
102
 
86
103
  ### Controllers
@@ -1,65 +1,89 @@
1
1
  module ActionDispatch::Routing
2
2
  class Mapper
3
3
  private
4
- def sockets_for(resource, options = {})
5
- @resources = resource.to_s.underscore.pluralize.to_sym
6
- @resource = resource.to_s.underscore.singularize.to_sym
7
4
 
5
+ # Generates five routes that all use GET requests.
6
+ # For example:
7
+ #
8
+ # sockets_for :messages
9
+ #
10
+ # will create the following routes:
11
+ #
12
+ # Prefix Verb URI Pattern Controller#Action
13
+ # create_messages GET /messages/create(.:format) messages#create
14
+ # update_message GET /messages/:id/update(.:format) messages#update
15
+ # destroy_message GET /messages/:id/destroy(.:format) messages#destroy
16
+ # messages GET /messages(.:format) messages#index
17
+ # message GET /messages/:id(.:format) messages#show
18
+ #
19
+ # This method can nested by passing a block, and
20
+ # the options :only and :except can be used just like
21
+ # with the method 'resources'
22
+ def sockets_for(*args, &block)
23
+ options = args.extract_options!
24
+ routes = infer_routes(options)
25
+ resource_routes = infer_resource_routes(routes)
26
+
27
+ # Generate index and show routes
28
+ resources *args, only: resource_routes do
29
+ # Generate create route
30
+ collection do
31
+ get 'create', as: :create if routes.include?(:create)
32
+ end
33
+
34
+ # Generate update and destroy routes
35
+ member do
36
+ get 'update', as: :update if routes.include?(:update)
37
+ get 'destroy', as: :destroy if routes.include?(:destroy)
38
+ end
39
+
40
+ # Nest routes
41
+ yield if block_given?
42
+ end
43
+ end
44
+
45
+ def default_routes
46
+ [:index, :create, :show, :destroy, :update]
47
+ end
48
+
49
+ # Find out which routes should be generated
50
+ # inside resources method. These can be :create,
51
+ # :update, and :destroy, and are the ones that
52
+ # need to be overridden to use GET requests
53
+ # instead of PATCH, POST and DELETE
54
+ def infer_routes(options)
8
55
  if options.any?
9
56
  if options[:only]
10
- if options[:only].is_a? Symbol
11
- send :"draw_#{options[:only]}"
12
- elsif options[:only].is_a? Array
13
- options[:only].each do |option|
14
- send :"draw_#{option}"
15
- end
57
+ if options[:only].is_a?(Symbol)
58
+ routes = [options[:only]]
59
+ elsif options[:only].is_a?(Array)
60
+ routes = options[:only]
16
61
  end
17
62
  elsif options[:except]
18
- if options[:except].is_a? Symbol
19
- (default_options - [options[:except]]).each do |option|
20
- send :"draw_#{option}"
21
- end
22
- elsif options[:except].is_a? Array
23
- (default_options - options[:except]).each do |option|
24
- send :"draw_#{option}"
25
- end
63
+ if options[:except].is_a?(Symbol)
64
+ routes = default_routes - [options[:except]]
65
+ elsif options[:except].is_a?(Array)
66
+ routes = default_routes - options[:except]
26
67
  end
27
68
  end
28
69
  else
29
- draw_all
70
+ routes = default_routes
30
71
  end
31
- end
32
-
33
- def default_options
34
- [:index, :create, :show, :destroy, :update]
35
- end
36
-
37
- def draw_all
38
- draw_index
39
- draw_create
40
- draw_show
41
- draw_destroy
42
- draw_update
43
- end
44
72
 
45
- def draw_index
46
- get :"/#{@resources}", to: "#{@resources}#index", as: @resources
73
+ routes
47
74
  end
48
75
 
49
- def draw_create
50
- get :"/#{@resources}/create", to: "#{@resources}#create", as: :"create_#{@resource}"
51
- end
76
+ # Find out if the resources method should create
77
+ # :index and :show routes. These two do not need
78
+ # to be overridden since they use GET requests
79
+ # by default
80
+ def infer_resource_routes(routes)
81
+ resource_routes = []
52
82
 
53
- def draw_show
54
- get :"/#{@resources}/:id", to: "#{@resources}#show", as: @resource
55
- end
56
-
57
- def draw_destroy
58
- get :"/#{@resources}/:id/destroy", to: "#{@resources}#destroy", as: :"destroy_#{@resource}"
59
- end
83
+ resource_routes << :index if routes.include?(:index)
84
+ resource_routes << :show if routes.include?(:show)
60
85
 
61
- def draw_update
62
- get :"/#{@resources}/:id/update", to: "#{@resources}#update", as: :"update_#{@resource}"
86
+ resource_routes
63
87
  end
64
88
  end
65
89
  end
@@ -1,3 +1,3 @@
1
1
  module Entangled
2
- VERSION = "0.0.18"
2
+ VERSION = "0.0.19"
3
3
  end
@@ -8,4 +8,9 @@ Rails.application.routes.draw do
8
8
  sockets_for :bars, only: [:index, :show]
9
9
  sockets_for :foobars, except: :index
10
10
  sockets_for :barfoos, except: [:index, :show]
11
+
12
+ # Test nested routes
13
+ sockets_for :lists do
14
+ sockets_for :items
15
+ end
11
16
  end
@@ -12,8 +12,8 @@ RSpec.describe MessagesController, type: :routing do
12
12
  end
13
13
 
14
14
  describe '#create' do
15
- it 'matches the create_message_path' do
16
- expect(create_message_path).to eq '/messages/create'
15
+ it 'matches the create_messages_path' do
16
+ expect(create_messages_path).to eq '/messages/create'
17
17
  end
18
18
 
19
19
  it 'routes to #create' do
@@ -0,0 +1,114 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe 'Nested routes', type: :routing do
4
+ before(:context) do
5
+ # Create arbitrary controllers here rather than
6
+ # having a separate file for each in app/controllers
7
+ class ListsController < ApplicationController; end
8
+ class ItemsController < ApplicationController; end
9
+ end
10
+
11
+ describe 'parent routes' do
12
+ describe '#index' do
13
+ it 'matches the lists_path' do
14
+ expect(lists_path).to eq '/lists'
15
+ end
16
+
17
+ it 'routes to #index' do
18
+ expect(get: '/lists').to route_to 'lists#index'
19
+ end
20
+ end
21
+
22
+ describe '#create' do
23
+ it 'matches the create_list_path' do
24
+ expect(create_lists_path).to eq '/lists/create'
25
+ end
26
+
27
+ it 'routes to #create' do
28
+ expect(get: '/lists/create').to route_to 'lists#create'
29
+ end
30
+ end
31
+
32
+ describe '#show' do
33
+ it 'matches the list_path' do
34
+ expect(list_path('1')).to eq '/lists/1'
35
+ end
36
+
37
+ it 'routes to #show' do
38
+ expect(get: '/lists/1').to route_to 'lists#show', id: '1'
39
+ end
40
+ end
41
+
42
+ describe '#destroy' do
43
+ it 'matches the destroy_list_path' do
44
+ expect(destroy_list_path('1')).to eq '/lists/1/destroy'
45
+ end
46
+
47
+ it 'routes to #destroy' do
48
+ expect(get: '/lists/1/destroy').to route_to 'lists#destroy', id: '1'
49
+ end
50
+ end
51
+
52
+ describe '#update' do
53
+ it 'matches the update_list_path' do
54
+ expect(update_list_path('1')).to eq '/lists/1/update'
55
+ end
56
+
57
+ it 'routes to #update' do
58
+ expect(get: '/lists/1/update').to route_to 'lists#update', id: '1'
59
+ end
60
+ end
61
+ end
62
+
63
+ describe 'child routes' do
64
+ describe '#index' do
65
+ it 'matches the list_item_path' do
66
+ expect(list_items_path('1')).to eq '/lists/1/items'
67
+ end
68
+
69
+ it 'routes to #index' do
70
+ expect(get: '/lists/1/items').to route_to 'items#index', list_id: '1'
71
+ end
72
+ end
73
+
74
+ describe '#create' do
75
+ it 'matches the create_list_item_path' do
76
+ expect(create_list_items_path('1')).to eq '/lists/1/items/create'
77
+ end
78
+
79
+ it 'routes to #create' do
80
+ expect(get: '/lists/1/items/create').to route_to 'items#create', list_id: '1'
81
+ end
82
+ end
83
+
84
+ describe '#show' do
85
+ it 'matches the list_item_path' do
86
+ expect(list_item_path(id: '1', list_id: '1')).to eq '/lists/1/items/1'
87
+ end
88
+
89
+ it 'routes to #show' do
90
+ expect(get: '/lists/1/items/1').to route_to 'items#show', list_id: '1', id: '1'
91
+ end
92
+ end
93
+
94
+ describe '#destroy' do
95
+ it 'matches the destroy_list_item_path' do
96
+ expect(destroy_list_item_path(id: '1', list_id: '1')).to eq '/lists/1/items/1/destroy'
97
+ end
98
+
99
+ it 'routes to #destroy' do
100
+ expect(get: '/lists/1/items/1/destroy').to route_to 'items#destroy', list_id: '1', id: '1'
101
+ end
102
+ end
103
+
104
+ describe '#update' do
105
+ it 'matches the update_list_item_path' do
106
+ expect(update_list_item_path(id: '1', list_id: '1')).to eq '/lists/1/items/1/update'
107
+ end
108
+
109
+ it 'routes to #update' do
110
+ expect(get: '/lists/1/items/1/update').to route_to 'items#update', list_id: '1', id: '1'
111
+ end
112
+ end
113
+ end
114
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: entangled
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.18
4
+ version: 0.0.19
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Charles Hackethal
@@ -232,6 +232,7 @@ files:
232
232
  - spec/models/message_spec.rb
233
233
  - spec/routing/inclusion_exclusion_routing_spec.rb
234
234
  - spec/routing/messages_routing_spec.rb
235
+ - spec/routing/nested_routing_spec.rb
235
236
  - spec/spec_helper.rb
236
237
  homepage: https://github.com/dchacke/entangled
237
238
  licenses:
@@ -318,4 +319,5 @@ test_files:
318
319
  - spec/models/message_spec.rb
319
320
  - spec/routing/inclusion_exclusion_routing_spec.rb
320
321
  - spec/routing/messages_routing_spec.rb
322
+ - spec/routing/nested_routing_spec.rb
321
323
  - spec/spec_helper.rb