Sha256: 55425ede53105bfbb9a3a69febcedc90dc61add469e3d40cecfa967457cbf2d4

Contents?: true

Size: 1.09 KB

Versions: 4

Compression:

Stored size: 1.09 KB

Contents

module TypeStation
  class Content
    include ::Mongoid::Document
    include ::Mongoid::Attributes::Dynamic

    TYPES = [:text, :html, :image, :file, :select, :multiple_select]

    # RELATIONS

    embedded_in :page

    # FIELDS
    field :name, type: Symbol
    field :type, type: Symbol, default: TYPES.first

    # VALIDATIONS

    validates :name, presence: true
    validates :type, presence: true

    # INSTANT METHODS

    def get
      self[self.type]
    end

    def set(value)
      self[self.type] = convert(value)
    end

    private

    def convert(value)
      case type
      #when :image
      #  preloaded = Cloudinary::PreloadedFile.new(value)         
      #  raise "Invalid upload signature" if !preloaded.valid?
      #  preloaded.identifier
      when :multiple_select
        convert_to_array(value)
      else
        value
      end
    end

    def convert_to_array(value)
      if value.is_a?(String)
        value.split(',').map(&:strip)
      elsif value.is_a?(Array)
        value
      else
        raise 'Dont know who to covert value to array'
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
type_station-0.0.3 app/models/type_station/content.rb
type_station-0.0.2 app/models/type_station/content.rb
type_station-0.0.1 app/models/type_station/content.rb
type_station-0.0.1.pre app/models/type_station/content.rb