Halaman

Tampilkan postingan dengan label patch. Tampilkan semua postingan
Tampilkan postingan dengan label patch. Tampilkan semua postingan

Senin, 04 Juli 2011

fixing option tag display none bug in webkit

The Problem

option tag using style display:none isn't hidden in browser using webkit engine (chrome, safari)
https://bugs.webkit.org/show_bug.cgi?id=8351

The workaround

using jQuery js framework with one simple function taken from jquery.extended_helper_2.6:
(function($) {
  $.fn.extend({
    /* hide select's specified option by removing the option and storing it in variable
     * this function is a workaround to fix webkit bug when hiding select's option:
     * https://bugs.webkit.org/show_bug.cgi?id=8351
     * hideSelectOptions(selector, options)
     * selector:
     * - string selector of options to hide
     * - null or undefined will not hide any options
     * - empty string will hide all options
     * - function will be ran for all options, return true to hide the options
     * options:
     * - toggleHide: true/false, activate/deactive toggle hide/show select if all options is hidden
     * - placeholder: string, add placeholder option if all options is hidden
     * usage:
     *  - $('select.certain_class').hideSelectOptions('.another_class'); // -> show previously hidden options and hide all options with 'another_class' class
     *  - $('select.certain_class').hideSelectOptions(''); // -> hide all options
     *  - $('select.certain_class').hideSelectOptions(); // -> show previously hidden options without hiding any options
     */
    hideSelectOptions: function(selector, options){
      if(!options) options = {};
      this.filter('select').each(function(){
        var $this = $(this);
        // restore previously hidden options and remove any placeholder option
        $this.append($this.data('hiddenOptions')).find('.placeholder_option').remove();

        // return if no selector specified
        if(!selector) return;
        
        // find, detach and store specified hidden options
        var hidden_options = $this.find('option');
        if($.isFunction(selector))
          hidden_options = hidden_options.deleteIf(function(){return !selector.call(this);});
        else
          hidden_options = hidden_options.filter(selector);
        $this.data('hiddenOptions',
          hidden_options
            .removeAttr('selected')
            .detach());

        // find visible options
        var visible_options = $this.find('option');

        // toggle hide/show select depend on visible option found
        if(options.toggleHide)
          $this.toggle(visible_options.length);
        if(options.placeholder && !visible_options.length)
          $this.append('');

        // select first option if no other visible option selected and trigger select change
        if(!visible_options.filter(':selected').length){
          visible_options.filter(':first').attr('selected', 'selected');
          $this.trigger('change');
        }
      });
      return this;
    }
  });
})(jQuery);

The Demonstration

Selasa, 28 Juni 2011

array flatten with level for ruby 1.8.7

straight to the code:
class Array
  alias_method :orig_flatten, :flatten
  
  def flatten level=nil
    if level.is_a?(Integer)
      temp_arr = clone
      level.times{ |i| temp_arr = temp_arr.inject([]){|s,v| v.is_a?(Array) ? s.concat(v) : s << v} }
      temp_arr
    else
      orig_flatten
    end
  end
end

of course although untested i'm sure it will have worse performance than the original flatten in ruby 1.9.2, but better than nothing right? ;)

ruby array to hash method

update 2012-12-21:
you should use flat_map instead of flatten, i'm too lazy to update the code though.. :P

we can convert hash to array easily using .to_a method for example:
hsh = {:a => 1, :b => 2, :c => 3}
arr = hsh.to_a # -> [[:a, 1], [:b, 2], [:c, 3]]

but there's no built in method to reverse that, there's no .to_hash instance method for array.

to solve that i added a simple instance method for array:
class Array
  def to_hash values=nil
    Hash[*(values.is_a?(Array) ? self.zip(values) : self).flatten(1)]
  end
end

hsh = {:a => 1, :b => 2, :c => 3}
arr = hsh.to_a # -> [[:a, 1], [:b, 2], [:c, 3]]
arr.to_hash == hsh # -> true
[:a,:b,:c].to_hash [1,2,3] # -> {:a => 1, :b => 2, :c => 3}

a little precaution, flatten(1) will not work properly in ruby 1.8.7 or older, therefore it won't be possible to create hash with array as key or value. but there's a simple workaround for that by overriding array flatten method to behave like ruby 1.9.2 in my next post :)

Rabu, 17 November 2010

[shared] array/hash support for system_settings

https://github.com/realityforge/rails-system-settings

# system_settings/lib/system_setting.rb

class SystemSetting < ActiveRecord::Base
@@data = {}

validates_length_of :name, :in => 1..255
validates_uniqueness_of :name

serialize :value

def self.[](name)
name = name.to_s unless name.is_a? String
return @@data[name] unless @@data[name].nil?
p = SystemSetting.find_by_name(name)
@@data[name] = p ? p.value.freeze : nil
end

def self.[]=(name, value)
name = name.to_s unless name.is_a? String
p = SystemSetting.find_or_initialize_by_name(name)
@@data[name] = p.value = value
p.save!
p.value.freeze
end
end

[shared] Unsigned int support for migration

currently rails only allow signed int for migration, but for best practice it's better to use unsigned int for some cases

you can force migration to unsigned int by doing something like this

t.column :unsigned_value, "integer unsigned"


but that kinda ugly to see.
luckily rob anderton (thewebfellas.com) already created monkey patch to add unsigned support: http://thewebfellas.com/assets/2008/9/30/active_record_unsigned.rb

the patch will add :unsigned option in migration so you can do something like this:

t.integer :unsigned_value, :unsigned => true


furthermore the patch will make default table id and foreign key to unsigned int(11), but make sure to use t.references to create foreign key or simply use :unsigned => true

important things:


- for existing project this patch might break table relation because it will create unsigned int foreign key but the primary key might be signed int from old migration
- some plugin like role_requirement will generate migration that using t.integer for foreign key instead of t.references, you need to change this manually
- the patch will only change mysql adapter, so it will not work for other adapter

Senin, 11 Oktober 2010

[shared] I18n pluralization patch

translate patch



module I18n
module Backend
module Base
def translate(locale, key, options = {})
raise InvalidLocale.new(locale) unless locale
return key.map { |k| translate(locale, k, options) } if key.is_a?(Array)

entry = key && lookup(locale, key, options[:scope], options)

if options.empty?
entry = resolve(locale, key, entry, options)
else
count, default = options.values_at(:count, :default)
values = options.except(*RESERVED_KEYS)
entry = entry.nil? && default ?
default(locale, key, default, options) : resolve(locale, key, entry, options)
end

raise(I18n::MissingTranslationData.new(locale, key, options)) if entry.nil?
entry = entry.dup if entry.is_a?(String)

entry = pluralize(locale, entry, count)
entry = interpolate(locale, entry, values) if values
entry
end
end
end
end


pluralize patch



module I18n
module Backend
class Simple
def pluralize(locale, entry, count)
if !defined?(count) || count.blank?
key = :none
elsif count == 0 && (entry.respond_to?(:has_key?) && entry.has_key?(:zero))
key = :zero
end
key ||= count == 1 ? :one : :other
unless entry.respond_to?(:has_key?) && entry.has_key?(key)
entry
else
entry[key]
end
end
end
end
end