Sha256: 36e775dad440d67c8606f82dbfb15e274f9ec1809ade5f1cdc18233e8bc06aca

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

module Locomotive
  class EditableFile < EditableElement

    ## behaviours ##
    mount_uploader 'source', EditableFileUploader

    replace_field 'source', ::String, true

    ## fields ##
    field :default_source_url, localize: true
    field :resize_format

    ## callbacks ##
    after_initialize :init_localized_source

    ## methods ##

    # Returns the url or the path to the uploaded file
    # if it exists. Otherwise returns the default url.
    #
    # @note This method is not used for the rendering, only for the back-office
    #
    # @return [String] The url or path of the file
    #
    def content
      self.source? ? self.source.url : self.default_source_url
    end

    alias :content= :source=

    def remove_source=(value)
      self.source_will_change! # notify the page to run the callbacks for that element
      super
    end

    private

    def init_localized_source
      # FIXME: the source of the element is not correctly set for the first time (certainly because the source is localized)
      attributes[:source] = {} unless self.persisted?
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
locomotivecms-3.0.0 app/models/locomotive/editable_file.rb
locomotivecms-3.0.0.rc7 app/models/locomotive/editable_file.rb