<commit_before># encoding: utf-8
require_relative '../visualization/collection'
require_relative '../visualization/member'

module CartoDB
  module Table
    class Relator
      INTERFACE = %w{
        table_visualization
        serialize_dependent_visualizations
        serialize_non_dependent_visualizations
        dependent_visualizations
        non_dependent_visualizations
        affected_visualizations
      }

      def initialize(db, table)
        @db     = db
        @table  = table
      end #initialize

      def table_visualization
        @table_visualization ||= Visualization::Collection.new.fetch(
          map_id: table.map_id,
          type:   'table'
        ).first
      end #table_visualization

      def serialize_dependent_visualizations
        dependent_visualizations.map { |object| preview_for(object) }
      end #serialize_dependent_visualizations

      def serialize_non_dependent_visualizations
        non_dependent_visualizations.map { |object| preview_for(object) }
      end #serialize_non_dependent_visualizations

      def dependent_visualizations
        affected_visualizations.select(&:dependent?)
      end #dependent_visualizations

      def non_dependent_visualizations
        affected_visualizations.select(&:non_dependent?)
      end #non_dependent_visualizations

      def affected_visualizations
        affected_visualization_records.map do |attributes|
          Visualization::Member.new(attributes)
        end
      end #affected_visualizations

      def preview_for(object)
        { id: object.id, name: object.name }
      end #preview_for

      private

      attr_reader :db, :table

      def affected_visualization_records
        db[:visualizations].with_sql(%Q{
          SELECT  *
          FROM    layers_user_tables, layers_maps, visualizations
          WHERE   layers_user_tables.user_table_id = #{table.id}
          AND     layers_user_tables.layer_id = layers_maps.layer_id
          AND     layers_maps.map_id = visualizations.map_id
        })
      end #affected_visualization_records
    end # Relator
  end # Table
end # CartoDB
<commit_msg>Return unique elements in Table#affected_visualizations<commit_after># encoding: utf-8
require_relative '../visualization/collection'
require_relative '../visualization/member'

module CartoDB
  module Table
    class Relator
      INTERFACE = %w{
        table_visualization
        serialize_dependent_visualizations
        serialize_non_dependent_visualizations
        dependent_visualizations
        non_dependent_visualizations
        affected_visualizations
      }

      def initialize(db, table)
        @db     = db
        @table  = table
      end #initialize

      def table_visualization
        @table_visualization ||= Visualization::Collection.new.fetch(
          map_id: table.map_id,
          type:   'table'
        ).first
      end #table_visualization

      def serialize_dependent_visualizations
        dependent_visualizations.map { |object| preview_for(object) }
      end #serialize_dependent_visualizations

      def serialize_non_dependent_visualizations
        non_dependent_visualizations.map { |object| preview_for(object) }
      end #serialize_non_dependent_visualizations

      def dependent_visualizations
        affected_visualizations.select(&:dependent?)
      end #dependent_visualizations

      def non_dependent_visualizations
        affected_visualizations.select(&:non_dependent?)
      end #non_dependent_visualizations

      def affected_visualizations
        affected_visualization_records.to_a.uniq { |attributes|
          attributes.fetch(:id)
        }.map { |attributes|
          Visualization::Member.new(attributes)
        }
      end #affected_visualizations

      def preview_for(object)
        { id: object.id, name: object.name }
      end #preview_for

      private

      attr_reader :db, :table

      def affected_visualization_records
        db[:visualizations].with_sql(%Q{
          SELECT  *
          FROM    layers_user_tables, layers_maps, visualizations
          WHERE   layers_user_tables.user_table_id = #{table.id}
          AND     layers_user_tables.layer_id = layers_maps.layer_id
          AND     layers_maps.map_id = visualizations.map_id
        })
      end #affected_visualization_records
    end # Relator
  end # Table
end # CartoDB

{'fraction_non_alphanumeric': 0.06147166361974406, 'fraction_numerical': 0.0004570383912248629, 'mean_word_length': 2.7603092783505154, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3682461', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1138, 'n_words': 227}