Class: Locomotive::EditableElement
- Inherits:
-
Object
- Object
- Locomotive::EditableElement
- Includes:
- Mongoid::Document
- Defined in:
- app/models/locomotive/editable_element.rb
Direct Known Subclasses
Instance Method Summary collapse
- #_run_rearrange_callbacks ⇒ Object
-
#add_current_locale ⇒ Object
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.
-
#by_priority ⇒ Object
scopes ##.
-
#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.
-
#copy_attributes(attributes) ⇒ Object
Copy attributes extracted from the corresponding Liquid tag Each editable element overrides this method.
-
#copy_attributes_from(el) ⇒ Object
Copy attributes from an existing editable element coming from the parent page.
-
#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.
- #default_content? ⇒ Boolean
-
#disabled? ⇒ Boolean
methods ##.
- #disabled_in_all_translations? ⇒ Boolean
-
#editable? ⇒ Boolean
Determines if the current element can be edited in the back-office.
-
#page ⇒ Object
associations ##.
-
#set_default_content_from(el) ⇒ Object
Set the default content from an existing editable element coming from the parent page.
-
#slug ⇒ Object
validations ##.
Instance Method Details
#_run_rearrange_callbacks ⇒ Object
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_locale ⇒ Object
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_priority ⇒ Object
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.
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.
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.
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.
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
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 ##
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
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
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 |
#page ⇒ Object
associations ##
18 |
# File 'app/models/locomotive/editable_element.rb', line 18 :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 |
#slug ⇒ Object
validations ##
8 |
# File 'app/models/locomotive/editable_element.rb', line 8 field :slug |