Skip to content

Commit

Permalink
cleaned up the test and factories, fixed a bug with the recipe model'…
Browse files Browse the repository at this point in the history
…s callback queue
  • Loading branch information
madebydna committed Sep 18, 2010
1 parent 2a6e209 commit a292703
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,5 @@ gem "compass"
gem "annotate-models", :group => :development
gem "flutie"
gem "simple_importer", :group => :development
gem "database_cleaner", :path => "~/rails3_sites/database_cleaner", :group => :test
gem "database_cleaner", :git => "git://github.com/madebydna/database_cleaner.git", :group => :test

5 changes: 3 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PATH
remote: /Users/devo/rails3_sites/database_cleaner
GIT
remote: git://github.com/madebydna/database_cleaner.git
revision: 04ba63b
specs:
database_cleaner (0.6.0.rc.2)

Expand Down
1 change: 0 additions & 1 deletion app/models/ingredient_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ class IngredientProfile < ActiveRecord::Base


def self.aggregated_nutrient_values_by_recipe(ingredient_ids)
# TODO: remove nutrient_id from ingredient_profiles table and change nutrient_name to be in synch with Food::BASE_PROFILE
select("ingredient_profiles.nutrient_name, ingredient_profiles.nutrient_unit, SUM(nutrient_amount) AS total").
where("ingredient_id IN (?)", ingredient_ids).
group("nutrient_name")
Expand Down
9 changes: 6 additions & 3 deletions app/models/recipe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#

class Recipe < ActiveRecord::Base

before_save :set_ingredient_flags
# NB: order in which the methods are listed is important.
# the methods seem to get executed in reverse order, contrary to what you would expect
after_save :update_or_create_recipe_profile, :trigger_ingredient_profiles

has_many :ingredients, :dependent => :destroy
validates_associated :ingredients
Expand All @@ -22,8 +27,6 @@ class Recipe < ActiveRecord::Base

validate :needs_at_least_one_ingredient

before_save :set_ingredient_flags
after_save :trigger_ingredient_profiles, :create_or_update_recipe_profile

def needs_at_least_one_ingredient
errors.add(:base, "must have at least one ingredient") if ingredients.size == 0
Expand All @@ -44,7 +47,7 @@ def trigger_ingredient_profiles
end
end

def create_or_update_recipe_profile
def update_or_create_recipe_profile
i_profiles = IngredientProfile.aggregated_nutrient_values_by_recipe(ingredients.collect(&:id))
# create or update nutrient entry
i_profiles.each do |ip|
Expand Down
39 changes: 39 additions & 0 deletions config/database.template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysql2
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: vegtastic_development
pool: 5
username: root
password:
socket: /opt/local/var/run/mysql5/mysqld.sock

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: vegtastic_development
pool: 5
username: root
password:
socket: ....

production:
adapter: mysql2
encoding: utf8
reconnect: false
database: vegtastic_development
pool: 5
username: root
password:
socket: ....
21 changes: 11 additions & 10 deletions lib/tasks/import_from_csv.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ require 'csv'

task :import_usda_data => :environment do
Rake::Task["import:foods"].invoke
Rake::Task["import:nutrients"].invoke
Rake::Task["import:nutrient_definitions"].invoke
Rake::Task["import:nutrients"].invoke
Rake::Task["import:weights"].invoke
end

Expand All @@ -18,23 +18,24 @@ namespace :import do
end
end

desc "Importing Nutrient Definitions"
task :nutrient_definitions => :environment do
CSV.foreach("lib/import_data/NUTR_DEF.csv", :col_sep => "|", :headers => true) do |row|
NutrientDefinition.create(:old_pk => row['Nutr_No'], :units => row['Units'], :tag => row['Tagname'], :description => row['NutrDesc'])
end
end

desc "Importing Nutrients"
task :nutrients => :environment do
CSV.foreach("lib/import_data/NUT_DATA_MIN.csv", :col_sep => "|", :headers => true) do |row|
food = Food.find_by_old_pk(row['NDB_No'])
if food
Nutrient.create(:food_id => food.id, :old_pk => row['Nutr_No'], :value => row['Nutr_Val'])
nd = NutrientDefinition.find_by_old_pk(row['Nutr_No'])
Nutrient.create(:food_id => food.id, :nutrient_definition_id => nd.id, :old_pk => row['Nutr_No'], :value => row['Nutr_Val'])
end
end
end

desc "Importing Nutrient Definitions"
task :nutrient_definitions => :environment do
CSV.foreach("lib/import_data/NUTR_DEF.csv", :col_sep => "|", :headers => true) do |row|
NutrientDefinition.create(:old_pk => row['Nutr_No'], :units => row['Units'], :tag => row['Tagname'], :description => row['NutrDesc'])
end
end


desc "Importing Weights"
task :weights => :environment do
CSV.foreach("lib/import_data/WEIGHT.csv", :col_sep => "|", :headers => true) do |row|
Expand Down
1 change: 0 additions & 1 deletion spec/factories/nutrient_definitions.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Read about factories at http://github.com/thoughtbot/factory_girl

Factory.define :nutrient_definition do |f|
f.association :nutrient
f.units "g"
f.description "Carbohydrate, by difference"
f.tag "CHOCDF"
Expand Down
1 change: 1 addition & 0 deletions spec/factories/nutrients.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

Factory.define :nutrient do |f|
f.association :food
f.association :nutrient_definition
f.value 0.06
end
8 changes: 4 additions & 4 deletions spec/models/ingredient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
# ensures that we are using foods and measurements that actually exist
@food = Factory(:food)
@weight = Factory(:weight, :food_id => @food.id)
@n1 = @food.nutrients.create({:value => 171})
@n1.create_nutrient_definition(Factory.attributes_for(:nutrient_definition))
@n2 = @food.nutrients.create({:value => 2})
@n2.create_nutrient_definition({:tag => "FAT", :description => "Total Fat", :units => "g"})
@nd1 = Factory.create(:nutrient_definition)
@n1 = @food.nutrients.create({:value => 171, :nutrient_definition_id => @nd1.id})
@nd2 = NutrientDefinition.create({:tag => "FAT", :description => "Total Fat", :units => "g"})
@n2 = @food.nutrients.create({:value => 2, :nutrient_definition_id => @nd2.id})
# ------------------------------------------------------------------------------------------
@recipe = Factory.build(:recipe)
@recipe.ingredients.build({:name => @food.long_desc, :amount => @weight.amount, :unit => @weight.measure_desc})
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require 'rspec/rails'
require 'remarkable/active_model'
require 'factory_girl'

require 'database_cleaner'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Expand All @@ -26,6 +26,7 @@
# examples within a transaction, comment the following line or assign false
# instead of true.
# config.use_transactional_fixtures = true

config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
Expand Down

0 comments on commit a292703

Please sign in to comment.