Module: Releaf::Content::Node

Extended by:
ActiveSupport::Concern
Defined in:
app/validators/releaf/content/node/root_validator.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 Classes: ParentValidator, RootValidator, SinglenessValidator

Instance Method Summary collapse

Instance Method Details

#add_error_and_raise(error) ⇒ Object

Raises:

  • (ActiveRecord::RecordInvalid)


121
122
123
124
# File 'lib/releaf/content/node.rb', line 121

def add_error_and_raise error
  errors.add(:base, error)
  raise ActiveRecord::RecordInvalid.new(self)
end

#attributes_to_copyObject



51
52
53
# File 'lib/releaf/content/node.rb', line 51

def attributes_to_copy
  self.class.column_names - attributes_to_not_copy
end

#attributes_to_not_copyObject



47
48
49
# File 'lib/releaf/content/node.rb', line 47

def attributes_to_not_copy
  %w[content_id depth id item_position lft rgt slug created_at updated_at]
end

#available?Boolean

Check whether object and all its ancestors are active

Returns:

  • (Boolean)

    returns true if object is available



115
116
117
118
119
# File 'lib/releaf/content/node.rb', line 115

def available?
  # There seams to be bug in Rails 4.0.0, that prevents us from using exists?
  # exists? will return nil or 1 in this query, instead of true/false (as it should)
  self_and_ancestors.where(active: false).any? == false
end

#build_content(params = {}, assignment_options = nil) ⇒ Object



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

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

#content_classObject



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

def content_class
  content_type.constantize unless content_type.blank?
end

#copy(parent_id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/releaf/content/node.rb', line 55

def copy parent_id
  prevent_infinite_copy_loop(parent_id)
  begin
    new_node = nil
    self.class.transaction do
      new_node = _copy!(parent_id)
    end
    new_node
  rescue ActiveRecord::RecordInvalid
    add_error_and_raise 'descendant invalid'
  else
    update_settings_timestamp
  end
end

#copy_attributes_from(node) ⇒ Object



134
135
136
137
138
# File 'lib/releaf/content/node.rb', line 134

def copy_attributes_from node
  node.attributes_to_copy.each do |attribute|
    send("#{attribute}=", node.send(attribute))
  end
end

#destroyObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/releaf/content/node.rb', line 31

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

#duplicate_contentObject



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

def duplicate_content
  return nil if content_id.blank?

  new_content = content.dup
  new_content.save!
  new_content
end

#duplicate_under!(parent_id) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/releaf/content/node.rb', line 140

def duplicate_under! parent_id
  new_node = nil
  self.class.transaction do
    new_node = self.class.new
    new_node.copy_attributes_from self
    new_node.content_id = duplicate_content.try(:id)
    new_node.prevent_auto_update_settings_timestamp do
      new_node.save_under(parent_id)
    end
  end
  new_node
end

#localeString

Returns closest existing locale starting from object itself

Returns:

  • (String)

    locale



102
103
104
105
106
107
108
109
110
111
# File 'lib/releaf/content/node.rb', line 102

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

TODO Node should be configurable

Returns:

  • (Boolean)


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

def locale_selection_enabled?
  false
end

#maintain_nameObject

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



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/releaf/content/node.rb', line 86

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

#move(parent_id) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/releaf/content/node.rb', line 70

def move parent_id
  return if parent_id.to_i == self.parent_id

  self.class.transaction do
    save_under(parent_id)
    descendants.each do |node|
      next if node.valid?
      add_error_and_raise 'descendant invalid'
    end
  end

  self
end

#pathObject

Return node public path



19
20
21
22
23
24
25
# File 'lib/releaf/content/node.rb', line 19

def path
  if parent
    parent.path + "/" + slug.to_s
  else
    "/" + slug.to_s
  end
end

#prevent_auto_update_settings_timestamp(&block) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/releaf/content/node.rb', line 173

def prevent_auto_update_settings_timestamp &block
  original = @prevent_auto_update_settings_timestamp
  @prevent_auto_update_settings_timestamp = true
  yield
ensure
  @prevent_auto_update_settings_timestamp = original
end

#reasign_slugObject



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

def reasign_slug
  self.slug = nil
  ensure_unique_url
end

#save_under(target_parent_node_id) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/releaf/content/node.rb', line 158

def save_under target_parent_node_id
  self.parent_id = target_parent_node_id
  if validate_root_locale_uniqueness?
    # When copying root nodes it is important to reset locale to nil.
    # Later user should fill in locale. This is needed to prevent
    # Rails errors about conflicting routes.
    self.locale = nil
  end

  self.item_position = self.class.children_max_item_position(self.parent) + 1
  maintain_name
  reasign_slug
  save!
end

#to_sObject



27
28
29
# File 'lib/releaf/content/node.rb', line 27

def to_s
  name
end