Class: Locomotive::EditableFile

Inherits:
EditableElement show all
Defined in:
app/models/locomotive/editable_file.rb

Instance Method Summary collapse

Methods inherited from EditableElement

#_run_rearrange_callbacks, #add_current_locale, #by_priority, #content_from_default=, #disabled?, #disabled_in_all_translations?, #editable?, #page, #slug

Instance Method Details

#contentString

Note:

This method is not used for the rendering, only for the back-office

Returns the url or the path to the uploaded file if it exists. Otherwise returns the default url.

Returns:

  • (String)

    The url or path of the file



24
25
26
# File 'app/models/locomotive/editable_file.rb', line 24

def content
  self.source? ? self.source.url : self.default_source_url
end

#copy_attributes(attributes) ⇒ Object



32
33
34
35
36
37
38
# File 'app/models/locomotive/editable_file.rb', line 32

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

  super(attributes)
end

#copy_attributes_from(el) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/locomotive/editable_file.rb', line 40

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

#default_content?Boolean

Returns:



28
29
30
# File 'app/models/locomotive/editable_file.rb', line 28

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

#default_source_urlObject

fields ##



10
# File 'app/models/locomotive/editable_file.rb', line 10

field :default_source_url, :localize => true

#remove_source=(value) ⇒ Object



64
65
66
67
68
# File 'app/models/locomotive/editable_file.rb', line 64

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

#set_default_content_from(el) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'app/models/locomotive/editable_file.rb', line 54

def set_default_content_from(el)
  super(el)

  locale = ::Mongoid::Fields::I18n.locale.to_s

  if self.attributes['default_source_url'][locale].nil?
    self.default_source_url = el.default_source_url
  end
end

#sourceObject

behaviours ##



5
# File 'app/models/locomotive/editable_file.rb', line 5

mount_uploader 'source', EditableFileUploader