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
81
82
83
84
85
86
87
88
89
90
# File 'app/models/static_blocks/snippet.rb', line 55

def self.import_translations(file)
  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 if the snippet doesn't exist
    return if static_block.nil?

    # find translation
    raw_sql = "
    SELECT s.title, t.* FROM static_blocks_snippet_translations t
    INNER JOIN static_blocks_snippets s
    ON t.static_blocks_snippet_id = s.id
    WHERE s.title='#{row['title']}' AND t.locale='#{row['locale']}'
    "
    translation = ActiveRecord::Base.connection.execute(raw_sql)

    if translation.present?
      # update existing translation
      sql_array = ["
        UPDATE static_blocks_snippet_translations
        SET content=?, created_at=?, updated_at=?
        WHERE static_blocks_snippet_id=? AND locale=?
        ", row['content'], row['created_at'], row['updated_at'], static_block.id, row['locale']]
      raw_sql = sanitize_sql_array(sql_array)
    else
      # 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']]
      raw_sql = sanitize_sql_array(sql_array)
    end
    ActiveRecord::Base.connection.execute(raw_sql)
  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