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
| blog = Blog.create | |
| post = blog.make_post text: 'great post', location_country: 'Canada', location_city: 'Toronto' | |
| post.make_comment text: 'great comment', location_country: 'Canada', location_city: 'Toronto' |
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 Blog < ActiveRecord::Base | |
| ... | |
| def all_posts_from country, city | |
| ... | |
| end | |
| def all_comments_from country, city | |
| ... | |
| 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 LocationPresenter | |
| def initialize country, city | |
| ... | |
| 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
| class Location < Struct.new(:country, :city) | |
| 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 Post < ActiveRecord::Base | |
| composed_of :location, mapping: [%w(location_country country), | |
| %w(location_city city)] | |
| def self.all_posts_from location | |
| Post.where location: location | |
| 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
| blog.make_post text: 'great post 2', location: Location.new('Canada', 'Toronto') |
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
| def Toronto | |
| Location.new('Canada', 'Toronto') | |
| end | |
| ... | |
| blog.make_post text: 'great post', location: Toronto |
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 Location < ActiveRecord::Base | |
| validates :city, :uniqueness => {:scope => :country} | |
| def self.get country, city | |
| location = Location.find_by_country_and_city(country, city) | |
| raise "There is no '#{city}' in '#{country}'" unless location | |
| location.readonly! | |
| location | |
| 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
| toronto = Location.get('Canada', 'Toronto') | |
| blog.make_post text: 'great post 2', location: toronto |
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
| # The User class has two attributes: name and age. | |
| # STEP 1: Call restrict_ar! inside your model definition. It will make most AR methods private. | |
| class User < ActiveRecord::Base | |
| restrict_ar! | |
| end | |
| # All AR class methods will become private. As a result, the following calls will raise an exception: | |
| User.create! # BOOM |