# encoding: utf-8 $: << File.expand_path('../lib', File.dirname(__FILE__)) $: << File.dirname(__FILE__) require 'test/unit' require 'stub/cgi' require 'htmlgrid/composite' require 'htmlgrid/inputtext' require 'htmlgrid/form' require 'htmlgrid/list' class StubComposite < HtmlGrid::Composite attr_writer :container COMPONENTS = { [0,0,0] => :baz, [0,0,1] => :foo, [0,0,2] => :baz, [0,1,0] => :baz, [0,1,1] => :baz, } LABELS = true SYMBOL_MAP = { :bar => HtmlGrid::InputText, } attr_reader :model, :session public :resolve_offset, :labels? def init @barcount=0 super end def foo(model) "Foo" end def baz(model) @barcount += 1 "Baz#{@barcount}" end end class StubCompositeComponent < HtmlGrid::Component def to_html(context) context.a(@attributes) { 'brafoo' } end end class StubComposite2 < HtmlGrid::Composite COMPONENTS = { [0,0] => StubCompositeComponent, } end class StubComposite3 < StubComposite2 COMPONENT_CSS_MAP = {[0,0,4,4]=>'standard'} end class StubComposite4 < StubComposite3 CSS_MAP = {[0,0]=>'dradnats'} COMPONENT_CSS_MAP = {[0,0,4,4]=>'standard'} end class StubCompositeNoLabel < HtmlGrid::Composite LABELS = false COMPONENTS = {} public :labels? end class StubCompositeModel end class StubCompositeLookandfeel def attributes(key) {} end def lookup(key) end def base_url 'http://www.oddb.org/de/gcc' end end class StubCompositeSession attr_accessor :event def lookandfeel StubCompositeLookandfeel.new end def error(key) end end class StubCompositeForm < HtmlGrid::Form COMPONENTS = { [0,0] => StubComposite } EVENT = :foo end class StubCompositeColspan1 < HtmlGrid::Composite COMPONENTS = {} end class StubCompositeColspan2 < HtmlGrid::Composite COMPONENTS = { [0,0] => :foo, } end class StubCompositeColspan3 < HtmlGrid::Composite COMPONENTS = { [0,0] => :foo, [1,0] => :bar, } end class StubCompositeColspan4 < HtmlGrid::Composite COMPONENTS = { [0,0] => :foo, [2,0] => :bar, } end class StubInteractionChooserDrugList < HtmlGrid::List attr_reader :model COMPONENTS = { [0,0] => :info_drug, } CSS_MAP = { [0,0] => 'flecken', } SORT_HEADER = false SORT_DEFAULT = :foo def initialize(model, session=@session) puts "StubDrugModel #{model.inspect}" @foo = model end end class StubDrugModel attr_reader :foo def initialize(foo) puts "StubDrugModel #{foo}" @foo = foo end def fachinfo @foo end def drug 'Drug' end end class StubInteractionChooserDrugHeader < HtmlGrid::Composite COMPONENTS = { [0,0] => :fachinfo, [1,0] => :atc, [2,0] => :delete, } CSS_MAP = { [0,0] => 'small', [1,0] => 'interaction-atc', [2,0] => 'small', } def init super end def fachinfo(model, session=@session) 'fachinfo' end def atc(model, session=@session) 'atc' end def delete(model, session=@session) 'delete' end end class StubInteractionChooserDrug < HtmlGrid::Composite COMPONENTS = { } CSS_MAP = {} CSS_CLASS = 'composite' @@barcode ||= 0 def init @@barcode += 1 components.store([0,0], :header_info) css_map.store([0,0], 'subheading') components.store([0,1], :text_info) @attributes.store('id', 'drugs_' + @@barcode.to_s) super end def header_info(model, session=@session) StubInteractionChooserDrugHeader.new(model, session, self) # 'ChooserDrug' end def text_info(model, session=@session) "#{model.foo}" end end class TestComposite < Test::Unit::TestCase def setup @composite = StubComposite.new(StubCompositeModel.new, StubCompositeSession.new) end def test_create_method foo = nil assert_nothing_raised { foo = @composite.create(:foo, @composite.model) } assert_equal("Foo", foo) end def test_to_html expected = '
Baz1FooBaz2
Baz3Baz4
' assert_equal(expected, @composite.to_html(CGI.new)) end def test_component_css_map composite = StubComposite2.new(StubCompositeModel.new, StubCompositeSession.new) expected = '
brafoo
' assert_equal(expected, composite.to_html(CGI.new)) composite = StubComposite3.new(StubCompositeModel.new, StubCompositeSession.new) expected = '
brafoo
' assert_equal(expected, composite.to_html(CGI.new)) composite = StubComposite4.new(StubCompositeModel.new, StubCompositeSession.new) end def test_interaction_to_html composite = StubInteractionChooserDrugHeader.new(StubCompositeModel.new, StubCompositeSession.new) expected = ''+ '
fachinfoatcdelete
' assert_equal(expected, composite.to_html(CGI.new)) composite = StubInteractionChooserDrug.new(StubDrugModel.new('Asp'), StubCompositeSession.new) expected = ''+ '
'+ ''+ '
fachinfoatcdelete
'+ '
Asp
' assert_equal(expected, composite.to_html(CGI.new)) end def test_interaction_list_to_html models = [ StubDrugModel.new('Aspirin'), StubDrugModel.new('Marcoumar'), ] composite = StubInteractionChooserDrugList.new(models, StubCompositeSession.new) expected = ''+ '
'+ ''+ '
fachinfoatcdelete
'+ '
Asp
' assert_equal(expected, composite.to_html(CGI.new)) puts "\n" + composite.to_html(CGI.new) end end old = %( #!/usr/bin/env ruby # encoding: utf-8 # ODDB::View::Drugs::InteractionChooser -- oddb.org -- 20.12.2012 -- yasaka@ywesee.com require 'csv' require 'cgi' require 'htmlentities' require 'view/drugs/privatetemplate' require 'view/drugs/centeredsearchform' require 'view/additional_information' require 'view/searchbar' require 'view/printtemplate' require 'view/publictemplate' require 'view/form' require 'view/chapter' module ODDB module View module Interactions # see http://matrix.epha.ch/#/56751,61537,39053,59256 Ratings = { 'A' => 'Keine Massnahmen erforderlich', 'B' => 'Vorsichtsmassnahmen empfohlen', 'C' => 'Regelmässige Überwachung', 'D' => 'Kombination vermeiden', 'X' => 'Kontraindiziert', } Colors = { 'A' => 'green', 'B' => 'yellow', 'C' => 'orange', 'D' => 'red', 'X' => 'firebrick', } class InteractionChooserDrugHeader < HtmlGrid::Composite include View::AdditionalInformation COMPONENTS = { [0,0] => :fachinfo, [1,0] => :drug, [2,0] => :delete, [3,0] => :atc_code, [4,0] => :epha_interaction, } CSS_MAP = { [0,0] => 'small', [1,0] => 'interaction-drug', [2,0] => 'small', [3,0] => 'interaction-atc', [4,0] => 'interaction-info', } def init @drugs = @session.persistent_user_input(:drugs) @index = (@drugs ? @drugs.length : 0).to_s @atc_codes = [] if @drugs and !@drugs.empty? @drugs.values.each do |pac| $stdout.puts "init adding pac # {pac.atc_class.code}" @atc_codes << pac.atc_class.code end end super end def fachinfo(model, session=@session) if fi = super(model, session, 'square bold infos') fi.set_attribute('target', '_blank') fi end end def drug(model, session=@session) div = HtmlGrid::Div.new(model, @session, self) div.set_attribute('class', 'interaction-drug') div.value = [] if model div.value << model.name_with_size if price = model.price_public div.value << ' - ' div.value << price.to_s end unless model.substances.empty? div.value << ' - ' div.value << model.substances.join(',') end if company = model.company_name div.value << ' - ' div.value << company end end div end def atc_code(model, session=@session) div = HtmlGrid::Div.new(model, @session, self) div.set_attribute('class', 'interaction-atc') div.value = [] div.value << model.atc_class.code + ': ' + model.atc_class.name div end def my_get_epha_interaction(atc_code_self, atc_code_other) @session.app.epha_interactions.each { |aInteraction| return aInteraction if aInteraction.atc_code_self.to_s == atc_code_self.to_s and aInteraction.atc_code_other.to_s == atc_code_other.to_s } nil end def get_interactions(atc_codes, my_atc_code) results = [] idx=atc_codes.index(my_atc_code) atc_codes[0..idx].combination(2).to_a.each { |combination| next unless combination.index(my_atc_code) [ @session.app.get_epha_interaction(combination[0], combination[1]), @session.app.get_epha_interaction(combination[1], combination[0]), ].each{ |interaction| next unless interaction header = '' header += interaction.atc_code_self + ': ' + interaction.atc_name + ' => ' header += interaction.atc_code_other + ': ' + interaction.name_other header += ' ' + interaction.info text = '' text += interaction.severity + ': ' + Ratings[interaction.severity] text += '
' + interaction.action text += '
' + interaction.measures + '
' results << { :header => header, :severity => interaction.severity, :color => Colors[interaction.severity], :text => text } } } $stdout.puts results.inspect results end def epha_interaction(model, session=@session) div = HtmlGrid::Div.new(model, @session, self) # the first element cannot have an interaction return div if @atc_codes.index(model.atc_class.code) == 0 div.set_attribute('class', 'interaction-info') div.value = [] list = HtmlGrid::Div.new(model, @session, self) list.value = [] get_interactions(@atc_codes, model.atc_class.code).each { |interaction| headerDiv = HtmlGrid::Div.new(model, @session, self) headerDiv.value = [] headerDiv.value << interaction[:header] list.value << headerDiv infoDiv = HtmlGrid::Div.new(model, @session, self) infoDiv.value = [] infoDiv.value << interaction[:text] infoDiv.set_attribute('style', "background-color: # {interaction[:color]}") unless interaction[:severity].eql?('A') list.value << infoDiv } div.value << list div end def delete(model, session=@session) if @container.is_a? InteractionChooserDrug and # hide at search result (@drugs and @drugs.length >= 1) link = HtmlGrid::Link.new(:minus, model, session, self) link.set_attribute('title', @lookandfeel.lookup(:delete)) link.css_class = 'delete square' args = [:ean, model.barcode] if model url = @session.lookandfeel.event_url(:ajax_delete_drug, args) link.onclick = "replace_element('drugs_# {model.barcode}', '# {url}');" link end end end class InteractionChooserDrug < HtmlGrid::Composite COMPONENTS = {} CSS_MAP = {} CSS_CLASS = 'composite' def init if @model.is_a? ODDB::Package components.store([0,0], :drug) css_map.store([0,0], 'subheading') @attributes.store('id', 'drugs_' + @model.barcode) end super end def drug(model, session) View::Interactions::InteractionChooserDrugHeader.new(model, session, self) end end class InteractionChooserDrugDiv < HtmlGrid::Div def init super @value = [] @drugs = @session.persistent_user_input(:drugs) if @drugs and !@drugs.empty? @drugs.values.each do |pac| @value << InteractionChooserDrug.new(pac, @session, self) end end end def to_html(context) div = HtmlGrid::Div.new(@model, @session, self) if @drugs and !@drugs.empty? delete_all_link = HtmlGrid::Link.new(:delete, @model, @session, self) delete_all_link.href = @lookandfeel._event_url(:delete_all, []) delete_all_link.value = @lookandfeel.lookup(:interaction_chooser_delete_all) delete_all_link.css_class = 'list' div.value = delete_all_link end div.set_attribute('id', 'drugs') @value << div # super end end class InteractionChooserInnerForm < HtmlGrid::Composite attr_reader :index_name FORM_METHOD = 'POST' COMPONENTS = { [0,0] => :searchbar, } SYMBOL_MAP = { :searchbar => View::InteractionChooserBar, } CSS_MAP = { [0,0] => 'searchbar', } COLSPAN_MAP = { [0,0] => 2, } def init super @index_name = 'oddb_package_name_with_size_company_name_ean13_fi' @additional_javascripts = [] end def javascripts(context) scripts = '' @additional_javascripts.each do |script| args = { 'type' => 'text/javascript', 'language' => 'JavaScript', } scripts << context.script(args) do script end end scripts end def to_html(context) javascripts(context).to_s << super end end class InteractionChooserForm < View::Form include HtmlGrid::InfoMessage COMPONENTS = { [0,0] => 'interaction_chooser_description', [0,1] => View::Interactions::InteractionChooserDrugDiv, [0,2] => View::Interactions::InteractionChooserInnerForm, [0,4] => :buttons, } CSS_MAP = { [0,0] => 'th bold', [0,1] => '', # none [0,2] => 'list', [0,4] => 'inner-button', } CSS_CLASS = 'composite' DEFAULT_CLASS = HtmlGrid::Value LABELS = true def buttons(model, session) post_event_button(:show_interaction) end private def init super self.onload = "document.getElementById('interaction_searchbar').focus();" @form_properties.update({ 'id' => 'interaction_chooser_form', 'target' => '_blank', }) end end class InteractionChooserComposite < HtmlGrid::Composite include AdditionalInformation COMPONENTS = { [0,0] => View::Interactions::InteractionChooserForm, } COMPONENT_CSS_MAP = { [0,0] => 'composite', } COLSPAN_MAP = { [0,0] => 12, } CSS_CLASS = 'composite' end class InteractionChooser < View::PrivateTemplate CONTENT = View::Interactions::InteractionChooserComposite SNAPBACK_EVENT = :home JAVASCRIPTS = ['admin'] SEARCH_HEAD = 'nbsp' def backtracking(model, session=@session) fields = [] fields << @lookandfeel.lookup(:th_pointer_descr) link = HtmlGrid::Link.new(:home_interaction, model, @session, self) link.css_class = "list" link.href = @lookandfeel._event_url(:home_interaction, []) link.value = @lookandfeel.lookup(:home) fields << link fields << ' - ' span = HtmlGrid::Span.new(model, session, self) span.value = @lookandfeel.lookup(:interaction_chooser) span.set_attribute('class', 'bold') fields << span fields end end end end end )