Last active
September 11, 2015 17:58
-
-
Save eternal44/09dae15d30f3b8ea533d to your computer and use it in GitHub Desktop.
lity.herokuapp.com - complex associations
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
# == Schema Information | |
# | |
# Table name: friendships | |
# | |
# id :integer not null, primary key | |
# user_id :integer | |
# friend_id :integer | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# | |
# Indexes | |
# | |
# index_friendships_on_friend_id (friend_id) | |
# index_friendships_on_user_id (user_id) | |
# | |
class Friendship < ActiveRecord::Base | |
after_create :create_inverse_relationship | |
after_destroy :destroy_inverse_relationship | |
belongs_to :user | |
belongs_to :friend, class_name: 'User' | |
validates :user_id, presence: true | |
validates :friend_id, presence: true, uniqueness: { scope: :user } | |
validate :not_self | |
private | |
def create_inverse_relationship | |
friend.friendships.create(friend: user) | |
end | |
def destroy_inverse_relationship | |
friendship = friend.friendships.find_by(friend: user) | |
friendship.destroy if friendship | |
end | |
def not_self | |
errors.add(:friend, "can't be equal to user") if user_id == friend_id | |
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
# == Schema Information | |
# | |
# Table name: groups | |
# | |
# id :integer not null, primary key | |
# group_name :string | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# | |
class Group < ActiveRecord::Base | |
has_many :memberships, dependent: :destroy | |
has_many :users, through: :memberships | |
has_many :archives, dependent: :destroy | |
has_many :comments, as: :commentable, dependent: :destroy | |
validates :group_name, presence: true | |
accepts_nested_attributes_for :archives | |
accepts_nested_attributes_for :memberships | |
validates_associated :archives | |
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
# == Schema Information | |
# | |
# Table name: memberships | |
# | |
# id :integer not null, primary key | |
# user_id :integer | |
# group_id :integer | |
# group_role :text default("Regular") | |
# created_at :datetime not null | |
# updated_at :datetime not null | |
# | |
class Membership < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :group | |
validates :user_id, presence: true | |
validates :group_id, presence: true | |
validates :group_role, inclusion: { in: %w(Admin Regular), | |
messge: "%(value) is not a valid membership type" } | |
# used in group_helper.rb | |
scope :admin, -> { where(group_role: 'Admin') } | |
scope :member, -> {where(group_role: 'Admin' || 'Regular' )} | |
def admin? | |
group_role == "Admin" | |
end | |
def regular? | |
group_role == "Admin" || "Regular" | |
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
# | |
# Table name: users | |
# | |
# id :integer not null, primary key | |
# email :string default(""), not null | |
# encrypted_password :string default(""), not null | |
# reset_password_token :string | |
# reset_password_sent_at :datetime | |
# remember_created_at :datetime | |
# sign_in_count :integer default(0), not null | |
# current_sign_in_at :datetime | |
# last_sign_in_at :datetime | |
# current_sign_in_ip :inet | |
# last_sign_in_ip :inet | |
# first_name :string | |
# last_name :string | |
# created_at :datetime | |
# updated_at :datetime | |
# | |
# Indexes | |
# | |
# index_users_on_email (email) UNIQUE | |
# index_users_on_reset_password_token (reset_password_token) UNIQUE | |
# | |
class User < ActiveRecord::Base | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
has_many :memberships, dependent: :destroy | |
has_many :groups, through: :memberships | |
has_many :archives, dependent: :destroy | |
has_many :friend_requests, dependent: :destroy | |
has_many :pending_friends, through: :friend_requests, source: :friend, dependent: :destroy | |
has_many :friendships, dependent: :destroy | |
has_many :friends, through: :friendships, dependent: :destroy | |
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
Rails.application.routes.draw do | |
devise_for :users | |
resources :users, only: [:index, :show, :destroy] do | |
resources :friend_requests, shallow: true do | |
end | |
end | |
# for friendship associations | |
resources :friends, only: [:destroy] | |
resources :groups do | |
scope module: 'groups' do | |
resources :archives | |
resources :memberships | |
resources :comments | |
end | |
end | |
resources :archives, only: [] do | |
resources :comments, module: :archives | |
end | |
# logged-in root | |
authenticated :user do | |
root to: "users#index", as: :authenticated_root, via: :get | |
end | |
# visitors root | |
unauthenticated do | |
root to: "static_pages#home" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used this app to test how back-end Rails components interact with each other.