diff --git a/app/models/legislation/annotation.rb b/app/models/legislation/annotation.rb index f9f612abd..096565404 100644 --- a/app/models/legislation/annotation.rb +++ b/app/models/legislation/annotation.rb @@ -14,7 +14,7 @@ class Legislation::Annotation < ActiveRecord::Base validates :draft_version, presence: true validates :author, presence: true - before_save :store_range + before_save :store_range, :store_context after_create :create_first_comment def store_range @@ -24,6 +24,15 @@ class Legislation::Annotation < ActiveRecord::Base self.range_end_offset = ranges.first["endOffset"] end + def store_context + html = draft_version.body_html + doc = Nokogiri::HTML(html) + selector = "/#{range_start}" + text = doc.xpath(selector).text + text[quote] = "#{quote}" + self.context = text + end + def create_first_comment comments.create(body: self.text, user: self.author) end diff --git a/app/views/legislation/annotations/show.html.erb b/app/views/legislation/annotations/show.html.erb index 25d56e99c..8529ea2af 100644 --- a/app/views/legislation/annotations/show.html.erb +++ b/app/views/legislation/annotations/show.html.erb @@ -15,7 +15,7 @@