Skip to content

Commit

Permalink
Merge ac349a0 into ab03a52
Browse files Browse the repository at this point in the history
  • Loading branch information
npauzenga committed Jan 24, 2016
2 parents ab03a52 + ac349a0 commit 46ff191
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 31 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem "interactor"
gem "figaro"
gem "knock"
gem "active_model_serializers"
gem "rolify"

group :developemnt, :test do
gem "pry-byebug"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ GEM
http-cookie (>= 1.0.2, < 2.0)
mime-types (>= 1.16, < 3.0)
netrc (~> 0.7)
rolify (5.0.0)
rspec (3.4.0)
rspec-core (~> 3.4.0)
rspec-expectations (~> 3.4.0)
Expand Down Expand Up @@ -257,6 +258,7 @@ DEPENDENCIES
puma
rails (= 4.2.4)
rails-api
rolify
rspec-rails (~> 3.0)
rubocop
rubocop-rspec
Expand Down
2 changes: 2 additions & 0 deletions app/models/game_night.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class GameNight < ActiveRecord::Base
resourcify

validates :time, presence: true
validates :location_name, presence: true

Expand Down
4 changes: 2 additions & 2 deletions app/models/group.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
class Group < ActiveRecord::Base
resourcify

has_many :user_group_memberships
has_many :users, through: :user_group_memberships

has_many :game_nights

belongs_to :moderator, class_name: :User, foreign_key: :moderator_id

validates :name, presence: true
end
10 changes: 10 additions & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, join_table: :users_roles
belongs_to :resource, polymorphic: true

validates :resource_type,
inclusion: { in: Rolify.resource_types },
allow_nil: true

scopify
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class User < ActiveRecord::Base
rolify
has_secure_password

has_many :user_group_memberships
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/rolify.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Rolify.configure do |config|
# By default ORM adapter is ActiveRecord. uncomment to use mongoid
# config.use_mongoid

# Dynamic shortcuts for User class (user.is_admin? like methods). Default is: false
# config.use_dynamic_shortcuts
end
19 changes: 19 additions & 0 deletions db/migrate/20160123201138_rolify_create_roles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class RolifyCreateRoles < ActiveRecord::Migration
def change
create_table(:roles) do |t|
t.string :name
t.references :resource, :polymorphic => true

t.timestamps
end

create_table(:users_roles, :id => false) do |t|
t.references :user
t.references :role
end

add_index(:roles, :name)
add_index(:roles, [ :name, :resource_type, :resource_id ])
add_index(:users_roles, [ :user_id, :role_id ])
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveOrganizerIdFromGameNights < ActiveRecord::Migration
def change
remove_column :game_nights, :organizer_id, :integer
end
end
5 changes: 5 additions & 0 deletions db/migrate/20160123223328_remove_moderator_id_from_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveModeratorIdFromGroups < ActiveRecord::Migration
def change
remove_column :groups, :moderator_id, :integer
end
end
26 changes: 21 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160110154158) do
ActiveRecord::Schema.define(version: 20160123223328) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -23,7 +23,6 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "group_id"
t.integer "organizer_id"
t.string "name"
end

Expand All @@ -36,11 +35,21 @@
t.string "country"
t.string "facebook"
t.string "twitter"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "moderator_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "roles", force: :cascade do |t|
t.string "name"
t.integer "resource_id"
t.string "resource_type"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "roles", ["name", "resource_type", "resource_id"], name: "index_roles_on_name_and_resource_type_and_resource_id", using: :btree
add_index "roles", ["name"], name: "index_roles_on_name", using: :btree

create_table "user_game_night_attendees", force: :cascade do |t|
t.integer "user_id"
t.integer "game_night_id"
Expand Down Expand Up @@ -77,6 +86,13 @@
t.string "country"
end

create_table "users_roles", id: false, force: :cascade do |t|
t.integer "user_id"
t.integer "role_id"
end

add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id", using: :btree

add_foreign_key "game_nights", "groups"
add_foreign_key "user_game_night_attendees", "game_nights"
add_foreign_key "user_game_night_attendees", "users"
Expand Down
37 changes: 30 additions & 7 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)
require 'faker'

# create users
5.times do
User.create(first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
email: Faker::Internet.email,
city: Faker::Address.city,
state: Faker::Address.state,
country: Faker::Address.country,
password: "helloworld",
email_confirmed: true)
end

# create game_night
5.times do
GameNight.create(name: Faker::Hipster.words(2),
time: rand(7).days.from_now.to_date,
location_name: Faker::Hipster.words(2),
location_address: Faker::Address.street_name)
end

# create groups
5.times do
Group.create(name: Faker::Hipster.words(2))
end

puts "#{User.count} Users created"
puts "#{GameNight.count} GameNights created"
puts "#{Group.count} Groups created"
18 changes: 7 additions & 11 deletions spec/models/game_night_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,27 @@
let(:group) { create(:group) }

let(:game_night) do
create(:game_night, group_id: group.id, organizer_id: user1.id)
create(:game_night, group_id: group.id)
end

before do
UserGameNightAttendee.create(user_id: user1.id,
game_night_id: game_night.id)
UserGameNightAttendance.create(user_id: user1.id,
game_night_id: game_night.id)

UserGameNightAttendee.create(user_id: user2.id,
game_night_id: game_night.id)
UserGameNightAttendance.create(user_id: user2.id,
game_night_id: game_night.id)
end

it "belongs to group" do
expect(game_night.group).to be_a(Group)
end

it "has many user_game_night_attendees" do
it "has many user_game_night_attendances" do
expect(game_night.user_game_night_attendees.count).to eq(2)
end

it "has many users through user_game_night_attendees" do
it "has many users through user_game_night_attendance" do
expect(game_night.users.count).to eq(2)
end

it "has one organizer" do
expect(game_night.organizer).to eq(user1)
end
end
end
7 changes: 1 addition & 6 deletions spec/models/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@
end

describe "associations" do
let(:moderator) { create(:confirmed_user) }
let(:user1) { create(:confirmed_user) }
let(:user2) { create(:confirmed_user) }
let(:group) { create(:group, moderator_id: moderator.id) }
let(:group) { create(:group) }

before do
UserGroupMembership.create(user_id: user1.id, group_id: group.id)
Expand All @@ -52,9 +51,5 @@

expect(group.game_nights.count).to eq(2)
end

it "belongs to a moderator" do
expect(group.moderator).to eq(moderator)
end
end
end

0 comments on commit 46ff191

Please sign in to comment.