Class: Vapid::Models::Record

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/vapid/models/record.rb

Overview

Record model

Instance Method Summary collapse

Instance Method Details

#content_summaryObject



25
26
27
# File 'lib/vapid/models/record.rb', line 25

def content_summary
  group.text_fields.any? ? content.values_at(*group.text_fields).join(", ") : content.to_json
end

#display_nameObject



9
10
11
# File 'lib/vapid/models/record.rb', line 9

def display_name
  content["name"] || content["title"]
end

#slugObject



13
14
15
16
# File 'lib/vapid/models/record.rb', line 13

def slug
  return unless display_name
  display_name.downcase.gsub(/[^a-z0-9]+/, "-").chomp("-")
end

#update_content(content, remove = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vapid/models/record.rb', line 29

def update_content(content, remove = {})
  content.each do |key, val|
    # TODO: File uploads?
    # if val.is_a?(Hash) && val[:tempfile]
    #   FileUtils.mkdir_p(settings.project_uploads)
    #   File.open File.join(settings.project_uploads, val[:filename]), "wb" do |file|
    #     file.write val[:tempfile].read
    #   end
    #   val = "/uploads/#{val[:filename]}"
    # end

    content_type = group.fields[key]
    self.content[key] = Directives[content_type].serialize_input(val)
  end

  remove.each do |key, _val|
    self.content[key] = nil
  end if remove

  save
end

#urlObject



18
19
20
21
22
23
# File 'lib/vapid/models/record.rb', line 18

def url
  return unless group.repeating?

  parts = [group.name, id, slug].reject(&:nil?)
  "/#{parts.join('/')}".downcase
end