From 1155ec791bd14fc319b66a13a1d48f093b244c20 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Fri, 12 Oct 2018 23:53:56 -0300 Subject: [PATCH 01/20] Adding method that verify the existence of a place --- indicaAi/app/models/local.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index b989b279..b54b548d 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -10,4 +10,13 @@ def self.find_by_name(params) def self.find_local_ratings(local_id) Local.find(local_id).local_ratings end + + def self.rate_a_place(local_name, user, rate) + existe = + + end + + def self.validate_local_existence(local_name) + Local.exists?('upper(name) like ?' , "%#{params.upcase}%") + end end From dca49b645b43ededab5679b2533a5a951c5761c3 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Sat, 13 Oct 2018 00:52:27 -0300 Subject: [PATCH 02/20] Adding Validate Local Existence test --- indicaAi/app/models/local.rb | 7 +++---- indicaAi/spec/models/local_spec.rb | 9 +++++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index b54b548d..3a5f93e8 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -12,11 +12,10 @@ def self.find_local_ratings(local_id) end def self.rate_a_place(local_name, user, rate) - existe = - + end def self.validate_local_existence(local_name) - Local.exists?('upper(name) like ?' , "%#{params.upcase}%") + Local.exists?(['upper(name) like ?' , "%#{local_name.upcase}%"]) end -end +end \ No newline at end of file diff --git a/indicaAi/spec/models/local_spec.rb b/indicaAi/spec/models/local_spec.rb index 452e9dc7..e25ac1a5 100644 --- a/indicaAi/spec/models/local_spec.rb +++ b/indicaAi/spec/models/local_spec.rb @@ -18,4 +18,13 @@ result = Local.find_local_ratings(id) assert result.first, nota end + + it 'Testing Validade Local Existence - Should return true if local exist' do + local = create(:local) + name = local.name + exist = true + + result = Local.validate_local_existence(name) + assert result, exist + end end From 21a5fc341d2f7ff2da66bf1de803d790ba434650 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Sat, 13 Oct 2018 13:40:22 -0300 Subject: [PATCH 03/20] Creating user rate model Co-authored-by: WeillerFernandes --- indicaAi/app/models/local.rb | 11 +++++++---- indicaAi/app/models/user_rate.rb | 8 ++++++++ .../db/migrate/20181013163629_create_user_rates.rb | 9 +++++++++ indicaAi/db/schema.rb | 8 +++++++- indicaAi/spec/models/local_spec.rb | 2 +- indicaAi/spec/models/user_rate_spec.rb | 5 +++++ 6 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 indicaAi/app/models/user_rate.rb create mode 100644 indicaAi/db/migrate/20181013163629_create_user_rates.rb create mode 100644 indicaAi/spec/models/user_rate_spec.rb diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 3a5f93e8..c851c45c 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -12,10 +12,13 @@ def self.find_local_ratings(local_id) end def self.rate_a_place(local_name, user, rate) - - end + exist = validate_local_existence(local_name) + if exist == true + + end + end def self.validate_local_existence(local_name) - Local.exists?(['upper(name) like ?' , "%#{local_name.upcase}%"]) + Local.exists?(['upper(name) like ?', "%#{local_name.upcase}%"]) end -end \ No newline at end of file +end diff --git a/indicaAi/app/models/user_rate.rb b/indicaAi/app/models/user_rate.rb new file mode 100644 index 00000000..9fd8f9e1 --- /dev/null +++ b/indicaAi/app/models/user_rate.rb @@ -0,0 +1,8 @@ +class UserRate < ApplicationRecord + belongs_to :user_identifier + validates_associated :user_identifier + validates :value, presence: true + validates :value, numericality: true + validates_numericality_of :value, less_than_or_equal_to: 5 + validates_numericality_of :value, greater_than_or_equal_to: 0 +end diff --git a/indicaAi/db/migrate/20181013163629_create_user_rates.rb b/indicaAi/db/migrate/20181013163629_create_user_rates.rb new file mode 100644 index 00000000..586c6777 --- /dev/null +++ b/indicaAi/db/migrate/20181013163629_create_user_rates.rb @@ -0,0 +1,9 @@ +class CreateUserRates < ActiveRecord::Migration[5.1] + def change + create_table :user_rates do |t| + t.integer :rating + + t.timestamps + end + end +end diff --git a/indicaAi/db/schema.rb b/indicaAi/db/schema.rb index eb06559b..d4c55299 100644 --- a/indicaAi/db/schema.rb +++ b/indicaAi/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180917225656) do +ActiveRecord::Schema.define(version: 20181013163629) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -47,6 +47,12 @@ t.datetime "updated_at", null: false end + create_table "user_rates", force: :cascade do |t| + t.integer "rating" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + add_foreign_key "favorite_locals", "locals" add_foreign_key "favorite_locals", "user_identifiers" add_foreign_key "local_ratings", "locals" diff --git a/indicaAi/spec/models/local_spec.rb b/indicaAi/spec/models/local_spec.rb index e25ac1a5..9a488813 100644 --- a/indicaAi/spec/models/local_spec.rb +++ b/indicaAi/spec/models/local_spec.rb @@ -25,6 +25,6 @@ exist = true result = Local.validate_local_existence(name) - assert result, exist + assert result, exist end end diff --git a/indicaAi/spec/models/user_rate_spec.rb b/indicaAi/spec/models/user_rate_spec.rb new file mode 100644 index 00000000..1a593ed1 --- /dev/null +++ b/indicaAi/spec/models/user_rate_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe UserRate, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From 06db7627534388df46a88f726dc79853c51a4909 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Sat, 13 Oct 2018 14:53:20 -0300 Subject: [PATCH 04/20] Adding user_rate model's teste --- indicaAi/app/models/user_identifier.rb | 1 + indicaAi/app/models/user_rate.rb | 10 +++++----- indicaAi/spec/models/user_rate_spec.rb | 4 +++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/indicaAi/app/models/user_identifier.rb b/indicaAi/app/models/user_identifier.rb index 48a8d18a..7f59a2e5 100644 --- a/indicaAi/app/models/user_identifier.rb +++ b/indicaAi/app/models/user_identifier.rb @@ -1,6 +1,7 @@ # Description of User Identifier Class class UserIdentifier < ApplicationRecord has_many :favorite_locals, dependent: :destroy + has_many :user_rate, dependent: :destroy validates :identifier, presence: true validates_numericality_of :identifier validates_uniqueness_of :identifier diff --git a/indicaAi/app/models/user_rate.rb b/indicaAi/app/models/user_rate.rb index 9fd8f9e1..35bd5cd0 100644 --- a/indicaAi/app/models/user_rate.rb +++ b/indicaAi/app/models/user_rate.rb @@ -1,8 +1,8 @@ +# Model of Users Rates class UserRate < ApplicationRecord belongs_to :user_identifier - validates_associated :user_identifier - validates :value, presence: true - validates :value, numericality: true - validates_numericality_of :value, less_than_or_equal_to: 5 - validates_numericality_of :value, greater_than_or_equal_to: 0 + validates :rating, numericality: true + validates_numericality_of :rating, less_than_or_equal_to: 5 + validates_numericality_of :rating, greater_than_or_equal_to: 0 + validates :user_identifier, presence: true end diff --git a/indicaAi/spec/models/user_rate_spec.rb b/indicaAi/spec/models/user_rate_spec.rb index 1a593ed1..aad6e984 100644 --- a/indicaAi/spec/models/user_rate_spec.rb +++ b/indicaAi/spec/models/user_rate_spec.rb @@ -1,5 +1,7 @@ require 'rails_helper' RSpec.describe UserRate, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + it { should validate_numericality_of(:rating) } + it { should validate_numericality_of(:rating).is_less_than_or_equal_to(5) } + it { should validate_numericality_of(:rating).is_greater_than_or_equal_to(0)} end From 12f8d5e8dd38f89b279519e1a01a3a0a171bd89d Mon Sep 17 00:00:00 2001 From: zarbielli Date: Mon, 15 Oct 2018 16:35:16 -0300 Subject: [PATCH 05/20] Adding validations Co-authored-by: WeillerFernandes --- indicaAi/app/models/local.rb | 2 ++ indicaAi/app/models/local_rating.rb | 12 +++++++----- indicaAi/app/models/user_identifier.rb | 5 +++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index c851c45c..9865baf2 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -1,6 +1,8 @@ # Description of Places Class class Local < ApplicationRecord has_many :local_ratings + has_many :user_identifiers, through: :local_ratings + validates :name, presence: true, length: { minimum: 2 } def self.find_by_name(params) diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb index d276e6a7..5766738f 100644 --- a/indicaAi/app/models/local_rating.rb +++ b/indicaAi/app/models/local_rating.rb @@ -1,9 +1,11 @@ # Description of Local Rating Class class LocalRating < ApplicationRecord belongs_to :local - validates_associated :local - validates :value, presence: true - validates :value, numericality: true - validates_numericality_of :value, less_than_or_equal_to: 5 - validates_numericality_of :value, greater_than_or_equal_to: 0 + belongs_to :user_identifier + + validates :value, numericality: { + only_integer: true, + less_than_or_equal_to: 5, + greater_than_or_equal_to: 0 + } end diff --git a/indicaAi/app/models/user_identifier.rb b/indicaAi/app/models/user_identifier.rb index 7f59a2e5..b202ea08 100644 --- a/indicaAi/app/models/user_identifier.rb +++ b/indicaAi/app/models/user_identifier.rb @@ -1,7 +1,8 @@ -# Description of User Identifier Class + # Description of User Identifier Class class UserIdentifier < ApplicationRecord has_many :favorite_locals, dependent: :destroy - has_many :user_rate, dependent: :destroy + has_many :locals, through: :local_ratings + validates :identifier, presence: true validates_numericality_of :identifier validates_uniqueness_of :identifier From 08f85b21e476383ad8a1889f62b8a4bd68412610 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Mon, 15 Oct 2018 16:36:16 -0300 Subject: [PATCH 06/20] Creating Local Ratings Controller --- indicaAi/app/controllers/local_ratings_controller.rb | 6 ++++++ indicaAi/db/migrate/20181015191722_local_rating.rb | 4 ++++ indicaAi/db/migrate/20181015191738_local.rb | 4 ++++ indicaAi/db/migrate/20181015191753_user_identifier.rb | 4 ++++ indicaAi/db/schema.rb | 2 +- 5 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 indicaAi/app/controllers/local_ratings_controller.rb create mode 100644 indicaAi/db/migrate/20181015191722_local_rating.rb create mode 100644 indicaAi/db/migrate/20181015191738_local.rb create mode 100644 indicaAi/db/migrate/20181015191753_user_identifier.rb diff --git a/indicaAi/app/controllers/local_ratings_controller.rb b/indicaAi/app/controllers/local_ratings_controller.rb new file mode 100644 index 00000000..cc33a271 --- /dev/null +++ b/indicaAi/app/controllers/local_ratings_controller.rb @@ -0,0 +1,6 @@ +# Local Ratings Controller +class LocalRatingsController < ApplicationController + # User rating a place + def rating_a_place + end +end \ No newline at end of file diff --git a/indicaAi/db/migrate/20181015191722_local_rating.rb b/indicaAi/db/migrate/20181015191722_local_rating.rb new file mode 100644 index 00000000..cd8c2875 --- /dev/null +++ b/indicaAi/db/migrate/20181015191722_local_rating.rb @@ -0,0 +1,4 @@ +class LocalRating < ActiveRecord::Migration[5.1] + def change + end +end diff --git a/indicaAi/db/migrate/20181015191738_local.rb b/indicaAi/db/migrate/20181015191738_local.rb new file mode 100644 index 00000000..611e7e72 --- /dev/null +++ b/indicaAi/db/migrate/20181015191738_local.rb @@ -0,0 +1,4 @@ +class Local < ActiveRecord::Migration[5.1] + def change + end +end diff --git a/indicaAi/db/migrate/20181015191753_user_identifier.rb b/indicaAi/db/migrate/20181015191753_user_identifier.rb new file mode 100644 index 00000000..6b86566d --- /dev/null +++ b/indicaAi/db/migrate/20181015191753_user_identifier.rb @@ -0,0 +1,4 @@ +class UserIdentifier < ActiveRecord::Migration[5.1] + def change + end +end diff --git a/indicaAi/db/schema.rb b/indicaAi/db/schema.rb index d4c55299..568e8d34 100644 --- a/indicaAi/db/schema.rb +++ b/indicaAi/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20181013163629) do +ActiveRecord::Schema.define(version: 20181015191753) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" From 661ceb82bee7deec862e710e5236949a9d34cbfd Mon Sep 17 00:00:00 2001 From: zarbielli Date: Tue, 16 Oct 2018 14:14:38 -0300 Subject: [PATCH 07/20] #198 - Adding validations of uniqueness Co-authored-by: WeillerFernandes Co-authored-by: mendesiasmin --- .../controllers/local_ratings_controller.rb | 8 ++++++- indicaAi/app/models/local.rb | 18 ++++---------- indicaAi/app/models/local_rating.rb | 24 +++++++++++++++++++ indicaAi/app/models/user_identifier.rb | 5 ++-- indicaAi/config/routes.rb | 1 + .../20180917225656_create_local_ratings.rb | 2 +- indicaAi/db/schema.rb | 3 +++ 7 files changed, 43 insertions(+), 18 deletions(-) diff --git a/indicaAi/app/controllers/local_ratings_controller.rb b/indicaAi/app/controllers/local_ratings_controller.rb index cc33a271..ee50d15d 100644 --- a/indicaAi/app/controllers/local_ratings_controller.rb +++ b/indicaAi/app/controllers/local_ratings_controller.rb @@ -1,6 +1,12 @@ # Local Ratings Controller class LocalRatingsController < ApplicationController - # User rating a place + # POST /local_rating/:local_name/:user_id/:rate def rating_a_place + a = LocalRating.rating_a_place(params[:local_name, :user_id, :rate]) + json_response(a) end + + #GET /local_rating/:local_name/:user_id + #def find_local_rating + end \ No newline at end of file diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 9865baf2..87afe52e 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -1,7 +1,7 @@ # Description of Places Class class Local < ApplicationRecord has_many :local_ratings - has_many :user_identifiers, through: :local_ratings + validates :name, presence: true, length: { minimum: 2 } @@ -9,18 +9,8 @@ def self.find_by_name(params) Local.where('upper(name) like ?', "%#{params.upcase}%") end - def self.find_local_ratings(local_id) - Local.find(local_id).local_ratings - end - - def self.rate_a_place(local_name, user, rate) - exist = validate_local_existence(local_name) - if exist == true - - end - end + #def self.find_local_ratings(local_id) + # Local.find(local_id).local_ratings + #end - def self.validate_local_existence(local_name) - Local.exists?(['upper(name) like ?', "%#{local_name.upcase}%"]) - end end diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb index 5766738f..810a8f41 100644 --- a/indicaAi/app/models/local_rating.rb +++ b/indicaAi/app/models/local_rating.rb @@ -1,11 +1,35 @@ # Description of Local Rating Class class LocalRating < ApplicationRecord + # Validations belongs_to :local belongs_to :user_identifier + validates_uniqueness_of :user_identifier, scope: :local_id validates :value, numericality: { only_integer: true, less_than_or_equal_to: 5, greater_than_or_equal_to: 0 } + + # Methods + def self.rating_a_place(params) + exist = validate_local_existence(local_name) + if exist == true + rating = LocalRating.new + rating.user_identifier = user_id + rating.local = local_id + rating.value = rate + if !rating.save + flash[:alert] = "The rating must be a integer number (0 - 5)" + end + end + end + + def validate_local_existence(local_name) + Local.exists?(['upper(name) like ?', "%#{local_name.upcase}%"]) + end + + def self.find_local_rating(params) + LocalRating.find(local_id).local_ratings + end end diff --git a/indicaAi/app/models/user_identifier.rb b/indicaAi/app/models/user_identifier.rb index b202ea08..227fa925 100644 --- a/indicaAi/app/models/user_identifier.rb +++ b/indicaAi/app/models/user_identifier.rb @@ -1,11 +1,12 @@ # Description of User Identifier Class class UserIdentifier < ApplicationRecord has_many :favorite_locals, dependent: :destroy - has_many :locals, through: :local_ratings + has_many :local_ratings + validates :identifier, presence: true validates_numericality_of :identifier - validates_uniqueness_of :identifier + def self.find_favorites(params) UserIdentifier.find(params).favorite_locals diff --git a/indicaAi/config/routes.rb b/indicaAi/config/routes.rb index 87906a2f..6678979a 100644 --- a/indicaAi/config/routes.rb +++ b/indicaAi/config/routes.rb @@ -5,4 +5,5 @@ get '/users', to: 'user_identifiers#index' get '/local/favorites/:local_id', to: 'locals#list_favorites' get '/user/favorites/:user_id', to: 'user_identifiers#list_favorites' + post '/local_ratings/:local_name/:user_id/:rate', to: 'local_ratings#rating_a_place' end diff --git a/indicaAi/db/migrate/20180917225656_create_local_ratings.rb b/indicaAi/db/migrate/20180917225656_create_local_ratings.rb index 3aaa8363..20513b30 100644 --- a/indicaAi/db/migrate/20180917225656_create_local_ratings.rb +++ b/indicaAi/db/migrate/20180917225656_create_local_ratings.rb @@ -3,7 +3,7 @@ def change create_table :local_ratings do |t| t.integer :value t.references :local, foreign_key: true - + t.references :user_identifier, foreign_key: true t.timestamps end end diff --git a/indicaAi/db/schema.rb b/indicaAi/db/schema.rb index 568e8d34..5add2fc0 100644 --- a/indicaAi/db/schema.rb +++ b/indicaAi/db/schema.rb @@ -29,9 +29,11 @@ create_table "local_ratings", force: :cascade do |t| t.integer "value" t.bigint "local_id" + t.bigint "user_identifier_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.index ["local_id"], name: "index_local_ratings_on_local_id" + t.index ["user_identifier_id"], name: "index_local_ratings_on_user_identifier_id" end create_table "locals", force: :cascade do |t| @@ -56,4 +58,5 @@ add_foreign_key "favorite_locals", "locals" add_foreign_key "favorite_locals", "user_identifiers" add_foreign_key "local_ratings", "locals" + add_foreign_key "local_ratings", "user_identifiers" end From 938303526c2ced55f565dc56ed3120c716f8145a Mon Sep 17 00:00:00 2001 From: zarbielli Date: Tue, 16 Oct 2018 22:27:47 -0300 Subject: [PATCH 08/20] Adding route to post a rating --- indicaAi/config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/indicaAi/config/routes.rb b/indicaAi/config/routes.rb index 6678979a..1e9712de 100644 --- a/indicaAi/config/routes.rb +++ b/indicaAi/config/routes.rb @@ -5,5 +5,5 @@ get '/users', to: 'user_identifiers#index' get '/local/favorites/:local_id', to: 'locals#list_favorites' get '/user/favorites/:user_id', to: 'user_identifiers#list_favorites' - post '/local_ratings/:local_name/:user_id/:rate', to: 'local_ratings#rating_a_place' + post '/local_ratings', to: 'local_ratings#rating_a_place' end From 7930fec2519476084bafc7ee15e087fc817968f8 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Tue, 16 Oct 2018 22:29:47 -0300 Subject: [PATCH 09/20] Adding LocalRating new tests --- indicaAi/spec/models/local_rating_spec.rb | 38 +++++++++++++++++------ 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/indicaAi/spec/models/local_rating_spec.rb b/indicaAi/spec/models/local_rating_spec.rb index 1c67d1b9..8d3fc6ad 100644 --- a/indicaAi/spec/models/local_rating_spec.rb +++ b/indicaAi/spec/models/local_rating_spec.rb @@ -4,24 +4,44 @@ it { should validate_numericality_of(:value).is_greater_than_or_equal_to(0) } describe 'testing validations' do - context 'ratings validation tests' do - let!(:note) { create(:local_rating) } - it 'should return a integer value' do - assert note.value.class == Integer - end - end it 'should return a integer though a float was given' do note2 = LocalRating.new note2.value = 3.4 assert note2.value == 3 end - it { should belong_to(:local) } + it 'Must exist a user' do + local = Local.create(name: 'Teste') + local.save + rating = LocalRating.new + rating.value = 4 + rating.local = local + assert rating.save == false + end - it { should validate_presence_of(:value) } + it 'Must exist a local' do + user = UserIdentifier.create(identifier: 345) + user.save + rating = LocalRating.new + rating.value = 4 + rating.user_identifier = user + assert rating.save == false + end + it 'Must exist a value' do + local = Local.create(name: 'Teste') + local.save + user = UserIdentifier.create(identifier: 345) + user.save + rating = LocalRating.new + rating.user_identifier = user + rating.local = local + assert rating.save == false + end + it { should belong_to(:user_identifier) } + it { should belong_to(:local) } + it { should validate_presence_of(:value) } it { should validate_numericality_of(:value) } - it { should validate_numericality_of(:value).is_less_than_or_equal_to(5) } end end From fd7c8a90f432a0ece26989ff934e7d0eb33e0f9c Mon Sep 17 00:00:00 2001 From: zarbielli Date: Tue, 16 Oct 2018 22:32:28 -0300 Subject: [PATCH 10/20] Adding validation to value Co-authored-by: WeillerFernandes --- indicaAi/app/models/local_rating.rb | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb index 810a8f41..1ce410f2 100644 --- a/indicaAi/app/models/local_rating.rb +++ b/indicaAi/app/models/local_rating.rb @@ -1,33 +1,15 @@ # Description of Local Rating Class class LocalRating < ApplicationRecord - # Validations + # Validations belongs_to :local belongs_to :user_identifier validates_uniqueness_of :user_identifier, scope: :local_id - + validates :value, presence: true validates :value, numericality: { only_integer: true, less_than_or_equal_to: 5, greater_than_or_equal_to: 0 - } - - # Methods - def self.rating_a_place(params) - exist = validate_local_existence(local_name) - if exist == true - rating = LocalRating.new - rating.user_identifier = user_id - rating.local = local_id - rating.value = rate - if !rating.save - flash[:alert] = "The rating must be a integer number (0 - 5)" - end - end - end - - def validate_local_existence(local_name) - Local.exists?(['upper(name) like ?', "%#{local_name.upcase}%"]) - end + } def self.find_local_rating(params) LocalRating.find(local_id).local_ratings From 8ff5de7904cd4aefa52422d765267eee9100260b Mon Sep 17 00:00:00 2001 From: zarbielli Date: Tue, 16 Oct 2018 22:34:16 -0300 Subject: [PATCH 11/20] Deleting UserRate model --- .../controllers/local_ratings_controller.rb | 26 +++++++++++++------ indicaAi/app/models/user_rate.rb | 8 ------ indicaAi/spec/models/user_rate_spec.rb | 7 ----- 3 files changed, 18 insertions(+), 23 deletions(-) delete mode 100644 indicaAi/app/models/user_rate.rb delete mode 100644 indicaAi/spec/models/user_rate_spec.rb diff --git a/indicaAi/app/controllers/local_ratings_controller.rb b/indicaAi/app/controllers/local_ratings_controller.rb index ee50d15d..19df6f67 100644 --- a/indicaAi/app/controllers/local_ratings_controller.rb +++ b/indicaAi/app/controllers/local_ratings_controller.rb @@ -1,12 +1,22 @@ # Local Ratings Controller class LocalRatingsController < ApplicationController - # POST /local_rating/:local_name/:user_id/:rate - def rating_a_place - a = LocalRating.rating_a_place(params[:local_name, :user_id, :rate]) - json_response(a) - end + # POST /local_rating/:local_name/:user_id/:rate + def rating_a_place + rating = LocalRating.new(local_rating_params) + if rating.save + render json: { status: :ok } + else + render json: { status: :error } + end + end - #GET /local_rating/:local_name/:user_id - #def find_local_rating + private -end \ No newline at end of file + def local_rating_params + { + local_id: params[:local_id], + user_identifier_id: params[:user_id], + value: params[:value] + } + end +end diff --git a/indicaAi/app/models/user_rate.rb b/indicaAi/app/models/user_rate.rb deleted file mode 100644 index 35bd5cd0..00000000 --- a/indicaAi/app/models/user_rate.rb +++ /dev/null @@ -1,8 +0,0 @@ -# Model of Users Rates -class UserRate < ApplicationRecord - belongs_to :user_identifier - validates :rating, numericality: true - validates_numericality_of :rating, less_than_or_equal_to: 5 - validates_numericality_of :rating, greater_than_or_equal_to: 0 - validates :user_identifier, presence: true -end diff --git a/indicaAi/spec/models/user_rate_spec.rb b/indicaAi/spec/models/user_rate_spec.rb deleted file mode 100644 index aad6e984..00000000 --- a/indicaAi/spec/models/user_rate_spec.rb +++ /dev/null @@ -1,7 +0,0 @@ -require 'rails_helper' - -RSpec.describe UserRate, type: :model do - it { should validate_numericality_of(:rating) } - it { should validate_numericality_of(:rating).is_less_than_or_equal_to(5) } - it { should validate_numericality_of(:rating).is_greater_than_or_equal_to(0)} -end From 73b548f4bb726ddf48aa8a920bacf20e29a1e27d Mon Sep 17 00:00:00 2001 From: zarbielli Date: Tue, 16 Oct 2018 22:43:40 -0300 Subject: [PATCH 12/20] Fitting to style sheet --- indicaAi/app/models/local.rb | 7 ------- 1 file changed, 7 deletions(-) diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 87afe52e..076b5e94 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -1,16 +1,9 @@ # Description of Places Class class Local < ApplicationRecord has_many :local_ratings - - validates :name, presence: true, length: { minimum: 2 } def self.find_by_name(params) Local.where('upper(name) like ?', "%#{params.upcase}%") end - - #def self.find_local_ratings(local_id) - # Local.find(local_id).local_ratings - #end - end From 840a38f118018365b12ac9fc69e07d430c899b5e Mon Sep 17 00:00:00 2001 From: zarbielli Date: Wed, 17 Oct 2018 17:17:21 -0300 Subject: [PATCH 13/20] #232 - Updating jekyll to fix the security vulnerability --- docs/Gemfile.lock | 62 +++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/docs/Gemfile.lock b/docs/Gemfile.lock index fb8ea4e7..090a562d 100644 --- a/docs/Gemfile.lock +++ b/docs/Gemfile.lock @@ -1,49 +1,65 @@ GEM remote: https://rubygems.org/ specs: - addressable (2.5.1) - public_suffix (~> 2.0, >= 2.0.2) + addressable (2.5.2) + public_suffix (>= 2.0.2, < 4.0) colorator (1.1.0) + concurrent-ruby (1.0.5) + em-websocket (0.5.1) + eventmachine (>= 0.12.9) + http_parser.rb (~> 0.6.0) + eventmachine (1.2.7) faraday (0.12.0.1) multipart-post (>= 1.2, < 3) ffi (1.9.25) forwardable-extended (2.6.0) - jekyll (3.4.3) + http_parser.rb (0.6.0) + i18n (0.9.5) + concurrent-ruby (~> 1.0) + jekyll (3.8.4) addressable (~> 2.4) colorator (~> 1.0) + em-websocket (~> 0.5) + i18n (~> 0.7) jekyll-sass-converter (~> 1.0) - jekyll-watch (~> 1.1) - kramdown (~> 1.3) - liquid (~> 3.0) + jekyll-watch (~> 2.0) + kramdown (~> 1.14) + liquid (~> 4.0) mercenary (~> 0.3.3) pathutil (~> 0.9) - rouge (~> 1.7) + rouge (>= 1.7, < 4) safe_yaml (~> 1.0) jekyll-gist (1.4.0) octokit (~> 4.2) jekyll-paginate (1.1.0) - jekyll-sass-converter (1.5.0) + jekyll-sass-converter (1.5.2) sass (~> 3.4) - jekyll-watch (1.5.0) - listen (~> 3.0, < 3.1) - kramdown (1.13.2) - liquid (3.0.6) - listen (3.0.6) - rb-fsevent (>= 0.9.3) - rb-inotify (>= 0.9.7) + jekyll-watch (2.1.2) + listen (~> 3.0) + kramdown (1.17.0) + liquid (4.0.1) + listen (3.1.5) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + ruby_dep (~> 1.2) mercenary (0.3.6) multipart-post (2.0.0) octokit (4.6.2) sawyer (~> 0.8.0, >= 0.5.3) - pathutil (0.14.0) + pathutil (0.16.1) forwardable-extended (~> 2.6) - public_suffix (2.0.5) - rb-fsevent (0.9.8) - rb-inotify (0.9.8) - ffi (>= 0.5.0) - rouge (1.11.1) + public_suffix (3.0.3) + rb-fsevent (0.10.3) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) + rouge (3.3.0) + ruby_dep (1.5.0) safe_yaml (1.0.4) - sass (3.4.23) + sass (3.6.0) + sass-listen (~> 4.0.0) + sass-listen (4.0.0) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) sawyer (0.8.1) addressable (>= 2.3.5, < 2.6) faraday (~> 0.8, < 1.0) @@ -60,4 +76,4 @@ DEPENDENCIES rouge BUNDLED WITH - 1.14.6 + 1.16.2 From 6bde50e258a8c5e3dc38984a2f84374a98a6ecf4 Mon Sep 17 00:00:00 2001 From: Weiller Fernandes Date: Wed, 17 Oct 2018 19:38:18 -0300 Subject: [PATCH 14/20] #198 applying tests in the controller Co-authored-by: zarbielli Co-authored-By: Ridersk --- .../controllers/local_ratings_controller.rb | 6 ++--- indicaAi/db/schema.rb | 2 -- .../local_ratings_controller_spec.rb | 23 +++++++++++++++++++ indicaAi/spec/factories/local_rating.rb | 3 ++- 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 indicaAi/spec/controllers/local_ratings_controller_spec.rb diff --git a/indicaAi/app/controllers/local_ratings_controller.rb b/indicaAi/app/controllers/local_ratings_controller.rb index 19df6f67..4c7386ad 100644 --- a/indicaAi/app/controllers/local_ratings_controller.rb +++ b/indicaAi/app/controllers/local_ratings_controller.rb @@ -2,8 +2,8 @@ class LocalRatingsController < ApplicationController # POST /local_rating/:local_name/:user_id/:rate def rating_a_place - rating = LocalRating.new(local_rating_params) - if rating.save + @rating = LocalRating.new(local_rating_params) + if @rating.save render json: { status: :ok } else render json: { status: :error } @@ -15,7 +15,7 @@ def rating_a_place def local_rating_params { local_id: params[:local_id], - user_identifier_id: params[:user_id], + user_identifier_id: params[:user_identifier_id], value: params[:value] } end diff --git a/indicaAi/db/schema.rb b/indicaAi/db/schema.rb index 5add2fc0..02697343 100644 --- a/indicaAi/db/schema.rb +++ b/indicaAi/db/schema.rb @@ -55,8 +55,6 @@ t.datetime "updated_at", null: false end - add_foreign_key "favorite_locals", "locals" - add_foreign_key "favorite_locals", "user_identifiers" add_foreign_key "local_ratings", "locals" add_foreign_key "local_ratings", "user_identifiers" end diff --git a/indicaAi/spec/controllers/local_ratings_controller_spec.rb b/indicaAi/spec/controllers/local_ratings_controller_spec.rb new file mode 100644 index 00000000..32fcac21 --- /dev/null +++ b/indicaAi/spec/controllers/local_ratings_controller_spec.rb @@ -0,0 +1,23 @@ +require 'rails_helper' + +RSpec.describe LocalRatingsController, type: :controller do + let!(:user_test) { create(:user_identifier) } + let!(:local_test) { create(:local) } + let!(:valid_params) do + { + local_id: local_test.id, + user_identifier_id: user_test.id, + value: Faker::Number.between(0, 5) + } + end + describe 'POST rating_a_place' do + it 'should create a LocalRatingsController' do + post :rating_a_place, params: valid_params + expect(response).to be_success + expect(assigns(:rating)).to have_attributes(valid_params) + expect(assigns(:rating)).to be_persisted + expect(assigns(:rating)).to be_a(LocalRating) + expect(assigns(:rating)).not_to eq(nil) + end + end +end diff --git a/indicaAi/spec/factories/local_rating.rb b/indicaAi/spec/factories/local_rating.rb index bc808f54..7096469a 100644 --- a/indicaAi/spec/factories/local_rating.rb +++ b/indicaAi/spec/factories/local_rating.rb @@ -1,6 +1,7 @@ FactoryBot.define do factory :local_rating do value { Faker::Number.between(0, 5) } - local { create(:local) } + local { FactoryBot.create :local } + user_identifier { FactoryBot.create :user_identifier } end end From 903ab50c8d5f48e117d3f6a8f0acd0ffe1d69749 Mon Sep 17 00:00:00 2001 From: Weiller Fernandes Date: Thu, 18 Oct 2018 11:08:46 -0300 Subject: [PATCH 15/20] #198 Fixing travis error --- indicaAi/config/routes.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/indicaAi/config/routes.rb b/indicaAi/config/routes.rb index 9ff9e507..7dad1530 100644 --- a/indicaAi/config/routes.rb +++ b/indicaAi/config/routes.rb @@ -4,8 +4,8 @@ get '/locals/name/:name', to: 'locals#search_locals' get '/users', to: 'user_identifiers#index' get '/local/favorites/:local_id', to: 'locals#list_favorites' - get '/user/favorites/:user_id', to: 'use - post '/local_ratings', to: 'local_ratings#rating_a_place' + get '/user/favorites/:user_id', to: 'use' + post '/local_ratings', to: 'local_ratings#rating_a_place' # Post Requests post '/favorite/create', to: 'favorite_locals#create', :defaults => { :format => 'json' } From e48fbb2c22549f758c98a15843a8b05cfb8e6306 Mon Sep 17 00:00:00 2001 From: Weiller Fernandes Date: Thu, 18 Oct 2018 11:32:44 -0300 Subject: [PATCH 16/20] #198 fixing codeclimate and travis errors --- indicaAi/app/models/local_rating.rb | 2 +- indicaAi/app/models/user_identifier.rb | 6 +++--- indicaAi/config/routes.rb | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/indicaAi/app/models/local_rating.rb b/indicaAi/app/models/local_rating.rb index 1ce410f2..80bd9d86 100644 --- a/indicaAi/app/models/local_rating.rb +++ b/indicaAi/app/models/local_rating.rb @@ -11,7 +11,7 @@ class LocalRating < ApplicationRecord greater_than_or_equal_to: 0 } - def self.find_local_rating(params) + def self.find_local_rating(_params) LocalRating.find(local_id).local_ratings end end diff --git a/indicaAi/app/models/user_identifier.rb b/indicaAi/app/models/user_identifier.rb index 93af583d..8db38519 100644 --- a/indicaAi/app/models/user_identifier.rb +++ b/indicaAi/app/models/user_identifier.rb @@ -1,12 +1,12 @@ - # Description of User Identifier Class +# Description of User Identifier Class class UserIdentifier < ApplicationRecord has_many :favorite_locals, dependent: :destroy has_many :local_ratings - + validates :identifier, presence: true validates_numericality_of :identifier validates_uniqueness_of :identifier - + def self.find_favorites(params) UserIdentifier.find(params).favorite_locals end diff --git a/indicaAi/config/routes.rb b/indicaAi/config/routes.rb index 7dad1530..2debabd0 100644 --- a/indicaAi/config/routes.rb +++ b/indicaAi/config/routes.rb @@ -4,8 +4,9 @@ get '/locals/name/:name', to: 'locals#search_locals' get '/users', to: 'user_identifiers#index' get '/local/favorites/:local_id', to: 'locals#list_favorites' - get '/user/favorites/:user_id', to: 'use' + get '/user/favorites/:user_id', to: 'user_identifiers#list_favorites' post '/local_ratings', to: 'local_ratings#rating_a_place' + # Post Requests post '/favorite/create', to: 'favorite_locals#create', :defaults => { :format => 'json' } From 8e0e040b2983ce99d82c79a973a27db825627a5f Mon Sep 17 00:00:00 2001 From: Weiller Fernandes Date: Sat, 20 Oct 2018 10:50:20 -0300 Subject: [PATCH 17/20] #198 adding find_local_ratings in local model Co-authored-by: zarbielli --- indicaAi/app/models/local.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 076b5e94..7c780e2c 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -6,4 +6,8 @@ class Local < ApplicationRecord def self.find_by_name(params) Local.where('upper(name) like ?', "%#{params.upcase}%") end + + def self.find_local_ratings(local_id) + Local.find(local_id).local_ratings + end end From dee146be94d63a9bd3a7e8f5d8a76a0f7c63014d Mon Sep 17 00:00:00 2001 From: zarbielli Date: Sat, 20 Oct 2018 13:09:46 -0300 Subject: [PATCH 18/20] #198 - Fitting to codeclimate --- indicaAi/app/models/local.rb | 2 +- indicaAi/spec/models/local_rating_spec.rb | 49 ++++++++++++----------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/indicaAi/app/models/local.rb b/indicaAi/app/models/local.rb index 7c780e2c..b989b279 100644 --- a/indicaAi/app/models/local.rb +++ b/indicaAi/app/models/local.rb @@ -9,5 +9,5 @@ def self.find_by_name(params) def self.find_local_ratings(local_id) Local.find(local_id).local_ratings - end + end end diff --git a/indicaAi/spec/models/local_rating_spec.rb b/indicaAi/spec/models/local_rating_spec.rb index 8d3fc6ad..73dcd6f6 100644 --- a/indicaAi/spec/models/local_rating_spec.rb +++ b/indicaAi/spec/models/local_rating_spec.rb @@ -18,30 +18,33 @@ rating.local = local assert rating.save == false end + end +end - it 'Must exist a local' do - user = UserIdentifier.create(identifier: 345) - user.save - rating = LocalRating.new - rating.value = 4 - rating.user_identifier = user - assert rating.save == false - end +RSpec.describe LocalRating, type: :model do + it 'Must exist a local' do + user = UserIdentifier.create(identifier: 345) + user.save + rating = LocalRating.new + rating.value = 4 + rating.user_identifier = user + assert rating.save == false + end - it 'Must exist a value' do - local = Local.create(name: 'Teste') - local.save - user = UserIdentifier.create(identifier: 345) - user.save - rating = LocalRating.new - rating.user_identifier = user - rating.local = local - assert rating.save == false - end - it { should belong_to(:user_identifier) } - it { should belong_to(:local) } - it { should validate_presence_of(:value) } - it { should validate_numericality_of(:value) } - it { should validate_numericality_of(:value).is_less_than_or_equal_to(5) } + it 'Must exist a value' do + local = Local.create(name: 'Teste') + local.save + user = UserIdentifier.create(identifier: 345) + user.save + rating = LocalRating.new + rating.user_identifier = user + rating.local = local + assert rating.save == false end + + it { should belong_to(:user_identifier) } + it { should belong_to(:local) } + it { should validate_presence_of(:value) } + it { should validate_numericality_of(:value) } + it { should validate_numericality_of(:value).is_less_than_or_equal_to(5) } end From 29b1293eda074bd6c332d79e9d2fa412e7272369 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Sat, 20 Oct 2018 13:19:15 -0300 Subject: [PATCH 19/20] #198 - Removing test that validates existence of a local --- indicaAi/spec/models/local_spec.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/indicaAi/spec/models/local_spec.rb b/indicaAi/spec/models/local_spec.rb index 9a488813..452e9dc7 100644 --- a/indicaAi/spec/models/local_spec.rb +++ b/indicaAi/spec/models/local_spec.rb @@ -18,13 +18,4 @@ result = Local.find_local_ratings(id) assert result.first, nota end - - it 'Testing Validade Local Existence - Should return true if local exist' do - local = create(:local) - name = local.name - exist = true - - result = Local.validate_local_existence(name) - assert result, exist - end end From 9520a362c6081b28838847bbffb05e7632e1e690 Mon Sep 17 00:00:00 2001 From: zarbielli Date: Sat, 20 Oct 2018 20:51:54 -0300 Subject: [PATCH 20/20] #198 - Removing unused local controller method --- indicaAi/app/controllers/locals_controller.rb | 9 +-------- indicaAi/config/routes.rb | 2 +- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/indicaAi/app/controllers/locals_controller.rb b/indicaAi/app/controllers/locals_controller.rb index 5b26a003..d4680256 100644 --- a/indicaAi/app/controllers/locals_controller.rb +++ b/indicaAi/app/controllers/locals_controller.rb @@ -17,14 +17,7 @@ def search_locals result << locals.as_json(methods: [:local_ratings]) json_response(result) end - - # GET /local/:id/rating - def show_rating - rating = Local.find_local_ratings(params[:id_local]) - local = Local.find(params[:id_local]) - json_response(locals: local, rating: rating) - end - + # GET /local/:id def show_place unique_local = Local.find(params[:local_id]) diff --git a/indicaAi/config/routes.rb b/indicaAi/config/routes.rb index f32dbc3c..3624a1f0 100644 --- a/indicaAi/config/routes.rb +++ b/indicaAi/config/routes.rb @@ -6,10 +6,10 @@ get '/local/favorites/:local_id', to: 'locals#list_favorites' get '/users', to: 'user_identifiers#index' get '/user/favorites/:user_id', to: 'user_identifiers#list_favorites' - post '/local_ratings', to: 'local_ratings#rating_a_place' # Post Requests post '/favorite/create', to: 'favorite_locals#create', :defaults => { :format => 'json' } + post '/local_ratings', to: 'local_ratings#rating_a_place' # Patch Requests patch '/favorite/update/:id', to: 'favorite_locals#update', :defaults => { :format => 'json' }