Skip to content

Commit

Permalink
Adds Bushido bootstrap and hooks. Readies the app for non-Bushido pla…
Browse files Browse the repository at this point in the history
…tforms.

Signed-off-by: Akash Manohar J <akash@akash.im>
  • Loading branch information
HashNuke committed Apr 4, 2012
1 parent a6ed5c7 commit ff36e73
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 26 deletions.
10 changes: 1 addition & 9 deletions app/assets/javascripts/backbone/widgets.js.coffee
Expand Up @@ -11,15 +11,7 @@ class Kandan.Widgets
$.each @widgets, (widget_namespace, el_name)=>
@init(widget_namespace)

@template: _.template '''
<div class="widget" id="widget_<%= element_id %>">
<div class="widget_header">
<h3 style="background-image:url(<%= icon_url %>);"><%= title %></h3>
</div>
<div class="widget_content" id="<%= element_id %>"></div>
<div class="widget_action_bar"></div>
</div>
'''
@template: JST['widget']

@init: (widget_namespace)->
widget = eval(widget_namespace)
Expand Down
7 changes: 7 additions & 0 deletions app/assets/templates/widget.jst.eco
@@ -0,0 +1,7 @@
<div class="widget" id="widget_<%= @element_id %>">
<div class="widget_header">
<h3 style="background-image:url(<%= @icon_url %>);"><%= @title %></h3>
</div>
<div class="widget_content" id="<%= @element_id %>"></div>
<div class="widget_action_bar"></div>
</div>
11 changes: 7 additions & 4 deletions app/models/user.rb
Expand Up @@ -4,12 +4,11 @@ class User < ActiveRecord::Base
before_save :ensure_authentication_token
before_save :ensure_gravatar_hash

# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :bushido_authenticatable, :token_authenticatable, :trackable
# Kandan.devise_modules is defined in config/initializers/kandan.rb
devise devise *Kandan.devise_modules

# Setup accessible (or protected) attributes for your model
attr_accessible :id, :email, :password, :password_confirmation, :remember_me, :ido_id, :first_name, :last_name, :locale, :gravatar_hash
attr_accessible :id, :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :locale, :gravatar_hash

def bushido_extra_attributes(extra_attributes)
self.first_name = extra_attributes["first_name"].to_s
Expand All @@ -21,4 +20,8 @@ def ensure_gravatar_hash
self.gravatar_hash = Digest::MD5.hexdigest self.email
end

def active_for_authentication?
super && active?
end

end
1 change: 0 additions & 1 deletion config/initializers/backbone.rb

This file was deleted.

12 changes: 11 additions & 1 deletion config/initializers/kandan.rb
@@ -1,6 +1,16 @@
require "#{Rails.root}/lib/kandan_config.rb"

Dir["#{Rails.root}/lib/broadcasters/**/*.rb"].each do |file|
require file
end

require "#{Rails.root}/lib/kandan_config.rb"

module Kandan
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
5 changes: 5 additions & 0 deletions db/migrate/20120404114631_add_active_to_users.rb
@@ -0,0 +1,5 @@
class AddActiveToUsers < ActiveRecord::Migration
def change
add_column :users, :active, :boolean, :default => true
end
end
11 changes: 6 additions & 5 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20120322155347) do
ActiveRecord::Schema.define(:version => 20120404114631) do

create_table "activities", :force => true do |t|
t.text "content"
Expand Down Expand Up @@ -51,8 +51,8 @@
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"

create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
Expand All @@ -66,9 +66,10 @@
t.text "last_name"
t.text "ido_id"
t.string "locale"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "gravatar_hash"
t.boolean "active", :default => true
end

add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token", :unique => true
Expand Down
43 changes: 43 additions & 0 deletions lib/bushido/bushido_bootstrap.rb
@@ -0,0 +1,43 @@
module Kandan
module Bushido
def self.enable_bushido!
self.load_hooks!
self.extend_user!
self.extend_project!
end

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

attr_accessible :ido_id
end

User.class_eval do
def bushido_extra_attributes(extra_attributes)
self.first_name = "#{extra_attributes['first_name']}"
self.last_name = "#{extra_attributes['last_name']}"
self.locale = extra_attributes['locale']
self.email = extra_attributes["email"]
end
end
end

def self.load_hooks!
Dir["#{Dir.pwd}/lib/bushido/**/*.rb"].each { |file| load file }
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
10 changes: 10 additions & 0 deletions lib/bushido/bushido_hooks/app_hooks.rb
@@ -0,0 +1,10 @@
class BushidoAppHooks < Bushido::EventObserver
# This is here for eventual lazy-registration. Create a fake user on
# rake bushido:install, and when the app is claimed, we'll get this
# event with the user info, so we convert the existing user and
# update their info here.
def app_claimed
User.order("created_at").first.update_attributes(:email => params['data']['email'],
:ido_id => params['data']['ido_id'])
end
end
44 changes: 44 additions & 0 deletions lib/bushido/bushido_hooks/user_hooks.rb
@@ -0,0 +1,44 @@
class BushidoUserHooks < Bushido::EventObserver
def user_added
puts "Adding a new user with incoming data #{params.inspect}"
puts "Devise username column: #{::Devise.cas_username_column}="
puts "Setting username to: #{params['data'].try(:[], 'ido_id')}"

user = User.new(:email => params['data'].try(:[], 'email'))
user.name = user.email.split('@').first
user.initials = user.email[0..1].upcase
user.send("#{::Devise.cas_username_column}=".to_sym, params['data'].try(:[], 'ido_id'))
user.save

# Add the new user to all existing projects
Project.all.each(&:add_all_users!)
end

def user_removed
puts "Removing user based on incoming data #{params.inspect}"
puts "Devise username column: #{::Devise.cas_username_column}="

user = User.find_by_ido_id(params['data']['ido_id'])

user.try(:remove_all_projects!)

# TODO: Disable the user instead of destroying them (to prevent data loss)
user.try(:destroy)
end

def user_updated
puts "Updating user based on incoming data #{params.inspect}"
puts "Devise username column: #{::Devise.cas_username_column}="

data = params['data']
user = User.find_by_ido_id(data['ido_id'])

if user
# Re-use the CAS login method to set all the extra attributes we
# care about (first_name, last_name, email, local, timezone,
# etc.)
user.bushido_extra_attributes(data)
user.save
end
end
end
25 changes: 19 additions & 6 deletions lib/tasks/bushido.rake
@@ -1,10 +1,23 @@
namespace :bushido do
desc "Run the initial setup for a Busido app. Copies config files and seeds db."
task :install => :environment do
channel = Channel.new :name => "Lobby"
if channel.save
puts "Created the default channel"
else
puts "Error creating the default channel"
user = User.first

if user.nil?
puts "Creating default user..."
user = User.new
user.email = "#{::Bushido::Platform.name}@#{ENV['BUSHIDO_HOST']}"
user.first_name = ::Bushido::Platform.name[0..1].upcase
user.last_name = ::Bushido::Platform.name
user.ido_id = "temporary_user"
user.save!
end

channel = Channel.first

if channel.nil?
puts "Creating default channel..."
channel = Channel.create :name => "Lobby"
end
end
end
end

0 comments on commit ff36e73

Please sign in to comment.