Skip to content

Commit ff36e73

Browse files
committed
Adds Bushido bootstrap and hooks. Readies the app for non-Bushido platforms.
Signed-off-by: Akash Manohar J <[email protected]>
1 parent a6ed5c7 commit ff36e73

File tree

11 files changed

+153
-26
lines changed

11 files changed

+153
-26
lines changed

app/assets/javascripts/backbone/widgets.js.coffee

+1-9
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,7 @@ class Kandan.Widgets
1111
$.each @widgets, (widget_namespace, el_name)=>
1212
@init(widget_namespace)
1313

14-
@template: _.template '''
15-
<div class="widget" id="widget_<%= element_id %>">
16-
<div class="widget_header">
17-
<h3 style="background-image:url(<%= icon_url %>);"><%= title %></h3>
18-
</div>
19-
<div class="widget_content" id="<%= element_id %>"></div>
20-
<div class="widget_action_bar"></div>
21-
</div>
22-
'''
14+
@template: JST['widget']
2315

2416
@init: (widget_namespace)->
2517
widget = eval(widget_namespace)

app/assets/templates/widget.jst.eco

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="widget" id="widget_<%= @element_id %>">
2+
<div class="widget_header">
3+
<h3 style="background-image:url(<%= @icon_url %>);"><%= @title %></h3>
4+
</div>
5+
<div class="widget_content" id="<%= @element_id %>"></div>
6+
<div class="widget_action_bar"></div>
7+
</div>

app/models/user.rb

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ class User < ActiveRecord::Base
44
before_save :ensure_authentication_token
55
before_save :ensure_gravatar_hash
66

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

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

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

23+
def active_for_authentication?
24+
super && active?
25+
end
26+
2427
end

config/initializers/backbone.rb

-1
This file was deleted.

config/initializers/kandan.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
require "#{Rails.root}/lib/kandan_config.rb"
2+
13
Dir["#{Rails.root}/lib/broadcasters/**/*.rb"].each do |file|
24
require file
35
end
46

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

8+
module Kandan
9+
def self.devise_modules
10+
standard = [:database_authenticatable, :registerable, :confirmable,
11+
:recoverable, :rememberable, :trackable, :validatable]
12+
bushido = [:bushido_authenticatable, :trackable, :token_authenticatable]
13+
14+
::Bushido::Platform.on_bushido? ? bushido : standard
15+
end
16+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddActiveToUsers < ActiveRecord::Migration
2+
def change
3+
add_column :users, :active, :boolean, :default => true
4+
end
5+
end

db/schema.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended to check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(:version => 20120322155347) do
14+
ActiveRecord::Schema.define(:version => 20120404114631) do
1515

1616
create_table "activities", :force => true do |t|
1717
t.text "content"
@@ -51,8 +51,8 @@
5151
add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at"
5252

5353
create_table "users", :force => true do |t|
54-
t.string "email", :default => "", :null => false
55-
t.string "encrypted_password", :limit => 128, :default => "", :null => false
54+
t.string "email", :default => "", :null => false
55+
t.string "encrypted_password", :limit => 128, :default => "", :null => false
5656
t.string "reset_password_token"
5757
t.datetime "reset_password_sent_at"
5858
t.datetime "remember_created_at"
@@ -66,9 +66,10 @@
6666
t.text "last_name"
6767
t.text "ido_id"
6868
t.string "locale"
69-
t.datetime "created_at", :null => false
70-
t.datetime "updated_at", :null => false
69+
t.datetime "created_at", :null => false
70+
t.datetime "updated_at", :null => false
7171
t.text "gravatar_hash"
72+
t.boolean "active", :default => true
7273
end
7374

7475
add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token", :unique => true

lib/bushido/bushido_bootstrap.rb

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
module Kandan
2+
module Bushido
3+
def self.enable_bushido!
4+
self.load_hooks!
5+
self.extend_user!
6+
self.extend_project!
7+
end
8+
9+
def self.extend_user!
10+
puts "Extending the User model"
11+
User.instance_eval do
12+
validates_presence_of :ido_id
13+
validates_uniqueness_of :ido_id
14+
15+
attr_accessible :ido_id
16+
end
17+
18+
User.class_eval do
19+
def bushido_extra_attributes(extra_attributes)
20+
self.first_name = "#{extra_attributes['first_name']}"
21+
self.last_name = "#{extra_attributes['last_name']}"
22+
self.locale = extra_attributes['locale']
23+
self.email = extra_attributes["email"]
24+
end
25+
end
26+
end
27+
28+
def self.load_hooks!
29+
Dir["#{Dir.pwd}/lib/bushido/**/*.rb"].each { |file| load file }
30+
end
31+
32+
end
33+
end
34+
35+
if Bushido::Platform.on_bushido?
36+
class BushidoRailtie < Rails::Railtie
37+
config.to_prepare do
38+
puts "Enabling Bushido"
39+
Fulcrum::Bushido.enable_bushido!
40+
puts "Finished enabling Bushido"
41+
end
42+
end
43+
end
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class BushidoAppHooks < Bushido::EventObserver
2+
# This is here for eventual lazy-registration. Create a fake user on
3+
# rake bushido:install, and when the app is claimed, we'll get this
4+
# event with the user info, so we convert the existing user and
5+
# update their info here.
6+
def app_claimed
7+
User.order("created_at").first.update_attributes(:email => params['data']['email'],
8+
:ido_id => params['data']['ido_id'])
9+
end
10+
end
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class BushidoUserHooks < Bushido::EventObserver
2+
def user_added
3+
puts "Adding a new user with incoming data #{params.inspect}"
4+
puts "Devise username column: #{::Devise.cas_username_column}="
5+
puts "Setting username to: #{params['data'].try(:[], 'ido_id')}"
6+
7+
user = User.new(:email => params['data'].try(:[], 'email'))
8+
user.name = user.email.split('@').first
9+
user.initials = user.email[0..1].upcase
10+
user.send("#{::Devise.cas_username_column}=".to_sym, params['data'].try(:[], 'ido_id'))
11+
user.save
12+
13+
# Add the new user to all existing projects
14+
Project.all.each(&:add_all_users!)
15+
end
16+
17+
def user_removed
18+
puts "Removing user based on incoming data #{params.inspect}"
19+
puts "Devise username column: #{::Devise.cas_username_column}="
20+
21+
user = User.find_by_ido_id(params['data']['ido_id'])
22+
23+
user.try(:remove_all_projects!)
24+
25+
# TODO: Disable the user instead of destroying them (to prevent data loss)
26+
user.try(:destroy)
27+
end
28+
29+
def user_updated
30+
puts "Updating user based on incoming data #{params.inspect}"
31+
puts "Devise username column: #{::Devise.cas_username_column}="
32+
33+
data = params['data']
34+
user = User.find_by_ido_id(data['ido_id'])
35+
36+
if user
37+
# Re-use the CAS login method to set all the extra attributes we
38+
# care about (first_name, last_name, email, local, timezone,
39+
# etc.)
40+
user.bushido_extra_attributes(data)
41+
user.save
42+
end
43+
end
44+
end

lib/tasks/bushido.rake

+19-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
namespace :bushido do
2+
desc "Run the initial setup for a Busido app. Copies config files and seeds db."
23
task :install => :environment do
3-
channel = Channel.new :name => "Lobby"
4-
if channel.save
5-
puts "Created the default channel"
6-
else
7-
puts "Error creating the default channel"
4+
user = User.first
5+
6+
if user.nil?
7+
puts "Creating default user..."
8+
user = User.new
9+
user.email = "#{::Bushido::Platform.name}@#{ENV['BUSHIDO_HOST']}"
10+
user.first_name = ::Bushido::Platform.name[0..1].upcase
11+
user.last_name = ::Bushido::Platform.name
12+
user.ido_id = "temporary_user"
13+
user.save!
14+
end
15+
16+
channel = Channel.first
17+
18+
if channel.nil?
19+
puts "Creating default channel..."
20+
channel = Channel.create :name => "Lobby"
821
end
922
end
10-
end
23+
end

0 commit comments

Comments
 (0)