<commit_before>module StaticBlocks
  class Snippet < ActiveRecord::Base
    attr_accessible :content, :status, :title
    after_save :clear_cache
    scope :published, where(:status => 'published')
    translates :content
    validates :title, uniqueness: true

    def to_s
      content
    end

    def clear_cache
      Rails.cache.delete("snippet::"+I18n.locale.to_s+"::"+title)
    end

    def self.to_csv(options = {})
      snippets = self.connection.select_all('select * from static_blocks_snippets')
      snippets_column_names = snippets.first.keys
      CSV.generate(options) do |csv|
        csv << snippets_column_names
        snippets.each do |s|
          csv << s.values_at(*snippets_column_names)
        end
      end
    end

    def self.translations_to_csv(options = {})
      translations = self.connection.select_all('
        SELECT s.title, t.* FROM static_blocks_snippet_translations t
        INNER JOIN static_blocks_snippets s
        ON t.static_blocks_snippet_id = s.id
        ')
      if translations.empty?
        flash[:error] = "There are no translations"
        return
      end
      translation_column_names = translations.first.keys
      CSV.generate(options) do |csv|
        csv << translation_column_names
        translations.each do |t|
          csv << t.values_at(*translation_column_names)
        end
      end
    end

    def self.import(file)
      CSV.foreach(file.path, headers: true) do |row|
        snippet = find_by_title(row["title"]) || new
        snippet.attributes = row.to_hash.slice(*accessible_attributes)
        snippet.save!
      end
    end

    def self.import_translations(file)
      # delete all translations
      delete_sql = "DELETE FROM static_blocks_snippet_translations"
      translation = ActiveRecord::Base.connection.execute(delete_sql)

      # add all translations
      CSV.foreach(file.path, headers: true) do |row|
        static_block = find_by_title(row['title'])

        # return if nil
        # we don't want to update or insert a translation if the snippet doesn't exist
        return if static_block.nil?

        # create new translation
        sql_array = ["
        INSERT INTO static_blocks_snippet_translations
        ('static_blocks_snippet_id', 'locale', 'content', 'created_at', 'updated_at') VALUES
        (?, ?, ?, ?, ?)", "#{static_block.id}", row['locale'], row['content'], row['created_at'], row['updated_at']]
        insert_sql = sanitize_sql_array(sql_array)

        ActiveRecord::Base.connection.execute(insert_sql)

        # manually clear the cache for that snippet
        Rails.cache.delete("snippet::#{row['locale']}::#{row['title']}")
      end
    end
  end
end<commit_msg>pg syntax error. removing quotes<commit_after>module StaticBlocks
  class Snippet < ActiveRecord::Base
    attr_accessible :content, :status, :title
    after_save :clear_cache
    scope :published, where(:status => 'published')
    translates :content
    validates :title, uniqueness: true

    def to_s
      content
    end

    def clear_cache
      Rails.cache.delete("snippet::"+I18n.locale.to_s+"::"+title)
    end

    def self.to_csv(options = {})
      snippets = self.connection.select_all('select * from static_blocks_snippets')
      snippets_column_names = snippets.first.keys
      CSV.generate(options) do |csv|
        csv << snippets_column_names
        snippets.each do |s|
          csv << s.values_at(*snippets_column_names)
        end
      end
    end

    def self.translations_to_csv(options = {})
      translations = self.connection.select_all('
        SELECT s.title, t.* FROM static_blocks_snippet_translations t
        INNER JOIN static_blocks_snippets s
        ON t.static_blocks_snippet_id = s.id
        ')
      if translations.empty?
        flash[:error] = "There are no translations"
        return
      end
      translation_column_names = translations.first.keys
      CSV.generate(options) do |csv|
        csv << translation_column_names
        translations.each do |t|
          csv << t.values_at(*translation_column_names)
        end
      end
    end

    def self.import(file)
      CSV.foreach(file.path, headers: true) do |row|
        snippet = find_by_title(row["title"]) || new
        snippet.attributes = row.to_hash.slice(*accessible_attributes)
        snippet.save!
      end
    end

    def self.import_translations(file)
      # delete all translations
      delete_sql = "DELETE FROM static_blocks_snippet_translations"
      translation = ActiveRecord::Base.connection.execute(delete_sql)

      # add all translations
      CSV.foreach(file.path, headers: true) do |row|
        static_block = find_by_title(row['title'])

        # return if nil
        # we don't want to update or insert a translation if the snippet doesn't exist
        return if static_block.nil?

        # create new translation
        sql_array = ["
        INSERT INTO static_blocks_snippet_translations
        (static_blocks_snippet_id, locale, content, created_at, updated_at) VALUES
        (?, ?, ?, ?, ?)", static_block.id, row['locale'], row['content'], row['created_at'], row['updated_at']]
        insert_sql = sanitize_sql_array(sql_array)

        ActiveRecord::Base.connection.execute(insert_sql)

        # manually clear the cache for that snippet
        Rails.cache.delete("snippet::#{row['locale']}::#{row['title']}")
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.10114433370247324, 'fraction_numerical': 0.0007382798080472499, 'mean_word_length': 2.8161971830985917, 'pattern_counts': {'":': 2, '<': 21, '<?xml version=': 0, '>': 5, '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': '5431440', 'n_tokens_mistral': 1658, 'n_tokens_neox': 1583, 'n_words': 363}