Skip to content

Commit

Permalink
Add dispute comment attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur Beljajev committed Feb 6, 2017
1 parent dd5ea22 commit d217de2
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/disputes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def destroy
private

def dispute_params
params.require(:dispute).permit(:domain_name, :expire_date, :password)
params.require(:dispute).permit(:domain_name, :expire_date, :password, :comment)
end
end
end
2 changes: 1 addition & 1 deletion app/models/dispute.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Dispute < ActiveRecord::Base
belongs_to :domain, required: true

validates :expire_date, :password, presence: true
validates :expire_date, :password, :comment, presence: true
validates :domain, uniqueness: true
validate :validate_expire_date_past

Expand Down
8 changes: 8 additions & 0 deletions app/views/admin/disputes/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@
</div>
</div>

<div class="form-group">
<%= f.label :comment, class: 'col-sm-2 control-label' %>

<div class="col-sm-4">
<%= f.text_area :comment, class: 'form-control', required: true %>
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-2">
<%= f.submit t(".#{dispute.new_record? ? 'create' : 'update'}_btn"), class: 'btn btn-success',
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20170206052644_add_comment_to_disputes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCommentToDisputes < ActiveRecord::Migration
def change
add_column :disputes, :comment, :text, null: false
end
end
3 changes: 2 additions & 1 deletion db/schema-read-only.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170205135240) do
ActiveRecord::Schema.define(version: 20170206052644) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -273,6 +273,7 @@
t.string "password"
t.date "expire_date"
t.datetime "created_at"
t.text "comment", null: false
end

add_index "disputes", ["domain_id"], name: "index_disputes_on_domain_id", unique: true, using: :btree
Expand Down
5 changes: 4 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,8 @@ CREATE TABLE disputes (
domain_id integer,
password character varying,
expire_date date,
created_at timestamp without time zone
created_at timestamp without time zone,
comment text NOT NULL
);


Expand Down Expand Up @@ -5346,3 +5347,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170203102059');

INSERT INTO schema_migrations (version) VALUES ('20170205135240');

INSERT INTO schema_migrations (version) VALUES ('20170206052644');

1 change: 1 addition & 0 deletions spec/factories/dispute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
factory :dispute do
expire_date Time.zone.today
password 'test'
comment 'test'
domain
end
end
10 changes: 10 additions & 0 deletions spec/models/dispute_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@
end
end

describe 'comment validation' do
let(:dispute) { described_class.new }

it 'rejects absent' do
dispute.comment = nil
dispute.validate
expect(dispute.errors).to have_key(:comment)
end
end

describe '::latest_on_top' do
it 'sorts by :create_time in descending order' do
expect(described_class).to receive(:order).with(create_time: :desc)
Expand Down
38 changes: 37 additions & 1 deletion spec/requests/admin/disputes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,46 @@

RSpec.describe Admin::DisputesController do
before :example do
travel_to Time.zone.parse('05.07.2010')
sign_in_to_admin_area
end

describe '#new' do
let!(:domain) { create(:domain) }
let(:request) { post admin_disputes_path,
{ dispute: attributes_for(:dispute, domain_name: domain.name) } }
{ dispute: attributes_for(:dispute,
domain_name: domain.name) }
}
subject(:dispute) { Dispute.first }

it 'creates new dispute' do
expect { request }.to change { Dispute.count }.from(0).to(1)
end

it 'saves date of expiry' do
post admin_disputes_path, { dispute: attributes_for(:dispute,
domain_name: domain.name,
expire_date: localize(Date.parse('05.07.2010')))
}
expect(dispute.expire_date).to eq(Date.parse('05.07.2010'))
end

it 'saves password' do
post admin_disputes_path, { dispute: attributes_for(:dispute,
domain_name: domain.name,
password: 'test')
}
expect(dispute.password).to eq('test')
end

it 'saves comment' do
post admin_disputes_path, { dispute: attributes_for(:dispute,
domain_name: domain.name,
comment: 'test')
}
expect(dispute.comment).to eq('test')
end

it 'redirects to dispute list' do
request
expect(response).to redirect_to admin_disputes_path
Expand All @@ -29,6 +57,14 @@
expect(dispute.expire_date).to eq(Time.zone.parse('06.07.2010').to_date)
end

it 'updates comment' do
dispute = create(:dispute, comment: 'test')
patch admin_dispute_path(dispute),
dispute: attributes_for(:dispute, comment: 'new comment')
dispute.reload
expect(dispute.comment).to eq('new comment')
end

it 'redirects to dispute list' do
dispute = create(:dispute)
patch admin_dispute_path(dispute), dispute: { password: 'test' }
Expand Down
9 changes: 9 additions & 0 deletions spec/views/admin/disputes/_form.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,13 @@
expect(field[:required]).to eq('required')
end
end

describe 'comment' do
let(:field) { page.find('[name="dispute[comment]"]') }

it 'is required' do
render
expect(field[:required]).to eq('required')
end
end
end

0 comments on commit d217de2

Please sign in to comment.