Skip to content

Commit

Permalink
Implements Bushido-specific functionality in the Bushido bootstrap file
Browse files Browse the repository at this point in the history
  • Loading branch information
sgrove committed Feb 23, 2012
1 parent be5fadf commit 3a902b8
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 5 deletions.
7 changes: 3 additions & 4 deletions app/models/user.rb
Expand Up @@ -5,19 +5,18 @@ class User < ActiveRecord::Base

# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
devise *Fulcrum.devise_modules

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me,
attr_accessible :email, :password, :password_confirmation, :remember_me, :ido_id,
:name, :initials, :email_delivery, :email_acceptance, :email_rejection

# Flag used to identify if the user was found or created from find_or_create
attr_accessor :was_created

has_and_belongs_to_many :projects, :uniq => true

before_validation :set_random_password_if_blank, :set_reset_password_token
before_validation :set_random_password_if_blank, :set_reset_password_token unless ::Bushido::Platform.on_bushido?

validates :name, :presence => true
validates :initials, :presence => true
Expand Down
1 change: 1 addition & 0 deletions config/initializers/bushido.rb
@@ -0,0 +1 @@
require './lib/bushido/bushido_bootstrap'
13 changes: 12 additions & 1 deletion config/initializers/fulcrum.rb
@@ -1 +1,12 @@
ActiveSupport.use_standard_json_time_format = false
ActiveSupport.use_standard_json_time_format = false

module Fulcrum
def self.devise_modules
standard = [:database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable]
bushido = [:bushido_authenticatable, :trackable, :token_authenticatable]

::Bushido::Platform.on_bushido? ? bushido : standard
end
end

1 change: 1 addition & 0 deletions db/migrate/20110210082458_devise_create_users.rb
@@ -1,6 +1,7 @@
class DeviseCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.bushido_authenticatable
t.database_authenticatable :null => false
t.recoverable
t.rememberable
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb
@@ -1,3 +1,4 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
Expand Down Expand Up @@ -60,6 +61,7 @@
end

create_table "users", :force => true do |t|
t.string "ido_id"
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
Expand Down
80 changes: 80 additions & 0 deletions lib/bushido/bushido_bootstrap.rb
@@ -0,0 +1,80 @@
module Fulcrum
module Bushido
def self.enable_bushido!
self.load_hooks!
self.extend_user!
self.extend_project!
self.disable_devise_for_bushido_controllers!
end

def self.extend_user!
puts "Extending the User model"
User.instance_eval do
validates_presence_of :ido_id
validates_uniqueness_of :ido_id

after_create :add_all_projects!
end

User.class_eval do
def bushido_extra_attributes(extra_attributes)
self.name = "#{extra_attributes['first_name'].to_s} #{extra_attributes['last_name'].to_s}"
if extra_attributes['first_name'] && extra_attributes['last_name']
self.initials = "#{extra_attributes['first_name'][0].upcase}#{extra_attributes['last_name'][0].upcase}"
else
self.initials = "#{extra_attributes['email'][0].upcase}#{extra_attributes['email'][1].upcase}"
end

self.email = extra_attributes["email"]
end

def add_all_projects!
Project.all.each { |project| project.users << self unless project.users.member?(self) }
end
end
end

def self.extend_project!
puts "Extending the Project model"
Project.instance_eval do
after_create :add_all_users!
end

Project.class_eval do
def add_all_users!
User.all.each do |user|
unless self.users.include?(user)
self.users << user
end
end
end
end
end

def self.load_hooks!
Dir["#{Dir.pwd}/lib/bushido/**/*.rb"].each { |file| load file }
end

# Temporary hack because all routes require authentication in
# Fulcrum
def self.disable_devise_for_bushido_controllers!
puts "Disabling devise auth protection on bushido controllers"

::Bushido::DataController.instance_eval { before_filter :authenticate_user!, :except => [:index] }
::Bushido::EnvsController.instance_eval { before_filter :authenticate_user!, :except => [:update] }
::Bushido::MailController.instance_eval { before_filter :authenticate_user!, :except => [:index] }

puts "Devise checks disabled for Bushido controllers"
end
end
end

if Bushido::Platform.on_bushido?
class BushidoRailtie < Rails::Railtie
config.to_prepare do
puts "Enabling Bushido"
Fulcrum::Bushido.enable_bushido!
puts "Finished enabling Bushido"
end
end
end

0 comments on commit 3a902b8

Please sign in to comment.