Skip to content

Commit

Permalink
Generalized categorization (working for account and contacts)
Browse files Browse the repository at this point in the history
  • Loading branch information
jose-gordo committed Sep 2, 2009
1 parent 8382b94 commit 0c50e3d
Show file tree
Hide file tree
Showing 48 changed files with 220 additions and 80 deletions.
@@ -1,11 +1,12 @@
class CategoriesController < ApplicationController
before_filter :set_current_tab, :only => [ :index, :show ]
class Admin::CategoriesController < Admin::ApplicationController
unloadable
before_filter :require_user
before_filter :set_current_tab

# GET /categories
# GET /categories.xml
def index
@categories = Category.all
@all_categories = Category.find(:all, :order=>"parent_id, name")
@categories = CategoryAccount.all

respond_to do |format|
format.html # index.html.erb
Expand All @@ -17,7 +18,7 @@ def index
# GET /categories/1.xml
def show
@category = Category.find(params[:id])
@all_categories = Category.find(:all, :order=>"parent_id, name")
@all_categories = Category.find(:all)

respond_to do |format|
format.html # show.html.erb
Expand All @@ -29,7 +30,7 @@ def show
# GET /categories/new.xml
def new
@category = Category.new
@all_categories = Category.find(:all, :order=>"parent_id, name")
@all_categories = Category.find(:all)

respond_to do |format|
format.html # new.html.erb
Expand All @@ -40,14 +41,14 @@ def new
# GET /categories/1/edit
def edit
@category = Category.find(params[:id])
@all_categories = Category.find(:all, :order=>"parent_id, name")
@all_categories = Category.find(:all)
end

# POST /categories
# POST /categories.xml
def create
@category = Category.new(params[:category])
@all_categories = Category.find(:all, :order=>"parent_id, name")
@all_categories = Category.find(:all)

respond_to do |format|
if @category.save
Expand All @@ -65,7 +66,7 @@ def create
# PUT /categories/1.xml
def update
@category = Category.find(params[:id])
@all_categories = Category.find(:all, :order=>"parent_id, name")
@all_categories = Category.find(:all)

respond_to do |format|
if @category.update_attributes(params[:category])
Expand Down
44 changes: 42 additions & 2 deletions app/helpers/categorization_helper.rb
Expand Up @@ -9,9 +9,49 @@ def show_categories(categories)

output
end

def all_categories()
Category.find(:all, :order => "name")
case controller.controller_name
when "accounts"
catmodel = "CategoryAccount"
when "contacts"
catmodel = "CategoryContact"
end

catmodel.constantize.find(:all)
end

def get_category_model_ids()
case controller.controller_name
when "accounts"
ref = "category_account_ids"
when "contacts"
ref = "category_contact_ids"
end
#'category_account_ids'
ref
end

def get_category_instance_variable()
case controller.controller_name
when "accounts"
ins = "@account.category_accounts"
when "contacts"
ins = "@contact.category_contacts"
end
ins
#@account.category_accounts
#instance_variable_get(get_category_instance_variable).send)
end

def get_category_model()
case controller.controller_name
when "accounts"
model = "account"
when "contacts"
model = "contact"
end
model
end

end
15 changes: 0 additions & 15 deletions app/helpers/tabs_helper.rb

This file was deleted.

3 changes: 1 addition & 2 deletions app/models/account_category.rb
@@ -1,5 +1,4 @@
class AccountCategory < ActiveRecord::Base
belongs_to :account
belongs_to :category
validates_presence_of :account_id, :category_id
belongs_to :category_account
end
4 changes: 2 additions & 2 deletions app/models/account_category_associations.rb
Expand Up @@ -2,8 +2,8 @@ module AccountCategoryAssociations

def self.included(base)
base.class_eval do
has_many :account_categories, :dependent => :destroy
has_many :categories, :through => :account_categories, :uniq => true
has_many :account_categories, :dependent => :destroy
has_many :category_accounts, :through => :account_categories, :uniq => true
end
end

Expand Down
52 changes: 21 additions & 31 deletions app/models/category.rb
@@ -1,41 +1,31 @@
class Category < ActiveRecord::Base
has_many :accounts, :through => :account_categories
has_many :account_categories, :dependent => :destroy
acts_as_tree :order => "name"

def self.root_nodes
find(:all, :conditions => 'parent_id IS NULL')
end

def self.find_children(start_id = nil)
start_id.to_i == 0 ? root_nodes : find(start_id).direct_children
end

def leaf
unknown? || children_count == 0
end

def to_json_with_leaf(options = {})
self.to_json_without_leaf(options.merge(:methods => :leaf))
end

alias_method_chain :to_json, :leaf

def ancestors_name
if parent
parent.ancestors_name + parent.name + ':'
parent.ancestors_name + parent.text + ' - '
else
""
end
end
def long_name
ancestors_name + name
end
ancestors_name + text
end
end

# has_many :account_categories, :dependent => :destroy
# has_many :categories, :through => :account_categories, :uniq => true
# belongs_to :user
# belongs_to :account
# belongs_to :assignee, :class_name => "User", :foreign_key => :assigned_to
# has_one :account_issue, :dependent => :destroy
# has_one :account, :through => :account_issue
# has_many :activities, :as => :subject, :order => 'created_at DESC'
#
# simple_column_search :name, :match => :middle, :escape => lambda { |query| query.gsub(/[^\w\s\-]/, "").strip }
#
# named_scope :with_ticket, lambda { |ticket| { :conditions => ["bug_ticket LIKE ?", ticket] } }
# named_scope :with_priority, lambda { |priority| { :conditions => ["priority LIKE ?", priority] } }
# named_scope :only_priorities, lambda { |filters| { :conditions => [ "priority IN (?)", filters ] } }
# named_scope :only_statuses, lambda { |filters| { :conditions => [ "status IN (?)", filters ] } }
#
# named_scope :with_status, lambda { |status| { :conditions => ["status = ?", status] } }
# named_scope :unresolved, :conditions => [ "status = ?", 0 ]
# named_scope :pending, :conditions => [ "status IS IN ?", [0,1] ]
# named_scope :resolved, :conditions => [ "status = ?", 2 ]
#
# uses_user_permissions
# acts_as_commentable
# acts_as_paranoid
6 changes: 6 additions & 0 deletions app/models/category_account.rb
@@ -0,0 +1,6 @@
class CategoryAccount < Category
has_many :accounts, :through => :account_categories
has_many :account_categories

acts_as_nested_set
end
6 changes: 6 additions & 0 deletions app/models/category_contact.rb
@@ -0,0 +1,6 @@
class CategoryContact < Category
has_many :contacts, :through => :contact_categories
has_many :contact_categories

acts_as_nested_set
end
4 changes: 4 additions & 0 deletions app/models/contact_category.rb
@@ -0,0 +1,4 @@
class ContactCategory < ActiveRecord::Base
belongs_to :contact
belongs_to :category_contact
end
10 changes: 10 additions & 0 deletions app/models/contact_category_associations.rb
@@ -0,0 +1,10 @@
module ContactCategoryAssociations

def self.included(base)
base.class_eval do
has_many :contact_categories, :dependent => :destroy
has_many :category_contacts, :through => :contact_categories, :uniq => true
end
end

end
File renamed without changes.
20 changes: 20 additions & 0 deletions app/views/admin/categories/.tmp_index.html.erb.62276~
@@ -0,0 +1,20 @@
<h2>Listing categories</h2>

<%= link_to 'New category', new_category_path %>

<br />

<table>
<tr>
<th>Name</th>
</tr>

<% @categories.each do |category| %>
<tr>
<td><%=h category.text %></td>
<td><%= link_to 'Show', category %></td>
<td><%= link_to 'Edit', edit_category_path(category) %></td>
<td><%= link_to 'Destroy', category, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
2 changes: 2 additions & 0 deletions app/views/admin/categories/.tmp_index.html.erb.70842~
@@ -0,0 +1,2 @@
<h2>Listing categories</h2>

File renamed without changes.
10 changes: 10 additions & 0 deletions app/views/admin/categories/Copy of _select_categories.rhtml
@@ -0,0 +1,10 @@
<%=
collection_select('account',
'category_ids',
all_categories,
:id,
:long_name,
{},
{:multiple => true, :size => '10'}
)
%>
6 changes: 6 additions & 0 deletions app/views/admin/categories/_categories.html.haml
@@ -0,0 +1,6 @@
%br
.subtitle#create_issue_title Categories
.remote#create_issue{ hidden }
.list#issues

= collection_select get_category_model, get_category_model_ids, all_categories, :id, :long_name, { }, { :multiple => true, :size => '10' }
6 changes: 6 additions & 0 deletions app/views/admin/categories/_show_categories.html.haml
@@ -0,0 +1,6 @@
%br
.subtitle#create_issue_title Categories
.remote#create_issue{ hidden }
.list#issues

= show_categories(eval(get_category_instance_variable))
20 changes: 20 additions & 0 deletions app/views/admin/categories/edit.html.erb
@@ -0,0 +1,20 @@
<h1>Editing category</h1>

<% form_for(@category) do |f| %>
<%= f.error_messages %>

<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :parent_id %><br />
<%= render(:partial => "select_categories") %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Show', {:admin, @category} %> |
<%= link_to 'Back', category_path %>
10 changes: 10 additions & 0 deletions app/views/admin/categories/index.html.erb
@@ -0,0 +1,10 @@
<h2>Listing categories</h2>

<table>
<% @categories.each do |category| %>
<tr>
<td><%=h category.text %></td>
</tr>
<% end %>
</table>

File renamed without changes.
File renamed without changes.
@@ -1,8 +1,11 @@
class CreateCategories < ActiveRecord::Migration
def self.up
create_table :categories do |t|
t.string :name
t.integer :parent_id
t.integer :lft
t.integer :rgt
t.string :text
t.string :type

t.timestamps
end
Expand All @@ -11,4 +14,4 @@ def self.up
def self.down
drop_table :categories
end
end
end
16 changes: 16 additions & 0 deletions db/migrate/20090901135750_create_contact_categories.rb
@@ -0,0 +1,16 @@
class CreateContactCategories < ActiveRecord::Migration
def self.up
create_table :contact_categories, :force => true do |t|
t.integer :contact_id
t.integer :category_contact_id
t.datetime :deleted_at
t.timestamps
end
add_index :contact_categories, [:contact_id, :category_contact_id]
add_index :contact_categories, :category_contact_id
end

def self.down
drop_table :contact_categories
end
end
@@ -1,13 +1,13 @@
class CreateAccountCategories < ActiveRecord::Migration
def self.up
create_table :account_categories, :force => true do |t|
t.references :account
t.references :category
t.integer :account_id
t.integer :category_account_id
t.datetime :deleted_at
t.timestamps
end
add_index :account_categories, [:account_id, :category_id]
add_index :account_categories, :category_id
add_index :account_categories, [:account_id, :category_account_id]
add_index :account_categories, :category_account_id
end

def self.down
Expand Down
4 changes: 4 additions & 0 deletions init.rb
Expand Up @@ -6,8 +6,12 @@
version "0.1"
description "Categorization System based on tags"
dependencies :haml, :simple_column_search, :uses_user_permissions
tab :admin do |tabs|
tabs.insert(1, { :text => "Categories", :url => { :controller => "categories" } })
end
end

# Require the actual code after all plugin dependencies have been resoled.
require "crm_categorization"
require "show_account_hook"
#require "top_section_account_hook.rb"

0 comments on commit 0c50e3d

Please sign in to comment.