Class: StaticBlocks::Snippet

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/static_blocks/snippet.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import(file) ⇒ Object



47
48
49
50
51
52
53
# File 'app/models/static_blocks/snippet.rb', line 47

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

.import_translations(file) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/static_blocks/snippet.rb', line 55

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

.to_csv(options = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'app/models/static_blocks/snippet.rb', line 17

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

.translations_to_csv(options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/static_blocks/snippet.rb', line 28

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

Instance Method Details

#clear_cacheObject



13
14
15
# File 'app/models/static_blocks/snippet.rb', line 13

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

#to_sObject



9
10
11
# File 'app/models/static_blocks/snippet.rb', line 9

def to_s
  content
end