Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validations for reward model #64

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions app/controllers/pledges_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class PledgesController < ApplicationController
before_action :require_login
# before_action :not_authenticated

def create
@project = Project.find(params[:project_id])
Expand All @@ -15,5 +15,5 @@ def create
render 'projects/show'
end
end

end
2 changes: 1 addition & 1 deletion app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ProjectsController < ApplicationController
before_action :require_login, only: [:new, :create]
before_action :not_authenticated, only: [:new, :create]

def index
@projects = Project.all
Expand Down
10 changes: 10 additions & 0 deletions app/models/pledge.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
class Pledge < ApplicationRecord
belongs_to :user
belongs_to :project
#project.user_id != pledge.user_id

validates :dollar_amount, presence: true
validates :user, presence: true
validate :validate_backer_is_not_owner
validates :dollar_amount, :numericality => { :greater_than => 0 }

def validate_backer_is_not_owner
if project.user == user
errors.add(:user_id, 'Owner cant back project')
end
end

end
1 change: 1 addition & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ class Project < ActiveRecord::Base
belongs_to :user # project owner

validates :title, :description, :goal, :start_date, :end_date, presence: true
validates :user, presence: true
end
9 changes: 9 additions & 0 deletions app/models/reward.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
class Reward < ActiveRecord::Base

# Reward dollar_amount must be positive number
validates :dollar_amount, :numericality => { :greater_than => 0 }
validates :description, presence: true
validates :dollar_amount, presence: true

belongs_to :project



end
8 changes: 8 additions & 0 deletions app/views/projects/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<% if @projects.errors.full_messages.any? %>
<% @projects.errors.full_messages.each do |msg| %>
<ul>
<li><%= msg %></li>
</ul>
<% end %>
<% end %>

<%= form_for(@project, html: {multipart: true}) do |f| %>
<div>
<%= f.label :title %>
Expand Down
2 changes: 1 addition & 1 deletion test/models/pledge_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PledgeTest < ActiveSupport::TestCase
project = new_project
project.user = owner
project.save
pledge = Pledge.new(dollar_amount: 3.00, project: project)
pledge = Pledge.new( dollar_amount: 3.00, project: project)
pledge.user = owner
pledge.save
assert pledge.invalid?, 'Owner should not be able to pledge towards own project'
Expand Down
11 changes: 11 additions & 0 deletions test/models/reward_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ class RewardTest < ActiveSupport::TestCase
assert reward.new_record?, 'Reward should not save without a description'
end

test 'Reward dollar_amount must be positive number' do
project = new_project
project.save
reward = Reward.create(
dollar_amount: -1,
description: 'Motherlover',
project: project
)
assert reward.invalid?
end

def new_project
Project.new(
title: 'Cool new boardgame',
Expand Down