Class: Pwb::Content

Inherits:
ApplicationRecord show all
Defined in:
app/models/pwb/content.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import(file) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/models/pwb/content.rb', line 23

def import(file)
  # byebug
  CSV.foreach(file.path, headers: true) do |row|
    if row.to_hash["locale"].present? && row.to_hash["key"].present?
      # Translation.create! row.to_hash
      trsl = find_by_key(row["key"]) || new
      trsl.attributes = row.to_hash.slice("key", "value", "locale")
      # *accessible_attributes)
      trsl.save!
    end
  end
end

.to_csv(export_column_names = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'app/models/pwb/content.rb', line 36

def to_csv(export_column_names = nil)
  # http://railscasts.com/episodes/362-exporting-csv-and-excel?view=asciicast
  export_column_names ||= column_names
  CSV.generate do |csv|
    csv << export_column_names
    all.each do |content|
      csv << content.attributes.values_at(*export_column_names)
    end
  end
end

Instance Method Details

#default_photoObject

globalize_accessors locales: [:en, :ca, :es, :fr, :ar, :de, :ru, :pt]



10
11
12
# File 'app/models/pwb/content.rb', line 10

def default_photo
  content_photos.first
end