Permalink
Browse files

Move categories from simple string to Category model. Yeah!

1 parent e362b08 commit 8df5e6a10bfe29a41e989635cce9867a128a044c @ook committed Oct 13, 2008
View
2 app/models/categories.rb
@@ -1,2 +0,0 @@
-class Categories < ActiveRecord::Base
-end
View
3 app/models/category.rb
@@ -0,0 +1,3 @@
+class Category < ActiveRecord::Base
+ has_many :tasks
+end
View
6 app/models/task.rb
@@ -1,8 +1,8 @@
require 'chronic'
class Task < ActiveRecord::Base
- has_one :category
-
+ belongs_to :category
+
attr_protected :user_id
acts_as_list :scope => :user_id
@@ -38,7 +38,7 @@ def extract_category_from_name
extracted_category, extracted_name = /^(?:\[([^\]]+)\]|)\s?(.*)$/.match(self.name)[1,2]
if extracted_category
- self.category = extracted_category
+ self.category = Category.find_or_create_by_name(extracted_category)
self.name = extracted_name
end
end
View
4 app/models/user.rb
@@ -62,7 +62,7 @@ def forget_me
end
def categories
- tasks.find(:all).collect(&:category.name).reject(&:blank?).uniq.sort
+ tasks.find(:all).collect(&:category).reject(&:blank?).uniq.collect(&:name).sort
end
protected
@@ -80,6 +80,6 @@ def password_required?
def create_sample_task
self.tasks.create :name => 'Try Donebox and be more productive than David Allen himself',
:due_on => Date.today,
- :category => 'donebox'
+ :category => Category.find_or_create_by_name('donebox')
end
end
View
2 app/views/tasks/_edit.html.erb
@@ -2,7 +2,7 @@
<%= f.text_field :name %><br/>
- <%= select :task, :category, current_user.categories, :include_blank => true %><br/>
+ <%= select :task, :category_id, Category.find(:all).collect { |c| [c.name, c.id]}, :include_blank => true %><br/>
<%= f.date_select :due_on, :start_year => Date.today.year, :include_blank => true %>
View
4 app/views/tasks/_task.html.erb
@@ -4,8 +4,8 @@
:loading => "new Effect.Puff('#{dom_id(task)}')"),
:class => 'require_js', :disabled => 'disabled' %>
<span class="handle">
- <% unless task.category.blank? %>
- <span class="category" onclick="Tasks.highlightCategories('<%=h task.category %>')"><%=h task.category %></span>
+ <% unless task.category.nil? %>
+ <span class="category" onclick="Tasks.highlightCategories('<%=h task.category.name %>')"><%=h task.category.name %></span>
<% end %>
<span class="name"><%= format_task_name task.name %></span>
</span>
View
2 db/migrate/20081002215203_create_categories.rb
@@ -7,9 +7,11 @@ def self.up
t.timestamps
end
+ add_column :tasks, :category_id, :integer
end
def self.down
+ remove_column :tasks, :category
drop_table :categories
rename_column :tasks, :legacy_cat, :category
end
View
3 db/migrate/20081004184825_remove_category_from_tasks.rb
@@ -2,7 +2,8 @@ class RemoveCategoryFromTasks < ActiveRecord::Migration
def self.up
Task.find(:all).each { |t|
if t.legacy_cat
- cat = Category.find(:name => t.legacy_cat) || Category.create(:name => t.legacy_cat)
+ cat = Category.find_or_create_by_name(t.legacy_cat)
+ puts "Category.class=#{cat.class.name}, name=#{cat.name}"
t.category = cat
t.save
end
View
0 log/developement.log
No changes.
View
78 log/development.log
@@ -0,0 +1,78 @@
+# Logfile created on Tue Jul 29 21:55:10 +0200 2008 by logger.rb/1.5.2.9
+
+
+Processing TasksController#index (for 127.0.0.1 at 2008-07-29 21:55:30) [GET]
+ Session ID: 473de0323a9889f0111e8110c59ddf2b
+ Parameters: {"action"=>"index", "controller"=>"tasks"}
+Redirected to http://localhost:3000/login
+Filter chain halted as [#<ActionController::Filters::ClassMethods::SymbolFilter:0x4163c18 @filter=:login_required>] returned false.
+Completed in 0.00010 (10000 reqs/sec) | 302 Found [http://localhost/]
+
+
+Processing SessionsController#new (for 127.0.0.1 at 2008-07-29 21:55:30) [GET]
+ Session ID: 473de0323a9889f0111e8110c59ddf2b
+ Parameters: {"action"=>"new", "controller"=>"sessions"}
+Rendering within layouts/login
+Rendering sessions/new
+Rendered layouts/_stats (0.03200)
+Completed in 0.03200 (31 reqs/sec) | Rendering: 0.03200 (100%) | 200 OK [http://localhost/login]
+
+
+Processing SessionsController#create (for 127.0.0.1 at 2008-07-29 21:55:35) [POST]
+ Session ID: 473de0323a9889f0111e8110c59ddf2b
+ Parameters: {"commit"=>"Log in", "action"=>"create", "controller"=>"sessions", "login"=>"ook", "password"=>"thorala"}
+ User Load (0.000000) SELECT * FROM users WHERE (users."login" = 'ook') LIMIT 1
+Rendering layoutfalseactionnew within layouts/login
+Rendering sessions/new
+Rendered layouts/_stats (0.00000)
+Completed in 0.68800 (1 reqs/sec) | Rendering: 0.03100 (4%) | DB: 0.00000 (0%) | 200 OK [http://localhost/sessions]
+
+
+Processing UsersController#new (for 127.0.0.1 at 2008-07-29 21:55:39) [GET]
+ Session ID: 473de0323a9889f0111e8110c59ddf2b
+ Parameters: {"action"=>"new", "controller"=>"users"}
+Rendering within layouts/login
+Rendering users/new
+Rendered layouts/_stats (0.00000)
+Completed in 0.01600 (62 reqs/sec) | Rendering: 0.01600 (100%) | 200 OK [http://localhost/signup]
+
+
+Processing UsersController#create (for 127.0.0.1 at 2008-07-29 21:55:46) [POST]
+ Session ID: 473de0323a9889f0111e8110c59ddf2b
+ Parameters: {"user"=>{"password_confirmation"=>"thorala", "login"=>"ook", "password"=>"thorala"}, "commit"=>"Sign up", "action"=>"create", "controller"=>"users"}
+ User Load (0.000000) SELECT * FROM users WHERE (LOWER(users.login) = 'ook') LIMIT 1
+ SQL (0.000000) INSERT INTO users ("salt", "updated_at", "crypted_password", "remember_token_expires_at", "remember_token", "login", "created_at", "email") VALUES('e81bbb9da240d2000cacafdb78f9c361feabc38f', '2008-07-29 21:55:46', 'd64e00f9946c6d81b5e39e390dce54ce01780c76', NULL, NULL, 'ook', '2008-07-29 21:55:46', NULL)
+ Task Load (0.000000) SELECT * FROM tasks WHERE (user_id = 1) ORDER BY position DESC LIMIT 1
+ SQL (0.000000) INSERT INTO tasks ("completed_at", "name", "category", "user_id", "position", "created_at", "due_on") VALUES(NULL, 'Try Donebox and be more productive than David Allen himself', 'donebox', 1, 1, '2008-07-29 21:55:46', '2008-07-29')
+Redirected to http://localhost:3000/
+Completed in 1.01500 (0 reqs/sec) | DB: 0.00000 (0%) | 302 Found [http://localhost/users]
+
+
+Processing TasksController#index (for 127.0.0.1 at 2008-07-29 21:55:47) [GET]
+ Session ID: 473de0323a9889f0111e8110c59ddf2b
+ Parameters: {"action"=>"index", "controller"=>"tasks"}
+ User Load (0.000000) SELECT * FROM users WHERE (users."id" = 1) LIMIT 1
+ Task Load (0.000000) SELECT * FROM tasks WHERE (tasks.user_id = 1 AND (due_on IS NOT NULL AND completed_at IS NULL)) ORDER BY due_on, position
+ Task Load (0.000000) SELECT * FROM tasks WHERE (tasks.user_id = 1 AND (due_on IS NULL AND completed_at IS NULL)) ORDER BY position
+ Task Load (0.000000) SELECT * FROM tasks WHERE (tasks.user_id = 1) 
+Cookie set: been_here=true; path=/; expires=Thu, 30 Jul 2009 01:55:47 GMT
+Rendering layoutfalsecontent_typetext/htmlactionindex within layouts/application
+Rendering tasks/index
+Rendered tasks/_first_time (0.00000)
+Rendered tasks/_task (0.01500)
+Rendered tasks/_tasks (0.03100)
+Rendered tasks/_tasks (0.00000)
+Rendered tasks/_tasks (0.00000)
+Rendered layouts/_stats (0.00000)
+Rendered layouts/_footer (0.00000)
+Completed in 0.18700 (5 reqs/sec) | Rendering: 0.07800 (41%) | DB: 0.00000 (0%) | 200 OK [http://localhost/]
+
+
+Processing TasksController#complete (for 127.0.0.1 at 2008-07-29 21:55:52) [POST]
+ Session ID: 473de0323a9889f0111e8110c59ddf2b
+ Parameters: {"action"=>"complete", "id"=>"1", "controller"=>"tasks"}
+ User Load (0.000000) SELECT * FROM users WHERE (users."id" = 1) LIMIT 1
+ Task Load (0.000000) SELECT * FROM tasks WHERE (tasks."id" = 1 AND (tasks.user_id = 1)) 
+ Task Update (0.000000) UPDATE tasks SET "created_at" = '2008-07-29 21:55:46', "category" = 'donebox', "name" = 'Try Donebox and be more productive than David Allen himself', "completed_at" = '2008-07-29 21:55:53', "due_on" = '2008-07-29 00:00:00', "position" = 1, "user_id" = 1 WHERE "id" = 1
+Rendering tasks/complete.rjs
+Completed in 0.68700 (1 reqs/sec) | Rendering: 0.01500 (2%) | DB: 0.00000 (0%) | 200 OK [http://localhost/tasks/1;complete]
View
0 log/test.log
No changes.
View
6 test/unit/task_test.rb
@@ -1,4 +1,4 @@
-require File.dirname(__FILE__) + '/../test_helper'
+ require File.dirname(__FILE__) + '/../test_helper'
class TaskTest < Test::Unit::TestCase
fixtures :tasks, :users
@@ -49,12 +49,12 @@ def test_create_with_name
def test_extract_category_from_name
task = Task.new :name => '[project] Write some tests'
task.instance_eval{extract_category_from_name}
- assert_equal 'project', task.category
+ assert_equal 'project', task.category.name
assert_equal 'Write some tests', task.name
task = Task.new :name => '[cool-project] Write some cooler tests'
task.instance_eval{extract_category_from_name}
- assert_equal 'cool-project', task.category
+ assert_equal 'cool-project', task.category.name
assert_equal 'Write some cooler tests', task.name
task = Task.new :name => 'Write some tests'

0 comments on commit 8df5e6a

Please sign in to comment.