Merge branch 'roles'

This commit is contained in:
Maxwell Salzberg 2012-04-27 16:15:07 -07:00
commit 62b604e4be
12 changed files with 131 additions and 15 deletions

View file

@ -0,0 +1,56 @@
body {
background-color: #fff;
color: #333;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }
p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }
pre {
background-color: #eee;
padding: 10px;
font-size: 11px; }
a {
color: #000;
&:visited {
color: #666; }
&:hover {
color: #fff;
background-color: #000; } }
div {
&.field, &.actions {
margin-bottom: 10px; } }
#notice {
color: green; }
.field_with_errors {
padding: 2px;
background-color: red;
display: table; }
#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px;
padding-bottom: 0;
margin-bottom: 20px;
background-color: #f0f0f0;
h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px;
margin-bottom: 0px;
background-color: #c00;
color: #fff; }
ul li {
font-size: 12px;
list-style: square; } }

View file

@ -103,7 +103,7 @@ class AccountDeleter
end end
def normal_ar_person_associates_to_delete def normal_ar_person_associates_to_delete
[:posts, :photos, :mentions, :participations] [:posts, :photos, :mentions, :participations, :roles]
end end
def ignored_or_special_ar_person_associations def ignored_or_special_ar_person_associations

View file

@ -36,6 +36,7 @@ class Person < ActiveRecord::Base
accepts_nested_attributes_for :profile accepts_nested_attributes_for :profile
before_validation :downcase_diaspora_handle before_validation :downcase_diaspora_handle
def downcase_diaspora_handle def downcase_diaspora_handle
diaspora_handle.downcase! unless diaspora_handle.blank? diaspora_handle.downcase! unless diaspora_handle.blank?
end end
@ -46,6 +47,8 @@ class Person < ActiveRecord::Base
has_many :comments, :foreign_key => :author_id, :dependent => :destroy # This person's own comments has_many :comments, :foreign_key => :author_id, :dependent => :destroy # This person's own comments
has_many :participations, :foreign_key => :author_id, :dependent => :destroy has_many :participations, :foreign_key => :author_id, :dependent => :destroy
has_many :roles
belongs_to :owner, :class_name => 'User' belongs_to :owner, :class_name => 'User'
has_many :notification_actors has_many :notification_actors
@ -88,7 +91,7 @@ class Person < ActiveRecord::Base
} }
def self.community_spotlight def self.community_spotlight
AppConfig[:community_spotlight].present? ? Person.where(:diaspora_handle => AppConfig[:community_spotlight]) : [] Person.joins(:roles).where(:roles => {:name => 'spotlight'})
end end
# Set a default of an empty profile when a new Person record is instantiated. # Set a default of an empty profile when a new Person record is instantiated.

36
app/models/role.rb Normal file
View file

@ -0,0 +1,36 @@
#NOTE add the person object you want to attach role to...
class Role < ActiveRecord::Base
belongs_to :person
def self.is_admin?(person)
find_by_person_id_and_name(person.id, 'admin')
end
def self.add_beta(person)
find_or_create_by_person_id_and_name(person.id, 'beta')
end
def self.add_admin(person)
find_or_create_by_person_id_and_name(person.id, 'admin')
end
def self.add_spotlight(person)
find_or_create_by_person_id_and_name(person.id, 'spotlight')
end
def self.load_admins
admins = AppConfig[:admins] || []
admins.each do |username|
u = User.find_by_username(username)
find_or_create_by_person_id_and_name(u.person.id, 'admin')
end
end
def self.load_spotlight
spotlighters = AppConfig[:community_spotlight] || []
spotlighters.each do |diaspora_handle|
person = Person.find_by_diaspora_handle(diaspora_handle)
find_or_create_by_person_id_and_name(person.id, 'spotlight')
end
end
end

View file

@ -430,9 +430,10 @@ class User < ActiveRecord::Base
end end
def admin? def admin?
AppConfig[:admins].present? && AppConfig[:admins].include?(self.username) Role.is_admin?(self.person)
end end
def guard_unconfirmed_email def guard_unconfirmed_email
self.unconfirmed_email = nil if unconfirmed_email.blank? || unconfirmed_email == email self.unconfirmed_email = nil if unconfirmed_email.blank? || unconfirmed_email == email

View file

@ -0,0 +1,10 @@
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
t.integer :person_id
t.string :name
t.timestamps
end
end
end

View file

@ -11,7 +11,7 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20120422072257) do ActiveRecord::Schema.define(:version => 20120427152648) do
create_table "account_deletions", :force => true do |t| create_table "account_deletions", :force => true do |t|
t.string "diaspora_handle" t.string "diaspora_handle"
@ -388,6 +388,13 @@ ActiveRecord::Schema.define(:version => 20120422072257) do
add_index "rails_admin_histories", ["item", "table", "month", "year"], :name => "index_rails_admin_histories" add_index "rails_admin_histories", ["item", "table", "month", "year"], :name => "index_rails_admin_histories"
create_table "roles", :force => true do |t|
t.integer "person_id"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "service_users", :force => true do |t| create_table "service_users", :force => true do |t|
t.string "uid", :null => false t.string "uid", :null => false
t.string "name", :null => false t.string "name", :null => false

View file

@ -20,7 +20,7 @@ describe AdminsController do
context 'admin signed in' do context 'admin signed in' do
before do before do
AppConfig[:admins] = [@user.username] Role.add_admin(@user.person)
end end
it 'succeeds and renders user_search' do it 'succeeds and renders user_search' do
@ -70,7 +70,7 @@ describe AdminsController do
context 'admin signed in' do context 'admin signed in' do
before do before do
AppConfig[:admins] = [@user.username] Role.add_admin(@user.person)
end end
it 'does not die if you do it twice' do it 'does not die if you do it twice' do
@ -90,7 +90,7 @@ describe AdminsController do
describe '#stats' do describe '#stats' do
before do before do
AppConfig[:admins] = [@user.username] Role.add_admin(@user.person)
end end
it 'succeeds and renders stats' do it 'succeeds and renders stats' do

View file

@ -96,7 +96,7 @@ describe ContactsController do
end end
it 'gets queries for users in the app config' do it 'gets queries for users in the app config' do
AppConfig[:community_spotlight] = [alice.diaspora_handle] Role.add_spotlight(alice.person)
get :spotlight get :spotlight
assigns[:people].should == [alice.person] assigns[:people].should == [alice.person]

View file

@ -11,7 +11,7 @@ describe StreamsController do
describe "#public" do describe "#public" do
it 'will succeed if admin' do it 'will succeed if admin' do
AppConfig[:admins] = [alice.username] Role.add_admin(alice.person)
get :public get :public
response.should be_success response.should be_success
end end

View file

@ -476,13 +476,11 @@ describe Person do
describe '.community_spotlight' do describe '.community_spotlight' do
describe "when the pod owner hasn't set up any community spotlight members" do describe "when the pod owner hasn't set up any community spotlight members" do
before do it 'returns people with the community spotlight role' do
@existing_community_spotlight = AppConfig[:community_spotlight] Role.add_spotlight(bob.person)
AppConfig[:community_spotlight] = nil Person.community_spotlight.should be_present
end
after do
AppConfig[:community_spotlight] = @existing_community_spotlight
end end
it "returns an empty array" do it "returns an empty array" do
Person.community_spotlight.should == [] Person.community_spotlight.should == []
end end

5
spec/models/role_spec.rb Normal file
View file

@ -0,0 +1,5 @@
require 'spec_helper'
describe Role do
pending "add some examples to (or delete) #{__FILE__}"
end