Class: Rticles::Paragraph

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rticles/paragraph.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#after_idObject

Returns the value of attribute after_id.



33
34
35
# File 'lib/rticles/paragraph.rb', line 33

def after_id
  @after_id
end

#before_idObject

Returns the value of attribute before_id.



33
34
35
# File 'lib/rticles/paragraph.rb', line 33

def before_id
  @before_id
end

#choices=(value) ⇒ Object

Sets the attribute choices

Parameters:

  • value

    the value to set the attribute choices to.



8
9
10
# File 'lib/rticles/paragraph.rb', line 8

def choices=(value)
  @choices = value
end

Class Method Details

.generate_html(paragraphs, options = {}) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
# File 'lib/rticles/paragraph.rb', line 238

def self.generate_html(paragraphs, options={})
  paragraph_groups = []
  paragraphs.each do |paragraph|
    if paragraph.continuation?
      paragraph_groups.last.push(paragraph)
    else
      paragraph_groups.push([paragraph])
    end
  end
  generate_html_for_paragraph_groups(paragraph_groups, options)
end

Instance Method Details

#ancestorsObject



91
92
93
94
95
96
# File 'lib/rticles/paragraph.rb', line 91

def ancestors
  node = self
  nodes = []
  nodes.push(node = node.parent) while node.parent
  nodes
end

#body_for_display(options = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/rticles/paragraph.rb', line 164

def body_for_display(options={})
  options = options.with_indifferent_access

  if options[:insertions]
    @insertions = options[:insertions]
  end

  if options[:choices]
    @choices = options[:choices]
  end

  with_meta_characters = options[:with_meta_characters] || false

  result = resolve_choices(body)
  return result if result.nil?

  result = resolve_references(result, with_meta_characters)
  result = resolve_insertions(result)

  if options[:with_index] && full_index(true, choices, options[:numbering_config])
    result = "#{full_index} #{result}"
  end

  result
end

#body_with_resolved_references(with_meta_characters = false) ⇒ Object



190
191
192
# File 'lib/rticles/paragraph.rb', line 190

def body_with_resolved_references(with_meta_characters=false)
  resolve_references(body, with_meta_characters)
end

#can_indent?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/rticles/paragraph.rb', line 106

def can_indent?
  !!higher_item
end

#can_move_higher?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/rticles/paragraph.rb', line 102

def can_move_higher?
  !!higher_item
end

#can_move_lower?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/rticles/paragraph.rb', line 98

def can_move_lower?
  !!lower_item
end

#can_outdent?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/rticles/paragraph.rb', line 118

def can_outdent?
  !!parent_id
end

#full_index(recalculate = false, choices = nil, numbering_config = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rticles/paragraph.rb', line 72

def full_index(recalculate=false, choices=nil, numbering_config=nil)
  return nil if heading? || continuation?

  return @full_index if @full_index && !recalculate

  if numbering_config.nil?
    numbering_config = Rticles::Numbering::Config.new
  end

  if numbering_config.innermost_only
    @full_index = numbering_config[level].format.sub('#', Rticles::Numbering.number_to_string(index(choices), numbering_config[level].style))
  else
    @full_index = ancestors.unshift(self).reverse.map do |p|
      numbering_config[p.level].format.sub('#', Rticles::Numbering.number_to_string(p.index(choices), numbering_config[p.level].style))
    end
    @full_index = @full_index.join(numbering_config.separator)
  end
end

#heading?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/rticles/paragraph.rb', line 48

def heading?
  heading && heading > 0
end

#heading_levelObject



52
53
54
# File 'lib/rticles/paragraph.rb', line 52

def heading_level
  ancestors.length + (heading ? heading : 0)
end

#higher_itemsObject



132
133
134
135
136
137
# File 'lib/rticles/paragraph.rb', line 132

def higher_items
  return nil unless in_list?
  acts_as_list_class.where(
    "#{scope_condition} AND #{position_column} < #{(send(position_column).to_i).to_s}"
  )
end

#indent!Object



110
111
112
113
114
115
116
# File 'lib/rticles/paragraph.rb', line 110

def indent!
  return unless can_indent?
  new_parent_id = higher_item.id
  remove_from_list
  update_attribute(:parent_id, new_parent_id)
  send(:assume_bottom_position)
end

#index(choices = nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rticles/paragraph.rb', line 60

def index(choices=nil)
  return nil if heading? || continuation?

  predecessors = higher_items.where(['(heading = 0 OR heading IS NULL) AND (continuation = ? OR continuation IS NULL)', false])

  if choices.present?
    predecessors = predecessors.for_choices(choices)
  end

  predecessors.count + 1
end

#levelObject



56
57
58
# File 'lib/rticles/paragraph.rb', line 56

def level
  ancestors.length + 1
end

#lower_itemsObject



139
140
141
142
143
144
# File 'lib/rticles/paragraph.rb', line 139

def lower_items
  return nil unless in_list?
  acts_as_list_class.where(
    "#{scope_condition} AND #{position_column} > #{(send(position_column).to_i).to_s}"
  )
end

#normalise_referencesObject



154
155
156
157
158
159
160
161
162
# File 'lib/rticles/paragraph.rb', line 154

def normalise_references
  return if body.blank?
  raw_reference_re = /!(\d\.)*\d/
  Rails.logger.debug("Body: #{body}")
  self.body = body.gsub(raw_reference_re) do |match|
    raw_reference = match.sub('!', '')
    '#rticles#' + document.paragraph_for_reference(raw_reference).id.to_s
  end
end

#outdent!Object



122
123
124
125
126
127
128
129
130
# File 'lib/rticles/paragraph.rb', line 122

def outdent!
  return unless can_outdent?
  new_parent_id = parent.parent_id
  new_position = parent.position + 1
  reparent_lower_items_under_self
  remove_from_list
  update_attribute(:parent_id, new_parent_id)
  insert_at(new_position)
end

#prepare_for_editingObject



233
234
235
236
# File 'lib/rticles/paragraph.rb', line 233

def prepare_for_editing
  self.body = body_with_resolved_references(true)
  self
end

#reparent_lower_items_under_selfObject



146
147
148
149
150
151
# File 'lib/rticles/paragraph.rb', line 146

def reparent_lower_items_under_self
  return unless in_list?
  acts_as_list_class.update_all(
    "#{position_column} = (#{position_column} - #{position}), parent_id = #{id}", "#{scope_condition} AND #{position_column} > #{send(position_column).to_i}"
  )
end

#resolve_choices(string) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/rticles/paragraph.rb', line 218

def resolve_choices(string)
  choice_re = /\A#rticles#(true|false)#([A-Za-z_]+) /
  match = string.match(choice_re)
  return string if !match

  choice_name = match[2]
  choice_parameter = match[1]

  if (choices[choice_name] && choice_parameter == 'true') || (!choices[choice_name] && choice_parameter == 'false')
    string.sub(choice_re, '')
  else
    nil
  end
end

#resolve_insertions(string) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rticles/paragraph.rb', line 205

def resolve_insertions(string)
  return string if string.blank?
  insertion_re = /#rticles#([A-Za-z_]+)/
  string.gsub(insertion_re) do |match|
    insertion_name = match.sub('#rticles#', '')
    if insertions[insertion_name].present?
      insertions[insertion_name].to_s.gsub("\n", "<br>")
    else
      "[#{insertion_name.humanize.upcase}]"
    end
  end
end

#resolve_references(string, with_meta_characters = false) ⇒ Object



194
195
196
197
198
199
200
201
202
203
# File 'lib/rticles/paragraph.rb', line 194

def resolve_references(string, with_meta_characters=false)
  return string if string.blank?
  normalised_reference_re = /#rticles#(\d+)/
  string.gsub(normalised_reference_re) do |match|
    normalised_reference = match.sub('#rticles#', '')
    result = with_meta_characters ? '!' : ''
    result += document.paragraphs.find(normalised_reference).full_index
    result
  end
end

#set_document_idObject



27
28
29
30
31
# File 'lib/rticles/paragraph.rb', line 27

def set_document_id
  if parent
    self.document_id ||= parent.document_id
  end
end

#set_parent_and_positionObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rticles/paragraph.rb', line 36

def set_parent_and_position
  if before_id.present?
    sibling = self.class.find(before_id)
    self.update_attribute(:parent_id, sibling.parent_id)
    insert_at(sibling.position)
  elsif after_id.present?
    sibling = self.class.find(after_id)
    self.update_attribute(:parent_id, sibling.parent_id)
    insert_at(self.class.find(after_id).position + 1)
  end
end