Last active
August 29, 2015 14:20
-
-
Save jkronz/0a791f1aa7286cd12bdf to your computer and use it in GitHub Desktop.
Refactoring AR Model to be an Entity and an Adapter by Peter Harkins @pushcx
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 Feed | |
attr_reader :user | |
include Adamantium | |
def initialize user | |
@user = user | |
end | |
def microposts | |
# extracting side effects from Micropost is an obvious next step | |
Micropost.from_users_followed_by(self) | |
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 Follows | |
attr_reader :user | |
include Adamantium | |
def initialize user | |
@user = user | |
end | |
def following?(other_user) | |
user.relationships.find_by_followed_id(other_user.id) | |
end | |
def follow!(other_user) | |
user.relationships.create!(followed_id: other_user.id) | |
end | |
def unfollow!(other_user) | |
user.relationships.find_by_followed_id(other_user.id).destroy | |
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 User < ActiveRecord::Base | |
attr_accessible :name, :email, :password, :password_confirmation | |
has_secure_password | |
has_many :microposts, dependent: :destroy | |
has_many :relationships, foreign_key: "follower_id", dependent: :destroy | |
has_many :followed_users, through: :relationships, source: :followed | |
has_many :reverse_relationships, foreign_key: "followed_id", | |
class_name: "Relationship", | |
dependent: :destroy | |
has_many :followers, through: :reverse_relationships, source: :follower | |
before_save { |user| user.email = user.email.downcase } | |
before_save :create_remember_token | |
validates :name, presence: true, length: { maximum: 50 } | |
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | |
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, | |
uniqueness: { case_sensitive: false } | |
validates :password, length: { minimum: 6 } | |
validates :password_confirmation, presence: true | |
def following?(other_user) | |
relationships.find_by_followed_id(other_user.id) | |
end | |
def follow!(other_user) | |
relationships.create!(followed_id: other_user.id) | |
end | |
def unfollow!(other_user) | |
relationships.find_by_followed_id(other_user.id).destroy | |
end | |
def feed | |
Micropost.from_users_followed_by(self) | |
end | |
private | |
def create_remember_token | |
self.remember_token = SecureRandom.urlsafe_base64 | |
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 User < ActiveRecord::Base | |
attr_accessible :name, :email, :password, :password_confirmation | |
has_secure_password | |
has_many :microposts, dependent: :destroy | |
has_many :relationships, foreign_key: "follower_id", dependent: :destroy | |
has_many :followed_users, through: :relationships, source: :followed | |
has_many :reverse_relationships, foreign_key: "followed_id", | |
class_name: "Relationship", | |
dependent: :destroy | |
has_many :followers, through: :reverse_relationships, source: :follower | |
after_initialize :create_remember_token | |
validates :name, presence: true, length: { maximum: 50 } | |
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i | |
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, | |
uniqueness: { case_sensitive: false } | |
validates :password, length: { minimum: 6 } | |
validates :password_confirmation, :remember_token, presence: true | |
def email= new_email | |
self[:email] = new_email.downcase | |
end | |
private | |
def create_remember_token | |
self.remember_token ||= SecureRandom.urlsafe_base64 | |
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 UserRepo | |
attr_reader :user | |
include Adamantium | |
def initialize user | |
@user = user | |
end | |
def save | |
@user.save | |
end | |
def destroy | |
@user.destroy | |
end | |
def self.by_id(id) | |
User.find(id) | |
end | |
def self.by_remember_token(token) | |
User.find_by_remember_token(token) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment