-
-
Save jccarbonfive/4591769 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Account < ActiveRecord::Base | |
attr_accessible :name, | |
:account_type, | |
:balance | |
def self.import(file, account_importer) | |
account_importer.import file | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rspec spec/models/account_spec.rb | |
. | |
Finished in 0.01197 seconds | |
1 example, 0 failures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rspec spec/requests/importing_accounts_spec.rb | |
F | |
Failures: | |
1) Importing accounts given a CSV file of accounts imports each account | |
Failure/Error: click_button 'Import' | |
ArgumentError: | |
wrong number of arguments (1 for 2) | |
# ./app/models/account.rb:6:in `import' | |
# ./app/controllers/accounts_controller.rb:6:in `create' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccountsController < ApplicationController | |
def index | |
end | |
def create | |
@imported = Account.import params[:accounts][:file], CsvAccountImporter.new | |
redirect_to accounts_path, | |
notice: "#{@imported} accounts imported" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rspec spec/requests/importing_accounts_spec.rb | |
F | |
Failures: | |
1) Importing accounts given a CSV file of accounts imports each account | |
Failure/Error: click_button 'Import' | |
NameError: | |
uninitialized constant AccountsController::CsvAccountImporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe CsvAccountImporter do | |
describe 'importing' do | |
context 'given a CSV file of accounts' do | |
before do | |
@account_count = Account.count | |
csv_file = File.open Rails.root.join 'spec', 'fixtures', 'accounts.csv' | |
uploaded_file = ActionDispatch::Http::UploadedFile.new filename: File.basename(csv_file), | |
tempfile: csv_file | |
csv_account_importer = CsvAccountImporter.new | |
@imported = csv_account_importer.import uploaded_file | |
end | |
it 'creates an account from each row' do | |
expect(Account.count).to eq(@account_count + 3) | |
end | |
it 'returns the total number of accounts imported' do | |
expect(@imported).to eq(3) | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'csv' | |
class CsvAccountImporter | |
def import(uploaded_file) | |
account_count = Account.count | |
CSV.foreach uploaded_file.tempfile do |row| | |
name, account_type, balance = row | |
Account.create!(name: name, | |
account_type: account_type, | |
balance: balance) | |
end | |
Account.count - account_count | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ rspec spec/requests/importing_accounts_spec.rb | |
. | |
Finished in 0.30801 seconds | |
1 example, 0 failures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AccountsController < ApplicationController | |
def index | |
end | |
def create | |
@imported = Account.import params[:accounts][:file] | |
redirect_to accounts_path, | |
notice: "#{@imported} accounts imported" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment