Skip to content

Commit

Permalink
#21 中断するならコミットしよう
Browse files Browse the repository at this point in the history
  • Loading branch information
meriy100 committed Nov 20, 2015
1 parent 11e29ee commit c312123
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def set_category
# Never trust parameters from the scary internet, only allow the white list through.
def category_params
params.require(:category).permit(
:name, :color_id, :user_id, :wallet_id,
:name, :color_id, :user_id, :from_wallet_id, :to_wallet_id,

short_ccs_attributes: [
:id, :_destroy,
short_cps_attributes: [
Expand Down
9 changes: 7 additions & 2 deletions app/models/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Record < ActiveRecord::Base
validates :to_wallet_id, presence: true, if: Proc.new{|r| r.record_type != PAYMENT}

before_validation :set_from_wallet, if: "from_wallet.blank?"
before_validation :set_to_wallet, if: "to_wallet.blank?"
before_validation :set_record_type, if: "record_type.blank?"


Expand Down Expand Up @@ -58,7 +59,11 @@ def reset_wallet
private

def set_from_wallet
self.from_wallet = self.sub_category.try(:wallet) || self.category.try(:wallet)
self.from_wallet = self.sub_category.try(:wallet) || self.category.try(:from_wallet)
end

def set_to_wallet
self.to_wallet = self.sub_category.try(:wallet) || self.category.try(:to_wallet)
end

def set_record_type
Expand All @@ -72,7 +77,7 @@ def update_wallet
end

if self.record_type != PAYMENT
to = self.to_wallet
to = self.to_wallet
to.update price: to.price + self.price
end
end
Expand Down
Binary file modified db/development.sqlite3
Binary file not shown.
Binary file modified db/test.sqlite3
Binary file not shown.
10 changes: 9 additions & 1 deletion spec/controllers/service_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@
describe "GET #create" do
it "with params[]"
describe "with valid params" do
it "creates a new Record"
before do
@shotr_cc = create :short_cc
@shotr_cp = create :short_cp
end
it "creates a new Record" do
expect {
get :create, {}, valid_session, short_cc: 1, short_cp: 1
}.to change(Record, :count).by 1
end
it "assigns a newly created record as @record"
end
describe "with invalid params"
Expand Down
8 changes: 8 additions & 0 deletions spec/factories/short_cc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryGirl.define do
factory :short_cc do
user_id 1
category_id 1
sub_category_id 1

end
end
7 changes: 7 additions & 0 deletions spec/factories/short_cp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :short_cp do
user_id 1
short_cc 1
price 1000
end
end

0 comments on commit c312123

Please sign in to comment.