class RenderSync::Partial

Attributes

context[RW]
name[RW]
resource[RW]

Public Class Methods

all(model, context, scope = nil) click to toggle source
# 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) click to toggle source
# 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
new(name, resource, scope, context) click to toggle source
# 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

Public Instance Methods

auth_token() click to toggle source
# File lib/render_sync/partial.rb, line 49
def auth_token
  @auth_token ||= Channel.new("#{polymorphic_path}-_#{name}").to_s
end
authorized?(auth_token) click to toggle source
# File lib/render_sync/partial.rb, line 45
def authorized?(auth_token)
  self.auth_token == auth_token
end
channel_for_action(action) click to toggle source
# 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_prefix() click to toggle source
# File lib/render_sync/partial.rb, line 62
def channel_prefix
  @channel_prefix ||= auth_token
end
creator_for_scope(scope) click to toggle source
# File lib/render_sync/partial.rb, line 87
def creator_for_scope(scope)
  PartialCreator.new(name, resource.model, scope, context)
end
message(action) click to toggle source
# 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_token() click to toggle source

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

# File lib/render_sync/partial.rb, line 58
def refetch_auth_token
  @refetch_auth_token ||= Channel.new("#{model_path}-_#{name}").to_s
end
render() click to toggle source
# File lib/render_sync/partial.rb, line 32
def render
  context.render(partial: path, locals: locals, formats: [:html])
end
render_to_string() click to toggle source
# File lib/render_sync/partial.rb, line 28
def render_to_string
  context.render_to_string(partial: path, locals: locals, formats: [:html])
end
selector_end() click to toggle source
# File lib/render_sync/partial.rb, line 83
def selector_end
  "#{channel_prefix}-end"
end
selector_start() click to toggle source
# File lib/render_sync/partial.rb, line 79
def selector_start
  "#{channel_prefix}-start"
end
sync(action) click to toggle source
# File lib/render_sync/partial.rb, line 36
def sync(action)
  message(action).publish
end
update_channel_prefix() click to toggle source
# File lib/render_sync/partial.rb, line 66
def update_channel_prefix
  @update_channel_prefix ||= refetch_auth_token
end

Private Instance Methods

locals() click to toggle source
# File lib/render_sync/partial.rb, line 98
def locals
  locals_hash = {}
  locals_hash[resource.base_name.to_sym] = resource.model
  locals_hash
end
model_path() click to toggle source
# File lib/render_sync/partial.rb, line 104
def model_path
  resource.model_path
end
path() click to toggle source
# File lib/render_sync/partial.rb, line 94
def path
  "sync/#{resource.plural_name}/#{name}"
end
polymorphic_path() click to toggle source
# File lib/render_sync/partial.rb, line 108
def polymorphic_path
  resource.polymorphic_path
end