Last active
August 29, 2015 14:16
-
-
Save coalwater/3584ed4bf2a15a88c3ee to your computer and use it in GitHub Desktop.
law of demeter and rails delegation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order < ActiveRecord::Base | |
delegate :name, :price, to: :item, prefix: true, allow_nil: true | |
delegate :number, to: :invoice, prefix: true, allow_nil: true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order < ActiveRecord::Base | |
delegate :name, :price, to: :item, prefix: true | |
delegate :number, to: :invoice, prefix: true | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= @order.item_name %> | |
<%= @order.item_price %> | |
<%= @order.invoice_number %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order < ActiveRecord::Base | |
delegate :item_name, :item_price, to: :item | |
delegate :invoice_number, to: :invoice | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item < ActiveRecord::Base | |
def item_name | |
name | |
end | |
def item_price | |
price | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user.address_street_name | |
recipe.igredient_name | |
car.horse_power |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user.address.street_name | |
recipe.igredient.name | |
car.engine.horse_power |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Order < ActiveRecord::Base | |
def item_name | |
item.name | |
end | |
def item_price | |
item.price | |
end | |
def invoice_number | |
invoice.number | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= @order.item.name %> | |
<%= @order.item.price %> | |
<%= @order.invoice.number %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment