<commit_before>module Merb
  module GlobalHelpers
    def url_for_loan(loan, action = '')
      # this is to generate links to loans, as the resouce method doesn't work for descendant classes of Loan
      # it expects the whole context (@branch, @center, @client) to exist
      base = url(:branch_center_client, @branch.id, @center.id, @client.id)
      base + "/loans/#{loan.id}/" + action
    end

    def select_staff_member_for(obj, col, attrs = {})
      id_col = "#{col.to_s}_staff_id".to_sym
      select col,
        :collection   => StaffMember.all(:active => true),
        :value_method => :id,
        :text_method  => :name,
        :name         => "#{obj.class.to_s.snake_case}[#{id_col}]",
        :id           => "#{obj.class.to_s.snake_case}_#{id_col}",
        :selected     => (obj.send(id_col) ? obj.send(id_col).to_s : nil),
        :prompt       => (attrs[:prompt] or "&lt;select a staff member&gt;")
    end

    def select_center_for(obj, col, attrs = {})
      id_col = "#{col.to_s}_id".to_sym
      collection = []
      catalog = Center.catalog
      catalog.keys.sort.each do |branch_name|
        collection << ['', branch_name]
        catalog[branch_name].each_pair { |k,v| collection << [k.to_s, "!!!!!!!!!#{v}"] }
      end
      html = select col,
        :collection   => collection,
        :name         => "#{obj.class.to_s.snake_case}[#{id_col}]",
        :id           => "#{obj.class.to_s.snake_case}_#{id_col}",
        :selected     => (obj.send(id_col) ? obj.send(id_col).to_s : nil),
        :prompt       => (attrs[:prompt] or "&lt;select a center&gt;")
      html.gsub('!!!', '&nbsp;')  # otherwise the &nbsp; entities get escaped
    end

    def select_funding_line_for(obj, col, attrs = {}) # Fix me: Refactor this with all select_*_for
      id_col = "#{col.to_s}_id".to_sym
      catalog = Funder.catalog
      collection = []
      catalog.keys.sort.each do |funder_name|
        collection << ['', funder_name]
        catalog[funder_name].each_pair { |k,v| collection << [k.to_s, "!!!!!!#{v}"]}
      end
      html = select col,
        :collection   => collection,
        :name         => "#{obj.class.to_s.snake_case}[#{id_col}]",
        :id           => "#{obj.class.to_s.snake_case}_#{id_col}",
        :selected     => (obj.send(id_col) ? obj.send(id_col).to_s : nil),
        :prompt       => (attrs[:prompt] or "&lt;select a funding line&gt;")
      html.gsub('!!!', '&nbsp;')  # otherwise the &nbsp; entities get escaped
    end


    MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    def date_select(name, date=Date.today)
      # defaults to Date.today
      # should refactor
      attrs = {}
      attrs.merge!(:name => name)
      attrs.merge!(:date => date)
      attrs.merge!(:id => name)
      attrs.merge(:date => date)
      date_select_html(attrs) 
    end

    def date_select_for(obj, col = nil, attrs = {})
      attrs.merge!(:name => "#{obj.class.to_s.snake_case}[#{col.to_s}]")
      attrs.merge!(:id   => "#{obj.class.to_s.snake_case}_#{col.to_s}")
      nullable = attrs[:nullable] ? true : false
      date = obj.send(col) 
      date = Date.today if date.blank? and not nullable
      date = nil        if date.blank? and nullable
      attrs.merge!(:date => date)
      date_select_html(attrs, obj, col)
#       errorify_field(attrs, col)
    end

    def date_select_html (attrs, obj = nil, col = nil)
      date = attrs[:date]
      nullable = attrs[:nullable]
      day_attrs = attrs.merge(
        :name       => attrs[:name] + '[day]',
        :id         => attrs[:id] + '_day',
        :selected   => (date ? date.day.to_s : ''),
        :class      => (obj and col) ? (obj.errors[col] ? 'error' : '') : nil,
        :collection =>  (nullable ? [['', '-']] : []) + (1..31).to_a.map{ |x| x = [x.to_s, x.to_s] }
      )
      
      count = 0
      month_attrs = attrs.merge(
        :name       => attrs[:name] + '[month]',
        :id         => attrs[:id] + '_month',
        :selected   => (date ? date.month.to_s : ''),
        :class      => obj ? (obj.errors[col] ? 'error' : '') : nil,
        :collection => (nullable ? [['', '-']] : []) + MONTHS.map { |x| count += 1; x = [count, x] }
      )
      
      year_attrs = attrs.merge(
        :name       => attrs[:name] + '[year]',
        :id         => attrs[:id] + '_year',
        :selected   => (date ? date.year.to_s : ''),
        :class      => obj ? (obj.errors[col] ? 'error' : '') : nil,
        :collection => (nullable ? [['', '-']] : []) + (1900..Time.now.year).to_a.reverse.map{|x| x = [x.to_s, x.to_s]}
      )
      select(month_attrs) + '&nbsp;' + select(day_attrs) + '&nbsp;' + select(year_attrs)
    end


    def ofc2(width, height, url, id = Time.now.usec, swf_base = '/')
      <<-HTML
        <div id='flashcontent_#{id}'></div>
        <script type="text/javascript">
          swfobject.embedSWF(
            "#{swf_base}open-flash-chart.swf", "flashcontent_#{id}",
            "#{width}", "#{height}", "9.0.0", "expressInstall.swf",
            {"data-file":"#{url}", "loading":"Waiting for data... (reload page when it takes too long)"} );
        </script>
      HTML
    end

    def breadcrums
      # breadcrums use the request.uri and the instance vars of the parent
      # resources (@branch, @center) that are available -- so no db queries
      crums, url = [], ''
      request.uri.split("?")[0][1..-1].split('/').each_with_index do |part, index|
        url  << '/' + part
        if part.to_i.to_s.length == part.length  # true when a number (id)
          o = instance_variable_get('@'+url.split('/')[-2].singular)  # get the object (@branch)
          s = (o.respond_to?(:name) ? link_to(o.name, url) : link_to('#'+o.object_id.to_s, url))
          crums[-1] += ": <b><i>#{s}</i></b>"  # merge the instance names (or numbers)
        else  # when not a number (id)
          crums << link_to(part.gsub('_', ' '), url)  # add the resource name
        end<commit_msg>date select<commit_after>module Merb
  module GlobalHelpers
    def url_for_loan(loan, action = '')
      # this is to generate links to loans, as the resouce method doesn't work for descendant classes of Loan
      # it expects the whole context (@branch, @center, @client) to exist
      base = url(:branch_center_client, @branch.id, @center.id, @client.id)
      base + "/loans/#{loan.id}/" + action
    end

    def select_staff_member_for(obj, col, attrs = {})
      id_col = "#{col.to_s}_staff_id".to_sym
      select col,
        :collection   => StaffMember.all(:active => true),
        :value_method => :id,
        :text_method  => :name,
        :name         => "#{obj.class.to_s.snake_case}[#{id_col}]",
        :id           => "#{obj.class.to_s.snake_case}_#{id_col}",
        :selected     => (obj.send(id_col) ? obj.send(id_col).to_s : nil),
        :prompt       => (attrs[:prompt] or "&lt;select a staff member&gt;")
    end

    def select_center_for(obj, col, attrs = {})
      id_col = "#{col.to_s}_id".to_sym
      collection = []
      catalog = Center.catalog
      catalog.keys.sort.each do |branch_name|
        collection << ['', branch_name]
        catalog[branch_name].each_pair { |k,v| collection << [k.to_s, "!!!!!!!!!#{v}"] }
      end
      html = select col,
        :collection   => collection,
        :name         => "#{obj.class.to_s.snake_case}[#{id_col}]",
        :id           => "#{obj.class.to_s.snake_case}_#{id_col}",
        :selected     => (obj.send(id_col) ? obj.send(id_col).to_s : nil),
        :prompt       => (attrs[:prompt] or "&lt;select a center&gt;")
      html.gsub('!!!', '&nbsp;')  # otherwise the &nbsp; entities get escaped
    end

    def select_funding_line_for(obj, col, attrs = {}) # Fix me: Refactor this with all select_*_for
      id_col = "#{col.to_s}_id".to_sym
      catalog = Funder.catalog
      collection = []
      catalog.keys.sort.each do |funder_name|
        collection << ['', funder_name]
        catalog[funder_name].each_pair { |k,v| collection << [k.to_s, "!!!!!!#{v}"]}
      end
      html = select col,
        :collection   => collection,
        :name         => "#{obj.class.to_s.snake_case}[#{id_col}]",
        :id           => "#{obj.class.to_s.snake_case}_#{id_col}",
        :selected     => (obj.send(id_col) ? obj.send(id_col).to_s : nil),
        :prompt       => (attrs[:prompt] or "&lt;select a funding line&gt;")
      html.gsub('!!!', '&nbsp;')  # otherwise the &nbsp; entities get escaped
    end


    MONTHS = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    def date_select(name, date=Date.today)
      # defaults to Date.today
      # should refactor
      attrs = {}
      attrs.merge!(:name => name)
      attrs.merge!(:date => date)
      attrs.merge!(:id => name)
      attrs.merge(:date => date)
      date_select_html(attrs) 
    end

    def date_select_for(obj, col = nil, attrs = {})
      attrs.merge!(:name => "#{obj.class.to_s.snake_case}[#{col.to_s}]")
      attrs.merge!(:id   => "#{obj.class.to_s.snake_case}_#{col.to_s}")
      nullable = attrs[:nullable] ? true : false
      date = obj.send(col) 
      date = Date.today if date.blank? and not nullable
      date = nil        if date.blank? and nullable
      attrs.merge!(:date => date)
      date_select_html(attrs, obj, col)
#       errorify_field(attrs, col)
    end

    def date_select_html (attrs, obj = nil, col = nil)
      date = attrs[:date]
      nullable = attrs[:nullable]
      day_attrs = attrs.merge(
        :name       => attrs[:name] + '[day]',
        :id         => attrs[:id] + '_day',
        :selected   => (date ? date.day.to_s : ''),
        :class      => (obj and col) ? (obj.errors[col] ? 'error' : '') : nil,
        :collection =>  (nullable ? [['', '-']] : []) + (1..31).to_a.map{ |x| x = [x.to_s, x.to_s] }
      )
      
      count = 0
      month_attrs = attrs.merge(
        :name       => attrs[:name] + '[month]',
        :id         => attrs[:id] + '_month',
        :selected   => (date ? date.month.to_s : ''),
        :class      => obj ? (obj.errors[col] ? 'error' : '') : nil,
        :collection => (nullable ? [['', '-']] : []) + MONTHS.map { |x| count += 1; x = [count, x] }
      )
      
      year_attrs = attrs.merge(
        :name       => attrs[:name] + '[year]',
        :id         => attrs[:id] + '_year',
        :selected   => (date ? date.year.to_s : ''),
        :class      => obj ? (obj.errors[col] ? 'error' : '') : nil,
        :collection => (nullable ? [['', '-']] : []) + (1900..Time.now.year + 3).to_a.reverse.map{|x| x = [x.to_s, x.to_s]}
      )
      select(month_attrs) + '&nbsp;' + select(day_attrs) + '&nbsp;' + select(year_attrs)
    end


    def ofc2(width, height, url, id = Time.now.usec, swf_base = '/')
      <<-HTML
        <div id='flashcontent_#{id}'></div>
        <script type="text/javascript">
          swfobject.embedSWF(
            "#{swf_base}open-flash-chart.swf", "flashcontent_#{id}",
            "#{width}", "#{height}", "9.0.0", "expressInstall.swf",
            {"data-file":"#{url}", "loading":"Waiting for data... (reload page when it takes too long)"} );
        </script>
      HTML
    end

    def breadcrums
      # breadcrums use the request.uri and the instance vars of the parent
      # resources (@branch, @center) that are available -- so no db queries
      crums, url = [], ''
      request.uri.split("?")[0][1..-1].split('/').each_with_index do |part, index|
        url  << '/' + part
        if part.to_i.to_s.length == part.length  # true when a number (id)
          o = instance_variable_get('@'+url.split('/')[-2].singular)  # get the object (@branch)
          s = (o.respond_to?(:name) ? link_to(o.name, url) : link_to('#'+o.object_id.to_s, url))
          crums[-1] += ": <b><i>#{s}</i></b>"  # merge the instance names (or numbers)
        else  # when not a number (id)
          crums << link_to(part.gsub('_', ' '), url)  # add the resource name
        end
{'fraction_non_alphanumeric': 0.18858333333333333, 'fraction_numerical': 0.0030833333333333333, 'mean_word_length': 2.3299112097669257, 'pattern_counts': {'":': 6, '<': 47, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3802910', 'n_tokens_mistral': 4173, 'n_tokens_neox': 4181, 'n_words': 867}