Class: Skyline::InlineRef
- Defined in:
- app/models/skyline/inline_ref.rb
Instance Attribute Summary collapse
-
#previous_referable ⇒ Object
Returns the value of attribute previous_referable.
Class Method Summary collapse
-
.convert(object, column_name, with_refs = false, options = {}) ⇒ Object
Convert [REF:id] tags to html tags.
-
.hash_refs_for_object(object, column_name) ⇒ Object
Create hash of InlineRef objects for specified object and column name.
-
.parse_html(html_node, refering_object, refering_column_name) ⇒ Object
Convert an html tag to RefObject tag and update or create RefObject in database.
Instance Attribute Details
#previous_referable ⇒ Object
Returns the value of attribute previous_referable.
3 4 5 |
# File 'app/models/skyline/inline_ref.rb', line 3 def previous_referable @previous_referable end |
Class Method Details
.convert(object, column_name, with_refs = false, options = {}) ⇒ Object
Convert [REF:id] tags to html tags
Parameters
- object<Object>
-
the object containing the string to be converted
- column_name<String>
-
name of column containing the string to be converted
- with_refs<Boolean>
-
boolean that sets wether to print skyline-reference tags into the html tag
Returns
- String
-
converted html text
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/models/skyline/inline_ref.rb', line 73 def convert(object,column_name,with_refs=false,={}) .reverse_merge! :nullify => false refs = self.hash_refs_for_object(object, column_name) value = object[column_name] return value unless value.kind_of?(String) v = value.gsub(/\[REF:(\d+)\]/) do |match| i = match[5..-2] refs[i.to_i].to_start_html(with_refs,) end outp = v.gsub(/\[\/REF:(\d+)\]/) do |match| i = match[6..-2] refs[i.to_i].to_end_html end end |
.hash_refs_for_object(object, column_name) ⇒ Object
Create hash of InlineRef objects for specified object and column name
Paramenters
- object<Object>
-
the object containing the Inline Refs
- column_name<String>
-
column name in which the inline refs are stored as [REF:id] tags
Retruns
- Hash
-
a hash of inlinerefs with id as key and object as value
58 59 60 61 62 |
# File 'app/models/skyline/inline_ref.rb', line 58 def hash_refs_for_object(object, column_name) types = [Skyline::ImageRef,Skyline::LinkRef].map(&:name) refs = Skyline::RefObject.all(:conditions => {:refering_id => object.id, :refering_type => object.class.name, :refering_column_name => column_name.to_s, :type => types}) refs.inject({}){|mem,o| mem[o.id] = o; mem } end |
.parse_html(html_node, refering_object, refering_column_name) ⇒ Object
Convert an html tag to RefObject tag and update or create RefObject in database
Parameters
- html_node<String>
-
the html node from the editor
- refering_object<Object>
-
refering object
- refering_column_name<Symbol>
-
column name in which the data is stored
Returns
- String
-
converted html
- Array
-
ids of refs in html
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/skyline/inline_ref.rb', line 17 def parse_html(html_node, refering_object, refering_column_name) old_refs = find_ref_ids_for_object(refering_object, refering_column_name) h = Hpricot(html_node) updated_refs = [] # html tags to be converted to [REF:id] iterate_elements = {"a" => {:src => "href", :inner_html => true, :skyline_class => Skyline::LinkRef}, "img" => {:src => "src", :inner_html => false, :skyline_class => Skyline::ImageRef} } iterate_elements.each do |tag, attributes| h.search("#{tag}[@skyline-referable-type]").each do |node| ref_obj_id = create_ref_from_node(node, refering_object, refering_column_name, attributes[:src], attributes[:skyline_class]) if attributes[:inner_html] node.swap("[REF:#{ref_obj_id}]#{node.inner_html}[/REF:#{ref_obj_id}]") else node.swap("[REF:#{ref_obj_id}]") end updated_refs << ref_obj_id end end del = (old_refs - updated_refs) unless del.blank? self.destroy_all("id IN(#{del.join(',')})") logger.warn("[InlineRef] Destroying refs for #{refering_object.class.name} id: #{refering_object.id} column: #{refering_column_name}. Ref's destroyed: #{del.inspect}") end [h.to_html, updated_refs] end |