Module: Releaf::Content::Node

Extended by:
ActiveSupport::Concern
Defined in:
app/services/releaf/content/node/copy.rb,
app/services/releaf/content/node/move.rb,
app/services/releaf/content/node/service.rb,
app/validators/releaf/content/node/root_validator.rb,
app/services/releaf/content/node/save_under_parent.rb,
app/validators/releaf/content/node/parent_validator.rb,
app/validators/releaf/content/node/singleness_validator.rb,
lib/releaf/content/node.rb

Defined Under Namespace

Modules: ClassMethods, Service Classes: Copy, Move, ParentValidator, RootValidator, SaveUnderParent, SinglenessValidator

Instance Method Summary collapse

Instance Method Details

#assign_attributes_from(source_node) ⇒ Object



131
132
133
134
135
# 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_copyObject



59
60
61
# File 'lib/releaf/content/node.rb', line 59

def attributes_to_copy
  self.class.column_names - attributes_to_not_copy
end

#attributes_to_not_copyObject



53
54
55
56
57
# 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?Boolean

Check whether object and all its ancestors are active

Returns:

  • (Boolean)

    returns true if object is available



118
119
120
# File 'lib/releaf/content/node.rb', line 118

def available?
  self_and_ancestors_array.all?(&:active?)
end

#build_content(params = {}) ⇒ Object



9
10
11
# File 'lib/releaf/content/node.rb', line 9

def build_content(params = {})
  self.content = content_class.new(params)
end

#content_classObject



13
14
15
# File 'lib/releaf/content/node.rb', line 13

def content_class
  content_type.constantize unless content_type.blank?
end

#copy(parent_id) ⇒ Object



63
64
65
# File 'lib/releaf/content/node.rb', line 63

def copy(parent_id)
  Releaf::Content::Node::Copy.call(node: self, parent_id: parent_id)
end

#destroyObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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.name.to_s == 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?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/releaf/content/node.rb', line 153

def invalid_slug_format?
  slug.present? && slug.to_url != slug
end

#localeString

Returns closest existing locale starting from object itself

Returns:

  • (String)

    locale



105
106
107
108
109
110
111
112
113
114
# 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?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/releaf/content/node.rb', line 5

def locale_selection_enabled?
  false
end

#maintain_nameObject

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



73
74
75
76
77
78
79
80
81
82
83
84
85
# 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_slugObject

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



89
90
91
92
93
94
95
96
97
98
99
100
101
# 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) ⇒ Object



67
68
69
# File 'lib/releaf/content/node.rb', line 67

def move(parent_id)
  Releaf::Content::Node::Move.call(node: self, parent_id: parent_id)
end

#pathObject

Return node public path



18
19
20
# File 'lib/releaf/content/node.rb', line 18

def path
  "/" + path_parts.join("/") + (trailing_slash_for_path? ? "/" : "")
end

#path_partsObject



22
23
24
25
26
27
# 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_timestampObject



137
138
139
140
141
142
143
# 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_slugObject



126
127
128
129
# File 'lib/releaf/content/node.rb', line 126

def reasign_slug
  self.slug = nil
  ensure_unique_url
end

#self_and_ancestors_arrayObject



122
123
124
# 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_sObject



33
34
35
# File 'lib/releaf/content/node.rb', line 33

def to_s
  name
end

#trailing_slash_for_path?Boolean

Returns:

  • (Boolean)


29
30
31
# 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_timestampObject



145
146
147
# File 'lib/releaf/content/node.rb', line 145

def update_settings_timestamp
  self.class.updated
end

#validate_root_locale_uniqueness?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/releaf/content/node.rb', line 149

def validate_root_locale_uniqueness?
  locale_selection_enabled? && root?
end