Skip to content

Commit

Permalink
Merge 4c16dd2 into de68c59
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielDVpereira committed Sep 26, 2018
2 parents de68c59 + 4c16dd2 commit 4517865
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 20 deletions.
17 changes: 15 additions & 2 deletions indicaAi/app/controllers/locals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,25 @@ class LocalsController < ApplicationController
# GET /locals
def index
locals = Local.all
json_response(locals)
result = []
locals.each do |local|
result << local.as_json(methods: [:local_ratings])
end
json_response(result)
end

# GET /locals/name/:name
def search_locals
locals = Local.find_by_name(params[:name])
json_response(locals)
result = []
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
end
8 changes: 6 additions & 2 deletions indicaAi/app/models/local.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# Description of Places Class
class Local < ApplicationRecord
has_many :favorite_locals, dependent: :destroy
has_many :local_ratings
validates :name, presence: true, length: { minimum: 2 }
validates :RatingRate, presence: true

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
9 changes: 9 additions & 0 deletions indicaAi/app/models/local_rating.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# 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
end
2 changes: 1 addition & 1 deletion indicaAi/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.routes.draw do

get '/locals', to: 'locals#index'
get '/locals/name/:name', to: 'locals#search_locals'
get '/users', to: 'user_identifiers#index'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveRatingRateFromLocals < ActiveRecord::Migration[5.1]
def change
remove_column :locals, :RatingRate, :float
end
end
10 changes: 10 additions & 0 deletions indicaAi/db/migrate/20180917225656_create_local_ratings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateLocalRatings < ActiveRecord::Migration[5.1]
def change
create_table :local_ratings do |t|
t.integer :value
t.references :local, foreign_key: true

t.timestamps
end
end
end
12 changes: 12 additions & 0 deletions indicaAi/spec/controllers/locals_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
require 'rails_helper'

RSpec.describe LocalsController, type: :controller do
it { should route(:get, '/locals').to(action: :index) }
it 'should return a JSON object' do
get :index, format: :JSON
expect(response).to be_success
end

context 'GET #search_locals' do
let!(:local) { create(:local, name: 'Xoxo') }
it 'should route locals/name/x to the corresponding action' do
should route(:get, '/locals/name/x').to(action: :search_locals, name: 'x')
end
end
end
6 changes: 4 additions & 2 deletions indicaAi/spec/factories/local.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FactoryBot.define do
factory :local do
name { Faker::Nation.capital_city }
RatingRate { Faker::Number.decimal(2) }
# generates locations from the tv serie 'Friends' :D
name { Faker::Friends.location }
# generates quotations from 'Friends'
description { Faker::Friends.quote }
end
end
6 changes: 6 additions & 0 deletions indicaAi/spec/factories/local_rating.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryBot.define do
factory :local_rating do
value { Faker::Number.between(0, 5) }
local { create(:local) }
end
end
27 changes: 27 additions & 0 deletions indicaAi/spec/models/local_rating_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'rails_helper'

RSpec.describe LocalRating, type: :model do
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 { 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
28 changes: 15 additions & 13 deletions indicaAi/spec/models/local_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
require 'rails_helper'

RSpec.describe Local, type: :model do
let!(:local) { create(:local) }
let!(:favorite_locals) { create_list(:favorite_local, 10, local: local) }
it { should have_many(:favorite_locals).dependent(:destroy) }
context 'Validate of UserIdentifier' do
it { should validate_presence_of(:name) }
it 'local valid' do
local = create(:local)
expect(local.valid?).to be_truthy
end
it { should validate_presence_of(:name) }

it 'local invalid' do
local.name = ''
expect(local.valid?).to be_falsey
end
it 'Testing Search By Name - Should return similar names to params' do
local = create(:local)
name = local.name
result = Local.find_by_name(name)
assert result.first, local
end

it 'Testing Find Local Ratings - Should return rating associated to parms' do
local = create(:local)
nota = create(:local_rating, local: local)

id = local.id
result = Local.find_local_ratings(id)
assert result.first, nota
end
end
10 changes: 10 additions & 0 deletions indicaAi/spec/requests/locals_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,14 @@
expect(response).to have_http_status(200)
end
end

describe 'GET /locals/name/:name' do
# make HTTP get request before each example
let!(:local) { create(:local, name: 'plaza') }
before { get '/locals/name/plaza' }
it 'return local by name' do
expect(json).not_to be_empty
expect(json[0][0]['name']).to eq('plaza')
end
end
end

0 comments on commit 4517865

Please sign in to comment.