Class: DynamicContent::Content

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/dynamic_content/content.rb

Constant Summary collapse

INPUT_TYPES =

hash key: dynamic content data type field: where content is stored on database as: formtastic input type wrapper_class: css class of element wrapper, like li tag on formtastic input_class: css class of input element

{
  default: { field: :content, as: :string, wrapper_class: 'string', input_class: '' },
  select: { as: :select },
  boolean: { as: :boolean },
  text: { as: :text },
  markdown: { as: :text },
  editor: { as: :text, input_class: 'complete-editor' },
  embed: {},
  uploader: { field: :file, as: :file },
  image: { field: :file },
  date: { field: :date, as: :date_select },
  time: { field: :date, as: :time_select },
  datetime: { field: :date, as: :datetime_select },
  video: { as: :url },
  currency: { as: :number },
  hidden: { as: :hidden }
}
INPUT_MODIFIERS =
{
  markdown: { string: { as: :string } },
  editor: { simple_editor: { input_class: 'simple-editor' }, 'list-editor': { input_class: 'list-editor' } }
}

Instance Method Summary collapse

Instance Method Details

#formtastic_compatible?Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'app/models/dynamic_content/content.rb', line 70

def formtastic_compatible?
  not_compatible = %w(image uploader embed)

  return !self.has_caption && !not_compatible.include?(self.data_type.to_s)
end

#get_content(*args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/dynamic_content/content.rb', line 76

def get_content *args
  case self.data_type
  when 'select'
    result = self.content.to_i
  when 'boolean'
    result = self.content.to_i == 1
  when 'currency'
    # result = self.content.gsub(',', '.').to_f
    result = ActionController::Base.helpers.number_to_currency(self.content.gsub(',', '.').to_f)
  when 'editor'
    result = self.content.html_safe
  when 'markdown'
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, tables: false, filter_html: true, no_styles: true, no_images: true, highlight: true)
    result = markdown.render(self.content).gsub('<p>', '').gsub('</p>', '').html_safe
    self.caption = markdown.render(self.caption).gsub('<p>', '').gsub('</p>', '') if self.has_caption
  when 'embed'
    result = self.content.html_safe
  when 'uploader'
    result = self.file.url
  when 'image'
    result = {content: (self.file ? self.file.url : ''), alt: self.content, uid: self.file_uid}
  when 'date'
    result = self.date.blank? ? nil : self.date.to_date
  when 'datetime'
    result = self.date
  when 'video'
    result = {content: '', provider: :none}

    unless self.content.blank?
      parsed_url = self.content.gsub('https', 'http').match(/http:\/\/(?:www\.)?(vimeo|youtube)\.com\/(?:watch\?v=)?(.*?)(?:\z|$|&)/)

      if !parsed_url.nil? && ['youtube', 'vimeo'].include?(parsed_url[1])
        result = {
          content: parsed_url[2],
          provider: parsed_url[1].to_sym,
          link: (parsed_url[1] == 'youtube' ? "http://youtu.be/#{parsed_url[2]}" : "https://vimeo.com/#{parsed_url[2]}"),
          embed: (parsed_url[1] == 'youtube' ? "https://www.youtube.com/embed/#{parsed_url[2]}" : "https://player.vimeo.com/video/#{parsed_url[2]}?color=ffffff")
        }
      end
    end
  else
    result = self.content
  end

  result[:data_type] = self.data_type if result.kind_of?(Hash)

  if self.has_caption
    return {caption: self.caption.html_safe, content: result}
  else
    return result
  end
end

#get_type_optionsObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/dynamic_content/content.rb', line 53

def get_type_options
  return @type_options unless @type_options.nil?

  @type_options = INPUT_TYPES[:default].dup
  current_type = self.data_type.to_sym

  @type_options.merge!(INPUT_TYPES[current_type]) if INPUT_TYPES.include?(current_type)

  if INPUT_MODIFIERS.include?(current_type) && INPUT_MODIFIERS[current_type].include?(self.type_options.to_sym)
    @type_options.merge!(INPUT_MODIFIERS[current_type][self.type_options.to_sym])
  end

  @type_options[:wrapper_class] += (self.has_divisor ? ' has-divisor' : '')

  return @type_options
end