Skip to content

Commit

Permalink
Wow ... lots of things
Browse files Browse the repository at this point in the history
  • Loading branch information
Gimi Liang committed Dec 15, 2009
1 parent 5f874f0 commit e381f84
Show file tree
Hide file tree
Showing 76 changed files with 1,158 additions and 262 deletions.
20 changes: 11 additions & 9 deletions Gemfile
Expand Up @@ -9,29 +9,31 @@ disable_system_gems

gem 'rack', '>= 1.0.1'
gem 'rails', '2.3.5'
gem 'activerecord', '2.3.5', :require_as => 'active_record' # for: paperclip, preferences, will_paginate
gem 'actionpack', '2.3.5', :require_as => ['action_pack', 'action_controller'] # for: haml, will_paginate

gem 'mysql', except: :test
gem 'activerecord', '2.3.5', :require_as => 'active_record' # require here or paperclip, preferences cannot find ActiveRecord!!
gem 'mysql', :except => :test
gem 'thoughtbot-paperclip', '>= 2.3.1', :require_as => 'paperclip'
gem 'state_machine', '>= 0.8.0'
gem 'preferences', '>= 0.3.1'
#gem 'matthuhiggins-foreigner', '>= 0.2.1', require_as: 'foreigner', only: :install
gem 'will_paginate', '>= 2.3.11'
#gem 'matthuhiggins-foreigner', '>= 0.2.1', :require_as => 'foreigner', :only => :install

gem 'actionpack', '2.3.5', :require_as => ['action_pack', 'action_controller'] # haml needs ActionView
gem 'haml', '>= 2.2.10'
gem 'compass', '>= 0.8.17'
gem 'mime-types', '>= 1.16', :require_as => 'mime/types'
gem 'bluecloth', '>= 2.0.5'

gem 'devise', '>= 0.4.3', only: :load_in_environment_rb
gem 'devise', '>= 0.6.3', :only => :load_in_environment_rb
#gem 'devise', '= 0.6.3', :only => :load_in_environment_rb
gem 'net-ldap', '>= 0.0.5', :require_as => 'net/ldap'

# gem 'thin', '>= 1.2.5', only: :install
# gem 'thin', '>= 1.2.5', :only => :install

only :test do
gem 'sqlite3-ruby', '>= 1.2.5', require_as: 'sqlite3'
gem 'thoughtbot-shoulda', '>= 2.10.2', require_as: 'shoulda'
gem 'machinist', '>= 1.0.5', require_as: ['machinist/active_record', 'sham']
gem 'sqlite3-ruby', '>= 1.2.5', :require_as => 'sqlite3'
gem 'thoughtbot-shoulda', '>= 2.10.2', :require_as => 'shoulda'
gem 'machinist', '>= 1.0.5', :require_as => ['machinist/active_record', 'sham']
end

bin_path "script/gems"
6 changes: 3 additions & 3 deletions app/controllers/admin/configurations_controller.rb
Expand Up @@ -7,10 +7,10 @@ def show
end

def update
if @configuration.set(params[@configuration.name])
flash[:success] = '.success'
if @configuration.set(params[@configuration.name], current_user.email)
flash[:success] = 'admin.configurations.update.success'
else
flash[:failure] = '.failure'
flash[:failure] = 'admin.configurations.update.failure'
end

redirect_to admin_root_path
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -11,6 +11,16 @@ class ApplicationController < ActionController::Base
before_filter :authenticate_user!
before_filter :set_locale

# a convenient helper to get page param.
# @return [Integer] page number
def page
(params[:page] || 1).to_i
end

def per_page
Configuration::Application.get('per_page', current_user.email)
end

private
def set_locale
I18n.locale = params[:locale] unless params[:locale].blank?
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/index_controller.rb
Expand Up @@ -7,4 +7,7 @@ def index
flash.discard # remove login messages
end

def search
end

end
40 changes: 35 additions & 5 deletions app/controllers/positions_controller.rb
Expand Up @@ -3,7 +3,7 @@ class PositionsController < ApplicationController
# GET /positions
def index
@categories = Category.all :order => :name
@positions = Position.all :include => [:category, :profiles], :order => 'id DESC'
@positions = Position.active.with_details.in_creation_order.paginate :page => page, :per_page => per_page
end

# POST /positions
Expand All @@ -14,7 +14,7 @@ def create
position.creator = current_user

respond_to do |format|
if position.save
if position.save!
format.json {
render(
json: {
Expand All @@ -36,15 +36,45 @@ def create
end
end

# GET /positions/1/edit
def edit
@position = Position.active.find params[:id]
@categories = Category.all :order => :name
render :partial => true if request.xhr?
end

# POST /positions/1
# POST /positions/1.json
def update
position = Position.active.find params[:id]
respond_to do |format|
if position.update_attributes params[:position]
format.json do
render :json => {
:position => {
:id => position.id,
:name => position.name,
:category => position.category.name,
:state => position.state,
:description => position.description
}
}
end
else
format.json { render :json => position.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /positions/1
# DELETE /positions/1.json
def destroy
@position = Position.find(params[:id])
@position.destroy
@position = Position.active.find(params[:id])
@position.close

respond_to do |format|
format.html { redirect_to(positions_url) }
format.json { head :ok }
format.json { render :json => {:redirect_to => positions_url} }
end
end
end
8 changes: 5 additions & 3 deletions app/controllers/profiles_controller.rb
Expand Up @@ -3,7 +3,9 @@ class ProfilesController < ApplicationController
before_filter :find_position

def index
@profiles = @position.profiles
@profiles = (
params[:show_all].blank? ? @position.not_closed_profiles : @position.profiles
).paginate(:page => page, :per_page => per_page)
end

# GET /profiles/new
Expand Down Expand Up @@ -78,10 +80,10 @@ def shift
def find_position
@position =
if params[:position_id]
Position.find params[:position_id]
Position.active.find params[:position_id]
elsif params[:profile] and (id = params[:profile][:position_id])
# So that raises an exception if position doesn't exist.
Position.find id
Position.active.find id
end
end

Expand Down
7 changes: 0 additions & 7 deletions app/helpers/admin/configuration_helper.rb

This file was deleted.

8 changes: 2 additions & 6 deletions app/helpers/admin_helper.rb
@@ -1,10 +1,6 @@
module AdminHelper
def render_configuration_partial conf
case conf
when Configuration::ProfileStatus
render :partial => 'admin/configurations/profile_status', :locals => {:configuration => conf}
else
render :partial => 'admin/configurations/edit', :locals => {:configuration => conf}
end
partial = conf.name.demodulize.underscore
render :partial => "admin/configurations/#{partial}", :locals => {:configuration => conf}
end
end
28 changes: 11 additions & 17 deletions app/helpers/profiles_helper.rb
Expand Up @@ -6,29 +6,23 @@ def sidebar_picture_for(profile)
end

def profile_state_select_tag(options={})
name = options.delete(:name) || 'profile[state]'
option_tags = options_for_select Configuration.group('ProfileStatus').preferred_statuses
select_tag name, option_tags, options
select_tag(options[:name] || 'profile[state]', option_tags, options)
end

def profile_events_links(profile)
unless (events = profile.state_events).blank?
content_tag 'ul', :class => 'horizontal' do
links = profile_event_link profile, events.shift
events.each do |event|
links << content_tag('li', '|', :class => 'separator')
links << profile_event_link(profile, event)
end
links
def assignable_users_select_tag(selected=nil, options={})
option_tags =
Configuration.group('LDAP').authorized_users.sort_by { |u| u.displayname.first }.
inject('') do |options, entry|
name, email = entry.displayname.first, entry.mail.first
options << %Q|<option value="#{email},#{name}"#{' selected="selected"' if name == selected}>#{name}</option>|
end
end
select_tag(options[:name] || 'profile[assign_to]', option_tags, options)
end

private
def profile_event_link(profile, event)
content_tag 'li' do
link_to event, handle_profile_path(:id => profile.id, :event => event), :class => 'event'
end
def assign_info(profile)
"#{h(profile.assign_to || 'nobody')} " \
"(#{(profile.assigned_at || profile.updated_at).distance})"
end

end
2 changes: 1 addition & 1 deletion app/models/category.rb
Expand Up @@ -3,7 +3,7 @@ class Category < ActiveRecord::Base
validates_presence_of :name
validates_length_of :name, :maximum => 255
validates_uniqueness_of :name
validates_length_of :description, :maximum => 1000
validates_length_of :description, :maximum => 1000, :allow_nil => true

has_many :positions, :dependent => :destroy

Expand Down
74 changes: 49 additions & 25 deletions app/models/configuration.rb
Expand Up @@ -2,8 +2,14 @@
# Inherit from this class instead of using it to group configurations.
class Configuration < ActiveRecord::Base

autoload :Application, 'configuration/application'
autoload :ProfileStatus, 'configuration/profile_status'
autoload :LDAP, 'configuration/ldap' # Ldap looks ugly

# does this configuration support group?
cattr_accessor :support_group
self.support_group = false

# Making SIT classes to be singleton classes will course many problems,
# so, just use these class properly, don't create/find/update/destroy them by hands.
# Always use their `instance` method.
Expand All @@ -14,25 +20,16 @@ class Configuration < ActiveRecord::Base
validates_uniqueness_of :name

class << self
def groups
@groups ||= []
end
def support_group!; self.support_group = true end

def groups; @groups ||= [] end

def inherited subclass
super
groups << subclass
end

def instance
return @instance if @instance
return nil unless ActiveRecord::Base.connection.tables.include?('configurations')
@instance ||= find_or_create_by_name(group_name)
@instance
end

def all_groups
groups.map(&:instance)
end
def all_groups; groups.map(&:instance) end

# Get/Set configuration name.
#
Expand All @@ -43,58 +40,85 @@ def all_groups
#
# @override group_name
# Get the group name, defaults the last part of the class name.
def group_name name = nil
def group_name(name=nil)
if name
@group = name
else
@group ||= self.name.split('::').last
end
end

def group name
def group(name)
g = groups.detect { |g| g.group_name == name }
g ? g.instance : nil
end

def get(key = nil)
def instance
return @instance if @instance
return nil unless ActiveRecord::Base.connection.tables.include?('configurations')
@instance ||= find_or_create_by_name(group_name)
@instance
end

def get(key=nil, group=nil)
key = key.to_s if key.is_a?(Symbol)
return nil unless config = self.instance
config.get key
config.get key, group
end
alias_method :[], :get

# Set the preferences as specified in a hash (like params[:preferences] from a post request)
def set(preferences={})
self.instance.set preferences
def set(preferences={}, group=nil)
self.instance.set preferences, group
end

alias_method :[], :get
def release(group=nil)
self.instance.release group
end
end

def get key = nil
def support_group?
self.class.support_group
end

def get(key=nil, group=nil)
group = nil if not support_group?
# preferences will be cached under the name of the class including this module (ex. Configuration::LDAP)
prefs = Rails.cache.fetch(self.class.to_s) { self.preferences }
prefs = Rails.cache.fetch(cache_key(group)) { self.preferences(group) }
return prefs if key.nil?
prefs[key]
end

def set preferences = {}
def set(preferences={}, group=nil)
group = nil if not support_group?
preferences.each do |key, value|
set_preference(key, value)
set_preference(key, value, group)
end
if save
Rails.cache.delete(self.class.to_s) { self.preferences }
Rails.cache.delete(cache_key(group)) { self.preferences(group) }
true
else
false
end
end

def release(group=nil)
group = nil if not support_group?
Rails.cache.delete cache_key(group)
end

private
def cache_key(group)
group ? "#{self.class.to_s}.#{group.to_s}" : self.class.to_s
end

end

# Rails loads constants lazily, in order to make
# Configuration.configurations work predictably,
# require Configuration subclasses here manually.
#
# ** Loading order matters
Configuration::Application
Configuration::ProfileStatus
Configuration::LDAP
11 changes: 11 additions & 0 deletions app/models/configuration/application.rb
@@ -0,0 +1,11 @@
class Configuration::Application < Configuration
support_group!

preference :per_page, :integer, :default => 20
preference :fold_positions, :boolean, :default => true

def set(preferences={}, group=nil)
preferences[:fold_positions] = !!preferences[:fold_positions]
super
end
end

0 comments on commit e381f84

Please sign in to comment.