Act as

download Act as

If you can't read please download the document

Transcript of Act as

ACTS AS

Andr Tagliati
@tagliati
http://www.tagliati.com.br
[email protected]

Tagliati

E @makotovh papagaio de pirata

http://www.cowboycoded.com/tag/acts_as/

http://www.flickr.com/photos/loop_oh/3338084111/

Apresentao tomando como base explicao em artigo de cowboycoded

DRY

DONT REPEAT YOURSELF

DRY

http://www.flickr.com/photos/vorty/850290006/

Review

Ou Acts as Reviewable

REVIEWABLE

Criar um 'plugin' que permita fazer reviews de um produto ou qualquer outra coisa

Hands On

Vamos ver como isso.

HANDS ON

http://www.flickr.com/photos/jurvetson/489257240/

rails g model review review_text:text

t.references :reviewable, :polymorphic => true

Ali! No teto!
Polymorphic!!!!Me enganei.
s um PomboRanger.Polymorphic relationships allow you to have a single model that can be associated to an arbitrary number of other model types.Example

For instance, let's say you have a contacts database utilizing two models, Person and Company. Both people and companies have addresses that you want to store in the database, and you want to be able to query against all addresses at once (say for a location-based search).

Polymorphic associations make this easy in Rails. Let's assume that you already have your Person and Company models created and are just creating your Address model. When you create your migration you will denote the polymorphic relationship using references: http://wiki.rubyonrails.org/howtos/db-relationships/polymorphic

class Review < ActiveRecord::Base belongs_to :reviewable, :polymorphic=>trueend

module Reviewable def is_reviewable has_many :reviews, :as=>:reviewable, :dependent=>:destroy include InstanceMethods end module InstanceMethods def reviewable? true end endendActiveRecord::Base.extend Reviewable

class Produto < ActiveRecord::Base is_reviewableend

class Categoria < ActiveRecord::Base is_reviewableend

class Artigo < ActiveRecord::Base is_reviewableend

hehaEHAEHaeHe
haHeAHeAHeAHe
HAEhaeAssim fica fcil!http://www.flickr.com/photos/80835774@N00/5442336248/

product = Product.new=> #product.reviewable?=> trueproduct.reviews=> [] #nenhum review aindaproduct.saveproduct.reviews.create(review_text: "test")=> #product.reviews.size=> 1

BLOCOS

Dvidas?