<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1062</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1062" rel="self"/>
  <title>Dmitry Polushkin</title>
  <updated>Thu May 14 13:09:01 -0700 2009</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code870</id>
    <published>2009-05-14T13:09:01-07:00</published>
    <updated>2009-05-14T15:50:37-07:00</updated>
    <title>[Ruby] Refactoring of the location selector (5 levels)</title>
    <content type="html">&lt;p&gt;It's a Location selector, from Continent till Suburb.
&lt;br /&gt;There are a different rules:&lt;/p&gt;

&lt;p&gt;* Next selection must appear if only previous level been selected
&lt;br /&gt;* If no children on current level, then all next levels show as a text fields
&lt;br /&gt;* If level &amp;gt; OPTION_CAPABLE, then must appear option &amp;quot;Other&amp;quot;
&lt;br /&gt;* If Other selected, then self and all next levels must show as a text fields&lt;/p&gt;

&lt;p&gt;/me start to write tests for this biiig view partial.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

&lt;pre&gt;## Model [Location]

acts_as_nested_set
OPTION_CAPABLE = 2
LEVELS = [:continent, :country, :region, :town, :suburb]

## View [html_rails]

&amp;lt;% ancestors = location.self_and_ancestors if location %&amp;gt;

&amp;lt;%= hidden_field_tag 'products[location_id]', location ? location.id : nil %&amp;gt;

&amp;lt;% is_text_field = false %&amp;gt;
&amp;lt;% Location::LEVELS.each_with_index do |level, level_number| %&amp;gt;
  &amp;lt;% previous_ancestor = (ancestors ? ancestors[level_number - 1] : nil) %&amp;gt;
  &amp;lt;% ancestor = (ancestors ? ancestors[level_number] : nil) %&amp;gt;
  &amp;lt;div id=&amp;quot;location_&amp;lt;%= level %&amp;gt;_container&amp;quot;&amp;gt;
    &amp;lt;% children = (previous_ancestor ? previous_ancestor.children : []) %&amp;gt;
    &amp;lt;% options = (0 == level_number ? Location.roots : children).map { |l| [l.name, l.id] } %&amp;gt;

    &amp;lt;% if options.size &amp;gt; 0 %&amp;gt;
      &amp;lt;%= label_tag :&amp;quot;select_location_#{level}&amp;quot; %&amp;gt;:

      &amp;lt;% options = [[Location.human_attribute_value(:name, :value =&amp;gt; :select), '']] + options %&amp;gt;
      &amp;lt;% options += [[Location.human_attribute_value(:name, :value =&amp;gt; :other), 0]] if level_number &amp;gt; Location::OPTION_CAPABLE %&amp;gt;

      &amp;lt;%= select_tag :&amp;quot;location_#{level}&amp;quot;, options_for_select(options, (ancestor ? ancestor.id : nil) || params[:other].to_i) %&amp;gt; - &amp;lt;%=  (ancestor ? ancestor.id : nil) || params[:other].to_i %&amp;gt;&amp;lt;br/&amp;gt;
    &amp;lt;% end %&amp;gt;
    
    &amp;lt;% if (Location::LEVELS.last != level &amp;amp;&amp;amp; !ancestor.nil? &amp;amp;&amp;amp; ancestor.children.size == 0) || (ancestor.nil? &amp;amp;&amp;amp; params[:other]) || is_text_field %&amp;gt;
      &amp;lt;% is_text_field = true %&amp;gt;

      &amp;lt;% unless !ancestor.nil? &amp;amp;&amp;amp; ancestor.children.size == 0 %&amp;gt;
        &amp;lt;%= label_tag :&amp;quot;type_location_#{level}&amp;quot; %&amp;gt;:
        &amp;lt;%= text_field_tag :a, :a %&amp;gt;
      &amp;lt;% end %&amp;gt;
    &amp;lt;% end %&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;% end %&amp;gt;&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/870-refactoring-of-the-location-selector-5-levels" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor148676</id>
    <published>2009-02-25T05:43:52-08:00</published>
    <title>[Ruby] On Too many for loops</title>
    <content type="html">&lt;p&gt;Why do you think is it bad?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/768-too-many-for-loops/refactors/148676" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code716</id>
    <published>2009-01-25T15:04:15-08:00</published>
    <updated>2009-06-04T00:22:17-07:00</updated>
    <title>[JavaScript] Check next option in select tag</title>
    <content type="html">&lt;p&gt;Could be written in plain javascript or prototype (or jQuery).&lt;/p&gt;

&lt;pre&gt;var next = false;
for (var i = 0; i &amp;lt; el.options.length; i++) {
  if (next) {
    next = false;
    el.options[i].selected = true;
    break;
  }
  if (el.options[i].selected) {
    el.options[i].selected = false;
    next = true;
  }
}
if (next) {
  el.options[0].selected = true;
}&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/716-check-next-option-in-select-tag" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144182</id>
    <published>2009-01-22T10:06:04-08:00</published>
    <title>[Ruby] On While loop to get unique value</title>
    <content type="html">&lt;p&gt;What about magic number '8'?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/697-while-loop-to-get-unique-value/refactors/144182" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor144181</id>
    <published>2009-01-22T10:01:13-08:00</published>
    <title>[Ruby] On Slurping popen</title>
    <content type="html">

&lt;pre&gt;%x[ls]&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/705-slurping-popen/refactors/144181" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55963</id>
    <published>2008-10-22T18:21:13-07:00</published>
    <title>[Ruby] On Conversion from the 2d array to hash.</title>
    <content type="html">&lt;p&gt;Something like I have written for the refactoring: &lt;a href="http://github.com/rails/rails/tree/master/activesupport/lib/active_support/ordered_hash.rb#L37" target="_blank"&gt;http://github.com/rails/rails/tree/master/activesupport/lib/active_support/ordered_hash.rb#L37&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I think I will use native activesupport OrderedHash class for converting to_hash...&lt;/p&gt;

&lt;p&gt;@danieharan: yes, in some cases I cant, but this array I'm getting from the Asset.count(:group =&amp;gt; 'column_type'), so it's fixed width with two values: column_type + count value. By the benchmark it's not very optimized, and also it's not so simple, that it might be.&lt;/p&gt;

&lt;p&gt;@Adam: I don't need the ordering, but anyway it's good that activesupport have that class. But why native Array class doesn't have to_h or to_hash? Strange for me.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/552-conversion-from-the-2d-array-to-hash/refactors/55963" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55938</id>
    <published>2008-10-22T14:24:03-07:00</published>
    <title>[Ruby] On Conversion from the 2d array to hash.</title>
    <content type="html">&lt;p&gt;@danielharan: is it faster than Hash[*a.flatten] ?&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/552-conversion-from-the-2d-array-to-hash/refactors/55938" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55937</id>
    <published>2008-10-22T14:20:29-07:00</published>
    <title>[Ruby] On Extract text between {} brackets</title>
    <content type="html">

&lt;pre&gt;res = path_without_calculation.scan(/\{(.*?)\}/).flatten&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/553-extract-text-between-brackets/refactors/55937" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55935</id>
    <published>2008-10-22T14:17:03-07:00</published>
    <title>[Ruby] On Conversion from the 2d array to hash.</title>
    <content type="html">&lt;p&gt;Thanks! Why no &amp;quot;to_h&amp;quot; method in ruby Hash native class? Inverting to_a changes? {:a =&amp;gt; 1, :b =&amp;gt; 2}.to_a.to_h&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/552-conversion-from-the-2d-array-to-hash/refactors/55935" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code552</id>
    <published>2008-10-22T09:11:29-07:00</published>
    <updated>2008-10-22T18:21:15-07:00</updated>
    <title>[Ruby] Conversion from the 2d array to hash.</title>
    <content type="html">&lt;p&gt;Conversion from the 2d array to hash.&lt;/p&gt;

&lt;pre&gt;class Array
  def to_h
    hash = {}
    self.each do |entry|
      hash[entry[0]] = entry[1]
      # Yes, we could write: key, value = entry, 
      # but it isn't optimized, to_h must be optimized, 
      # because it's a very little method
    end
    hash
  end
end

p [['a', '1'], ['b', '2']].to_h
# {&amp;quot;a&amp;quot;=&amp;gt;&amp;quot;1&amp;quot;, &amp;quot;b&amp;quot;=&amp;gt;&amp;quot;2&amp;quot;}&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/552-conversion-from-the-2d-array-to-hash" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55260</id>
    <published>2008-10-16T06:20:39-07:00</published>
    <title>[Ruby] On Open hours grouping</title>
    <content type="html">&lt;p&gt;I have some idea how to improve it, but don't have free time to implement. Will write my refactoring in about a one-two weeks.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/526-open-hours-grouping/refactors/55260" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55259</id>
    <published>2008-10-16T06:18:47-07:00</published>
    <title>[Ruby] On Open hours grouping</title>
    <content type="html">&lt;p&gt;@tmpr: You have an error in your output. Must be:
&lt;br /&gt;&amp;quot;opening_hours_mon - opening_hours_tue, opening_hours_thu: 9:00 - 20:00
&lt;br /&gt;Also must be sorted in a better way, not: sun, sat, fri, mon, tue... but mon, tue, wed... etc.
&lt;br /&gt;Also is very strange:
&lt;br /&gt;&amp;quot;opening_hours_sat, opening_hours_sun: closed&amp;quot;, I think it must be &amp;quot;opening_hours_sat - opening_hours_sun: closed&amp;quot;, by the previous example (opening_hours_tue - opening_hours_mon, opening_hours_thu: 9:00 - 20:00) logic?&lt;/p&gt;

&lt;p&gt;I like code of the previous one, by Adam.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/526-open-hours-grouping/refactors/55259" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code526</id>
    <published>2008-10-08T09:13:00-07:00</published>
    <updated>2008-10-16T06:20:40-07:00</updated>
    <title>[Ruby] Open hours grouping</title>
    <content type="html">&lt;p&gt;Actually I really don't know how to refactor it into a better way.&lt;/p&gt;

&lt;p&gt;Thanks for your help.&lt;/p&gt;

&lt;pre&gt;WEEKDAYS = %w(mon tue wed thu fri sat sun)

dates = [
  {:day =&amp;gt; WEEKDAYS[0], :time =&amp;gt; '9:00 - 20:00'},
  {:day =&amp;gt; WEEKDAYS[1], :time =&amp;gt; '9:00 - 20:00'},
  {:day =&amp;gt; WEEKDAYS[2], :time =&amp;gt; '9:00 - 20:00'},
  {:day =&amp;gt; WEEKDAYS[3], :time =&amp;gt; '8:00 - 19:00'},
  {:day =&amp;gt; WEEKDAYS[4], :time =&amp;gt; '9:00 - 20:00'},
  {:day =&amp;gt; WEEKDAYS[5], :time =&amp;gt; '8:00 - 19:00'},
  {:day =&amp;gt; WEEKDAYS[6], :time =&amp;gt; 'closed'}
]



def build_opening_hours dates
  ret = []

  dates.each do |day|
    d = ret.detect {|v| day[:time] == v[:time] }
    if d.nil?
      ret.push({:days =&amp;gt; [day[:day]], :time =&amp;gt; day[:time]})
    else
      d[:days] &amp;lt;&amp;lt; day[:day]
    end
  end

  data = []

  ret.each do |v|
    previous = nil
    v[:days].each do |day|
      index = WEEKDAYS.index(day)
      day_text = &amp;quot;opening_hours_#{day}&amp;quot;
      if previous.nil?
        data.push([day_text, v[:time]])
      else
        if ((index - 1) == previous)
          data.last[0] = data.last[0].gsub(/ - .*$/, '')
          data.last[0] += ' - ' + day_text
        else
          data.last[0] += ', ' + day_text
        end
      end
      previous = index
    end
  end

  ret = []

  data.each do |d|
    ret.push(d.join(': '))
  end

  ret
end

p build_opening_hours(dates)

# [
# &amp;quot;opening_hours_mon - opening_hours_wed, opening_hours_fri: 9:00 - 20:00&amp;quot;,
# &amp;quot;opening_hours_thu, opening_hours_sat: 8:00 - 19:00&amp;quot;,
# &amp;quot;opening_hours_sun: closed&amp;quot;
# ]&lt;/pre&gt;</content>
    <author>
      <name>Dmitry Polushkin</name>
      <email>dmitry.polushkin@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/526-open-hours-grouping" rel="alternate"/>
  </entry>
</feed>

