Skip to content

Commit

Permalink
added result_created_at field to BrandResult to hold the result's cre…
Browse files Browse the repository at this point in the history
…ated_at timestap
  • Loading branch information
cristi committed Jul 5, 2010
1 parent e7470b4 commit 11711ce
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 29 deletions.
8 changes: 4 additions & 4 deletions TODO
Expand Up @@ -27,8 +27,8 @@ results:
- Jeff's notes

Optimizations:
- add direct has many association between team and brand result (now it goes through brand)
+ add direct has many association between team and brand result (now it goes through brand)

- Querying brand results
- add result_created_at field to BrandResult to hold the result's created_at timestap;
- when linking brands with results, create the BrandResult explicitly and set the result_created_at timestamp
+ Querying brand results
+ add result_created_at field to BrandResult to hold the result's created_at timestap;
+ when linking brands with results, create the BrandResult explicitly and set the result_created_at timestamp
2 changes: 1 addition & 1 deletion app/controllers/brand_results_controller.rb
Expand Up @@ -9,7 +9,7 @@ def index
@brand_results = @search.paginate(
:page => params[:page],
:include => :result,
:order => "#{Result.table_name}.created_at DESC")
:order => "result_created_at DESC")
end

def show
Expand Down
21 changes: 11 additions & 10 deletions app/models/brand_result.rb
Expand Up @@ -2,16 +2,17 @@
#
# Table name: brand_results
#
# id :integer(4) not null, primary key
# brand_id :integer(4) indexed, indexed => [result_id]
# result_id :integer(4) indexed, indexed => [brand_id]
# created_at :datetime
# updated_at :datetime
# state :string(255) indexed
# comments_count :integer(4) default(0)
# temperature :integer(4) indexed
# read :boolean(1) default(FALSE), indexed
# team_id :integer(4) indexed
# id :integer(4) not null, primary key
# brand_id :integer(4) indexed, indexed => [result_id]
# result_id :integer(4) indexed, indexed => [brand_id]
# created_at :datetime
# updated_at :datetime
# state :string(255) indexed
# comments_count :integer(4) default(0)
# temperature :integer(4) indexed
# read :boolean(1) default(FALSE), indexed
# team_id :integer(4) indexed
# result_created_at :datetime indexed
#

class BrandResult < ActiveRecord::Base
Expand Down
2 changes: 1 addition & 1 deletion app/models/result.rb
Expand Up @@ -21,6 +21,6 @@ def twitter?
end

def add_brand(brand)
self.brand_results.create(:brand => brand, :team => brand.team) unless brands.include?(brand)
brand_results.create(:brand => brand, :team => brand.team, :result_created_at => created_at) unless brands.include?(brand)
end
end
17 changes: 17 additions & 0 deletions db/migrate/20100705085415_add_result_created_at_to_brand_result.rb
@@ -0,0 +1,17 @@
class AddResultCreatedAtToBrandResult < ActiveRecord::Migration
def self.up
add_column :brand_results, :result_created_at, :datetime
add_index :brand_results, :result_created_at

BrandResult.reset_column_information

BrandResult.all.each do |br|
br.update_attribute(:result_created_at, br.result.created_at)
end
end

def self.down
remove_index :brand_results, :result_created_at
remove_column :brand_results, :result_created_at
end
end
8 changes: 5 additions & 3 deletions db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20100705074120) do
ActiveRecord::Schema.define(:version => 20100705085415) do

create_table "accounts", :force => true do |t|
t.integer "user_id"
Expand Down Expand Up @@ -47,15 +47,17 @@
t.datetime "created_at"
t.datetime "updated_at"
t.string "state"
t.integer "comments_count", :default => 0
t.integer "comments_count", :default => 0
t.integer "temperature"
t.boolean "read", :default => false
t.boolean "read", :default => false
t.integer "team_id"
t.datetime "result_created_at"
end

add_index "brand_results", ["brand_id", "result_id"], :name => "index_brand_results_on_brand_id_and_result_id"
add_index "brand_results", ["brand_id"], :name => "index_brand_results_on_brand_id"
add_index "brand_results", ["read"], :name => "index_brand_results_on_read"
add_index "brand_results", ["result_created_at"], :name => "index_brand_results_on_result_created_at"
add_index "brand_results", ["result_id"], :name => "index_brand_results_on_result_id"
add_index "brand_results", ["state"], :name => "index_brand_results_on_state"
add_index "brand_results", ["team_id"], :name => "index_brand_results_on_team_id"
Expand Down
22 changes: 12 additions & 10 deletions spec/models/brand_result_spec.rb
Expand Up @@ -2,16 +2,17 @@
#
# Table name: brand_results
#
# id :integer(4) not null, primary key
# brand_id :integer(4) indexed, indexed => [result_id]
# result_id :integer(4) indexed, indexed => [brand_id]
# created_at :datetime
# updated_at :datetime
# state :string(255) indexed
# comments_count :integer(4) default(0)
# temperature :integer(4) indexed
# read :boolean(1) default(FALSE), indexed
# team_id :integer(4) indexed
# id :integer(4) not null, primary key
# brand_id :integer(4) indexed, indexed => [result_id]
# result_id :integer(4) indexed, indexed => [brand_id]
# created_at :datetime
# updated_at :datetime
# state :string(255) indexed
# comments_count :integer(4) default(0)
# temperature :integer(4) indexed
# read :boolean(1) default(FALSE), indexed
# team_id :integer(4) indexed
# result_created_at :datetime indexed
#

require 'spec_helper'
Expand All @@ -21,6 +22,7 @@
should_have_column :state, :type => :string
should_have_column :temperature, :comments_count, :type => :integer
should_have_column :read, :type => :boolean
should_have_column :result_created_at, :type => :datetime

#associations
should_belong_to :brand
Expand Down
6 changes: 6 additions & 0 deletions spec/models/result_spec.rb
Expand Up @@ -63,6 +63,12 @@
subject.reload.brands.should include(new_brand)
end

it "creates a new brand_result if it does not have the brand already" do
subject.brand_results.should_receive(:create).
with(:brand => new_brand, :team => new_brand.team, :result_created_at => subject.created_at)
subject.add_brand(new_brand)
end

it "does not add the brand if it has it already" do
expect {
subject.add_brand(existing_brand)
Expand Down

0 comments on commit 11711ce

Please sign in to comment.