module Releaf::Content::Node

Public Instance Methods

assign_attributes_from(source_node) click to toggle source
# File lib/releaf/content/node.rb, line 131
def assign_attributes_from(source_node)
  source_node.attributes_to_copy.each do |attribute|
    send("#{attribute}=", source_node.send(attribute))
  end
end
attributes_to_copy() click to toggle source
# File lib/releaf/content/node.rb, line 59
def attributes_to_copy
  self.class.column_names - attributes_to_not_copy
end
attributes_to_not_copy() click to toggle source
# File lib/releaf/content/node.rb, line 53
def attributes_to_not_copy
  list = %w[content_id depth id item_position lft rgt created_at updated_at]
  list << "locale" if locale_before_type_cast.blank?
  list
end
available?() click to toggle source

Check whether object and all its ancestors are active @return [Boolean] returns true if object is available

# File lib/releaf/content/node.rb, line 118
def available?
  self_and_ancestors_array.all?(&:active?)
end
build_content(params = {}) click to toggle source
# File lib/releaf/content/node.rb, line 9
def build_content(params = {})
  self.content = content_class.new(params)
end
content_class() click to toggle source
# File lib/releaf/content/node.rb, line 13
def content_class
  content_type.constantize unless content_type.blank?
end
copy(parent_id) click to toggle source
# File lib/releaf/content/node.rb, line 63
def copy(parent_id)
  Releaf::Content::Node::Copy.call(node: self, parent_id: parent_id)
end
destroy() click to toggle source
Calls superclass method
# File lib/releaf/content/node.rb, line 37
def destroy
  begin
    content
  rescue NameError => e
    raise if content_id.nil? && content_type.nil?
    raise unless e.message == "uninitialized constant #{content_type}"
    # Class was deleted from project.
    # Lets try one more time, but this time set content_type to nil, so
    # rails doesn't try to constantize it
    update_columns(content_id: nil, content_type: nil)
  end

  super
  self.class.updated
end
invalid_slug_format?() click to toggle source
# File lib/releaf/content/node.rb, line 153
def invalid_slug_format?
  slug.present? && slug.to_url != slug
end
locale() click to toggle source

Returns closest existing locale starting from object itself @return [String] locale

Calls superclass method
# File lib/releaf/content/node.rb, line 105
def locale
  own = super
  if own
    own
  else
    ancestors.reorder("depth DESC").
      where("locale IS NOT NULL").
      first.try(:locale)
  end
end
locale_selection_enabled?() click to toggle source
# File lib/releaf/content/node.rb, line 5
def locale_selection_enabled?
  false
end
maintain_name() click to toggle source

Maintain unique name within parent_id scope. If name is not unique add numeric postfix.

# File lib/releaf/content/node.rb, line 73
def maintain_name
  postfix = nil
  total_count = 0

  while self.class.where(parent_id: parent_id, name: "#{name}#{postfix}").where("id != ?", id.to_i).exists? do
    total_count += 1
    postfix = "(#{total_count})"
  end

  if postfix
    self.name = "#{name}#{postfix}"
  end
end
maintain_slug() click to toggle source

Maintain unique slug within parent_id scope. If slug is not unique add numeric postfix.

# File lib/releaf/content/node.rb, line 89
def maintain_slug
  postfix = nil
  total_count = 0

  while self.class.where(parent_id: parent_id, slug: "#{slug}#{postfix}").where("id != ?", id.to_i).exists? do
    total_count += 1
    postfix = "-#{total_count}"
  end

  if postfix
    self.slug = "#{slug}#{postfix}"
  end
end
move(parent_id) click to toggle source
# File lib/releaf/content/node.rb, line 67
def move(parent_id)
  Releaf::Content::Node::Move.call(node: self, parent_id: parent_id)
end
path() click to toggle source

Return node public path

# File lib/releaf/content/node.rb, line 18
def path
  "/" + path_parts.join("/") + (trailing_slash_for_path? ? "/" : "")
end
path_parts() click to toggle source
# File lib/releaf/content/node.rb, line 22
def path_parts
  list = []
  list += parent.path_parts if parent
  list << slug.to_s
  list
end
prevent_auto_update_settings_timestamp() { || ... } click to toggle source
# File lib/releaf/content/node.rb, line 137
def prevent_auto_update_settings_timestamp
  original = @prevent_auto_update_settings_timestamp
  @prevent_auto_update_settings_timestamp = true
  yield
ensure
  @prevent_auto_update_settings_timestamp = original
end
reasign_slug() click to toggle source
# File lib/releaf/content/node.rb, line 126
def reasign_slug
  self.slug = nil
  ensure_unique_url
end
self_and_ancestors_array() click to toggle source
# File lib/releaf/content/node.rb, line 122
def self_and_ancestors_array
  preloaded_self_and_ancestors.nil? ? self_and_ancestors.to_a : preloaded_self_and_ancestors
end
to_s() click to toggle source
# File lib/releaf/content/node.rb, line 33
def to_s
  name
end
trailing_slash_for_path?() click to toggle source
# File lib/releaf/content/node.rb, line 29
def trailing_slash_for_path?
  Rails.application.routes.default_url_options[:trailing_slash] == true
end
update_settings_timestamp() click to toggle source
# File lib/releaf/content/node.rb, line 145
def update_settings_timestamp
  self.class.updated
end
validate_root_locale_uniqueness?() click to toggle source
# File lib/releaf/content/node.rb, line 149
def validate_root_locale_uniqueness?
  locale_selection_enabled? && root?
end

Protected Instance Methods

validate_parent_is_not_descendant() click to toggle source
# File lib/releaf/content/node.rb, line 165
def validate_parent_is_not_descendant
  return if parent_id.nil?
  return if self.descendants.find_by_id(parent_id).blank?
  self.errors.add(:parent_id, "descendant can't be parent")
end
validate_parent_node_is_not_self() click to toggle source
# File lib/releaf/content/node.rb, line 159
def validate_parent_node_is_not_self
  return if parent_id.nil?
  return if parent_id.to_i != id
  self.errors.add(:parent_id, "can't be parent to itself")
end
validate_slug() click to toggle source
# File lib/releaf/content/node.rb, line 171
def validate_slug
  errors.add(:slug, :invalid) if invalid_slug_format?
end

Private Instance Methods

prevent_auto_update_settings_timestamp?() click to toggle source
# File lib/releaf/content/node.rb, line 177
def prevent_auto_update_settings_timestamp?
  @prevent_auto_update_settings_timestamp == true
end