Halaman

Sabtu, 30 Oktober 2010

named scope conditions eager loaded warning

http://jitu-blog.blogspot.com/2009/07/looking-into-rails-namedscope.html

Senin, 25 Oktober 2010

[shared] range overlap check

overlap = !((end2 < start1) || (start2 > end1))

r = Range.find_all(:conditions => ["NOT ((end < ?) || (start > ?))", new_start, new_end])

overlap = r.present?
number_of_overlaps = r.length

Kamis, 21 Oktober 2010

dynamic form with accept_nested_attributes

http://railsforum.com/viewtopic.php?id=28447

Selasa, 19 Oktober 2010

jQuery ExtendedHelper

http://code.google.com/p/jquery-extendedhelper/

jQuery hash merge

merge hash2 to hash1:

$.extend(hash1, hash2);


for recursive merge:

$.extend(true, hash1, hash2);

Senin, 18 Oktober 2010

add javascript files to rails default

environment.rb

# reset:
ActionView::Helpers::AssetTagHelper::JAVASCRIPT_DEFAULT_SOURCES = [
'jquery-1.4.3', 'jquery-ui-1.8.6.custom', 'jrails',
'jquery.extended_helper_2.0', 'date-id', 'jquery.gritter'
]
ActionView::Helpers::AssetTagHelper::reset_javascript_include_default

# adding:
ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'extended_helper', 'date'

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

Rabu, 06 Oktober 2010

[shared] Netbeans FUSCK (Frequently Used ShortCut Keys)



  • ctrl+c, ctrl+v, alt+v :
    - Copy, Paste, Display paste history (CopyPasteHistory Plugin required)


  • ctrl+f, ctrl+h :
    - find, replace in file


  • ctrl+shift+f, ctrl+shift+h :
    - find, replace in project


  • alt+shift+f :
    - format codes indentation in file


  • ctrl+shift+up/down arrow
    - copy up/down selection line. very useful combined with alt+shift+arrow


  • alt+shift+up/down/left/right arrow
    - move selection


  • ctrl+space
    - show code completion


  • ctrl+shift+a
    - go to view from controller or to controller from view


  • ctrl+click / ctrl+b
    - go to declaration


  • ctrl+tab, ctrl+shift+tab
    - next/prev tab


  • alt+shift+r
    - open rake task window


  • alt+insert
    - open generate window


  • ctrl+w
    - close document


  • ctrl+shift+w
    - close all documents


  • alt+shift+w
    - [custom shortcut] close other documents

[shared] netbeans useful plugins



  • Bundled subversion client for windows
    - What: self explanatory
    - Install: Tools, Plugins


  • Cucumber features
    - What: support cucumber files and add cucumber functionality
    - Install: http://github.com/QuBiT/cucumber-netbeans-plugin


  • Copy paste history
    - What: add copy (ctrl+c) history functionality, very useful!
    - Install: http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=78


  • jVi
    - What: vi/vim editor clone
    - Install: http://sourceforge.net/projects/jvi/files/

[shared] netbeans rails additional code template

Ruby


1. model

#--
# constants
#++

#--
# require
#++

#--
# include
#++

#--
# attr
#++

#--
# extra capabilities
#++

#--
# belongs_to
#++

#--
# has_one
#++

#--
# has_many
#++

#--
# accepts_nested_attributes_for
#++

#--
# validation
#++

#--
# callbacks
#++

#--
# named_scope
#++

#--
# other definition such as alias_method
#++

#--
# public class methods
#++

#--
# public instance methods
#++

private
#--
# private class methods
#++

#--
# private instance methods
#++

protected
#--
# protected class methods
#++

#--
# protected instance methods
#++


2. resource

before_filter :prepare_account, :only => [:show, :edit, :update, :destroy]

# GET new_account_url
def new
# return an HTML form for describing the new account
end

# POST account_url
def create
# create an account
end

# GET account_url
def show
# find and return the account
end

# GET edit_account_url
def edit
# return an HTML form for editing the account
end

# PUT account_url
def update
# find and update the account
unless @account.update_attributes params[:account]
# failure code here..
end
end

# DELETE account_url
def destroy
# delete the account
@account.destroy
end

private

def prepare_account
@account = Account.first
end


3. resources

before_filter :prepare_messages, :only => [:index]
before_filter :prepare_message, :only => [:show, :edit, :update, :destroy]

# GET messages_url
def index
# return all messages
end

# GET new_message_url
def new
# return an HTML form for describing a new message
end

# POST messages_url
def create
# create a new message
end

# GET message_url(:id => 1)
def show
# find and return a specific message
end

# GET edit_message_url(:id => 1)
def edit
# return an HTML form for editing a specific message
end

# PUT message_url(:id => 1)
def update
# find and update a specific message
unless @message.update_attributes params[:message]
# failure code here..
end
end

# DELETE message_url(:id => 1)
def destroy
# delete a specific message
@message.destroy
end

private

def prepare_messages
@messages = Message.all
end

def prepare_message
@message = Message.find params[:id]
end


4. vf

validates_format_of :${1 default="attribute"}


5. vn

validates_numericality_of :${1 default="attribute"}


Ruby HTML (RHTML)


1. script




2. style