<commit_before>module Sync

  module ViewHelpers
    def sync(options = {})
      channel      = options[:channel]
      partial_name = options.fetch(:partial, channel)
      collection   = options[:collection] || [options.fetch(:resource)]
      
      result = [] 
      collection.each do |resource|
        partial = Sync::Partial.new(partial_name, resource, channel, self)
        result << "
          <script type='text/javascript' data-sync-id='#{partial.selector_start}'>
            Sync.onReady(function(){
              var partial = new Sync.Partial(
                '#{partial.name}',
                '#{partial.channel_for_action(:update)}',
                '#{partial.channel_for_action(:destroy)}',
                '#{partial.selector_start}',
                '#{partial.selector_end}'
              );
              partial.subscribe();
            });
          </script>
        ".html_safe
        result << partial.render
        result << "
          <script type='text/javascript' data-sync-id='#{partial.selector_end}'>
          </script>
        ".html_safe
      end

      safe_join(result)
    end

    def sync_new(options = {})
      partial_name = options.fetch(:partial)
      resource     = options.fetch(:resource)
      scope        = options[:scope]
      direction    = options.fetch :direction, 'append'

      creator = Sync::PartialCreator.new(partial_name, resource, scope, self)
      "
        <script type='text/javascript' data-sync-id='#{creator.selector}'>
          Sync.onReady(function(){
            var creator = new Sync.PartialCreator(
              '#{partial_name}',
              '#{creator.channel}',
              '#{creator.selector}',
              '#{direction}'
            );
            creator.subscribe();<commit_msg>Add documentation for Sync view helpers.<commit_after>module Sync

  module ViewHelpers

    # 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 %>
    #
    def sync(options = {})
      channel      = options[:channel]
      partial_name = options.fetch(:partial, channel)
      collection   = options[:collection] || [options.fetch(:resource)]
      
      result = [] 
      collection.each do |resource|
        partial = Sync::Partial.new(partial_name, resource, channel, self)
        result << "
          <script type='text/javascript' data-sync-id='#{partial.selector_start}'>
            Sync.onReady(function(){
              var partial = new Sync.Partial(
                '#{partial.name}',
                '#{partial.channel_for_action(:update)}',
                '#{partial.channel_for_action(:destroy)}',
                '#{partial.selector_start}',
                '#{partial.selector_end}'
              );
              partial.subscribe();
            });
          </script>
        ".html_safe
        result << partial.render
        result << "
          <script type='text/javascript' data-sync-id='#{partial.selector_end}'>
          </script>
        ".html_safe
      end

      safe_join(result)
    end

    # Setup listener for new resource from sync_new channel, appending
    # partial in place
    #
    # options - The Hash of options
    #   partial - The String partial filename without leading underscore
    #   resource - The ActiveModel resource
    #   scope - The ActiveModel resource to scope the new channel publishes to.
    #           Used for restricting new resource publishes to 'owner' models.
    #           ie, current_user, project, group, etc. When excluded, listens 
    #           for global resource creates.
    # 
    #   direction - The String/Symbol direction to insert rendered partials.
    #               One of :append, :prepend. Defaults to :append
    #
    # Examples
    #   <%= sync_new partial: 'todo', resource: Todo.new, scope: @project %>
    #   <%= sync_new partial: 'todo', resource: Todo.new, scope: @project, direction: :prepend %>
    #  
    def sync_new(options = {})
      partial_name = options.fetch(:partial)
      resource     = options.fetch(:resource)
      scope        = options[:scope]
      direction    = options.fetch :direction, 'append'

      creator = Sync::PartialCreator.new(partial_name, resource, scope, self)
      "
        <script type='text/javascript' data-sync-id='#{creator.selector}'>
          Sync.onReady(function(){
            var creator = new Sync.PartialCreator(
              '#{partial_name}',
              '#{creator.channel}',
              '#{creator.selector}',
              '#{direction}'
            );
            creator.subscribe();
{'fraction_non_alphanumeric': 0.11874875473201833, 'fraction_numerical': 0.0, 'mean_word_length': 2.020457280385078, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4789573', 'n_tokens_mistral': 1333, 'n_tokens_neox': 1335, 'n_words': 315}