Class: RenderSync::Partial

Inherits:
Object
  • Object
show all
Defined in:
lib/render_sync/partial.rb

Direct Known Subclasses

RefetchPartial

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, resource, scope, context) ⇒ Partial



22
23
24
25
26
# File 'lib/render_sync/partial.rb', line 22

def initialize(name, resource, scope, context)
  self.name = name
  self.resource = Resource.new(resource, scope)
  self.context = context
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



3
4
5
# File 'lib/render_sync/partial.rb', line 3

def context
  @context
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/render_sync/partial.rb', line 3

def name
  @name
end

#resourceObject

Returns the value of attribute resource.



3
4
5
# File 'lib/render_sync/partial.rb', line 3

def resource
  @resource
end

Class Method Details

.all(model, context, scope = nil) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/render_sync/partial.rb', line 5

def self.all(model, context, scope = nil)
  resource = Resource.new(model, scope)

  Dir["#{RenderSync.views_root}/#{resource.plural_name}/_*.*"].map do |partial|
    partial_name = File.basename(partial)
    Partial.new(partial_name[1...partial_name.index('.')], resource.model, scope, context)
  end
end

.find(model, partial_name, context) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/render_sync/partial.rb', line 14

def self.find(model, partial_name, context)
  resource = Resource.new(model)
  plural_name = resource.plural_name
  partial = Dir["app/views/sync/#{plural_name}/_#{partial_name}.*"].first
  return unless partial
  Partial.new(partial_name, resource.model, nil, context)
end

Instance Method Details

#auth_tokenObject



49
50
51
# File 'lib/render_sync/partial.rb', line 49

def auth_token
  @auth_token ||= Channel.new("#{polymorphic_path}-_#{name}").to_s
end

#authorized?(auth_token) ⇒ Boolean



45
46
47
# File 'lib/render_sync/partial.rb', line 45

def authorized?(auth_token)
  self.auth_token == auth_token
end

#channel_for_action(action) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/render_sync/partial.rb', line 70

def channel_for_action(action)
  case action
  when :update
    "#{update_channel_prefix}-#{action}"
  else
    "#{channel_prefix}-#{action}"
  end
end

#channel_prefixObject



62
63
64
# File 'lib/render_sync/partial.rb', line 62

def channel_prefix
  @channel_prefix ||= auth_token
end

#creator_for_scope(scope) ⇒ Object



87
88
89
# File 'lib/render_sync/partial.rb', line 87

def creator_for_scope(scope)
  PartialCreator.new(name, resource.model, scope, context)
end

#message(action) ⇒ Object



40
41
42
43
# File 'lib/render_sync/partial.rb', line 40

def message(action)
  RenderSync.client.build_message channel_for_action(action),
    html: (render_to_string unless action.to_s == "destroy")
end

#refetch_auth_tokenObject

For the refetch feature we need an auth_token that wasn’t created with scopes, because the scope information is not available on the refetch-request. So we create a refetch_auth_token which is based only on model_name and id plus the name of this partial



58
59
60
# File 'lib/render_sync/partial.rb', line 58

def refetch_auth_token
  @refetch_auth_token ||= Channel.new("#{model_path}-_#{name}").to_s
end

#renderObject



32
33
34
# File 'lib/render_sync/partial.rb', line 32

def render
  context.render(partial: path, locals: locals, formats: [:html])
end

#render_to_stringObject



28
29
30
# File 'lib/render_sync/partial.rb', line 28

def render_to_string
  context.render_to_string(partial: path, locals: locals, formats: [:html])
end

#selector_endObject



83
84
85
# File 'lib/render_sync/partial.rb', line 83

def selector_end
  "#{channel_prefix}-end"
end

#selector_startObject



79
80
81
# File 'lib/render_sync/partial.rb', line 79

def selector_start
  "#{channel_prefix}-start"
end

#sync(action) ⇒ Object



36
37
38
# File 'lib/render_sync/partial.rb', line 36

def sync(action)
  message(action).publish
end

#update_channel_prefixObject



66
67
68
# File 'lib/render_sync/partial.rb', line 66

def update_channel_prefix
  @update_channel_prefix ||= refetch_auth_token
end