Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #474 from education/flipping-flippers
Browse files Browse the repository at this point in the history
Add Flipper to Classroom
  • Loading branch information
tarebyte committed Mar 2, 2016
2 parents 5e32bda + 251fd65 commit 7d7d6d5
Show file tree
Hide file tree
Showing 15 changed files with 134 additions and 53 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ gem 'coffee-rails', '~> 4.1.0'
gem 'draper'

gem 'faraday-http-cache'
gem 'flipper'
gem 'flipper-redis'
gem 'flipper-ui'

gem 'geo_pattern'

Expand Down Expand Up @@ -39,6 +42,7 @@ gem 'puma'
gem 'puma_worker_killer'

gem 'rack-canonical-host'
gem 'redis-namespace'

gem 'sprockets'
gem 'sass-rails', '~> 5.0'
Expand Down
15 changes: 15 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ GEM
multipart-post (>= 1.2, < 3)
faraday-http-cache (1.2.2)
faraday (~> 0.8)
flipper (0.7.4)
flipper-redis (0.7.4)
flipper (~> 0.7.4)
redis (>= 2.2, < 4.0.0)
flipper-ui (0.7.4)
erubis (~> 2.7.0)
flipper (~> 0.7.4)
rack (~> 1.4, < 1.7)
rack-protection (~> 1.5.3)
foreman (0.78.0)
thor (~> 0.19.1)
geo_pattern (1.4.0)
Expand Down Expand Up @@ -257,6 +266,8 @@ GEM
rainbow (2.0.0)
rake (10.5.0)
redis (3.2.2)
redis-namespace (1.5.2)
redis (~> 3.0, >= 3.0.4)
request_store (1.2.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
Expand Down Expand Up @@ -353,6 +364,9 @@ DEPENDENCIES
factory_girl_rails
faker
faraday-http-cache
flipper
flipper-redis
flipper-ui
foreman
geo_pattern
jbuilder
Expand Down Expand Up @@ -381,6 +395,7 @@ DEPENDENCIES
rack-tracker
rails (= 4.2.5.2)
rails_12factor
redis-namespace
rspec-rails
rubocop
sass-rails (~> 5.0)
Expand Down
1 change: 1 addition & 0 deletions app/models/assignment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Assignment < ActiveRecord::Base
include Flippable
include GitHubPlan
include Sluggable

Expand Down
7 changes: 7 additions & 0 deletions app/models/concerns/flippable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Flippable
extend ActiveSupport::Concern

def flipper_id
"#{self.class}:#{id}"
end
end
1 change: 1 addition & 0 deletions app/models/group_assignment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class GroupAssignment < ActiveRecord::Base
include Flippable
include GitHubPlan
include Sluggable

Expand Down
5 changes: 1 addition & 4 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Organization < ActiveRecord::Base
include Flippable
include Sluggable

update_index('stafftools#organization') { self }
Expand All @@ -19,14 +20,10 @@ class Organization < ActiveRecord::Base

validates :slug, uniqueness: true

# Public
#
def all_assignments
assignments + group_assignments
end

# Public
#
def github_client
users.sample.github_client
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
include Flippable

update_index('stafftools#user') { self }

has_many :repo_accesses, dependent: :destroy
Expand Down
12 changes: 7 additions & 5 deletions app/views/stafftools/_navigation.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<nav class="menu">
<span class="menu-heading"><%= octicon('rocket') %> Stafftools</span>
<%= link_to stafftools_root_path, class: 'menu-item selected' do %>
<%= octicon('search') %>
Search
<% end %>
<span class="menu-heading"><%= octicon('rocket') %> Site Admin</span>
<%= link_to 'Search', stafftools_root_path, class: 'menu-item selected' %>
</nav>

<nav class="menu">
<span class="menu-heading"><%= octicon('code') %> Developer Tools</span>
<%= link_to 'Features', stafftools_flipper_path, class: 'menu-item' %>
</nav>
7 changes: 7 additions & 0 deletions config/initializers/01_redis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Classroom
REDIS_URL = ENV['REDISTOGO_URL'] || 'redis://localhost:6379/0'

def self.redis
@redis ||= Redis.new(url: REDIS_URL)
end
end
22 changes: 22 additions & 0 deletions config/initializers/flipper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Classroom
def self.flipper
@flipper ||= self.flipper!
end

def self.flipper!
adapter = if Rails.env.test?
require 'flipper/adapters/memory'
Flipper::Adapters::Memory.new
else
namespaced_client = Redis::Namespace.new(:flipper, :redis => Classroom.redis)
Flipper::Adapters::Redis.new(namespaced_client)
end

Flipper.new(adapter)
end
end

# Flipper group for staff
Flipper.register(:staff) do |user|
user.respond_to?(:staff?) && user.staff?
end
5 changes: 4 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@
end

namespace :stafftools do
mount Sidekiq::Web => '/sidekiq', constraints: StaffConstraint.new
constraints StaffConstraint.new do
mount Sidekiq::Web => '/sidekiq'
mount Flipper::UI.app(Classroom.flipper) => '/flipper', as: 'flipper'
end

root to: 'resources#index'
get '/resource_search', to: 'resources#search'
Expand Down
42 changes: 24 additions & 18 deletions spec/models/assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@
end
end

describe 'when the title is updated' do
subject { create(:assignment) }

it 'updates the slug' do
subject.update_attributes(title: 'New Title')
expect(subject.slug).to eql('new-title')
end
end

describe 'uniqueness of title across organization' do
let(:organization) { create(:organization) }
let(:creator) { organization.users.first }
Expand Down Expand Up @@ -57,17 +48,32 @@
end
end

describe '#public?' do
it 'returns true if Assignments public_repo column is true' do
assignment = create(:assignment)
expect(assignment.public?).to be(true)
context 'with assignment' do
subject { create(:assignment) }

describe 'when the title is updated' do
it 'updates the slug' do
subject.update_attributes(title: 'New Title')
expect(subject.slug).to eql('new-title')
end
end

describe '#flipper_id' do
it 'should return an id' do
expect(subject.flipper_id).to eq("Assignment:#{subject.id}")
end
end

describe '#public?' do
it 'returns true if Assignments public_repo column is true' do
expect(subject.public?).to be(true)
end
end
end

describe '#private?' do
it 'returns false if Assignments public_repo column is true' do
assignment = create(:assignment)
expect(assignment.private?).to be(false)
describe '#private?' do
it 'returns false if Assignments public_repo column is true' do
expect(subject.private?).to be(false)
end
end
end
end
42 changes: 24 additions & 18 deletions spec/models/group_assignment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@
end
end

describe 'when the title is updated' do
subject { create(:group_assignment) }

it 'updates the slug' do
subject.update_attributes(title: 'New Title')
expect(subject.slug).to eql('new-title')
end
end

describe 'uniqueness of title across organization' do
let(:organization) { create(:organization) }
let(:creator) { organization.users.first }
Expand Down Expand Up @@ -53,17 +44,32 @@
end
end

describe '#public?' do
it 'returns true if Assignments public_repo column is true' do
group_assignment = create(:group_assignment)
expect(group_assignment.public?).to be(true)
context 'with group_assignment' do
subject { create(:group_assignment) }

describe 'when the title is updated' do
it 'updates the slug' do
subject.update_attributes(title: 'New Title')
expect(subject.slug).to eql('new-title')
end
end

describe '#flipper_id' do
it 'should return an id' do
expect(subject.flipper_id).to eq("GroupAssignment:#{subject.id}")
end
end

describe '#public?' do
it 'returns true if Assignments public_repo column is true' do
expect(subject.public?).to be(true)
end
end
end

describe '#private?' do
it 'returns false if Assignments public_repo column is true' do
group_assignment = create(:group_assignment)
expect(group_assignment.private?).to be(false)
describe '#private?' do
it 'returns false if Assignments public_repo column is true' do
expect(subject.private?).to be(false)
end
end
end
end
16 changes: 9 additions & 7 deletions spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
require 'rails_helper'

RSpec.describe Organization, type: :model do
describe 'when title is changed' do
subject { create(:organization) }
subject { create(:organization) }

describe 'when title is changed' do
it 'updates the slug' do
subject.update_attributes(title: 'New Title')
expect(subject.slug).to eql("#{subject.github_id}-new-title")
end
end

describe '#all_assignments' do
subject { create(:organization) }

context 'new Organization' do
it 'returns an empty array' do
expect(subject.all_assignments).to be_kind_of(Array)
Expand Down Expand Up @@ -40,11 +38,15 @@
end
end

describe '#github_client' do
let(:organization) { create(:organization) }
describe '#flipper_id' do
it 'should return an id' do
expect(subject.flipper_id).to eq("Organization:#{subject.id}")
end
end

describe '#github_client' do
it 'selects a users github_client at random' do
expect(organization.github_client.class).to eql(Octokit::Client)
expect(subject.github_client.class).to eql(Octokit::Client)
end
end
end
6 changes: 6 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
end
end

describe '#flipper_id' do
it 'should return an id' do
expect(user.flipper_id).to eq("User:#{user.id}")
end
end

describe '#github_client' do
it 'sets or creates a new GitHubClient with the users token' do
expect(user.github_client.class).to eql(Octokit::Client)
Expand Down

0 comments on commit 7d7d6d5

Please sign in to comment.