lib/sync/view_helpers.rb
# Surround partial render in script tags, watching for 
# sync_update and sync_destroy channels from pubsub server
#
# options - The Hash of options
#   partial - The String partial filename without leading underscore
#   resource - The ActiveModel resource
#   collection - The Array of ActiveModel resources to use in place of 
#                single resource
#
# Examples
#   <%= sync partial: 'todo', resource: todo %>
#   <%= sync partial: 'todo', collection: todos %>
#
Surround partial render in script tags, watching for
sync_update and sync_destroy channels from pubsub server

The Hash of options
partial - The String partial filename without leading underscore
resource - The ActiveModel resource
collection - The Array of ActiveModel resources to use in place of
single resource


Surround partial render in script tags watching for sync_update and sync_destroy channels from pubsub server The Hash of options partial - The String partial filename without leading underscore resource - The ActiveModel resource collection - The Array of ActiveModel resources to use in place of single resource
def sync(options = {})
      channel      = options[:channel]
      partial_name = options.fetch(:partial, channel)
      collection   = options[:collection] || [options.fetch(:resource)]
      refetch      = options.fetch(:refetch, false)
      results = []
      collection.each do |resource|
        if refetch
          partial = RefetchPartial.new(partial_name, resource, channel, self)
        else
          partial = Partial.new(partial_name, resource, channel, self)
        end
        results << "
          <script type='text/javascript' data-sync-id='#{partial.selector_start}'>
            Sync.onReady(function(){
              var partial = new Sync.Partial({
                name:           '#{partial.name}',
                resourceName:   '#{partial.resource.name}',
                resourceId:     '#{resource.id}',
                authToken:      '#{partial.auth_token}',
                channelUpdate:  '#{partial.channel_for_action(:update)}',
                channelDestroy: '#{partial.channel_for_action(:destroy)}',
                selectorStart:  '#{partial.selector_start}',
                selectorEnd:    '#{partial.selector_end}',
                refetch:        #{refetch}
              });
              partial.subscribe();
            });
          </script>
        ".html_safe
        results << partial.render
        results << "
          <script type='text/javascript' data-sync-id='#{partial.selector_end}'>
          </script>
        ".html_safe
      end
      safe_join(results)
    end
def sync ( options = { } ) channel = options [ :channel ] partial_name = options . fetch ( :partial , channel ) collection = options [ :collection ] || [ options . fetch ( :resource ) ] refetch = options . fetch ( :refetch , false ) results = [ ] collection . each do | resource | if refetch partial = RefetchPartial . new ( partial_name , resource , channel , self ) else partial = Partial . new ( partial_name , resource , channel , self ) end results << "
          <script type='text/javascript' data-sync-id='#{partial.selector_start}'>
            Sync.onReady(function(){
              var partial = new Sync.Partial({
                name:           '#{partial.name}',
                resourceName:   '#{partial.resource.name}',
                resourceId:     '#{resource.id}',
                authToken:      '#{partial.auth_token}',
                channelUpdate:  '#{partial.channel_for_action(:update)}',
                channelDestroy: '#{partial.channel_for_action(:destroy)}',
                selectorStart:  '#{partial.selector_start}',
                selectorEnd:    '#{partial.selector_end}',
                refetch:        #{refetch}
              });
              partial.subscribe();
            });
          </script>
        " . html_safe results << partial . render results << "
          <script type='text/javascript' data-sync-id='#{partial.selector_end}'>
          </script>
        " . html_safe end safe_join ( results ) end
Surround partial render in script tags, watching for
sync_update and sync_destroy channels from pubsub server
Surround partial render in script tags watching for sync_update and sync_destroy channels from pubsub server
options