Skip to content

Commit

Permalink
Add tztime and give each user a time zone
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://phoenix/srv/svn/webistrano/trunk@51 e1153f85-6c6c-dc11-afa8-0013d3c39b19
  • Loading branch information
jweiss committed Nov 27, 2007
1 parent 112c37f commit ebbecd4
Show file tree
Hide file tree
Showing 633 changed files with 51,058 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.txt
@@ -1,5 +1,7 @@
* SVN *

* Add tztime and give each user a time zone

* Introduce a 5 second sleep between mongrel stop and start while restarting'

* Only admins can manage recipes, hosts, and users. Normal users can only view.
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/application.rb
Expand Up @@ -4,6 +4,7 @@ class ApplicationController < ActionController::Base
include AuthenticatedSystem

before_filter :login_from_cookie, :login_required
around_filter :set_timezone

# Pick a unique cookie name to distinguish our session data from others'
session :session_key => '_webistrano_session_id'
Expand All @@ -13,6 +14,15 @@ class ApplicationController < ActionController::Base
helper_method :current_stage, :current_project

protected

def set_timezone
# default timezone is UTC (Edinburgh)
TzTime.zone = logged_in? ? current_user.tz : TimeZone['Edinburgh']
yield
TzTime.reset!
TzTime.zone = TimeZone['Edinburgh']
end

def load_project
@project = Project.find(params[:project_id])
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/configuration_parameter.rb
Expand Up @@ -6,6 +6,8 @@ class ConfigurationParameter < ActiveRecord::Base

before_validation :empty_value_if_deploy_is_set

tz_time_attributes :created_at, :updated_at

def validate
self.errors.add('value', 'must be empty if prompt on deploy is set') if (self.prompt? && !self.value.blank?)
end
Expand Down
6 changes: 4 additions & 2 deletions app/models/deployment.rb
Expand Up @@ -9,6 +9,8 @@ class Deployment < ActiveRecord::Base

attr_accessible :task, :prompt_config, :description

tz_time_attributes :created_at, :updated_at, :completed_at

# given configuration hash on create in order to satisfy prompt configurations
attr_accessor :prompt_config

Expand Down Expand Up @@ -72,7 +74,7 @@ def status_image
def complete_with_error!
raise 'cannot complete a second time' if self.completed?
self.success = 0
self.completed_at = Time.now.utc
self.completed_at = TzTime.now
self.save!

self.stage.emails.each do |email|
Expand All @@ -83,7 +85,7 @@ def complete_with_error!
def complete_successfully!
raise 'cannot complete a second time' if self.completed?
self.success = 1
self.completed_at = Time.now.utc
self.completed_at = TzTime.now
self.save!

self.stage.emails.each do |email|
Expand Down
2 changes: 2 additions & 0 deletions app/models/host.rb
Expand Up @@ -8,6 +8,8 @@ class Host < ActiveRecord::Base

attr_accessible :name

tz_time_attributes :created_at, :updated_at

before_validation :strip_whitespace

def strip_whitespace
Expand Down
2 changes: 1 addition & 1 deletion app/models/notification.rb
Expand Up @@ -11,7 +11,7 @@ def deployment(deployment, email)
@body = {:deployment => deployment}
@recipients = email
@from = @@webistrano_sender_address
@sent_on = Time.now
@sent_on = TzTime.now
@headers = {}
end
end
1 change: 1 addition & 0 deletions app/models/project.rb
Expand Up @@ -12,6 +12,7 @@ class Project < ActiveRecord::Base

attr_accessible :name, :description, :template

tz_time_attributes :created_at, :updated_at

# creates the default configuration parameters based on the template
def create_template_defaults
Expand Down
2 changes: 2 additions & 0 deletions app/models/recipe.rb
Expand Up @@ -6,4 +6,6 @@ class Recipe < ActiveRecord::Base
validates_length_of :name, :maximum => 250

attr_accessible :name, :body, :description

tz_time_attributes :created_at, :updated_at
end
2 changes: 2 additions & 0 deletions app/models/role.rb
Expand Up @@ -14,6 +14,8 @@ class Role < ActiveRecord::Base

attr_accessor :custom_name

tz_time_attributes :created_at, :updated_at

DEFAULT_NAMES = %w(app db www)

before_validation :set_name_from_custom_name
Expand Down
2 changes: 2 additions & 0 deletions app/models/stage.rb
Expand Up @@ -12,6 +12,8 @@ class Stage < ActiveRecord::Base

attr_accessible :name, :alert_emails

tz_time_attributes :created_at, :updated_at

# fake attr (Hash) that hold info why deployment is not possible
# (think model.errors lite)
attr_accessor :deployment_problems
Expand Down
9 changes: 6 additions & 3 deletions app/models/user.rb
Expand Up @@ -5,7 +5,8 @@ class User < ActiveRecord::Base
# Virtual attribute for the unencrypted password
attr_accessor :password

attr_accessible :login, :email, :password, :password_confirmation
attr_accessible :login, :email, :password, :password_confirmation, :time_zone, :tz
composed_of :tz, :class_name => 'TZInfo::Timezone', :mapping => %w( time_zone time_zone )

validates_presence_of :login, :email
validates_presence_of :password, :if => :password_required?
Expand All @@ -17,6 +18,8 @@ class User < ActiveRecord::Base
validates_uniqueness_of :login, :email, :case_sensitive => false
before_save :encrypt_password

tz_time_attributes :created_at, :updated_at

def validate_on_update
if User.find(self.id).admin? && !self.admin?
errors.add('admin', 'status can no be revoked as there needs to be one admin left.') if User.admin_count == 1
Expand Down Expand Up @@ -44,7 +47,7 @@ def authenticated?(password)
end

def remember_token?
remember_token_expires_at && Time.now.utc < remember_token_expires_at
remember_token_expires_at && TzTime.now < remember_token_expires_at
end

# These create and unset the fields required for remembering users between browser closes
Expand Down Expand Up @@ -94,7 +97,7 @@ def recent_deployments(limit=3)
# before filter
def encrypt_password
return if password.blank?
self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{login}--") if new_record?
self.salt = Digest::SHA1.hexdigest("--#{TzTime.now.to_s}--#{login}--") if new_record?
self.crypted_password = encrypt(password)
end

Expand Down
5 changes: 5 additions & 0 deletions app/views/users/_form.rhtml
Expand Up @@ -19,6 +19,11 @@
<%= password_field 'user', 'password_confirmation', :style =>'width:330px;' %>
</p>

<p>
<b>Timezone</b><br />
<%= time_zone_select 'user', 'time_zone', TZInfo::Timezone.all.sort, {:model => TZInfo::Timezone},{:style => "width:330px;"} %>
</p>

<% if logged_in? && current_user.admin? -%>
<p>
<b>Admin?</b><br />
Expand Down
4 changes: 4 additions & 0 deletions config/environment.rb
Expand Up @@ -92,3 +92,7 @@

# delete cached stylesheet on boot in order to delete stale versions
File.delete("#{RAILS_ROOT}/public/stylesheets/application.css") if File.exists?("#{RAILS_ROOT}/public/stylesheets/application.css")

# set default time_zone to UTC
ENV['TZ'] = 'UTC'
TzTime.zone = TimeZone['Edinburgh']
14 changes: 14 additions & 0 deletions db/migrate/022_add_time_zone_to_user.rb
@@ -0,0 +1,14 @@
class User < ActiveRecord::Base
end

class AddTimeZoneToUser < ActiveRecord::Migration
def self.up
add_column :users, :time_zone, :string, :default => 'UTC'
User.reset_column_information
User.update_all("time_zone = 'UTC'") # in order to set for PGSQL
end

def self.down
remove_column :users, :time_zone
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -2,7 +2,7 @@
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.

ActiveRecord::Schema.define(:version => 21) do
ActiveRecord::Schema.define(:version => 22) do

create_table "configuration_parameters", :force => true do |t|
t.string "name"
Expand Down Expand Up @@ -95,6 +95,7 @@
t.string "remember_token"
t.datetime "remember_token_expires_at"
t.integer "admin", :default => 0
t.string "time_zone", :default => "UTC"
end

end

0 comments on commit ebbecd4

Please sign in to comment.