Class: Blacksand::Property

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActionView::Helpers::TextHelper
Defined in:
app/models/blacksand/property.rb

Direct Known Subclasses

AsArray, File, Gallery, Slide

Defined Under Namespace

Classes: AsArray, File, Gallery, Slide

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_property(page, field) ⇒ Object



66
67
68
69
70
71
72
73
74
# File 'app/models/blacksand/property.rb', line 66

def self.build_property(page, field)
  if field.field_type.in? %w[file slide gallery]
    page.properties.build(field: field, type: "Blacksand::Property::#{field.field_type.capitalize}")
  elsif field.field_type == 'array'
    page.properties.build(field: field, type: "Blacksand::Property::As#{field.field_type.capitalize}")
  else
    page.properties.build(field: field)
  end
end

Instance Method Details

#as_subtypeObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/blacksand/property.rb', line 53

def as_subtype
  return if self.value.blank?

  case self.field.field_type
    when 'date' then
      Date.strptime(self.value, '%F')
    when 'array' then
      self.values
    else
      self.value
  end
end

#check_field_requiredObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/blacksand/property.rb', line 19

def check_field_required
  case (self.field.field_type)
    when 'image', 'slide' then
      errors.add(:image, "#{field.description}不能为空") if field.required? && !image.present?
    when 'gallery' then
      errors.add(:gallery, "#{field.description}不能为空") if field.required? && !pictures.any?
    when 'file' then
      errors.add(:file, "#{field.description}不能为空") if field.required? && !file.present?
    when 'array' then
      errors.add(:values, "#{field.description}不能为空") if field.required? && !values.present?
    else
      errors.add(:value, "#{field.description}不能为空") if field.required? && !value.present?
  end
end

#contentObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/models/blacksand/property.rb', line 34

def content
  case (self.field.field_type)
    when 'textarea'
      simple_format(self.value)
    when 'image', 'slide' then
      self.image
    when 'gallery' then
      self.pictures
    when 'file' then
      self.file
    when 'page' then
      Page.find_by(id: self.value)
    when 'array' then
      self.values
    else
      self.value
  end
end