Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: wding/Codebreaker
base: 29028309e9
...
head fork: wding/Codebreaker
compare: 6ebb747e4c
Checking mergeability… Don't worry, you can still create the pull request.
  • 12 commits
  • 19 files changed
  • 0 commit comments
  • 3 contributors
View
5 app/controllers/consultants_controller.rb
@@ -2,4 +2,9 @@ class ConsultantsController < ApplicationController
def index
@consultants = Consultant.all
end
+
+ def generate
+ @consultants = Consultant.all.sort_by{rand}[0..1]
+ render :index
+ end
end
View
5 app/controllers/import_csv_controller.rb
@@ -2,7 +2,10 @@
class ImportCsvController < ApplicationController
def upload
- CSVParser.parse params[:dump][:file]
+ consultants = CSVParser.parse((params[:dump][:file]).read)
+ consultants.each do |c|
+ Consultant.create(c)
+ end
redirect_to :action => :index
end
end
View
1  app/models/consultant.rb
@@ -1,2 +1,3 @@
class Consultant < ActiveRecord::Base
+ has_and_belongs_to_many :skills
end
View
2  app/models/consultant_skill.rb
@@ -0,0 +1,2 @@
+class ConsultantSkill < ActiveRecord::Base
+end
View
3  app/models/skill.rb
@@ -0,0 +1,3 @@
+class Skill < ActiveRecord::Base
+ has_and_belongs_to_many :consultants
+end
View
1  config/routes.rb
@@ -1,4 +1,5 @@
Codebreaker::Application.routes.draw do
+ match 'consultants/generate' => 'consultants#generate', :via => 'get'
resources :consultants
match 'import_csv/' => 'import_csv#index', :via => 'get'
match 'import_csv/' => 'import_csv#upload', :via => 'post'
View
9 db/migrate/20111118054750_create_skills.rb
@@ -0,0 +1,9 @@
+class CreateSkills < ActiveRecord::Migration
+ def change
+ create_table :skills do |t|
+ t.string :name
+
+ t.timestamps
+ end
+ end
+end
View
10 db/migrate/20111118055057_create_consultants_skills.rb
@@ -0,0 +1,10 @@
+class CreateConsultantsSkills < ActiveRecord::Migration
+ def change
+ create_table :consultants_skills do |t|
+ t.integer :consultant_id
+ t.integer :skill_id
+
+ t.timestamps
+ end
+ end
+end
View
9 db/migrate/20111118055641_remove_skill_column_from_consultant.rb
@@ -0,0 +1,9 @@
+class RemoveSkillColumnFromConsultant < ActiveRecord::Migration
+ def up
+ remove_column :consultants, :skill
+ end
+
+ def down
+ add_column :consultants, :skill, :integer
+ end
+end
View
16 db/schema.rb
@@ -11,14 +11,26 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20111117033352) do
+ActiveRecord::Schema.define(:version => 20111118055641) do
create_table "consultants", :force => true do |t|
t.string "name"
t.string "email"
t.string "role"
t.string "grade"
- t.string "skill"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "consultants_skills", :force => true do |t|
+ t.integer "consultant_id"
+ t.integer "skill_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+ create_table "skills", :force => true do |t|
+ t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
View
12 lib/csv_parser.rb
@@ -1,4 +1,16 @@
+require 'csv'
+
class CSVParser
def self.parse file
+ parsed_file = CSV::parse(file)
+ parsed_file.collect do |row|
+ {
+ :name => row[0],
+ :role => row[1],
+ :grade => row[2],
+ :email => row[3],
+ :skill => row[4]
+ }
+ end
end
end
View
14 spec/controllers/consultants_controller_spec.rb
@@ -13,4 +13,18 @@
end
end
+ describe "GET generate" do
+ it "should list two random consultants" do
+ (1..5).each do |i|
+ Consultant.create(:name => "David" + i.to_s)
+ end
+
+ get 'generate'
+ consultants = assigns[:consultants]
+ consultants.should_not be_nil
+ consultants.size.should == 2
+ end
+ end
+
+
end
View
28 spec/controllers/import_csv_controller_spec.rb
@@ -3,9 +3,31 @@
describe ImportCsvController do
describe "upload csv" do
- it 'should handle the csv file' do
- CSVParser.should_receive(:parse).with("haha")
- post 'upload', {:dump => {:file => "haha" }}
+ before :each do
+ file = ""
+ file.stub(:read).and_return("haha")
+ controller.stub!(:params).and_return({
+ :dump => {:file => file}
+ })
+ end
+
+ it 'should parse the csv file' do
+ CSVParser.should_receive(:parse).with("haha").and_return([])
+ post 'upload'
+ end
+
+ it "should create the same number of consultants that was parsed" do
+ hashes = [{}, {}]
+ CSVParser.stub(:parse).and_return(hashes)
+ Consultant.should_receive(:create).twice
+ post 'upload'
+ end
+
+ it "should create consultants with the parsed attributes" do
+ hashes = [{:key=>nil}]
+ CSVParser.stub(:parse).and_return(hashes)
+ Consultant.should_receive(:create).with(hashes.first)
+ post 'upload'
end
end
View
4 spec/fixtures/consultants.csv
@@ -0,0 +1,4 @@
+David Oh,Dev,Consultant,doh@thoughtworks.com,ruby
+Weiwei Ding,Dev,Consultant,wding@thoughtworks.com,ruby
+Liauw Fendy,Dev,Consultant,lfendy@thoughtworks.com,ruby
+Pei Shi Yong,Dev,Consultant,pyong@thoughtworks.com,ruby
View
1  spec/helpers/import_csv_helper_spec.rb
@@ -11,5 +11,4 @@
# end
# end
describe ImportCsvHelper do
- pending "add some examples to (or delete) #{__FILE__}"
end
View
23 spec/integration/upload_file_spec.rb
@@ -0,0 +1,23 @@
+require 'spec_helper'
+
+describe "upload_file" do
+
+ describe "uploads file and populate the database" do
+ let(:test_file) do
+ fixture_file_upload('/consultants.csv', 'text/plain')
+ end
+
+ it "should create entries in database" do
+ post 'import_csv', {:dump => {:file => test_file}}
+ Consultant.count.should_not == 0
+ c = Consultant.find_by_name("David Oh")
+ c.should be
+ c.role.should == "Dev"
+ c.email.should == "doh@thoughtworks.com"
+ c.skill.should == "ruby"
+ c.grade.should == "Consultant"
+ end
+ end
+
+end
+
View
31 spec/lib/csv_parser_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe CSVParser do
+ describe 'parse' do
+ it 'should parse a file with a single csv line' do
+ results = CSVParser.parse 'David Oh,Dev,Consultant,doh@thoughtworks.com,ruby'
+ results.first.should == {
+ :name => 'David Oh',
+ :role => 'Dev',
+ :grade => 'Consultant',
+ :email => 'doh@thoughtworks.com',
+ :skill => 'ruby'
+ }
+ results.size.should == 1
+ end
+
+ it 'should parse a file with multiple csv lines' do
+ str = <<TAG
+ David
+ Pei Shi
+ Weiwei
+ Fendy
+TAG
+ results = CSVParser.parse str
+ results.size.should == 4
+ end
+
+ end
+
+end
+
View
5 spec/models/consultant_skill_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe ConsultantSkill do
+ pending "add some examples to (or delete) #{__FILE__}"
+end
View
5 spec/models/skill_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe Skill do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

No commit comments for this range

Something went wrong with that request. Please try again.