Class: Rticles::Paragraph
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Rticles::Paragraph
- Defined in:
- lib/rticles/paragraph.rb
Instance Attribute Summary collapse
-
#after_id ⇒ Object
Returns the value of attribute after_id.
-
#before_id ⇒ Object
Returns the value of attribute before_id.
-
#choices ⇒ Object
writeonly
Sets the attribute choices.
Class Method Summary collapse
Instance Method Summary collapse
- #ancestors ⇒ Object
- #body_for_display(options = {}) ⇒ Object
- #body_with_resolved_references(with_meta_characters = false) ⇒ Object
- #can_indent? ⇒ Boolean
- #can_move_higher? ⇒ Boolean
- #can_move_lower? ⇒ Boolean
- #can_outdent? ⇒ Boolean
- #full_index(recalculate = false, choices = nil, numbering_config = nil) ⇒ Object
- #heading? ⇒ Boolean
- #heading_level ⇒ Object
- #higher_items ⇒ Object
- #indent! ⇒ Object
- #index(choices = nil) ⇒ Object
- #level ⇒ Object
- #lower_items ⇒ Object
- #normalise_references ⇒ Object
- #outdent! ⇒ Object
- #prepare_for_editing ⇒ Object
- #reparent_lower_items_under_self ⇒ Object
- #resolve_choices(string) ⇒ Object
- #resolve_insertions(string) ⇒ Object
- #resolve_references(string, with_meta_characters = false) ⇒ Object
- #set_document_id ⇒ Object
- #set_parent_and_position ⇒ Object
Instance Attribute Details
#after_id ⇒ Object
Returns the value of attribute after_id.
33 34 35 |
# File 'lib/rticles/paragraph.rb', line 33 def after_id @after_id end |
#before_id ⇒ Object
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
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, ={}) 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, ) end |
Instance Method Details
#ancestors ⇒ Object
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(={}) = .with_indifferent_access if [:insertions] @insertions = [:insertions] end if [:choices] @choices = [:choices] end = [:with_meta_characters] || false result = resolve_choices(body) return result if result.nil? result = resolve_references(result, ) result = resolve_insertions(result) if [:with_index] && full_index(true, choices, [: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(=false) resolve_references(body, ) end |
#can_indent? ⇒ Boolean
106 107 108 |
# File 'lib/rticles/paragraph.rb', line 106 def can_indent? !!higher_item end |
#can_move_higher? ⇒ Boolean
102 103 104 |
# File 'lib/rticles/paragraph.rb', line 102 def can_move_higher? !!higher_item end |
#can_move_lower? ⇒ Boolean
98 99 100 |
# File 'lib/rticles/paragraph.rb', line 98 def can_move_lower? !!lower_item end |
#can_outdent? ⇒ 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
48 49 50 |
# File 'lib/rticles/paragraph.rb', line 48 def heading? heading && heading > 0 end |
#heading_level ⇒ Object
52 53 54 |
# File 'lib/rticles/paragraph.rb', line 52 def heading_level ancestors.length + (heading ? heading : 0) end |
#higher_items ⇒ Object
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 |
#level ⇒ Object
56 57 58 |
# File 'lib/rticles/paragraph.rb', line 56 def level ancestors.length + 1 end |
#lower_items ⇒ Object
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_references ⇒ Object
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_editing ⇒ Object
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_self ⇒ Object
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, =false) return string if string.blank? normalised_reference_re = /#rticles#(\d+)/ string.gsub(normalised_reference_re) do |match| normalised_reference = match.sub('#rticles#', '') result = ? '!' : '' result += document.paragraphs.find(normalised_reference).full_index result end end |
#set_document_id ⇒ Object
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_position ⇒ Object
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 |