Error
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.

attributes

ActiveForm
Tuesday, 05 April 2011 09:32
Simplest wrapper for a Rails model, that for view or controlloer behaves like model itself (at least in my app :) ).


class ActiveForm
extend Forwardable

def attributes= attributes
@attributes = attributes
end

def initialize object
@object = object
end

def save
@object.attributes = @attributes
@object.save
end

def self.attr_delegators *attrs
def_delegators :@object, *attrs
end

def_delegators :@object, :errors, :to_key, :to_param, :persisted?

cattr_accessor :model_class

def self.model_name
@@model_class.model_name
end
end


Example form:

class EntryForm < ActiveForm
self.model_class = Entry
attr_delegators :text, :title, :add_to
end


In controller:

def update
@entry = EntryForm.new Entry.find(params[:id])
@entry.attributes = params[:entry]

if @entry.save
redirect_to @entry
else
render :edit
end
end

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/quhWsvpUWgc/13097

 
FormModel
Tuesday, 05 April 2011 09:32
Simplest wrapper for a Rails model, that for view or controlloer behaves like model itself (at least in my app :) ).


class FormModel
extend Forwardable

def attributes= attributes
@attributes = attributes
end

def initialize object
@object = object
end

def save
@object.attributes = @attributes
@object.save
end

def self.attr_delegators *attrs
def_delegators :@object, *attrs
end

def_delegators :@object, :errors, :to_key, :to_param, :persisted?

cattr_accessor :model_class

def self.model_name
@@model_class.model_name
end
end


Example form:

class EntryForm < ActiveForm
self.model_class = Entry
attr_delegators :text, :title, :add_to
end


In controller:

def update
@entry = EntryForm.new Entry.find(params[:id])
@entry.attributes = params[:entry]

if @entry.save
redirect_to @entry
else
render :edit
end
end

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/quhWsvpUWgc/13097

 
ActiveRecord::Base.foreach (for Oracle users)
Tuesday, 02 November 2010 10:03
# Iterate over one record at a time using ActiveRecord, instead of being slurpy.


def self.foreach(conditions = nil, &block)
conn = connection.raw_connection

sql = "select * from #{table_name}"
sql += " where #{conditions}" if conditions

begin
raw_cursor = conn.exec(sql)

while rec = raw_cursor.fetch_hash
object = self.new

# To get around protected attributes we must assign all attributes
# individually, instead of passing a single hash to self.new.
rec.each{ |key, value|
cname = key.downcase
object.send("#{cname}=", value)
}

yield object
end
ensure
raw_cursor.close if raw_cursor
end
end

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/QyD6ZAtVq5w/12559

 
Mind-Blowing Video App Can Transform Any Flabby Slob Into a Hottie [Video]
Monday, 11 October 2010 22:50
Forget about Photoshop. This video demonstration is so amazing that it defies belief. It can take any video, identify the people in it, and change their physical attributes automagically. Yes, it even has a breast size slider. More »


Read more: http://feeds.gawker.com/~r/gizmodo/full/~3/g20POPdBJpY/mind+blowing-video-app-can-transform-any-flabby-slob-into-a-hottie

 


Taxonomy by Zaragoza Online