Last active
August 17, 2019 11:20
-
-
Save dgorodnichy/69480f53a3ad68630bbbc51cb74494a7 to your computer and use it in GitHub Desktop.
This file contains 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
# frozen_string_literal: true | |
require 'bundler/inline' | |
gemfile(true) do | |
source 'https://rubygems.org' | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
gem 'rails', '= 6.0.0' | |
gem 'sqlite3' | |
end | |
require 'active_record' | |
require 'minitest/autorun' | |
if ARGV.include?('log') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
require 'logger' | |
end | |
class Participation < ActiveRecord::Base | |
belongs_to :project | |
belongs_to :user | |
enum role: { user: 0, admin: 1 } | |
end | |
class Project < ActiveRecord::Base | |
has_and_belongs_to_many :participants, join_table: :participations, class_name: 'User' | |
def admins | |
participants_relation(:admin) | |
end | |
def users | |
participants_relation(:user) | |
end | |
private | |
def participants_relation(role) | |
participants.where(participations: { role: Participation.roles[role] }) | |
end | |
end | |
class User < ActiveRecord::Base | |
has_and_belongs_to_many :projects, join_table: :participations | |
end | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Schema.define do | |
create_table :participations do |t| | |
t.integer :user_id | |
t.integer :project_id | |
t.integer :role | |
t.timestamps | |
end | |
add_index :participations, %i[project_id user_id] | |
add_index :participations, :role, name: 'index_projects_admins', where: "(role = #{Participation.roles[:admin]})" | |
create_table :users do |t| | |
t.string :name | |
t.timestamps | |
end | |
create_table :projects do |t| | |
t.string :name | |
t.timestamps | |
end | |
end | |
class TestRelations < Minitest::Test | |
def setup | |
@paul = User.create(name: 'Paul') | |
@neeraj = User.create(name: 'Neeraj') | |
@jt_project = Project.create(name: 'JT Project') | |
@bb_project = Project.create(name: 'BB Project') | |
Participation.create(user: @paul, project: @jt_project, role: :admin) | |
Participation.create(user: @paul, project: @bb_project, role: :user) | |
Participation.create(user: @neeraj, project: @jt_project, role: :user) | |
Participation.create(user: @neeraj, project: @bb_project, role: :admin) | |
end | |
def teardown | |
[Participation, Project, User].map(&:destroy_all) | |
end | |
def test_jt_admins_ids | |
assert_equal @jt_project.admins.ids, [@paul.id] | |
end | |
def test_bb_users_names | |
assert_equal @bb_project.users.pluck(:name), ['Paul'] | |
end | |
def test_count_admins_by_project_id | |
admins_by_project_id = Participation | |
.where(role: :admin) | |
.group(:project_id) | |
.count | |
assert_equal admins_by_project_id, @jt_project.id => 1, @bb_project.id => 1 | |
end | |
def test_count_admins_by_project_name | |
admins_by_project_name = Participation | |
.where(role: :admin) | |
.joins(:project) | |
.group('projects.name') | |
.count | |
assert_equal admins_by_project_name, 'JT Project' => 1, 'BB Project' => 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment