Class: Locomotive::EditableElement

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/locomotive/editable_element.rb

Direct Known Subclasses

EditableControl, EditableFile, EditableShortText

Instance Method Summary collapse

Instance Method Details

#_run_rearrange_callbacksObject



45
46
47
# File 'app/models/locomotive/editable_element.rb', line 45

def _run_rearrange_callbacks
  # callback from page/tree. not needed in the editable elements
end

#add_current_localeObject

Make sure the current locale is added to the list of locales for the current element so that we know in which languages the element was translated.



85
86
87
88
# File 'app/models/locomotive/editable_element.rb', line 85

def add_current_locale
  locale = ::Mongoid::Fields::I18n.locale.to_s
  self.locales << locale unless self.locales.include?(locale)
end

#by_priorityObject

scopes ##



27
# File 'app/models/locomotive/editable_element.rb', line 27

scope :by_priority, :order_by => [[:priority, :desc]]

#content_from_default=(content) ⇒ Object

Set the content of the editable element with a default value only if the content has not already been modified by the user.

Parameters:

  • content (String)

    The default content.



95
96
97
# File 'app/models/locomotive/editable_element.rb', line 95

def content_from_default=(content)
  # needs to be overridden for each kind of elements
end

#copy_attributes(attributes) ⇒ Object

Copy attributes extracted from the corresponding Liquid tag Each editable element overrides this method.

Parameters:

  • attributes (Hash)

    The up-to-date attributes



59
60
61
# File 'app/models/locomotive/editable_element.rb', line 59

def copy_attributes(attributes)
  self.attributes = attributes
end

#copy_attributes_from(el) ⇒ Object

Copy attributes from an existing editable element coming from the parent page. Each editable element may or not override this method. The source element is a new record.

Parameters:



69
70
71
72
# File 'app/models/locomotive/editable_element.rb', line 69

def copy_attributes_from(el)
  self.attributes   = el.attributes.reject { |attr| !%w(slug block hint priority fixed disabled locales from_parent).include?(attr) }
  self.from_parent  = true
end

#default_content?Boolean

Returns:



49
50
51
52
# File 'app/models/locomotive/editable_element.rb', line 49

def default_content?
  # needs to be overridden for each kind of elements
  true
end

#disabled?Boolean

methods ##

Returns:



31
32
33
# File 'app/models/locomotive/editable_element.rb', line 31

def disabled?
  !!self.disabled # the original method does not work quite well with the localization
end

#disabled_in_all_translations?Boolean

Returns:



35
36
37
# File 'app/models/locomotive/editable_element.rb', line 35

def disabled_in_all_translations?
  self.disabled_translations.all? { |_, v| v == true }
end

#editable?Boolean

Determines if the current element can be edited in the back-office

Returns:



41
42
43
# File 'app/models/locomotive/editable_element.rb', line 41

def editable?
  !self.disabled? && self.locales.include?(::Mongoid::Fields::I18n.locale.to_s) && (!self.fixed? || !self.from_parent?)
end

#pageObject

associations ##



18
# File 'app/models/locomotive/editable_element.rb', line 18

embedded_in :page, :class_name => 'Locomotive::Page', :inverse_of => :editable_elements

#set_default_content_from(el) ⇒ Object

Set the default content from an existing editable element coming from the parent page. Each editable element may or not override this method. The source element is an existing record.



77
78
79
# File 'app/models/locomotive/editable_element.rb', line 77

def set_default_content_from(el)
  self.add_current_locale
end

#slugObject

validations ##



8
# File 'app/models/locomotive/editable_element.rb', line 8

field :slug