Sha256: 4feaf56ecd2ef9dd24c729df7ae5fcef023d65005aed91ffc51e2e1ef5ad7206

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

module Locomotive
  class EditableFile < EditableElement

    ## behaviours ##
    mount_uploader 'source', EditableFileUploader

    replace_field 'source', ::String, true

    ## fields ##
    field :default_source_url, :localize => true

    ## callbacks ##
    after_save :propagate_content

    ## 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

    def default_content?
      !self.source? && self.default_source_url.present?
    end

    def copy_attributes(attributes)
      unless self.default_content?
        attributes.delete(:default_source_url)
      end

      super(attributes)
    end

    def copy_attributes_from(el)
      super(el)

      if el.source_translations.nil?
        self.attributes['default_source_url'] = el.attributes['default_source_url'] || {}
      else
        el.source_translations.keys.each do |locale|
          ::Mongoid::Fields::I18n.with_locale(locale) do
            self.default_source_url = el.source? ? el.source.url : el.default_source_url
          end
        end
      end
    end

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

    def as_json(options = {})
      Locomotive::EditableFilePresenter.new(self).as_json
    end

    protected

    def propagate_content
      if self.source_changed?
        operations  = {
          '$set' => {
            "editable_elements.$.default_source_url.#{::Mongoid::Fields::I18n.locale}" => self.source.url
          }
        }

        self.page.collection.update self._selector, operations, :multi => true
      end
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
locomotive_cms-2.0.0.rc12 app/models/locomotive/editable_file.rb
locomotive_cms-2.0.0.rc11 app/models/locomotive/editable_file.rb
locomotive_cms-2.0.0.rc10 app/models/locomotive/editable_file.rb
locomotive_cms-2.0.0.rc9 app/models/locomotive/editable_file.rb
locomotive_cms-2.0.0.rc8 app/models/locomotive/editable_file.rb
locomotive_cms-2.0.0.rc7 app/models/locomotive/editable_file.rb
locomotive_cms-2.0.0.rc6 app/models/locomotive/editable_file.rb
locomotive_cms-2.0.0.rc5 app/models/locomotive/editable_file.rb
locomotive_cms-2.0.0.rc4 app/models/locomotive/editable_file.rb