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



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

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.



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

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.



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

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



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

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:



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

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:



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

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:



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

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
# 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)
  self.disabled_translations.all? { |_, v| v == true }
end

#editable?Boolean

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

Returns:



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

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.



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

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