Commit d2da3e7d0bd66d45e7dd68a92e22440818b8d8c7
1 parent
7ed7d525
Exists in
colab
and in
4 other branches
Basic Repository validations
Still missing valid address validation
Showing
6 changed files
with
32 additions
and
13 deletions
Show diff stats
app/controllers/repositories_controller.rb
| @@ -40,6 +40,9 @@ class RepositoriesController < ApplicationController | @@ -40,6 +40,9 @@ class RepositoriesController < ApplicationController | ||
| 40 | format.html { redirect_to project_path(params[:project_id]), notice: 'Repository was successfully created.' } | 40 | format.html { redirect_to project_path(params[:project_id]), notice: 'Repository was successfully created.' } |
| 41 | format.json { render action: 'show', status: :created, location: @repository } | 41 | format.json { render action: 'show', status: :created, location: @repository } |
| 42 | else | 42 | else |
| 43 | + @project_id = params[:project_id] | ||
| 44 | + @repository_types = Repository.repository_types | ||
| 45 | + | ||
| 43 | format.html { render action: 'new' } | 46 | format.html { render action: 'new' } |
| 44 | format.json { render json: @repository.errors, status: :unprocessable_entity } | 47 | format.json { render json: @repository.errors, status: :unprocessable_entity } |
| 45 | end | 48 | end |
app/models/repository.rb
| 1 | class Repository < KalibroEntities::Entities::Repository | 1 | class Repository < KalibroEntities::Entities::Repository |
| 2 | include KalibroRecord | 2 | include KalibroRecord |
| 3 | 3 | ||
| 4 | + validates :name, presence: true, kalibro_uniqueness: true | ||
| 5 | + validates :address, presence: true | ||
| 6 | + | ||
| 4 | def last_processing | 7 | def last_processing |
| 5 | Processing.processing_of(@id) | 8 | Processing.processing_of(@id) |
| 6 | end | 9 | end |
app/views/repositories/_form.html.erb
| 1 | <%= render :partial => 'shared/form_errors', :locals => {:object => @repository} %> | 1 | <%= render :partial => 'shared/form_errors', :locals => {:object => @repository} %> |
| 2 | 2 | ||
| 3 | -<% if @repository.errors.any? %> | ||
| 4 | - <div id="error_explanation"> | ||
| 5 | - <h2><%= pluralize(@repository.errors.count, "error") %> prohibited this repository from being saved:</h2> | ||
| 6 | - | ||
| 7 | - <ul> | ||
| 8 | - <% @repository.errors.full_messages.each do |msg| %> | ||
| 9 | - <li><%= msg %></li> | ||
| 10 | - <% end %> | ||
| 11 | - </ul> | ||
| 12 | - </div> | ||
| 13 | -<% end %> | ||
| 14 | - | ||
| 15 | <div class="field"> | 3 | <div class="field"> |
| 16 | <%= f.label :name %><br> | 4 | <%= f.label :name %><br> |
| 17 | <%= f.text_field :name %> | 5 | <%= f.text_field :name %> |
app/views/shared/_form_errors.html.erb
| 1 | <% if object.errors.any? || object.kalibro_errors.any? %> | 1 | <% if object.errors.any? || object.kalibro_errors.any? %> |
| 2 | <div id="error_explanation"> | 2 | <div id="error_explanation"> |
| 3 | - <h2><%= pluralize(object.errors.count + object.kalibro_errors.count, "error") %> prohibited this project from being saved:</h2> | 3 | + <h2><%= pluralize(object.errors.count + object.kalibro_errors.count, "error") %> prohibited this <%= object.class.to_s %> from getting saved:</h2> |
| 4 | 4 | ||
| 5 | <ul> | 5 | <ul> |
| 6 | <% object.errors.full_messages.each do |msg| %> | 6 | <% object.errors.full_messages.each do |msg| %> |
spec/controllers/repositories_controller_spec.rb
| @@ -60,6 +60,7 @@ describe RepositoriesController do | @@ -60,6 +60,7 @@ describe RepositoriesController do | ||
| 60 | before :each do | 60 | before :each do |
| 61 | Repository.any_instance.expects(:save).returns(false) | 61 | Repository.any_instance.expects(:save).returns(false) |
| 62 | Repository.any_instance.expects(:persisted?).at_least_once.returns(false) | 62 | Repository.any_instance.expects(:persisted?).at_least_once.returns(false) |
| 63 | + Repository.expects(:repository_types).returns([]) | ||
| 63 | 64 | ||
| 64 | post :create, project_id: project.id.to_s, repository: repository_params | 65 | post :create, project_id: project.id.to_s, repository: repository_params |
| 65 | end | 66 | end |
spec/models/repository_spec.rb
| @@ -34,4 +34,28 @@ describe Repository do | @@ -34,4 +34,28 @@ describe Repository do | ||
| 34 | end | 34 | end |
| 35 | end | 35 | end |
| 36 | end | 36 | end |
| 37 | + | ||
| 38 | + describe 'validations' do | ||
| 39 | + subject {FactoryGirl.build(:repository)} | ||
| 40 | + | ||
| 41 | + context 'active model validations' do | ||
| 42 | + before :each do | ||
| 43 | + Repository.expects(:all).at_least_once.returns([]) | ||
| 44 | + end | ||
| 45 | + | ||
| 46 | + it { should validate_presence_of(:name) } | ||
| 47 | + it { should validate_presence_of(:address) } | ||
| 48 | + end | ||
| 49 | + | ||
| 50 | + context 'kalibro validations' do | ||
| 51 | + before :each do | ||
| 52 | + Repository.expects(:request).returns(42) | ||
| 53 | + end | ||
| 54 | + | ||
| 55 | + it 'should validate uniqueness' do | ||
| 56 | + KalibroUniquenessValidator.any_instance.expects(:validate_each).with(subject, :name, subject.name) | ||
| 57 | + subject.save | ||
| 58 | + end | ||
| 59 | + end | ||
| 60 | + end | ||
| 37 | end | 61 | end |