Class: Locomotive::EditableElement

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

Direct Known Subclasses

EditableControl, EditableFile, EditableText

Instance Method Summary collapse

Instance Method Details

#_run_rearrange_callbacksObject



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

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.



109
110
111
112
# File 'app/models/locomotive/editable_element.rb', line 109

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.



119
120
121
# File 'app/models/locomotive/editable_element.rb', line 119

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



64
65
66
67
68
69
70
71
# File 'app/models/locomotive/editable_element.rb', line 64

def copy_attributes(attributes)
  # _type is among the mass-assign protected attributes.
  if type = attributes.delete(:_type)
    self._type = type
  end

  self.attributes = attributes
end

#copy_attributes_from(el) ⇒ Object

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

Parameters:



79
80
81
82
# File 'app/models/locomotive/editable_element.rb', line 79

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

#copy_default_attributes_from(el) ⇒ Object

Copy the default attributes: _type, hint, fixed, priority and locales from an existing editable element coming from the parent page. Each type of an editable element may or not override this method for options for instance.

Parameters:



91
92
93
94
95
96
# File 'app/models/locomotive/editable_element.rb', line 91

def copy_default_attributes_from(el)
  # only the type, hint and fixed properties can be modified from the element
  %w(_type hint fixed priority locales).each do |attr|
    self.send(:"#{attr}=", el.send(attr.to_sym))
  end
end

#default_content?Boolean

Returns:



54
55
56
57
# File 'app/models/locomotive/editable_element.rb', line 54

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
38
39
# File 'app/models/locomotive/editable_element.rb', line 35

def disabled_in_all_translations?
  return self.disabled_translations if self.disabled_translations.is_a?(Boolean)
  return false if disabled_translations.blank?
  self.disabled_translations.all? { |_, v| v == true }
end

#editable?Boolean

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

Returns:



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

def editable?
  !self.disabled? &&
  self.locales.include?(::Mongoid::Fields::I18n.locale.to_s) &&
  (!self.fixed? || !self.from_parent?) &&
  !self.destroyed?
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.



101
102
103
# File 'app/models/locomotive/editable_element.rb', line 101

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