# encoding: utf-8

module CartoDB
  module Visualization
    class TableBlender
      def initialize(user, tables=[])
        @user   = user
        @tables = tables
      end

      def blend
        raise "Viewer users can't blend tables" if user.viewer

        maps            = tables.map(&:map)
        copier          = CartoDB::Map::Copier.new
        destination_map = copier.new_map_from(maps.first).save

        copier.copy_base_layer(maps.first, destination_map)
        maps.each { |map| copier.copy_data_layers(map, destination_map) }

        destination_map.user = user
        destination_map.save
        destination_map
      end

      def blended_privacy
        return Visualization::Member::PRIVACY_PRIVATE if tables.map{|t| t.private?}.any?
        return Visualization::Member::PRIVACY_LINK if tables.map{|t| t.public_with_link_only?}.any?
        Visualization::Member::PRIVACY_PUBLIC
      end

      private

      attr_reader :tables, :user
    end
  end
end

# encoding: utf-8

require_dependency 'map/copier'

module CartoDB
  module Visualization
    class TableBlender
      def initialize(user, tables=[])
        @user   = user
        @tables = tables
      end

      def blend
        raise "Viewer users can't blend tables" if user.viewer

        maps            = tables.map(&:map)
        copier          = CartoDB::Map::Copier.new
        destination_map = copier.new_map_from(maps.first).save

        copier.copy_base_layer(maps.first, destination_map)
        maps.each { |map| copier.copy_data_layers(map, destination_map) }

        destination_map.user = user
        destination_map.save
        destination_map
      end

      def blended_privacy
        return Visualization::Member::PRIVACY_PRIVATE if tables.map{|t| t.private?}.any?
        return Visualization::Member::PRIVACY_LINK if tables.map{|t| t.public_with_link_only?}.any?
        Visualization::Member::PRIVACY_PUBLIC
      end

      private

      attr_reader :tables, :user
    end
  end
end

Add map copier dependency Before it was taken implicitly from the controllers
Add map copier dependency
Before it was taken implicitly from the controllers
