Skip to content

Commit 9116fc5

Browse files
committed
Add ability to narrow scope to custom channel for sync_new publishes. Close chrismccord#45.
1 parent 357c14a commit 9116fc5

6 files changed

Lines changed: 189 additions & 23 deletions

File tree

lib/sync/partial.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Sync
22
class Partial
3-
attr_accessor :name, :resource, :channel, :context
3+
attr_accessor :name, :resource, :context
44

55
def self.all(model, context)
66
resource = Resource.new(model)
@@ -13,8 +13,7 @@ def self.all(model, context)
1313

1414
def initialize(name, resource, channel, context)
1515
self.name = name
16-
self.resource = Resource.new(resource)
17-
self.channel = channel
16+
self.resource = Resource.new(resource, channel)
1817
self.context = context
1918
end
2019

@@ -78,11 +77,7 @@ def locals
7877
end
7978

8079
def polymorphic_path
81-
if channel
82-
"/#{resource.plural_name}/#{channel}"
83-
else
84-
resource.polymorphic_path
85-
end
80+
resource.polymorphic_path
8681
end
8782
end
8883
end

lib/sync/partial_creator.rb

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
module Sync
22
class PartialCreator
3-
attr_accessor :name, :resource, :scoped_resource, :context, :partial
3+
attr_accessor :name, :resource, :context, :partial
44

55
def initialize(name, resource, scoped_resource, context)
66
self.name = name
77
self.resource = Resource.new(resource)
8-
self.scoped_resource = Resource.new(scoped_resource) if scoped_resource
98
self.context = context
9+
if scoped_resource
10+
scopes = [scoped_resource].flatten
11+
self.resource.parent = Resource.new(scopes.first, scopes[1..-1])
12+
end
1013
self.partial = Partial.new(name, self.resource.model, nil, context)
1114
end
1215

@@ -42,11 +45,7 @@ def message
4245
private
4346

4447
def polymorphic_path
45-
if scoped_resource
46-
"#{scoped_resource.polymorphic_path}#{resource.polymorphic_new_path}"
47-
else
48-
"#{resource.polymorphic_new_path}"
49-
end
48+
resource.polymorphic_new_path
5049
end
5150
end
5251
end

lib/sync/refetch_partial_creator.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ module Sync
22
class RefetchPartialCreator < PartialCreator
33

44
def initialize(name, resource, scoped_resource, context)
5-
self.name = name
6-
self.resource = Resource.new(resource)
7-
self.scoped_resource = Resource.new(scoped_resource) if scoped_resource
8-
self.context = context
5+
super
96
self.partial = RefetchPartial.new(name, self.resource.model, nil, context)
107
end
118

lib/sync/resource.rb

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,55 @@
1+
require 'pathname'
2+
13
module Sync
24
class Resource
3-
attr_accessor :model
5+
attr_accessor :model, :channel, :parent
46

5-
def initialize(model)
7+
# Constructor
8+
#
9+
# model - The ActiveModel instace for this Resource
10+
# channel - The optional scoped channel to prefix polymorphic paths for.
11+
# One of String/Symbol or Array of String/Symbol.
12+
#
13+
# Examples
14+
#
15+
# user = User.find(1)
16+
#
17+
# resource = Resource.new(user)
18+
# resource.polymorphic_path => "/users/1"
19+
# resource.polymorphic_new_path => "/users/new"
20+
#
21+
# resource = Resource.new(user, :admin)
22+
# resource.polymorphic_path => "/admin/users/1"
23+
# resource.polymorphic_new_path => "/admin/users/new"
24+
#
25+
# resource = Resource.new(user, [:staff, :restricted])
26+
# resource.polymorphic_path => "/staff/restricted/users/1"
27+
# resource.polymorphic_new_path => "/staff/restricted/users/new"
28+
#
29+
def initialize(model, channel = nil)
630
self.model = model
31+
self.channel = if channel.is_a? Array
32+
channel.join("/")
33+
else
34+
channel
35+
end
36+
end
37+
38+
# The Resource to use for prefixing polymorphic paths with parent paths.
39+
# Default NullResource.new
40+
#
41+
# Examples
42+
#
43+
# user = User.find(1)
44+
# project = Project.find(123)
45+
#
46+
# resource = Resource.new(user)
47+
# resource.parent = Resource.new(project)
48+
# resource.polymorphic_path => "/projects/123/users/1"
49+
# resource.polymorphic_new_path => "/projects/123/users/new"
50+
#
51+
def parent
52+
@parent || NullResource.new
753
end
854

955
def id
@@ -18,12 +64,28 @@ def plural_name
1864
name.pluralize
1965
end
2066

67+
# Returns the Pathname for model and parent resource
68+
def polymorphic_path
69+
parent.polymorphic_path.join(channel.to_s, plural_name, id.to_s)
70+
end
71+
72+
# Returns the Pathname for a new model and parent resource
73+
def polymorphic_new_path
74+
parent.polymorphic_path.join(channel.to_s, plural_name, "new")
75+
end
76+
end
77+
78+
class NullResource < Resource
79+
80+
def initialize
81+
end
82+
2183
def polymorphic_path
22-
"/#{plural_name}/#{id}"
84+
Pathname.new("/")
2385
end
2486

2587
def polymorphic_new_path
26-
"/#{plural_name}/new"
88+
Pathname.new("/")
2789
end
2890
end
2991
end

test/sync/project.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Project
2+
3+
def self.find(*args)
4+
self.new
5+
end
6+
7+
def self.model_name
8+
"Project"
9+
end
10+
11+
def id
12+
1
13+
end
14+
end

test/sync/resource_test.rb

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
require 'rails/all'
2+
require_relative '../test_helper'
3+
require_relative 'abstract_controller'
4+
require_relative 'user'
5+
require_relative 'project'
6+
7+
describe Sync::Partial do
8+
include TestHelper
9+
10+
before do
11+
@user = User.new
12+
@project = Project.new
13+
end
14+
15+
describe '#name' do
16+
it 'returns the underscored model class name' do
17+
assert_equal "user", Sync::Resource.new(@user).name
18+
end
19+
end
20+
21+
describe '#plural_name' do
22+
it 'returns the underscored model class name' do
23+
assert_equal "users", Sync::Resource.new(@user).plural_name
24+
end
25+
end
26+
27+
describe '#id' do
28+
it 'returns the models id' do
29+
assert_equal 1, Sync::Resource.new(@user).id
30+
end
31+
end
32+
33+
describe '#channel' do
34+
it 'returns nil when no channel is given' do
35+
refute Sync::Resource.new(@user).channel
36+
end
37+
38+
it 'sets the channel to the passed string' do
39+
assert_equal "admin", Sync::Resource.new(@user, "admin").channel
40+
end
41+
42+
it 'sets the channel as the joined array of symbols' do
43+
assert_equal "admin/restricted", Sync::Resource.new(@user, [:admin, :restricted]).channel
44+
end
45+
46+
it 'sets the channel as the joined array of strings' do
47+
assert_equal "admin/restricted", Sync::Resource.new(@user, ["admin", "restricted"]).channel
48+
end
49+
end
50+
51+
describe 'parent' do
52+
it 'defaults to NullResource if no parent is given' do
53+
assert_equal Sync::NullResource, Sync::Resource.new(@user).parent.class
54+
end
55+
56+
it 'sets the parent to given resource' do
57+
resource = Sync::Resource.new(@user)
58+
parent = Sync::Resource.new(@user)
59+
resource.parent = parent
60+
assert_equal parent, resource.parent
61+
end
62+
end
63+
64+
describe '#polymorphic_path' do
65+
it 'returns the path for the model' do
66+
assert_equal "/users/1", Sync::Resource.new(@user).polymorphic_path.to_s
67+
end
68+
69+
it 'returns the path for the model, prefixed by parent path' do
70+
child = Sync::Resource.new(@user)
71+
child.parent = Sync::Resource.new(@project)
72+
assert_equal "/projects/1/users/1", child.polymorphic_path.to_s
73+
end
74+
75+
it 'returns the path for the model, prefixed by parent path and channel' do
76+
child = Sync::Resource.new(@user)
77+
child.parent = Sync::Resource.new(@project, :admin)
78+
assert_equal "/admin/projects/1/users/1", child.polymorphic_path.to_s
79+
end
80+
end
81+
82+
describe '#polymorphic_new_path' do
83+
it 'returns the path for the model' do
84+
assert_equal "/users/new", Sync::Resource.new(@user).polymorphic_new_path.to_s
85+
end
86+
87+
it 'returns the path for the model, prefixed by parent path' do
88+
child = Sync::Resource.new(@user)
89+
child.parent = Sync::Resource.new(@project)
90+
assert_equal "/projects/1/users/new", child.polymorphic_new_path.to_s
91+
end
92+
93+
it 'returns the path for the model, prefixed by parent path and channel' do
94+
child = Sync::Resource.new(@user)
95+
child.parent = Sync::Resource.new(@project, :admin)
96+
assert_equal "/admin/projects/1/users/new", child.polymorphic_new_path.to_s
97+
end
98+
end
99+
end

0 commit comments

Comments
 (0)